request.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // -------------------------------------------------------
  17. // If we have already determined the URI, return it.
  18. // -------------------------------------------------------
  19. if ( ! is_null(static::$uri))
  20. {
  21. return static::$uri;
  22. }
  23. // -------------------------------------------------------
  24. // If the PATH_INFO is available, use it.
  25. // -------------------------------------------------------
  26. if (isset($_SERVER['PATH_INFO']))
  27. {
  28. $uri = $_SERVER['PATH_INFO'];
  29. }
  30. // -------------------------------------------------------
  31. // No PATH_INFO? Let's try REQUEST_URI.
  32. // -------------------------------------------------------
  33. elseif (isset($_SERVER['REQUEST_URI']))
  34. {
  35. $uri = str_replace('/index.php', '', $_SERVER['REQUEST_URI']);
  36. }
  37. // -------------------------------------------------------
  38. // Neither PATH_INFO or REQUEST_URI are available.
  39. // -------------------------------------------------------
  40. else
  41. {
  42. throw new \Exception('Unable to determine the request URI.');
  43. }
  44. $uri = trim($uri, '/');
  45. // -------------------------------------------------------
  46. // If the requests is to the root of the application, we
  47. // always return a single forward slash.
  48. // -------------------------------------------------------
  49. return static::$uri = ($uri == '') ? '/' : Str::lower($uri);
  50. }
  51. /**
  52. * Get the request method.
  53. *
  54. * @return string
  55. */
  56. public static function method()
  57. {
  58. // --------------------------------------------------------------
  59. // The method can be spoofed using a POST variable, allowing HTML
  60. // forms to simulate PUT and DELETE requests.
  61. // --------------------------------------------------------------
  62. return (isset($_POST['request_method'])) ? $_POST['request_method'] : $_SERVER['REQUEST_METHOD'];
  63. }
  64. /**
  65. * Get the requestor's IP address.
  66. *
  67. * @return string
  68. */
  69. public static function ip()
  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. }
  84. /**
  85. * Determine if the request is using HTTPS.
  86. *
  87. * @return bool
  88. */
  89. public static function is_secure()
  90. {
  91. return (static::protocol() == 'https');
  92. }
  93. /**
  94. * Get the HTTP protocol for the request.
  95. *
  96. * @return string
  97. */
  98. public static function protocol()
  99. {
  100. return (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
  101. }
  102. /**
  103. * Determine if the request is an AJAX request.
  104. *
  105. * @return bool
  106. */
  107. public static function is_ajax()
  108. {
  109. return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and Str::lower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
  110. }
  111. }