Blame view

sources/lib/private/request.php 7.35 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
  <?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 {
a293d369c   Kload   Update sources to...
10
11
12
13
  
  	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]*#';
03e52840d   Kload   Init
14
15
  	/**
  	 * @brief Check overwrite condition
31b7f2792   Kload   Upgrade to ownclo...
16
  	 * @param string $type
03e52840d   Kload   Init
17
18
19
20
21
22
23
24
25
  	 * @returns bool
  	 */
  	private static function isOverwriteCondition($type = '') {
  		$regex = '/' . OC_Config::getValue('overwritecondaddr', '')  . '/';
  		return $regex === '//' or preg_match($regex, $_SERVER['REMOTE_ADDR']) === 1
  			or ($type !== 'protocol' and OC_Config::getValue('forcessl', false));
  	}
  
  	/**
a293d369c   Kload   Update sources to...
26
27
28
29
30
31
32
33
34
35
  	 * @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);
  	}
  
  	/**
03e52840d   Kload   Init
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  	 * @brief Returns the server host
  	 * @returns string the server host
  	 *
  	 * Returns the server host, even if the website uses one or more
  	 * reverse proxies
  	 */
  	public static function serverHost() {
  		if(OC::$CLI) {
  			return 'localhost';
  		}
  		if(OC_Config::getValue('overwritehost', '') !== '' and self::isOverwriteCondition()) {
  			return OC_Config::getValue('overwritehost');
  		}
  		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{
a293d369c   Kload   Update sources to...
54
  				$host = $_SERVER['HTTP_X_FORWARDED_HOST'];
03e52840d   Kload   Init
55
  			}
a293d369c   Kload   Update sources to...
56
  		} else {
03e52840d   Kload   Init
57
  			if (isset($_SERVER['HTTP_HOST'])) {
a293d369c   Kload   Update sources to...
58
  				$host = $_SERVER['HTTP_HOST'];
03e52840d   Kload   Init
59
  			}
a293d369c   Kload   Update sources to...
60
61
  			else if (isset($_SERVER['SERVER_NAME'])) {
  				$host = $_SERVER['SERVER_NAME'];
03e52840d   Kload   Init
62
  			}
03e52840d   Kload   Init
63
  		}
03e52840d   Kload   Init
64

a293d369c   Kload   Update sources to...
65
66
67
68
69
70
71
72
73
74
  		// 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];
  		}
  	}
03e52840d   Kload   Init
75
76
77
78
79
80
81
82
83
84
85
86
87
  
  	/**
  	* @brief Returns the server protocol
  	* @returns string the server protocol
  	*
  	* Returns the server protocol. It respects reverse proxy servers and load balancers
  	*/
  	public static function serverProtocol() {
  		if(OC_Config::getValue('overwriteprotocol', '') !== '' and self::isOverwriteCondition('protocol')) {
  			return OC_Config::getValue('overwriteprotocol');
  		}
  		if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
  			$proto = strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']);
a293d369c   Kload   Update sources to...
88
89
90
  			// Verify that the protocol is always HTTP or HTTPS
  			// default to http if an invalid value is provided
  			return $proto === 'https' ? 'https' : 'http';
03e52840d   Kload   Init
91
  		}
a293d369c   Kload   Update sources to...
92
93
94
95
  		if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') {
  			return 'https';
  		}
  		return 'http';
