request.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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($type = '') {
  14. $regex = '/' . OC_Config::getValue('overwritecondaddr', '') . '/';
  15. return $regex === '//' or preg_match($regex, $_SERVER['REMOTE_ADDR']) === 1
  16. or ($type !== 'protocol' and OC_Config::getValue('forcessl', false));
  17. }
  18. /**
  19. * @brief Returns the server host
  20. * @returns the server host
  21. *
  22. * Returns the server host, even if the website uses one or more
  23. * reverse proxies
  24. */
  25. public static function serverHost() {
  26. if(OC::$CLI) {
  27. return 'localhost';
  28. }
  29. if(OC_Config::getValue('overwritehost', '') !== '' and self::isOverwriteCondition()) {
  30. return OC_Config::getValue('overwritehost');
  31. }
  32. if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
  33. if (strpos($_SERVER['HTTP_X_FORWARDED_HOST'], ",") !== false) {
  34. $host = trim(array_pop(explode(",", $_SERVER['HTTP_X_FORWARDED_HOST'])));
  35. }
  36. else{
  37. $host=$_SERVER['HTTP_X_FORWARDED_HOST'];
  38. }
  39. }
  40. else{
  41. $host = $_SERVER['HTTP_HOST'];
  42. }
  43. return $host;
  44. }
  45. /**
  46. * @brief Returns the server protocol
  47. * @returns the server protocol
  48. *
  49. * Returns the server protocol. It respects reverse proxy servers and load balancers
  50. */
  51. public static function serverProtocol() {
  52. if(OC_Config::getValue('overwriteprotocol', '') !== '' and self::isOverwriteCondition('protocol')) {
  53. return OC_Config::getValue('overwriteprotocol');
  54. }
  55. if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
  56. $proto = strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']);
  57. }else{
  58. if(isset($_SERVER['HTTPS']) and !empty($_SERVER['HTTPS']) and ($_SERVER['HTTPS']!='off')) {
  59. $proto = 'https';
  60. }else{
  61. $proto = 'http';
  62. }
  63. }
  64. return $proto;
  65. }
  66. /**
  67. * @brief Returns the request uri
  68. * @returns the request uri
  69. *
  70. * Returns the request uri, even if the website uses one or more
  71. * reverse proxies
  72. */
  73. public static function requestUri() {
  74. $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
  75. if (OC_Config::getValue('overwritewebroot', '') !== '' and self::isOverwriteCondition()) {
  76. $uri = self::scriptName() . substr($uri, strlen($_SERVER['SCRIPT_NAME']));
  77. }
  78. return $uri;
  79. }
  80. /**
  81. * @brief Returns the script name
  82. * @returns the script name
  83. *
  84. * Returns the script name, even if the website uses one or more
  85. * reverse proxies
  86. */
  87. public static function scriptName() {
  88. $name = $_SERVER['SCRIPT_NAME'];
  89. if (OC_Config::getValue('overwritewebroot', '') !== '' and self::isOverwriteCondition()) {
  90. $serverroot = str_replace("\\", '/', substr(__DIR__, 0, -4));
  91. $suburi = str_replace("\\", "/", substr(realpath($_SERVER["SCRIPT_FILENAME"]), strlen($serverroot)));
  92. $name = OC_Config::getValue('overwritewebroot', '') . $suburi;
  93. }
  94. return $name;
  95. }
  96. /**
  97. * @brief get Path info from request
  98. * @returns string Path info or false when not found
  99. */
  100. public static function getPathInfo() {
  101. if (array_key_exists('PATH_INFO', $_SERVER)) {
  102. $path_info = $_SERVER['PATH_INFO'];
  103. }else{
  104. $path_info = self::getRawPathInfo();
  105. // following is taken from Sabre_DAV_URLUtil::decodePathSegment
  106. $path_info = rawurldecode($path_info);
  107. $encoding = mb_detect_encoding($path_info, array('UTF-8', 'ISO-8859-1'));
  108. switch($encoding) {
  109. case 'ISO-8859-1' :
  110. $path_info = utf8_encode($path_info);
  111. }
  112. // end copy
  113. }
  114. return $path_info;
  115. }
  116. /**
  117. * @brief get Path info from request, not urldecoded
  118. * @returns string Path info or false when not found
  119. */
  120. public static function getRawPathInfo() {
  121. $path_info = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
  122. // Remove the query string from REQUEST_URI
  123. if ($pos = strpos($path_info, '?')) {
  124. $path_info = substr($path_info, 0, $pos);
  125. }
  126. return $path_info;
  127. }
  128. /**
  129. * @brief Check if this is a no-cache request
  130. * @returns true for no-cache
  131. */
  132. static public function isNoCache() {
  133. if (!isset($_SERVER['HTTP_CACHE_CONTROL'])) {
  134. return false;
  135. }
  136. return $_SERVER['HTTP_CACHE_CONTROL'] == 'no-cache';
  137. }
  138. /**
  139. * @brief Check if the requestor understands gzip
  140. * @returns true for gzip encoding supported
  141. */
  142. static public function acceptGZip() {
  143. if (!isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  144. return false;
  145. }
  146. $HTTP_ACCEPT_ENCODING = $_SERVER["HTTP_ACCEPT_ENCODING"];
  147. if( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false )
  148. return 'x-gzip';
  149. else if( strpos($HTTP_ACCEPT_ENCODING, 'gzip') !== false )
  150. return 'gzip';
  151. return false;
  152. }
  153. /**
  154. * @brief Check if the requester sent along an mtime
  155. * @returns false or an mtime
  156. */
  157. static public function hasModificationTime () {
  158. if (isset($_SERVER['HTTP_X_OC_MTIME'])) {
  159. return $_SERVER['HTTP_X_OC_MTIME'];
  160. } else {
  161. return false;
  162. }
  163. }
  164. }