request.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php namespace Laravel; use Closure;
  2. class Request {
  3. /**
  4. * The request URI for the current request.
  5. *
  6. * @var URI
  7. */
  8. public static $uri;
  9. /**
  10. * The route handling the current request.
  11. *
  12. * @var Routing\Route
  13. */
  14. public static $route;
  15. /**
  16. * The request data key that is used to indicate a spoofed request method.
  17. *
  18. * @var string
  19. */
  20. const spoofer = '__spoofer';
  21. /**
  22. * Get the URI instance for the current request.
  23. *
  24. * @return URI
  25. */
  26. public static function uri()
  27. {
  28. return (is_null(static::$uri)) ? static::$uri = new URI($_SERVER) : static::$uri;
  29. }
  30. /**
  31. * Get the request format.
  32. *
  33. * The format is determined by taking the "extension" of the URI.
  34. *
  35. * @param string $uri
  36. * @return string
  37. */
  38. public static function format($uri = null)
  39. {
  40. if (is_null($uri)) $uri = static::uri()->get();
  41. return (($extension = pathinfo($uri, PATHINFO_EXTENSION)) !== '') ? $extension : 'html';
  42. }
  43. /**
  44. * Get the request method.
  45. *
  46. * This will usually be the value of the REQUEST_METHOD $_SERVER variable
  47. * However, when the request method is spoofed using a hidden form value,
  48. * the method will be stored in the $_POST array.
  49. *
  50. * @return string
  51. */
  52. public static function method()
  53. {
  54. return (static::spoofed()) ? $_POST[Request::spoofer] : $_SERVER['REQUEST_METHOD'];
  55. }
  56. /**
  57. * Get an item from the $_SERVER array.
  58. *
  59. * Like most array retrieval methods, a default value may be specified.
  60. *
  61. * @param string $key
  62. * @param mixed $default
  63. * @return string
  64. */
  65. public static function server($key = null, $default = null)
  66. {
  67. return Arr::get($_SERVER, strtoupper($key), $default);
  68. }
  69. /**
  70. * Determine if the request method is being spoofed by a hidden Form element.
  71. *
  72. * @return bool
  73. */
  74. public static function spoofed()
  75. {
  76. return is_array($_POST) and array_key_exists(Request::spoofer, $_POST);
  77. }
  78. /**
  79. * Get the requestor's IP address.
  80. *
  81. * @param mixed $default
  82. * @return string
  83. */
  84. public static function ip($default = '0.0.0.0')
  85. {
  86. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  87. {
  88. return $_SERVER['HTTP_X_FORWARDED_FOR'];
  89. }
  90. elseif (isset($_SERVER['HTTP_CLIENT_IP']))
  91. {
  92. return $_SERVER['HTTP_CLIENT_IP'];
  93. }
  94. elseif (isset($_SERVER['REMOTE_ADDR']))
  95. {
  96. return $_SERVER['REMOTE_ADDR'];
  97. }
  98. return ($default instanceof Closure) ? call_user_func($default) : $default;
  99. }
  100. /**
  101. * Get the HTTP protocol for the request.
  102. *
  103. * @return string
  104. */
  105. public static function protocol()
  106. {
  107. return Arr::get($_SERVER, 'SERVER_PROTOCOL', 'HTTP/1.1');
  108. }
  109. /**
  110. * Determine if the current request is using HTTPS.
  111. *
  112. * @return bool
  113. */
  114. public static function secure()
  115. {
  116. return isset($_SERVER['HTTPS']) and strtolower($_SERVER['HTTPS']) !== 'off';
  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. }