request.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php namespace Laravel; use Closure;
  2. class Request {
  3. /**
  4. * All of the route instances handling the request.
  5. *
  6. * @var array
  7. */
  8. public static $route;
  9. /**
  10. * The request data key that is used to indicate a spoofed request method.
  11. *
  12. * @var string
  13. */
  14. const spoofer = '__spoofer';
  15. /**
  16. * Get the URI for the current request.
  17. *
  18. * @return string
  19. */
  20. public static function uri()
  21. {
  22. return URI::current();
  23. }
  24. /**
  25. * Get the request method.
  26. *
  27. * @return string
  28. */
  29. public static function method()
  30. {
  31. if ($_SERVER['REQUEST_METHOD'] == 'HEAD')
  32. {
  33. return 'GET';
  34. }
  35. return (static::spoofed()) ? $_POST[Request::spoofer] : $_SERVER['REQUEST_METHOD'];
  36. }
  37. /**
  38. * Get an item from the $_SERVER array.
  39. *
  40. * @param string $key
  41. * @param mixed $default
  42. * @return string
  43. */
  44. public static function server($key = null, $default = null)
  45. {
  46. return array_get($_SERVER, strtoupper($key), $default);
  47. }
  48. /**
  49. * Determine if the request method is being spoofed by a hidden Form element.
  50. *
  51. * @return bool
  52. */
  53. public static function spoofed()
  54. {
  55. return is_array($_POST) and array_key_exists(Request::spoofer, $_POST);
  56. }
  57. /**
  58. * Get the requestor's IP address.
  59. *
  60. * @param mixed $default
  61. * @return string
  62. */
  63. public static function ip($default = '0.0.0.0')
  64. {
  65. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  66. {
  67. return $_SERVER['HTTP_X_FORWARDED_FOR'];
  68. }
  69. elseif (isset($_SERVER['HTTP_CLIENT_IP']))
  70. {
  71. return $_SERVER['HTTP_CLIENT_IP'];
  72. }
  73. elseif (isset($_SERVER['REMOTE_ADDR']))
  74. {
  75. return $_SERVER['REMOTE_ADDR'];
  76. }
  77. return value($default);
  78. }
  79. /**
  80. * Get the HTTP protocol for the request.
  81. *
  82. * @return string
  83. */
  84. public static function protocol()
  85. {
  86. return array_get($_SERVER, 'SERVER_PROTOCOL', 'HTTP/1.1');
  87. }
  88. /**
  89. * Determine if the current request is using HTTPS.
  90. *
  91. * @return bool
  92. */
  93. public static function secure()
  94. {
  95. return isset($_SERVER['HTTPS']) and strtolower($_SERVER['HTTPS']) !== 'off';
  96. }
  97. /**
  98. * Determine if the request has been forged.
  99. *
  100. * The session CSRF token will be compared to the CSRF token in the request input.
  101. *
  102. * @return bool
  103. */
  104. public static function forged()
  105. {
  106. return Input::get(Session::csrf_token) !== Session::token();
  107. }
  108. /**
  109. * Determine if the current request is an AJAX request.
  110. *
  111. * @return bool
  112. */
  113. public static function ajax()
  114. {
  115. if ( ! isset($_SERVER['HTTP_X_REQUESTED_WITH'])) return false;
  116. return strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
  117. }
  118. /**
  119. * Determine if the current request is via the command line.
  120. *
  121. * @return bool
  122. */
  123. public static function cli()
  124. {
  125. return defined('STDIN');
  126. }
  127. /**
  128. * Get the Laravel environment for the current request.
  129. *
  130. * @return string|null
  131. */
  132. public static function env()
  133. {
  134. if (isset($_SERVER['LARAVEL_ENV'])) return $_SERVER['LARAVEL_ENV'];
  135. }
  136. /**
  137. * Determine the current request environment.
  138. *
  139. * @param string $env
  140. * @return bool
  141. */
  142. public static function is_env($env)
  143. {
  144. return static::env() === $env;
  145. }
  146. /**
  147. * Get the main route handling the request.
  148. *
  149. * @return Route
  150. */
  151. public static function route()
  152. {
  153. return static::$route;
  154. }
  155. }