request.php 4.4 KB

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