request.php 5.1 KB

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