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.

100 lines
2.7 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. */
  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 = 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 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 setCookie($var, $value, $time = 0, $path = '/')
  59. {
  60. return setcookie($var, $value, $time, $path);
  61. }
  62. static public function Files($name = '', $default = array(), $param = false)
  63. {
  64. if (!isset($_FILES)) {
  65. return $default;
  66. }
  67. if (empty($name)) {
  68. return $_FILES;
  69. }
  70. $res = isset($_FILES[$name]) ? $_FILES[$name] : $default;
  71. return $param ? $res[$param] : $res;
  72. }
  73. static public function getParam($key = null, $default = false)
  74. {
  75. if ($key === null) {
  76. return self::$params;
  77. }
  78. return (isset(self::$params[$key])) ? self::$params[$key] : $default;
  79. }
  80. static public function setParam($key, $value)
  81. {
  82. self::$params[$key] = $value;
  83. }
  84. static public function setParams($params = array())
  85. {
  86. self::$params = self::$params + $params;
  87. }
  88. }