request.php 3.1 KB

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