request.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class OC_Request {
  9. /**
  10. * @brief Check overwrite condition
  11. * @returns true/false
  12. */
  13. private static function isOverwriteCondition() {
  14. $regex = '/' . OC_Config::getValue('overwritecondaddr', '') . '/';
  15. return $regex === '//' or preg_match($regex, $_SERVER['REMOTE_ADDR']) === 1;
  16. }
  17. /**
  18. * @brief Returns the server host
  19. * @returns the server host
  20. *
  21. * Returns the server host, even if the website uses one or more
  22. * reverse proxies
  23. */
  24. public static function serverHost() {
  25. if(OC::$CLI) {
  26. return 'localhost';
  27. }
  28. if(OC_Config::getValue('overwritehost', '')<>'' and self::isOverwriteCondition()) {
  29. return OC_Config::getValue('overwritehost');
  30. }
  31. if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
  32. if (strpos($_SERVER['HTTP_X_FORWARDED_HOST'], ",") !== false) {
  33. $host = trim(array_pop(explode(",", $_SERVER['HTTP_X_FORWARDED_HOST'])));
  34. }
  35. else{
  36. $host=$_SERVER['HTTP_X_FORWARDED_HOST'];
  37. }
  38. }
  39. else{
  40. $host = $_SERVER['HTTP_HOST'];
  41. }
  42. return $host;
  43. }
  44. /**
  45. * @brief Returns the server protocol
  46. * @returns the server protocol
  47. *
  48. * Returns the server protocol. It respects reverse proxy servers and load balancers
  49. */
  50. public static function serverProtocol() {
  51. if(OC_Config::getValue('overwriteprotocol', '')<>'' and self::isOverwriteCondition()) {
  52. return OC_Config::getValue('overwriteprotocol');
  53. }
  54. if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
  55. $proto = strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']);
  56. }else{
  57. if(isset($_SERVER['HTTPS']) and !empty($_SERVER['HTTPS']) and ($_SERVER['HTTPS']!='off')) {
  58. $proto = 'https';
  59. }else{
  60. $proto = 'http';
  61. }
  62. }
  63. return $proto;
  64. }
  65. /**
  66. * @brief Returns the request uri
  67. * @returns the request uri
  68. *
  69. * Returns the request uri, even if the website uses one or more
  70. * reverse proxies
  71. */
  72. public static function requestUri() {
  73. $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
  74. if (OC_Config::getValue('overwritewebroot', '') <> '' and self::isOverwriteCondition()) {
  75. $uri = self::scriptName() . substr($uri, strlen($_SERVER['SCRIPT_NAME']));
  76. }
  77. return $uri;
  78. }
  79. /**
  80. * @brief Returns the script name
  81. * @returns the script name
  82. *
  83. * Returns the script name, even if the website uses one or more
  84. * reverse proxies
  85. */
  86. public static function scriptName() {
  87. $name = $_SERVER['SCRIPT_NAME'];
  88. if (OC_Config::getValue('overwritewebroot', '') <> '' and self::isOverwriteCondition()) {
  89. $serverroot = str_replace("\\", '/', substr(__DIR__, 0, -4));
  90. $suburi = str_replace("\\", "/", substr(realpath($_SERVER["SCRIPT_FILENAME"]), strlen($serverroot)));
  91. $name = OC_Config::getValue('overwritewebroot', '') . $suburi;
  92. }
  93. return $name;
  94. }
  95. /**
  96. * @brief get Path info from request
  97. * @returns string Path info or false when not found
  98. */
  99. public static function getPathInfo() {
  100. if (array_key_exists('PATH_INFO', $_SERVER)) {
  101. $path_info = $_SERVER['PATH_INFO'];
  102. }else{
  103. $path_info = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
  104. // following is taken from Sabre_DAV_URLUtil::decodePathSegment
  105. $path_info = rawurldecode($path_info);
  106. $encoding = mb_detect_encoding($path_info, array('UTF-8', 'ISO-8859-1'));
  107. switch($encoding) {
  108. case 'ISO-8859-1' :
  109. $path_info = utf8_encode($path_info);
  110. }
  111. // end copy
  112. }
  113. return $path_info;
  114. }
  115. /**
  116. * @brief Check if this is a no-cache request
  117. * @returns true for no-cache
  118. */
  119. static public function isNoCache() {
  120. if (!isset($_SERVER['HTTP_CACHE_CONTROL'])) {
  121. return false;
  122. }
  123. return $_SERVER['HTTP_CACHE_CONTROL'] == 'no-cache';
  124. }
  125. /**
  126. * @brief Check if the requestor understands gzip
  127. * @returns true for gzip encoding supported
  128. */
  129. static public function acceptGZip() {
  130. if (!isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  131. return false;
  132. }
  133. $HTTP_ACCEPT_ENCODING = $_SERVER["HTTP_ACCEPT_ENCODING"];
  134. if( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false )
  135. return 'x-gzip';
  136. else if( strpos($HTTP_ACCEPT_ENCODING, 'gzip') !== false )
  137. return 'gzip';
  138. return false;
  139. }
  140. }