request.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. return (static::spoofed()) ? $_POST[Request::spoofer] : $_SERVER['REQUEST_METHOD'];
  32. }
  33. /**
  34. * Get an item from the $_SERVER array.
  35. *
  36. * @param string $key
  37. * @param mixed $default
  38. * @return string
  39. */
  40. public static function server($key = null, $default = null)
  41. {
  42. return array_get($_SERVER, strtoupper($key), $default);
  43. }
  44. /**
  45. * Determine if the request method is being spoofed by a hidden Form element.
  46. *
  47. * @return bool
  48. */
  49. public static function spoofed()
  50. {
  51. return is_array($_POST) and array_key_exists(Request::spoofer, $_POST);
  52. }
  53. /**
  54. * Get the requestor's IP address.
  55. *
  56. * @param mixed $default
  57. * @return string
  58. */
  59. public static function ip($default = '0.0.0.0')
  60. {
  61. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  62. {
  63. return $_SERVER['HTTP_X_FORWARDED_FOR'];
  64. }
  65. elseif (isset($_SERVER['HTTP_CLIENT_IP']))
  66. {
  67. return $_SERVER['HTTP_CLIENT_IP'];
  68. }
  69. elseif (isset($_SERVER['REMOTE_ADDR']))
  70. {
  71. return $_SERVER['REMOTE_ADDR'];
  72. }
  73. return value($default);
  74. }
  75. /**
  76. * Get the HTTP protocol for the request.
  77. *
  78. * @return string
  79. */
  80. public static function protocol()
  81. {
  82. return array_get($_SERVER, 'SERVER_PROTOCOL', 'HTTP/1.1');
  83. }
  84. /**
  85. * Determine if the current request is using HTTPS.
  86. *
  87. * @return bool
  88. */
  89. public static function secure()
  90. {
  91. return isset($_SERVER['HTTPS']) and strtolower($_SERVER['HTTPS']) !== 'off';
  92. }
  93. /**
  94. * Determine if the request has been forged.
  95. *
  96. * The session CSRF token will be compared to the CSRF token in the request input.
  97. *
  98. * @return bool
  99. */
  100. public static function forged()
  101. {
  102. return Input::get(Session::csrf_token) !== Session::token();
  103. }
  104. /**
  105. * Determine if the current request is an AJAX request.
  106. *
  107. * @return bool
  108. */
  109. public static function ajax()
  110. {
  111. if ( ! isset($_SERVER['HTTP_X_REQUESTED_WITH'])) return false;
  112. return strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
  113. }
  114. /**
  115. * Determine if the current request is via the command line.
  116. *
  117. * @return bool
  118. */
  119. public static function cli()
  120. {
  121. return defined('STDIN');
  122. }
  123. /**
  124. * Get the main route handling the request.
  125. *
  126. * @return Route
  127. */
  128. public static function route()
  129. {
  130. return static::$route;
  131. }
  132. }