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.

102 lines
2.6 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 public function getRequestUri()
  17. {
  18. if (self::$request === null) {
  19. // removes get params
  20. list(self::$request, ) = explode('?', Env::Server('REQUEST_URI'));
  21. // removes base url
  22. $base = FrontController::getInstance()->getBaseUrl();
  23. if (($length = strlen($base)) > 0 && strpos(self::$request, $base) === 0) {
  24. self::$request = (string) substr(self::$request, $length);
  25. }
  26. }
  27. return self::$request;
  28. }
  29. static public function Get($key = null, $default = null)
  30. {
  31. if ($key === null) {
  32. return $_GET;
  33. }
  34. return (isset($_GET[$key])) ? $_GET[$key] : $default;
  35. }
  36. static public function Post($key = null, $default = null)
  37. {
  38. if ($key === null) {
  39. return $_POST;
  40. }
  41. return (isset($_POST[$key])) ? $_POST[$key] : $default;
  42. }
  43. static public function Cookie($key = null, $default = false)
  44. {
  45. if ($key === null) {
  46. return $_COOKIE;
  47. }
  48. return (isset($_COOKIE[$key])) ? $_COOKIE[$key] : $default;
  49. }
  50. static public function Server($key = null, $default = null)
  51. {
  52. if ($key === null) {
  53. return $_SERVER;
  54. }
  55. return (isset($_SERVER[$key])) ? $_SERVER[$key] : $default;
  56. }
  57. static public function Session($var, $default = false)
  58. {
  59. return isset($_SESSION[$var]) ? $_SESSION[$var] : $default;
  60. }
  61. static public function setSession($var, $value)
  62. {
  63. $_SESSION[$var] = $value;
  64. }
  65. /**
  66. * Unsets session var
  67. *
  68. * @param string $var
  69. */
  70. static public function unsetSession($var)
  71. {
  72. if (isset($_SESSION[$var])) {
  73. unset($_SESSION[$var]);
  74. }
  75. }
  76. static public function setCookie($var, $value, $time = 0, $path = '/')
  77. {
  78. return setcookie($var, $value, $time, $path);
  79. }
  80. static public function Files($name = '', $default = array(), $param = false)
  81. {
  82. if (!isset($_FILES)) {
  83. return $default;
  84. }
  85. if (empty($name)) {
  86. return $_FILES;
  87. }
  88. $res = isset($_FILES[$name]) ? $_FILES[$name] : $default;
  89. return $param ? $res[$param] : $res;
  90. }
  91. }