request.php 2.6 KB

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