request.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php namespace Laravel;
  2. class Request {
  3. /**
  4. * The request URI.
  5. *
  6. * @var string
  7. */
  8. public $uri;
  9. /**
  10. * The request method (GET, POST, PUT, or DELETE).
  11. *
  12. * @var string
  13. */
  14. public $method;
  15. /**
  16. * Indicates if the request method is being spoofed by a hidden form element.
  17. *
  18. * @var bool
  19. */
  20. public $spoofed;
  21. /**
  22. * The requestor's IP address.
  23. *
  24. * @var string
  25. */
  26. public $ip;
  27. /**
  28. * Indicates if the request is using HTTPS.
  29. *
  30. * @var bool
  31. */
  32. public $secure;
  33. /**
  34. * Indicates if the request is an AJAX request.
  35. *
  36. * @var bool
  37. */
  38. public $ajax;
  39. /**
  40. * The input instance for the request.
  41. *
  42. * @var Input
  43. */
  44. public $input;
  45. /**
  46. * The $_SERVER array for the request.
  47. *
  48. * @var array
  49. */
  50. public $server;
  51. /**
  52. * The route handling the current request.
  53. *
  54. * @var Routing\Route
  55. */
  56. public $route;
  57. /**
  58. * Create a new request instance.
  59. *
  60. * @param array $server
  61. * @param string $url
  62. * @return void
  63. */
  64. public function __construct($server, $url)
  65. {
  66. $this->server = $server;
  67. $this->uri = $this->uri($url);
  68. foreach (array('method', 'spoofed', 'ip', 'secure', 'ajax') as $item)
  69. {
  70. $this->$item = $this->$item();
  71. }
  72. }
  73. /**
  74. * Determine the request URI.
  75. *
  76. * The request URI will be trimmed to remove to the application URL and application index file.
  77. * If the request is to the root of the application, the URI will be set to a forward slash.
  78. *
  79. * If the $_SERVER "PATH_INFO" variable is available, it will be used; otherwise, we will try
  80. * to determine the URI using the REQUEST_URI variable. If neither are available, an exception
  81. * will be thrown by the method.
  82. *
  83. * @param string $url
  84. * @return string
  85. */
  86. private function uri($url)
  87. {
  88. if (isset($this->server['PATH_INFO']))
  89. {
  90. $uri = $this->server['PATH_INFO'];
  91. }
  92. elseif (isset($this->server['REQUEST_URI']))
  93. {
  94. $uri = parse_url($this->server['REQUEST_URI'], PHP_URL_PATH);
  95. }
  96. else
  97. {
  98. throw new \Exception('Unable to determine the request URI.');
  99. }
  100. if ($uri === false) throw new \Exception('Malformed request URI. Request terminated.');
  101. foreach (array(parse_url($url, PHP_URL_PATH), '/index.php') as $value)
  102. {
  103. $uri = (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri;
  104. }
  105. return (($uri = trim($uri, '/')) == '') ? '/' : $uri;
  106. }
  107. /**
  108. * Get the request method.
  109. *
  110. * Typically, this will be the value of the REQUEST_METHOD $_SERVER variable.
  111. * However, when the request is being spoofed by a hidden form value, the request
  112. * method will be stored in the $_POST array.
  113. *
  114. * @return string
  115. */
  116. private function method()
  117. {
  118. return ($this->spoofed()) ? $_POST['REQUEST_METHOD'] : $this->server['REQUEST_METHOD'];
  119. }
  120. /**
  121. * Determine if the request method is being spoofed by a hidden Form element.
  122. *
  123. * Hidden elements are used to spoof PUT and DELETE requests since they are not supported by HTML forms.
  124. *
  125. * @return bool
  126. */
  127. private function spoofed()
  128. {
  129. return is_array($_POST) and array_key_exists('REQUEST_METHOD', $_POST);
  130. }
  131. /**
  132. * Get the requestor's IP address.
  133. *
  134. * @return string
  135. */
  136. private function ip()
  137. {
  138. if (isset($this->server['HTTP_X_FORWARDED_FOR']))
  139. {
  140. return $this->server['HTTP_X_FORWARDED_FOR'];
  141. }
  142. elseif (isset($this->server['HTTP_CLIENT_IP']))
  143. {
  144. return $this->server['HTTP_CLIENT_IP'];
  145. }
  146. elseif (isset($this->server['REMOTE_ADDR']))
  147. {
  148. return $this->server['REMOTE_ADDR'];
  149. }
  150. }
  151. /**
  152. * Get the HTTP protocol for the request.
  153. *
  154. * @return string
  155. */
  156. private function protocol()
  157. {
  158. return (isset($this->server['HTTPS']) and $this->server['HTTPS'] !== 'off') ? 'https' : 'http';
  159. }
  160. /**
  161. * Determine if the request is using HTTPS.
  162. *
  163. * @return bool
  164. */
  165. private function secure()
  166. {
  167. return ($this->protocol() == 'https');
  168. }
  169. /**
  170. * Determine if the request is an AJAX request.
  171. *
  172. * @return bool
  173. */
  174. private function ajax()
  175. {
  176. if ( ! isset($this->server['HTTP_X_REQUESTED_WITH'])) return false;
  177. return strtolower($this->server['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
  178. }
  179. /**
  180. * Determine if the route handling the request has a given name.
  181. *
  182. * <code>
  183. * // Determine if the route handling the request is named "profile"
  184. * $profile = Request::active()->route_is('profile');
  185. * </code>
  186. *
  187. * @param string $name
  188. * @return bool
  189. */
  190. public function route_is($name)
  191. {
  192. if (is_null($this->route) or ! is_array($this->route->callback) or ! isset($this->route->callback['name'])) return false;
  193. return $this->route->callback['name'] === $name;
  194. }
  195. /**
  196. * Magic Method to handle dynamic method calls to determine the route handling the request.
  197. *
  198. * <code>
  199. * // Determine if the route handling the request is named "profile"
  200. * $profile = Request::active()->route_is_profile();
  201. * </code>
  202. */
  203. public function __call($method, $parameters)
  204. {
  205. if (strpos($method, 'route_is_') === 0)
  206. {
  207. return $this->route_is(substr($method, 9));
  208. }
  209. }
  210. }