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.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 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 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. }