2012-06-15 14:07:31 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class OC_Request {
|
2013-01-06 15:24:40 +04:00
|
|
|
/**
|
|
|
|
* @brief Check overwrite condition
|
2013-07-02 01:51:43 +04:00
|
|
|
* @returns bool
|
2013-01-06 15:24:40 +04:00
|
|
|
*/
|
2013-02-23 15:58:06 +04:00
|
|
|
private static function isOverwriteCondition($type = '') {
|
2013-01-06 15:24:40 +04:00
|
|
|
$regex = '/' . OC_Config::getValue('overwritecondaddr', '') . '/';
|
2013-02-23 15:58:06 +04:00
|
|
|
return $regex === '//' or preg_match($regex, $_SERVER['REMOTE_ADDR']) === 1
|
2013-03-09 14:39:20 +04:00
|
|
|
or ($type !== 'protocol' and OC_Config::getValue('forcessl', false));
|
2013-01-06 15:24:40 +04:00
|
|
|
}
|
|
|
|
|
2012-08-07 00:16:45 +04:00
|
|
|
/**
|
|
|
|
* @brief Returns the server host
|
2013-06-17 02:02:42 +04:00
|
|
|
* @returns string the server host
|
2012-08-07 00:16:45 +04:00
|
|
|
*
|
|
|
|
* Returns the server host, even if the website uses one or more
|
|
|
|
* reverse proxies
|
|
|
|
*/
|
|
|
|
public static function serverHost() {
|
2012-09-07 17:22:01 +04:00
|
|
|
if(OC::$CLI) {
|
2012-08-07 00:16:45 +04:00
|
|
|
return 'localhost';
|
|
|
|
}
|
2013-03-09 14:39:20 +04:00
|
|
|
if(OC_Config::getValue('overwritehost', '') !== '' and self::isOverwriteCondition()) {
|
2013-01-14 23:30:39 +04:00
|
|
|
return OC_Config::getValue('overwritehost');
|
2012-11-22 22:22:00 +04:00
|
|
|
}
|
2012-08-07 00:16:45 +04:00
|
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
|
|
|
|
if (strpos($_SERVER['HTTP_X_FORWARDED_HOST'], ",") !== false) {
|
|
|
|
$host = trim(array_pop(explode(",", $_SERVER['HTTP_X_FORWARDED_HOST'])));
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
$host=$_SERVER['HTTP_X_FORWARDED_HOST'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2013-07-02 01:51:43 +04:00
|
|
|
if (isset($_SERVER['HTTP_HOST'])) {
|
|
|
|
return $_SERVER['HTTP_HOST'];
|
|
|
|
}
|
|
|
|
if (isset($_SERVER['SERVER_NAME'])) {
|
|
|
|
return $_SERVER['SERVER_NAME'];
|
|
|
|
}
|
|
|
|
return 'localhost';
|
2012-08-07 00:16:45 +04:00
|
|
|
}
|
|
|
|
return $host;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the server protocol
|
2013-06-17 02:02:42 +04:00
|
|
|
* @returns string the server protocol
|
2012-08-07 00:16:45 +04:00
|
|
|
*
|
|
|
|
* Returns the server protocol. It respects reverse proxy servers and load balancers
|
|
|
|
*/
|
|
|
|
public static function serverProtocol() {
|
2013-03-09 14:39:20 +04:00
|
|
|
if(OC_Config::getValue('overwriteprotocol', '') !== '' and self::isOverwriteCondition('protocol')) {
|
2013-01-14 23:30:39 +04:00
|
|
|
return OC_Config::getValue('overwriteprotocol');
|
2012-11-22 22:22:00 +04:00
|
|
|
}
|
2012-08-07 00:16:45 +04:00
|
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
|
|
|
|
$proto = strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']);
|
|
|
|
}else{
|
|
|
|
if(isset($_SERVER['HTTPS']) and !empty($_SERVER['HTTPS']) and ($_SERVER['HTTPS']!='off')) {
|
|
|
|
$proto = 'https';
|
|
|
|
}else{
|
|
|
|
$proto = 'http';
|
|
|
|
}
|
|
|
|
}
|
2012-09-04 10:18:28 +04:00
|
|
|
return $proto;
|
2012-08-07 00:16:45 +04:00
|
|
|
}
|
|
|
|
|
2012-09-09 14:54:47 +04:00
|
|
|
/**
|
|
|
|
* @brief Returns the request uri
|
2013-06-17 02:02:42 +04:00
|
|
|
* @returns string the request uri
|
2012-09-09 14:54:47 +04:00
|
|
|
*
|
|
|
|
* Returns the request uri, even if the website uses one or more
|
|
|
|
* reverse proxies
|
|
|
|
*/
|
|
|
|
public static function requestUri() {
|
|
|
|
$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
|
2013-03-09 14:39:20 +04:00
|
|
|
if (OC_Config::getValue('overwritewebroot', '') !== '' and self::isOverwriteCondition()) {
|
2012-09-09 14:54:47 +04:00
|
|
|
$uri = self::scriptName() . substr($uri, strlen($_SERVER['SCRIPT_NAME']));
|
|
|
|
}
|
|
|
|
return $uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the script name
|
2013-06-17 02:02:42 +04:00
|
|
|
* @returns string the script name
|
2012-09-09 14:54:47 +04:00
|
|
|
*
|
|
|
|
* Returns the script name, even if the website uses one or more
|
|
|
|
* reverse proxies
|
|
|
|
*/
|
|
|
|
public static function scriptName() {
|
|
|
|
$name = $_SERVER['SCRIPT_NAME'];
|
2013-03-09 14:39:20 +04:00
|
|
|
if (OC_Config::getValue('overwritewebroot', '') !== '' and self::isOverwriteCondition()) {
|
2012-09-09 14:54:47 +04:00
|
|
|
$serverroot = str_replace("\\", '/', substr(__DIR__, 0, -4));
|
|
|
|
$suburi = str_replace("\\", "/", substr(realpath($_SERVER["SCRIPT_FILENAME"]), strlen($serverroot)));
|
|
|
|
$name = OC_Config::getValue('overwritewebroot', '') . $suburi;
|
|
|
|
}
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2012-08-07 22:33:25 +04:00
|
|
|
/**
|
|
|
|
* @brief get Path info from request
|
|
|
|
* @returns string Path info or false when not found
|
|
|
|
*/
|
|
|
|
public static function getPathInfo() {
|
2012-09-07 17:22:01 +04:00
|
|
|
if (array_key_exists('PATH_INFO', $_SERVER)) {
|
2012-08-07 22:33:25 +04:00
|
|
|
$path_info = $_SERVER['PATH_INFO'];
|
|
|
|
}else{
|
2013-02-28 00:38:58 +04:00
|
|
|
$path_info = self::getRawPathInfo();
|
2012-08-07 22:42:54 +04:00
|
|
|
// following is taken from Sabre_DAV_URLUtil::decodePathSegment
|
|
|
|
$path_info = rawurldecode($path_info);
|
2012-11-04 14:10:46 +04:00
|
|
|
$encoding = mb_detect_encoding($path_info, array('UTF-8', 'ISO-8859-1'));
|
2012-08-07 22:42:54 +04:00
|
|
|
|
|
|
|
switch($encoding) {
|
|
|
|
|
2013-02-10 01:44:11 +04:00
|
|
|
case 'ISO-8859-1' :
|
|
|
|
$path_info = utf8_encode($path_info);
|
2012-08-07 22:42:54 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
// end copy
|
2012-08-07 22:33:25 +04:00
|
|
|
}
|
|
|
|
return $path_info;
|
|
|
|
}
|
|
|
|
|
2013-02-28 00:38:58 +04:00
|
|
|
/**
|
|
|
|
* @brief get Path info from request, not urldecoded
|
|
|
|
* @returns string Path info or false when not found
|
|
|
|
*/
|
|
|
|
public static function getRawPathInfo() {
|
|
|
|
$path_info = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
|
|
|
|
// Remove the query string from REQUEST_URI
|
|
|
|
if ($pos = strpos($path_info, '?')) {
|
|
|
|
$path_info = substr($path_info, 0, $pos);
|
|
|
|
}
|
|
|
|
return $path_info;
|
|
|
|
}
|
|
|
|
|
2012-08-07 00:17:10 +04:00
|
|
|
/**
|
|
|
|
* @brief Check if this is a no-cache request
|
2013-06-17 02:02:42 +04:00
|
|
|
* @returns boolean true for no-cache
|
2012-08-07 00:17:10 +04:00
|
|
|
*/
|
2012-06-15 14:07:31 +04:00
|
|
|
static public function isNoCache() {
|
|
|
|
if (!isset($_SERVER['HTTP_CACHE_CONTROL'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $_SERVER['HTTP_CACHE_CONTROL'] == 'no-cache';
|
|
|
|
}
|
|
|
|
|
2012-08-07 00:17:10 +04:00
|
|
|
/**
|
|
|
|
* @brief Check if the requestor understands gzip
|
2013-06-17 02:02:42 +04:00
|
|
|
* @returns boolean true for gzip encoding supported
|
2012-08-07 00:17:10 +04:00
|
|
|
*/
|
2012-06-15 14:07:31 +04:00
|
|
|
static public function acceptGZip() {
|
2012-06-18 13:33:24 +04:00
|
|
|
if (!isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-15 14:07:31 +04:00
|
|
|
$HTTP_ACCEPT_ENCODING = $_SERVER["HTTP_ACCEPT_ENCODING"];
|
|
|
|
if( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false )
|
|
|
|
return 'x-gzip';
|
2012-11-04 14:10:46 +04:00
|
|
|
else if( strpos($HTTP_ACCEPT_ENCODING, 'gzip') !== false )
|
2012-06-15 14:07:31 +04:00
|
|
|
return 'gzip';
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-22 20:21:57 +04:00
|
|
|
|
2013-02-10 14:05:43 +04:00
|
|
|
/**
|
|
|
|
* @brief Check if the requester sent along an mtime
|
|
|
|
* @returns false or an mtime
|
|
|
|
*/
|
|
|
|
static public function hasModificationTime () {
|
|
|
|
if (isset($_SERVER['HTTP_X_OC_MTIME'])) {
|
|
|
|
return $_SERVER['HTTP_X_OC_MTIME'];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-06-15 14:07:31 +04:00
|
|
|
}
|