2012-06-15 10:07:31 +00: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-12-12 10:32:56 +00:00
|
|
|
|
|
|
|
const USER_AGENT_IE = '/MSIE/';
|
|
|
|
// Android Chrome user agent: https://developers.google.com/chrome/mobile/docs/user-agent
|
|
|
|
const USER_AGENT_ANDROID_MOBILE_CHROME = '#Android.*Chrome/[.0-9]*#';
|
2014-01-29 09:58:34 +00:00
|
|
|
const USER_AGENT_FREEBOX = '#^Mozilla/5\.0$#';
|
2013-12-12 10:32:56 +00:00
|
|
|
|
2013-01-06 11:24:40 +00:00
|
|
|
/**
|
|
|
|
* @brief Check overwrite condition
|
2013-11-24 20:18:14 +00:00
|
|
|
* @param string $type
|
2013-07-01 21:51:43 +00:00
|
|
|
* @returns bool
|
2013-01-06 11:24:40 +00:00
|
|
|
*/
|
2013-02-23 11:58:06 +00:00
|
|
|
private static function isOverwriteCondition($type = '') {
|
2013-01-06 11:24:40 +00:00
|
|
|
$regex = '/' . OC_Config::getValue('overwritecondaddr', '') . '/';
|
2013-02-23 11:58:06 +00:00
|
|
|
return $regex === '//' or preg_match($regex, $_SERVER['REMOTE_ADDR']) === 1
|
2013-03-09 10:39:20 +00:00
|
|
|
or ($type !== 'protocol' and OC_Config::getValue('forcessl', false));
|
2013-01-06 11:24:40 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 15:26:37 +00:00
|
|
|
/**
|
|
|
|
* @brief Checks whether a domain is considered as trusted. This is used to prevent Host Header Poisoning.
|
|
|
|
* @param string $host
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isTrustedDomain($domain) {
|
|
|
|
$trustedList = \OC_Config::getValue('trusted_domains', array(''));
|
|
|
|
return in_array($domain, $trustedList);
|
|
|
|
}
|
|
|
|
|
2012-08-06 20:16:45 +00:00
|
|
|
/**
|
|
|
|
* @brief Returns the server host
|
2013-06-16 22:02:42 +00:00
|
|
|
* @returns string the server host
|
2012-08-06 20:16:45 +00:00
|
|
|
*
|
|
|
|
* Returns the server host, even if the website uses one or more
|
|
|
|
* reverse proxies
|
|
|
|
*/
|
|
|
|
public static function serverHost() {
|
2012-09-07 13:22:01 +00:00
|
|
|
if(OC::$CLI) {
|
2012-08-06 20:16:45 +00:00
|
|
|
return 'localhost';
|
|
|
|
}
|
2013-03-09 10:39:20 +00:00
|
|
|
if(OC_Config::getValue('overwritehost', '') !== '' and self::isOverwriteCondition()) {
|
2013-01-14 19:30:39 +00:00
|
|
|
return OC_Config::getValue('overwritehost');
|
2012-11-22 18:22:00 +00:00
|
|
|
}
|
2012-08-06 20:16:45 +00: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{
|
2014-02-18 15:26:37 +00:00
|
|
|
$host = $_SERVER['HTTP_X_FORWARDED_HOST'];
|
2012-08-06 20:16:45 +00:00
|
|
|
}
|
2014-02-18 15:26:37 +00:00
|
|
|
} else {
|
2013-07-01 21:51:43 +00:00
|
|
|
if (isset($_SERVER['HTTP_HOST'])) {
|
2014-02-18 15:26:37 +00:00
|
|
|
$host = $_SERVER['HTTP_HOST'];
|
2013-07-01 21:51:43 +00:00
|
|
|
}
|
2014-02-25 10:22:53 +00:00
|
|
|
else if (isset($_SERVER['SERVER_NAME'])) {
|
2014-02-18 15:26:37 +00:00
|
|
|
$host = $_SERVER['SERVER_NAME'];
|
2013-07-01 21:51:43 +00:00
|
|
|
}
|
2012-08-06 20:16:45 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 15:26:37 +00:00
|
|
|
// Verify that the host is a trusted domain if the trusted domains
|
|
|
|
// are defined
|
|
|
|
// If no trusted domain is provided the first trusted domain is returned
|
|
|
|
if(self::isTrustedDomain($host) || \OC_Config::getValue('trusted_domains', "") === "") {
|
|
|
|
return $host;
|
|
|
|
} else {
|
|
|
|
$trustedList = \OC_Config::getValue('trusted_domains', array(''));
|
|
|
|
return $trustedList[0];
|
|
|
|
}
|
|
|
|
}
|
2012-08-06 20:16:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the server protocol
|
2013-06-16 22:02:42 +00:00
|
|
|
* @returns string the server protocol
|
2012-08-06 20:16:45 +00:00
|
|
|
*
|
|
|
|
* Returns the server protocol. It respects reverse proxy servers and load balancers
|
|
|
|
*/
|
|
|
|
public static function serverProtocol() {
|
2013-03-09 10:39:20 +00:00
|
|
|
if(OC_Config::getValue('overwriteprotocol', '') !== '' and self::isOverwriteCondition('protocol')) {
|
2013-01-14 19:30:39 +00:00
|
|
|
return OC_Config::getValue('overwriteprotocol');
|
2012-11-22 18:22:00 +00:00
|
|
|
}
|
2012-08-06 20:16:45 +00:00
|
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
|
|
|
|
$proto = strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']);
|
2014-02-18 15:26:37 +00:00
|
|
|
// Verify that the protocol is always HTTP or HTTPS
|
|
|
|
// default to http if an invalid value is provided
|
|
|
|
return $proto === 'https' ? 'https' : 'http';
|
|
|
|
}
|
|
|
|
if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') {
|
|
|
|
return 'https';
|
2012-08-06 20:16:45 +00:00
|
|
|
}
|
2014-02-18 15:26:37 +00:00
|
|
|
return 'http';
|
2012-08-06 20:16:45 +00:00
|
|
|
}
|
|
|
|
|
2012-09-09 10:54:47 +00:00
|
|
|
/**
|
|
|
|
* @brief Returns the request uri
|
2013-06-16 22:02:42 +00:00
|
|
|
* @returns string the request uri
|
2012-09-09 10:54:47 +00:00
|
|
|
*
|
|
|
|
* Returns the request uri, even if the website uses one or more
|
|
|
|
* reverse proxies
|
2014-02-06 15:30:58 +00:00
|
|
|
* @return string
|
2012-09-09 10:54:47 +00:00
|
|
|
*/
|
|
|
|
public static function requestUri() {
|
|
|
|
$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
|
2013-03-09 10:39:20 +00:00
|
|
|
if (OC_Config::getValue('overwritewebroot', '') !== '' and self::isOverwriteCondition()) {
|
2012-09-09 10:54:47 +00:00
|
|
|
$uri = self::scriptName() . substr($uri, strlen($_SERVER['SCRIPT_NAME']));
|
|
|
|
}
|
|
|
|
return $uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the script name
|
2014-02-08 10:47:55 +00:00
|
|
|
* @return string the script name
|
2012-09-09 10:54:47 +00: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 10:39:20 +00:00
|
|
|
if (OC_Config::getValue('overwritewebroot', '') !== '' and self::isOverwriteCondition()) {
|
2013-11-24 11:41:09 +00:00
|
|
|
$serverroot = str_replace("\\", '/', substr(__DIR__, 0, -strlen('lib/private/')));
|
2012-09-09 10:54:47 +00:00
|
|
|
$suburi = str_replace("\\", "/", substr(realpath($_SERVER["SCRIPT_FILENAME"]), strlen($serverroot)));
|
|
|
|
$name = OC_Config::getValue('overwritewebroot', '') . $suburi;
|
|
|
|
}
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:33:25 +00:00
|
|
|
/**
|
|
|
|
* @brief get Path info from request
|
2014-02-08 10:47:55 +00:00
|
|
|
* @return string Path info or false when not found
|
2012-08-07 18:33:25 +00:00
|
|
|
*/
|
|
|
|
public static function getPathInfo() {
|
2012-09-07 13:22:01 +00:00
|
|
|
if (array_key_exists('PATH_INFO', $_SERVER)) {
|
2012-08-07 18:33:25 +00:00
|
|
|
$path_info = $_SERVER['PATH_INFO'];
|
|
|
|
}else{
|
2013-02-27 20:38:58 +00:00
|
|
|
$path_info = self::getRawPathInfo();
|
2012-08-07 18:42:54 +00:00
|
|
|
// following is taken from Sabre_DAV_URLUtil::decodePathSegment
|
|
|
|
$path_info = rawurldecode($path_info);
|
2012-11-04 10:10:46 +00:00
|
|
|
$encoding = mb_detect_encoding($path_info, array('UTF-8', 'ISO-8859-1'));
|
2012-08-07 18:42:54 +00:00
|
|
|
|
|
|
|
switch($encoding) {
|
|
|
|
|
2013-02-09 21:44:11 +00:00
|
|
|
case 'ISO-8859-1' :
|
|
|
|
$path_info = utf8_encode($path_info);
|
2012-08-07 18:42:54 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
// end copy
|
2012-08-07 18:33:25 +00:00
|
|
|
}
|
|
|
|
return $path_info;
|
|
|
|
}
|
|
|
|
|
2013-02-27 20:38:58 +00:00
|
|
|
/**
|
|
|
|
* @brief get Path info from request, not urldecoded
|
2014-02-08 10:47:55 +00:00
|
|
|
* @return string Path info or false when not found
|
2013-02-27 20:38:58 +00:00
|
|
|
*/
|
|
|
|
public static function getRawPathInfo() {
|
2013-11-25 13:21:51 +00:00
|
|
|
$requestUri = $_SERVER['REQUEST_URI'];
|
|
|
|
// remove too many leading slashes - can be caused by reverse proxy configuration
|
2013-11-25 13:42:34 +00:00
|
|
|
if (strpos($requestUri, '/') === 0) {
|
|
|
|
$requestUri = '/' . ltrim($requestUri, '/');
|
|
|
|
}
|
|
|
|
|
2013-11-26 13:13:33 +00:00
|
|
|
// Remove the query string from REQUEST_URI
|
|
|
|
if ($pos = strpos($requestUri, '?')) {
|
|
|
|
$requestUri = substr($requestUri, 0, $pos);
|
|
|
|
}
|
|
|
|
|
2013-11-25 13:42:34 +00:00
|
|
|
$scriptName = $_SERVER['SCRIPT_NAME'];
|
2013-11-26 13:13:33 +00:00
|
|
|
$path_info = $requestUri;
|
|
|
|
|
|
|
|
// strip off the script name's dir and file name
|
|
|
|
list($path, $name) = \Sabre_DAV_URLUtil::splitPath($scriptName);
|
|
|
|
if (!empty($path)) {
|
|
|
|
if( $path === $path_info || strpos($path_info, $path.'/') === 0) {
|
|
|
|
$path_info = substr($path_info, strlen($path));
|
|
|
|
} else {
|
|
|
|
throw new Exception("The requested uri($requestUri) cannot be processed by the script '$scriptName')");
|
|
|
|
}
|
2013-11-25 13:42:34 +00:00
|
|
|
}
|
2013-11-26 15:52:33 +00:00
|
|
|
if (strpos($path_info, '/'.$name) === 0) {
|
2013-11-26 13:13:33 +00:00
|
|
|
$path_info = substr($path_info, strlen($name) + 1);
|
2013-02-27 20:38:58 +00:00
|
|
|
}
|
2013-11-26 13:13:33 +00:00
|
|
|
if (strpos($path_info, $name) === 0) {
|
|
|
|
$path_info = substr($path_info, strlen($name));
|
|
|
|
}
|
2013-11-27 20:28:54 +00:00
|
|
|
if($path_info === '/'){
|
|
|
|
return '';
|
|
|
|
} else {
|
|
|
|
return $path_info;
|
|
|
|
}
|
2013-02-27 20:38:58 +00:00
|
|
|
}
|
|
|
|
|
2013-02-10 10:05:43 +00:00
|
|
|
/**
|
|
|
|
* @brief Check if the requester sent along an mtime
|
2014-02-08 10:47:55 +00:00
|
|
|
* @return false or an mtime
|
2013-02-10 10:05:43 +00:00
|
|
|
*/
|
|
|
|
static public function hasModificationTime () {
|
|
|
|
if (isset($_SERVER['HTTP_X_OC_MTIME'])) {
|
|
|
|
return $_SERVER['HTTP_X_OC_MTIME'];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-12-12 10:32:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the user agent matches a given regex
|
|
|
|
* @param string|array $agent agent name or array of agent names
|
|
|
|
* @return boolean true if at least one of the given agent matches,
|
|
|
|
* false otherwise
|
|
|
|
*/
|
|
|
|
static public function isUserAgent($agent) {
|
|
|
|
if (!is_array($agent)) {
|
|
|
|
$agent = array($agent);
|
|
|
|
}
|
|
|
|
foreach ($agent as $regex) {
|
|
|
|
if (preg_match($regex, $_SERVER['HTTP_USER_AGENT'])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-15 10:07:31 +00:00
|
|
|
}
|