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