request.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php namespace Laravel; use Closure;
  2. class Request {
  3. /**
  4. * The request URI for the current request.
  5. *
  6. * @var string
  7. */
  8. public static $uri;
  9. /**
  10. * The route handling the current request.
  11. *
  12. * @var Routing\Route
  13. */
  14. public static $route;
  15. /**
  16. * The request data key that is used to indicate a spoofed request method.
  17. *
  18. * @var string
  19. */
  20. const spoofer = '__spoofer';
  21. /**
  22. * Get the URI for the current request.
  23. *
  24. * If the request is to the root of the application, a single forward slash
  25. * will be returned. Otherwise, the URI will be returned without any leading
  26. * or trailing slashes.
  27. *
  28. * @return string
  29. */
  30. public static function uri()
  31. {
  32. if ( ! is_null(static::$uri)) return static::$uri;
  33. $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
  34. // Remove the root application URL from the request URI. If the application
  35. // is nested within a sub-directory of the web document root, this will get
  36. // rid of the sub-directories from the request URI.
  37. $base = parse_url(Config::$items['application']['url'], PHP_URL_PATH);
  38. if (strpos($uri, $base) === 0)
  39. {
  40. $uri = substr($uri, strlen($base));
  41. }
  42. // Remove the application index file. It is not used for anything as far
  43. // as the framework and routing is concerned, so it's worthless.
  44. $index = '/'.Config::$items['application']['index'];
  45. if ($index !== '/' and strpos($uri, $index) === 0)
  46. {
  47. $uri = substr($uri, strlen($index));
  48. }
  49. // Format the final request URI. If there is nothing left, we will just
  50. // return a single forward slash. Otherwise, we'll remove all of the
  51. // leading and trailing spaces from the URI.
  52. return static::$uri = (($uri = trim($uri, '/')) !== '') ? $uri : '/';
  53. }
  54. /**
  55. * Get the request method.
  56. *
  57. * This will usually be the value of the REQUEST_METHOD $_SERVER variable
  58. * However, when the request method is spoofed using a hidden form value,
  59. * the method will be stored in the $_POST array.
  60. *
  61. * @return string
  62. */
  63. public static function method()
  64. {
  65. return (static::spoofed()) ? $_POST[Request::spoofer] : $_SERVER['REQUEST_METHOD'];
  66. }
  67. /**
  68. * Get an item from the $_SERVER array.
  69. *
  70. * Like most array retrieval methods, a default value may be specified.
  71. *
  72. * @param string $key
  73. * @param mixed $default
  74. * @return string
  75. */
  76. public static function server($key = null, $default = null)
  77. {
  78. return Arr::get($_SERVER, strtoupper($key), $default);
  79. }
  80. /**
  81. * Determine if the request method is being spoofed by a hidden Form element.
  82. *
  83. * @return bool
  84. */
  85. public static function spoofed()
  86. {
  87. return is_array($_POST) and array_key_exists(Request::spoofer, $_POST);
  88. }
  89. /**
  90. * Get the requestor's IP address.
  91. *
  92. * @param mixed $default
  93. * @return string
  94. */
  95. public static function ip($default = '0.0.0.0')
  96. {
  97. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  98. {
  99. return $_SERVER['HTTP_X_FORWARDED_FOR'];
  100. }
  101. elseif (isset($_SERVER['HTTP_CLIENT_IP']))
  102. {
  103. return $_SERVER['HTTP_CLIENT_IP'];
  104. }
  105. elseif (isset($_SERVER['REMOTE_ADDR']))
  106. {
  107. return $_SERVER['REMOTE_ADDR'];
  108. }
  109. return ($default instanceof Closure) ? call_user_func($default) : $default;
  110. }
  111. /**
  112. * Get the HTTP protocol for the request.
  113. *
  114. * @return string
  115. */
  116. public static function protocol()
  117. {
  118. return Arr::get($_SERVER, 'SERVER_PROTOCOL', 'HTTP/1.1');
  119. }
  120. /**
  121. * Determine if the current request is using HTTPS.
  122. *
  123. * @return bool
  124. */
  125. public static function secure()
  126. {
  127. return isset($_SERVER['HTTPS']) and strtolower($_SERVER['HTTPS']) !== 'off';
  128. }
  129. /**
  130. * Determine if the request has been forged.
  131. *
  132. * The session CSRF token will be compared to the CSRF token in the request input.
  133. *
  134. * @return bool
  135. */
  136. public static function forged()
  137. {
  138. return Input::get('csrf_token') !== Session::token();
  139. }
  140. /**
  141. * Determine if the current request is an AJAX request.
  142. *
  143. * @return bool
  144. */
  145. public static function ajax()
  146. {
  147. if ( ! isset($_SERVER['HTTP_X_REQUESTED_WITH'])) return false;
  148. return strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
  149. }
  150. /**
  151. * Get the route handling the current request.
  152. *
  153. * @return Route
  154. */
  155. public static function route()
  156. {
  157. return static::$route;
  158. }
  159. }