request.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php namespace Laravel;
  2. class Request {
  3. /**
  4. * The route handling the current request.
  5. *
  6. * @var Routing\Route
  7. */
  8. public $route;
  9. /**
  10. * The $_SERVER array for the current request.
  11. *
  12. * @var array
  13. */
  14. protected $server;
  15. /**
  16. * The $_POST array for the current request.
  17. *
  18. * @var array
  19. */
  20. protected $post;
  21. /**
  22. * The request data key that is used to indicate a spoofed request method.
  23. *
  24. * @var string
  25. */
  26. const spoofer = '__spoofer';
  27. /**
  28. * Create a new request instance.
  29. *
  30. * @param URI $uri
  31. * @param array $server
  32. * @param array $post
  33. * @return void
  34. */
  35. public function __construct(URI $uri, $server, $post)
  36. {
  37. $this->uri = $uri;
  38. $this->post = $post;
  39. $this->server = $server;
  40. }
  41. /**
  42. * Get the URI for the current request.
  43. *
  44. * Note: This method is the equivalent of calling the URI::get method.
  45. *
  46. * @return string
  47. */
  48. public function uri()
  49. {
  50. return $this->uri->get();
  51. }
  52. /**
  53. * Get the request format.
  54. *
  55. * The format is determined by essentially taking the "extension" of the URI.
  56. *
  57. * @return string
  58. */
  59. public function format()
  60. {
  61. return (($extension = pathinfo($this->uri->get(), PATHINFO_EXTENSION)) !== '') ? $extension : 'html';
  62. }
  63. /**
  64. * Get the request method.
  65. *
  66. * Typically, this will be the value of the REQUEST_METHOD $_SERVER variable.
  67. * However, when the request is being spoofed by a hidden form value, the request
  68. * method will be stored in the $_POST array.
  69. *
  70. * @return string
  71. */
  72. public function method()
  73. {
  74. return ($this->spoofed()) ? $this->post[Request::spoofer] : $this->server['REQUEST_METHOD'];
  75. }
  76. /**
  77. * Get an item from the $_SERVER array.
  78. *
  79. * Like most array retrieval methods, a default value may be specified.
  80. *
  81. * @param string $key
  82. * @param mixed $default
  83. * @return string
  84. */
  85. public function server($key = null, $default = null)
  86. {
  87. return Arr::get($this->server, strtoupper($key), $default);
  88. }
  89. /**
  90. * Determine if the request method is being spoofed by a hidden Form element.
  91. *
  92. * Hidden elements are used to spoof PUT and DELETE requests since they are not supported
  93. * by HTML forms. If the request is being spoofed, Laravel considers the spoofed request
  94. * method the actual request method throughout the framework.
  95. *
  96. * @return bool
  97. */
  98. public function spoofed()
  99. {
  100. return is_array($this->post) and array_key_exists(Request::spoofer, $this->post);
  101. }
  102. /**
  103. * Get the requestor's IP address.
  104. *
  105. * @param mixed $default
  106. * @return string
  107. */
  108. public function ip($default = '0.0.0.0')
  109. {
  110. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  111. {
  112. return $_SERVER['HTTP_X_FORWARDED_FOR'];
  113. }
  114. elseif (isset($_SERVER['HTTP_CLIENT_IP']))
  115. {
  116. return $_SERVER['HTTP_CLIENT_IP'];
  117. }
  118. elseif (isset($_SERVER['REMOTE_ADDR']))
  119. {
  120. return $_SERVER['REMOTE_ADDR'];
  121. }
  122. return ($default instanceof \Closure) ? call_user_func($default) : $default;
  123. }
  124. /**
  125. * Get the HTTP protocol for the request.
  126. *
  127. * This method will return either "https" or "http", depending on whether HTTPS
  128. * is being used for the current request.
  129. *
  130. * @return string
  131. */
  132. public function protocol()
  133. {
  134. return (isset($this->server['HTTPS']) and $this->server['HTTPS'] !== 'off') ? 'https' : 'http';
  135. }
  136. /**
  137. * Determine if the current request is using HTTPS.
  138. *
  139. * @return bool
  140. */
  141. public function secure()
  142. {
  143. return $this->protocol() == 'https';
  144. }
  145. /**
  146. * Determine if the current request is an AJAX request.
  147. *
  148. * @return bool
  149. */
  150. public function ajax()
  151. {
  152. if ( ! isset($this->server['HTTP_X_REQUESTED_WITH'])) return false;
  153. return strtolower($this->server['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
  154. }
  155. /**
  156. * Get the route handling the current request.
  157. *
  158. * @return Route
  159. */
  160. public function route() { return $this->route; }
  161. }