03e52840d   Kload   Init
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  	}
  
  	/**
  	 * @brief Returns the request uri
  	 * @returns string the request uri
  	 *
  	 * 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'] : '';
  		if (OC_Config::getValue('overwritewebroot', '') !== '' and self::isOverwriteCondition()) {
  			$uri = self::scriptName() . substr($uri, strlen($_SERVER['SCRIPT_NAME']));
  		}
  		return $uri;
  	}
  
  	/**
  	 * @brief Returns the script name
  	 * @returns string the script name
  	 *
  	 * Returns the script name, even if the website uses one or more
  	 * reverse proxies
  	 */
  	public static function scriptName() {
  		$name = $_SERVER['SCRIPT_NAME'];
  		if (OC_Config::getValue('overwritewebroot', '') !== '' and self::isOverwriteCondition()) {
31b7f2792   Kload   Upgrade to ownclo...
123
  			$serverroot = str_replace("\\", '/', substr(__DIR__, 0, -strlen('lib/private/')));
03e52840d   Kload   Init
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  			$suburi = str_replace("\\", "/", substr(realpath($_SERVER["SCRIPT_FILENAME"]), strlen($serverroot)));
  			$name = OC_Config::getValue('overwritewebroot', '') . $suburi;
  		}
  		return $name;
  	}
  
  	/**
  	 * @brief get Path info from request
  	 * @returns string Path info or false when not found
  	 */
  	public static function getPathInfo() {
  		if (array_key_exists('PATH_INFO', $_SERVER)) {
  			$path_info = $_SERVER['PATH_INFO'];
  		}else{
  			$path_info = self::getRawPathInfo();
  			// following is taken from Sabre_DAV_URLUtil::decodePathSegment
  			$path_info = rawurldecode($path_info);
  			$encoding = mb_detect_encoding($path_info, array('UTF-8', 'ISO-8859-1'));
  
  			switch($encoding) {
  
  				case 'ISO-8859-1' :
  					$path_info = utf8_encode($path_info);
  
  			}
  			// end copy
  		}
  		return $path_info;
  	}
  
  	/**
  	 * @brief get Path info from request, not urldecoded
  	 * @returns string Path info or false when not found
  	 */
  	public static function getRawPathInfo() {
31b7f2792   Kload   Upgrade to ownclo...
159
160
161
162
163
  		$requestUri = $_SERVER['REQUEST_URI'];
  		// remove too many leading slashes - can be caused by reverse proxy configuration
  		if (strpos($requestUri, '/') === 0) {
  			$requestUri = '/' . ltrim($requestUri, '/');
  		}
03e52840d   Kload   Init
164
  		// Remove the query string from REQUEST_URI
31b7f2792   Kload   Upgrade to ownclo...
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  		if ($pos = strpos($requestUri, '?')) {
  			$requestUri = substr($requestUri, 0, $pos);
  		}
  
  		$scriptName = $_SERVER['SCRIPT_NAME'];
  		$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')");
  			}
  		}
  		if (strpos($path_info, '/'.$name) === 0) {
  			$path_info = substr($path_info, strlen($name) + 1);
  		}
  		if (strpos($path_info, $name) === 0) {
  			$path_info = substr($path_info, strlen($name));
  		}
  		if($path_info === '/'){
  			return '';
  		} else {
  			return $path_info;
03e52840d   Kload   Init
191
  		}
03e52840d   Kload   Init
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
  	}
  
  	/**
  	 * @brief Check if this is a no-cache request
  	 * @returns boolean true for no-cache
  	 */
  	static public function isNoCache() {
  		if (!isset($_SERVER['HTTP_CACHE_CONTROL'])) {
  			return false;
  		}
  		return $_SERVER['HTTP_CACHE_CONTROL'] == 'no-cache';
  	}
  
  	/**
  	 * @brief Check if the requestor understands gzip
  	 * @returns boolean true for gzip encoding supported
  	 */
  	static public function acceptGZip() {
  		if (!isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  			return false;
  		}
  		$HTTP_ACCEPT_ENCODING = $_SERVER["HTTP_ACCEPT_ENCODING"];
  		if( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false )
  			return 'x-gzip';
  		else if( strpos($HTTP_ACCEPT_ENCODING, 'gzip') !== false )
  			return 'gzip';
  		return false;
  	}
  
  	/**
  	 * @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;
  		}
  	}
a293d369c   Kload   Update sources to...
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
  
  	/**
  	 * 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;
  	}
03e52840d   Kload   Init
250
  }