request.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 Returns the server host
  11. * @returns the server host
  12. *
  13. * Returns the server host, even if the website uses one or more
  14. * reverse proxies
  15. */
  16. public static function serverHost() {
  17. if(OC::$CLI) {
  18. return 'localhost';
  19. }
  20. if(OC_Config::getValue('overwritehost', '')<>'') {
  21. return OC_Config::getValue('overwritehost');
  22. }
  23. if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
  24. if (strpos($_SERVER['HTTP_X_FORWARDED_HOST'], ",") !== false) {
  25. $host = trim(array_pop(explode(",", $_SERVER['HTTP_X_FORWARDED_HOST'])));
  26. }
  27. else{
  28. $host=$_SERVER['HTTP_X_FORWARDED_HOST'];
  29. }
  30. }
  31. else{
  32. $host = $_SERVER['HTTP_HOST'];
  33. }
  34. return $host;
  35. }
  36. /**
  37. * @brief Returns the server protocol
  38. * @returns the server protocol
  39. *
  40. * Returns the server protocol. It respects reverse proxy servers and load balancers
  41. */
  42. public static function serverProtocol() {
  43. if(OC_Config::getValue('overwriteprotocol', '')<>'') {
  44. return OC_Config::getValue('overwriteprotocol');
  45. }
  46. if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
  47. $proto = strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']);
  48. }else{
  49. if(isset($_SERVER['HTTPS']) and !empty($_SERVER['HTTPS']) and ($_SERVER['HTTPS']!='off')) {
  50. $proto = 'https';
  51. }else{
  52. $proto = 'http';
  53. }
  54. }
  55. return $proto;
  56. }
  57. /**
  58. * @brief get Path info from request
  59. * @returns string Path info or false when not found
  60. */
  61. public static function getPathInfo() {
  62. if (array_key_exists('PATH_INFO', $_SERVER)) {
  63. $path_info = $_SERVER['PATH_INFO'];
  64. }else{
  65. $path_info = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
  66. // following is taken from Sabre_DAV_URLUtil::decodePathSegment
  67. $path_info = rawurldecode($path_info);
  68. $encoding = mb_detect_encoding($path_info, array('UTF-8', 'ISO-8859-1'));
  69. switch($encoding) {
  70. case 'ISO-8859-1' :
  71. $path_info = utf8_encode($path_info);
  72. }
  73. // end copy
  74. }
  75. return $path_info;
  76. }
  77. /**
  78. * @brief Check if this is a no-cache request
  79. * @returns true for no-cache
  80. */
  81. static public function isNoCache() {
  82. if (!isset($_SERVER['HTTP_CACHE_CONTROL'])) {
  83. return false;
  84. }
  85. return $_SERVER['HTTP_CACHE_CONTROL'] == 'no-cache';
  86. }
  87. /**
  88. * @brief Check if the requestor understands gzip
  89. * @returns true for gzip encoding supported
  90. */
  91. static public function acceptGZip() {
  92. if (!isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  93. return false;
  94. }
  95. $HTTP_ACCEPT_ENCODING = $_SERVER["HTTP_ACCEPT_ENCODING"];
  96. if( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false )
  97. return 'x-gzip';
  98. else if( strpos($HTTP_ACCEPT_ENCODING, 'gzip') !== false )
  99. return 'gzip';
  100. return false;
  101. }
  102. }