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.

123 lines
3.1 KiB

  1. <?php
  2. /**
  3. * Класс для работы с переменными окружения.
  4. *
  5. * @copyright NetMonsters <team@netmonsters.ru>
  6. * @link http://netmonsters.ru
  7. * @package Majestic
  8. * @subpackage env
  9. * @since
  10. * @version SVN: $Id$
  11. * @filesource $URL$
  12. */
  13. class Env
  14. {
  15. static protected $request = null;
  16. static protected $params = array();
  17. static public function getRequestUri()
  18. {
  19. if (self::$request === null) {
  20. // removes get params
  21. list(self::$request, ) = explode('?', Env::Server('REQUEST_URI'));
  22. // removes base url
  23. $base = FrontController::getInstance()->getBaseUrl();
  24. if (($length = strlen($base)) > 0 && strpos(self::$request, $base) === 0) {
  25. self::$request = (string) substr(self::$request, $length);
  26. }
  27. }
  28. return self::$request;
  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 Cookie($key = null, $default = false)
  45. {
  46. if ($key === null) {
  47. return $_COOKIE;
  48. }
  49. return (isset($_COOKIE[$key])) ? $_COOKIE[$key] : $default;
  50. }
  51. static public function Server($key = null, $default = null)
  52. {
  53. if ($key === null) {
  54. return $_SERVER;
  55. }
  56. return (isset($_SERVER[$key])) ? $_SERVER[$key] : $default;
  57. }
  58. static public function Session($var, $default = false)
  59. {
  60. return isset($_SESSION[$var]) ? $_SESSION[$var] : $default;
  61. }
  62. static public function setSession($var, $value)
  63. {
  64. $_SESSION[$var] = $value;
  65. }
  66. /**
  67. * Unsets session var
  68. *
  69. * @param string $var
  70. */
  71. static public function unsetSession($var)
  72. {
  73. if (isset($_SESSION[$var])) {
  74. unset($_SESSION[$var]);
  75. }
  76. }
  77. static public function setCookie($var, $value, $time = 0, $path = '/')
  78. {
  79. return setcookie($var, $value, $time, $path);
  80. }
  81. static public function Files($name = '', $default = array(), $param = false)
  82. {
  83. if (!isset($_FILES)) {
  84. return $default;
  85. }
  86. if (empty($name)) {
  87. return $_FILES;
  88. }
  89. $res = isset($_FILES[$name]) ? $_FILES[$name] : $default;
  90. return $param ? $res[$param] : $res;
  91. }
  92. static public function getParam($key = null, $default = false)
  93. {
  94. if ($key === null) {
  95. return self::$params;
  96. }
  97. return (isset(self::$params[$key])) ? self::$params[$key] : $default;
  98. }
  99. static public function setParam($key, $value)
  100. {
  101. self::$params[$key] = $value;
  102. }
  103. static public function setParams($params = array())
  104. {
  105. self::$params = self::$params + $params;
  106. }
  107. }