You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.0 KiB

11 years ago
11 years ago
  1. <?php namespace Majestic;
  2. /**
  3. * Класс для работы с переменными окружения.
  4. *
  5. * @copyright NetMonsters <team@netmonsters.ru>
  6. * @link http://netmonsters.ru
  7. * @package Majestic
  8. * @subpackage env
  9. * @since
  10. */
  11. class Env
  12. {
  13. static protected $request = array();
  14. static protected $params = array();
  15. static public function getRequestUri($trim_base = false)
  16. {
  17. if (!isset(self::$request[$trim_base])) {
  18. // removes get params
  19. list(self::$request[$trim_base], ) = explode('?', Env::Server('REQUEST_URI'));
  20. if ($trim_base) {
  21. // removes base url
  22. $base = \Majestic\App\FrontController::getInstance()->getBaseUrl();
  23. if (($length = strlen($base)) > 0 && strpos(self::$request[$trim_base], $base) === 0) {
  24. self::$request[$trim_base] = (string) substr(self::$request[$trim_base], $length);
  25. }
  26. }
  27. }
  28. return self::$request[$trim_base];
  29. }
  30. static public function Get($key = null, $default = null)
  31. {
  32. if ($key === null) {
  33. return $_GET;
  34. }
  35. return (isset($_GET[$key])) ? $_GET[$key] : $default;
  36. }
  37. static public function Post($key = null, $default = null)
  38. {
  39. if ($key === null) {
  40. return $_POST;
  41. }
  42. return (isset($_POST[$key])) ? $_POST[$key] : $default;
  43. }
  44. static public function Request($key = null, $default = null)
  45. {
  46. if ($key === null) {
  47. return $_REQUEST;
  48. }
  49. return (isset($_REQUEST[$key])) ? $_REQUEST[$key] : $default;
  50. }
  51. static public function Cookie($key = null, $default = false)
  52. {
  53. if ($key === null) {
  54. return $_COOKIE;
  55. }
  56. return (isset($_COOKIE[$key])) ? $_COOKIE[$key] : $default;
  57. }
  58. static public function Server($key = null, $default = null)
  59. {
  60. if ($key === null) {
  61. return $_SERVER;
  62. }
  63. return (isset($_SERVER[$key])) ? $_SERVER[$key] : $default;
  64. }
  65. static public function setCookie($var, $value, $time = 0, $path = '/')
  66. {
  67. return setcookie($var, $value, $time, $path);
  68. }
  69. static public function Files($name = '', $default = array(), $param = false)
  70. {
  71. if (!isset($_FILES)) {
  72. return $default;
  73. }
  74. if (empty($name)) {
  75. return $_FILES;
  76. }
  77. $res = isset($_FILES[$name]) ? $_FILES[$name] : $default;
  78. return $param ? $res[$param] : $res;
  79. }
  80. static public function getParam($key = null, $default = false)
  81. {
  82. if ($key === null) {
  83. return self::$params;
  84. }
  85. return (isset(self::$params[$key])) ? self::$params[$key] : $default;
  86. }
  87. static public function setParam($key, $value)
  88. {
  89. self::$params[$key] = $value;
  90. }
  91. static public function setParams($params = array())
  92. {
  93. self::$params = self::$params + $params;
  94. }
  95. }