request.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php namespace System;
  2. class Request {
  3. /**
  4. * The request URI.
  5. *
  6. * @var string
  7. */
  8. public static $uri;
  9. /**
  10. * Get the request URI.
  11. *
  12. * @return string
  13. */
  14. public static function uri()
  15. {
  16. if ( ! is_null(static::$uri))
  17. {
  18. return static::$uri;
  19. }
  20. if (isset($_SERVER['PATH_INFO']))
  21. {
  22. return static::$uri = static::tidy($_SERVER['PATH_INFO']);
  23. }
  24. if ( ! isset($_SERVER['REQUEST_URI']))
  25. {
  26. throw new \Exception('Unable to determine the request URI.');
  27. }
  28. $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
  29. // --------------------------------------------------------------
  30. // Slice the application URL off of the URI.
  31. // --------------------------------------------------------------
  32. if (strpos($uri, $base_url = parse_url(Config::get('application.url'), PHP_URL_PATH)) === 0)
  33. {
  34. $uri = substr($uri, strlen($base_url));
  35. }
  36. return static::$uri = static::tidy($uri);
  37. }
  38. /**
  39. * Tidy up a URI for use by Laravel. For empty URIs, a forward
  40. * slash will be returned.
  41. *
  42. * @param string $uri
  43. * @return string
  44. */
  45. private static function tidy($uri)
  46. {
  47. return ($uri != '/') ? Str::lower(trim($uri, '/')) : '/';
  48. }
  49. /**
  50. * Get the request method.
  51. *
  52. * @return string
  53. */
  54. public static function method()
  55. {
  56. // --------------------------------------------------------------
  57. // The method can be spoofed using a POST variable. This allows
  58. // HTML forms to simulate PUT and DELETE methods.
  59. // --------------------------------------------------------------
  60. return (isset($_POST['request_method'])) ? $_POST['request_method'] : $_SERVER['REQUEST_METHOD'];
  61. }
  62. /**
  63. * Get the requestor's IP address.
  64. *
  65. * @return string
  66. */
  67. public static function ip()
  68. {
  69. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  70. {
  71. return $_SERVER['HTTP_X_FORWARDED_FOR'];
  72. }
  73. elseif (isset($_SERVER['HTTP_CLIENT_IP']))
  74. {
  75. return $_SERVER['HTTP_CLIENT_IP'];
  76. }
  77. elseif (isset($_SERVER['REMOTE_ADDR']))
  78. {
  79. return $_SERVER['REMOTE_ADDR'];
  80. }
  81. }
  82. /**
  83. * Determine if the request is using HTTPS.
  84. *
  85. * @return bool
  86. */
  87. public static function is_secure()
  88. {
  89. return (static::protocol() == 'https');
  90. }
  91. /**
  92. * Get the HTTP protocol for the request.
  93. *
  94. * @return string
  95. */
  96. public static function protocol()
  97. {
  98. return (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
  99. }
  100. /**
  101. * Determine if the request is an AJAX request.
  102. *
  103. * @return bool
  104. */
  105. public static function is_ajax()
  106. {
  107. return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and Str::lower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
  108. }
  109. }