request.php 2.7 KB

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