request.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php namespace System;
  2. class Request {
  3. /**
  4. * The request URI.
  5. *
  6. * @var string
  7. */
  8. public static $uri;
  9. /**
  10. * The route handling the current request.
  11. *
  12. * @var Route
  13. */
  14. public static $route;
  15. /**
  16. * Get the request URI.
  17. *
  18. * @return string
  19. */
  20. public static function uri()
  21. {
  22. if ( ! is_null(static::$uri))
  23. {
  24. return static::$uri;
  25. }
  26. if (isset($_SERVER['PATH_INFO']))
  27. {
  28. $uri = $_SERVER['PATH_INFO'];
  29. }
  30. elseif (isset($_SERVER['REQUEST_URI']))
  31. {
  32. $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
  33. }
  34. else
  35. {
  36. throw new \Exception('Unable to determine the request URI.');
  37. }
  38. if ($uri === false)
  39. {
  40. throw new \Exception("Malformed request URI. Request terminated.");
  41. }
  42. $uri = static::remove_from_uri($uri, parse_url(Config::get('application.url'), PHP_URL_PATH));
  43. $uri = static::remove_from_uri($uri, '/'.Config::get('application.index'));
  44. $uri = trim($uri, '/');
  45. return ($uri == '') ? '/' : strtolower($uri);
  46. }
  47. /**
  48. * Remove a string from the beginning of a URI.
  49. *
  50. * @param string $uri
  51. * @param string $value
  52. * @return string
  53. */
  54. private static function remove_from_uri($uri, $value)
  55. {
  56. return (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri;
  57. }
  58. /**
  59. * Get the request method.
  60. *
  61. * The request method may be spoofed if a hidden "REQUEST_METHOD" POST element
  62. * is present, allowing HTML forms to simulate PUT and DELETE requests.
  63. *
  64. * @return string
  65. */
  66. public static function method()
  67. {
  68. return (array_key_exists('REQUEST_METHOD', $_POST)) ? $_POST['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD'];
  69. }
  70. /**
  71. * Get the requestor's IP address.
  72. *
  73. * @return string
  74. */
  75. public static function ip()
  76. {
  77. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  78. {
  79. return $_SERVER['HTTP_X_FORWARDED_FOR'];
  80. }
  81. elseif (isset($_SERVER['HTTP_CLIENT_IP']))
  82. {
  83. return $_SERVER['HTTP_CLIENT_IP'];
  84. }
  85. elseif (isset($_SERVER['REMOTE_ADDR']))
  86. {
  87. return $_SERVER['REMOTE_ADDR'];
  88. }
  89. }
  90. /**
  91. * Get the HTTP protocol for the request.
  92. *
  93. * @return string
  94. */
  95. public static function protocol()
  96. {
  97. return (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
  98. }
  99. /**
  100. * Determine if the request is using HTTPS.
  101. *
  102. * @return bool
  103. */
  104. public static function is_secure()
  105. {
  106. return (static::protocol() == 'https');
  107. }
  108. /**
  109. * Determine if the request is an AJAX request.
  110. *
  111. * @return bool
  112. */
  113. public static function is_ajax()
  114. {
  115. return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
  116. }
  117. /**
  118. * Determine if the route handling the request is a given name.
  119. *
  120. * @param string $name
  121. * @return bool
  122. */
  123. public static function route_is($name)
  124. {
  125. return (is_array(static::$route->callback) and isset(static::$route->callback['name']) and static::$route->callback['name'] === $name);
  126. }
  127. /**
  128. * Magic Method to handle dynamic static methods.
  129. */
  130. public static function __callStatic($method, $parameters)
  131. {
  132. if (strpos($method, 'route_is_') === 0)
  133. {
  134. return static::route_is(substr($method, 9));
  135. }
  136. }
  137. }