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