request.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php namespace Laravel; use Closure;
  2. class Request {
  3. /**
  4. * The route handling the current request.
  5. *
  6. * @var Routing\Route
  7. */
  8. public static $route;
  9. /**
  10. * The request URI for the current request.
  11. *
  12. * @var string
  13. */
  14. public static $uri;
  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. * Note: This method is the equivalent of calling the URI::get method.
  25. *
  26. * @return string
  27. */
  28. public static function uri()
  29. {
  30. if ( ! is_null(static::$uri)) return static::$uri;
  31. // Sniff the request URI out of the $_SERVER array. The PATH_IFNO
  32. // variable contains the URI without the base URL or index page,
  33. // so we will use that if possible, otherwise we will parse the
  34. // URI out of the REQUEST_URI element.
  35. if (isset($_SERVER['PATH_INFO']))
  36. {
  37. $uri = $_SERVER['PATH_INFO'];
  38. }
  39. elseif (isset($_SERVER['REQUEST_URI']))
  40. {
  41. $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
  42. if ($uri === false)
  43. {
  44. throw new \Exception("Invalid request URI. Request terminated.");
  45. }
  46. }
  47. else
  48. {
  49. throw new \Exception("Unable to determine the request URI.");
  50. }
  51. // Remove the application URL and the application index page from
  52. // the request URI. Both of these elements will interfere with the
  53. // routing engine and are extraneous, so they will be removed.
  54. $base = parse_url(Config::$items['application']['url'], PHP_URL_PATH);
  55. if (strpos($uri, $base) === 0)
  56. {
  57. $uri = substr($uri, strlen($base));
  58. }
  59. $index = '/'.Config::$items['application']['index'];
  60. if (trim($index) !== '/' and strpos($uri, $index) === 0)
  61. {
  62. $uri = substr($uri, strlen($index));
  63. }
  64. // Request URIs to the root of the application will be returned
  65. // as a single forward slash. Otherwise, the request URI will be
  66. // returned without leading or trailing slashes.
  67. return static::$uri = (($uri = trim($uri, '/')) == '') ? '/' : $uri;
  68. }
  69. /**
  70. * Get the request format.
  71. *
  72. * The format is determined by essentially taking the "extension" of the URI.
  73. *
  74. * @return string
  75. */
  76. public static function format()
  77. {
  78. return (($extension = pathinfo(static::uri(), PATHINFO_EXTENSION)) !== '') ? $extension : 'html';
  79. }
  80. /**
  81. * Get the request method.
  82. *
  83. * Typically, this will be the value of the REQUEST_METHOD $_SERVER variable.
  84. * However, when the request is being spoofed by a hidden form value, the request
  85. * method will be stored in the $_POST array.
  86. *
  87. * @return string
  88. */
  89. public static function method()
  90. {
  91. return (static::spoofed()) ? $_POST[Request::spoofer] : $_SERVER['REQUEST_METHOD'];
  92. }
  93. /**
  94. * Get an item from the $_SERVER array.
  95. *
  96. * Like most array retrieval methods, a default value may be specified.
  97. *
  98. * @param string $key
  99. * @param mixed $default
  100. * @return string
  101. */
  102. public static function server($key = null, $default = null)
  103. {
  104. return Arr::get($_SERVER, strtoupper($key), $default);
  105. }
  106. /**
  107. * Determine if the request method is being spoofed by a hidden Form element.
  108. *
  109. * Hidden elements are used to spoof PUT and DELETE requests since they are not supported
  110. * by HTML forms. If the request is being spoofed, Laravel considers the spoofed request
  111. * method the actual request method throughout the framework.
  112. *
  113. * @return bool
  114. */
  115. public static function spoofed()
  116. {
  117. return is_array($_POST) and array_key_exists(Request::spoofer, $_POST);
  118. }
  119. /**
  120. * Get the requestor's IP address.
  121. *
  122. * @param mixed $default
  123. * @return string
  124. */
  125. public static function ip($default = '0.0.0.0')
  126. {
  127. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  128. {
  129. return $_SERVER['HTTP_X_FORWARDED_FOR'];
  130. }
  131. elseif (isset($_SERVER['HTTP_CLIENT_IP']))
  132. {
  133. return $_SERVER['HTTP_CLIENT_IP'];
  134. }
  135. elseif (isset($_SERVER['REMOTE_ADDR']))
  136. {
  137. return $_SERVER['REMOTE_ADDR'];
  138. }
  139. return ($default instanceof Closure) ? call_user_func($default) : $default;
  140. }
  141. /**
  142. * Get the HTTP protocol for the request.
  143. *
  144. * This method will return either "https" or "http", depending on whether HTTPS
  145. * is being used for the current request.
  146. *
  147. * @return string
  148. */
  149. public static function protocol()
  150. {
  151. return (isset($_SERVER['HTTPS']) and strtolower($_SERVER['HTTPS']) !== 'off') ? 'https' : 'http';
  152. }
  153. /**
  154. * Determine if the current request is using HTTPS.
  155. *
  156. * @return bool
  157. */
  158. public static function secure()
  159. {
  160. return static::protocol() == 'https';
  161. }
  162. /**
  163. * Determine if the current request is an AJAX request.
  164. *
  165. * @return bool
  166. */
  167. public static function ajax()
  168. {
  169. if ( ! isset($_SERVER['HTTP_X_REQUESTED_WITH'])) return false;
  170. return strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
  171. }
  172. /**
  173. * Get the route handling the current request.
  174. *
  175. * @return Route
  176. */
  177. public static function route()
  178. {
  179. return static::$route;
  180. }
  181. }