request.php 5.9 KB

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