request.php 3.2 KB

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