Blame view

sources/lib/private/request.php 7.94 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]*#';
6d9380f96   Cédric Dupont   Update sources OC...
14
  	const USER_AGENT_FREEBOX = '#^Mozilla/5\.0$#';
a293d369c   Kload   Update sources to...
15

837968727   Kload   [enh] Upgrade to ...
16
  	const REGEX_LOCALHOST = '/^(127\.0\.0\.1|localhost)(:[0-9]+|)$/';
03e52840d   Kload   Init
17
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
18
  	 * Check overwrite condition
31b7f2792   Kload   Upgrade to ownclo...
19
  	 * @param string $type
6d9380f96   Cédric Dupont   Update sources OC...
20
  	 * @return bool
03e52840d   Kload   Init
21
22
23
24
25
26
27
28
  	 */
  	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));
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
29
  	 * Checks whether a domain is considered as trusted from the list
837968727   Kload   [enh] Upgrade to ...
30
31
32
  	 * of trusted domains. If no trusted domains have been configured, returns
  	 * true.
  	 * This is used to prevent Host Header Poisoning.
6d9380f96   Cédric Dupont   Update sources OC...
33
  	 * @param string $domain
837968727   Kload   [enh] Upgrade to ...
34
35
  	 * @return bool true if the given domain is trusted or if no trusted domains
  	 * have been configured
a293d369c   Kload   Update sources to...
36
37
  	 */
  	public static function isTrustedDomain($domain) {
837968727   Kload   [enh] Upgrade to ...
38
39
40
41
42
43
44
  		$trustedList = \OC_Config::getValue('trusted_domains', array());
  		if (empty($trustedList)) {
  			return true;
  		}
  		if (preg_match(self::REGEX_LOCALHOST, $domain) === 1) {
  			return true;
  		}
a293d369c   Kload   Update sources to...
45
46
47
48
  		return in_array($domain, $trustedList);
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
49
  	 * Returns the unverified server host from the headers without checking
837968727   Kload   [enh] Upgrade to ...
50
  	 * whether it is a trusted domain
6d9380f96   Cédric Dupont   Update sources OC...
51
  	 * @return string the server host
03e52840d   Kload   Init
52
53
54
55
  	 *
  	 * Returns the server host, even if the website uses one or more
  	 * reverse proxies
  	 */
837968727   Kload   [enh] Upgrade to ...
56
57
  	public static function insecureServerHost() {
  		$host = null;
03e52840d   Kload   Init
58
59
  		if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
  			if (strpos($_SERVER['HTTP_X_FORWARDED_HOST'], ",") !== false) {
837968727   Kload   [enh] Upgrade to ...
60
61
62
  				$parts = explode(',', $_SERVER['HTTP_X_FORWARDED_HOST']);
  				$host = trim(current($parts));
  			} else {
a293d369c   Kload   Update sources to...
63
  				$host = $_SERVER['HTTP_X_FORWARDED_HOST'];
03e52840d   Kload   Init
64
  			}
a293d369c   Kload   Update sources to...
65
  		} else {
03e52840d   Kload   Init
66
  			if (isset($_SERVER['HTTP_HOST'])) {
a293d369c   Kload   Update sources to...
67
  				$host = $_SERVER['HTTP_HOST'];
837968727   Kload   [enh] Upgrade to ...
68
  			} else if (isset($_SERVER['SERVER_NAME'])) {
a293d369c   Kload   Update sources to...
69
  				$host = $_SERVER['SERVER_NAME'];
03e52840d   Kload   Init
70
  			}
03e52840d   Kload   Init
71
  		}
837968727   Kload   [enh] Upgrade to ...
72
73
74
75
76
77
  		return $host;
  	}
  
  	/**
  	 * Returns the overwritehost setting from the config if set and
  	 * if the overwrite condition is met
6d9380f96   Cédric Dupont   Update sources OC...
78
  	 * @return string|null overwritehost value or null if not defined or the defined condition
837968727   Kload   [enh] Upgrade to ...
79
80
81
82
83
84
85
86
87
88
  	 * isn't met
  	 */
  	public static function getOverwriteHost() {
  		if(OC_Config::getValue('overwritehost', '') !== '' and self::isOverwriteCondition()) {
  			return OC_Config::getValue('overwritehost');
  		}
  		return null;
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
89
  	 * Returns the server host from the headers, or the first configured
837968727   Kload   [enh] Upgrade to ...
90
  	 * trusted domain if the host isn't in the trusted list
6d9380f96   Cédric Dupont   Update sources OC...
91
  	 * @return string the server host
837968727   Kload   [enh] Upgrade to ...
92
93
94
95
96
  	 *
  	 * Returns the server host, even if the website uses one or more
  	 * reverse proxies
  	 */
  	public static function serverHost() {
6d9380f96   Cédric Dupont   Update sources OC...
97
  		if (OC::$CLI && defined('PHPUNIT_RUN')) {
837968727   Kload   [enh] Upgrade to ...
98
99
100
101
102
103
104
105
106
107
108
  			return 'localhost';
  		}
  
  		// overwritehost is always trusted
  		$host = self::getOverwriteHost();
  		if ($host !== null) {
  			return $host;
  		}
  
  		// get the host from the headers
  		$host = self::insecureServerHost();
03e52840d   Kload   Init
109

a293d369c   Kload   Update sources to...
110
111
112
  		// 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
837968727   Kload   [enh] Upgrade to ...
113
  		if (self::isTrustedDomain($host)) {
a293d369c   Kload   Update sources to...
114
115
116
117
118
119
  			return $host;
  		} else {
  			$trustedList = \OC_Config::getValue('trusted_domains', array(''));
  			return $trustedList[0];
  		}
  	}
03e52840d   Kload   Init
120
121
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
122
123
  	* Returns the server protocol
  	* @return string the server protocol
03e52840d   Kload   Init
124
125
126
127
128
129
130
131
132
  	*
  	* 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...
133
134
135
  			// 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
136
  		}
a293d369c   Kload   Update sources to...
137
138
139
140
  		if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') {
  			return 'https';
  		}
  		return 'http';
03e52840d   Kload   Init
141
142
143
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
144
145
  	 * Returns the request uri
  	 * @return string the request uri
03e52840d   Kload   Init
146
147
148
  	 *
  	 * Returns the request uri, even if the website uses one or more
  	 * reverse proxies
6d9380f96   Cédric Dupont   Update sources OC...
149
  	 * @return string
03e52840d   Kload   Init
150
151
152
153
154
155
156
157
158
159
  	 */
  	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;
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
160
161
  	 * Returns the script name
  	 * @return string the script name
03e52840d   Kload   Init
162
163
164
165
166
167
  	 *
  	 * Returns the script name, even if the website uses one or more
  	 * reverse proxies
  	 */
  	public static function scriptName() {
  		$name = $_SERVER['SCRIPT_NAME'];
6d9380f96   Cédric Dupont   Update sources OC...
168
169
  		$overwriteWebRoot = OC_Config::getValue('overwritewebroot', '');
  		if ($overwriteWebRoot !== '' and self::isOverwriteCondition()) {
31b7f2792   Kload   Upgrade to ownclo...
170
  			$serverroot = str_replace("\\", '/', substr(__DIR__, 0, -strlen('lib/private/')));
03e52840d   Kload   Init
171
  			$suburi = str_replace("\\", "/", substr(realpath($_SERVER["SCRIPT_FILENAME"]), strlen($serverroot)));
6d9380f96   Cédric Dupont   Update sources OC...
172
  			$name = '/' . ltrim($overwriteWebRoot . $suburi, '/');
03e52840d   Kload   Init
173
174
175
176
177
  		}
  		return $name;
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
178
179
  	 * get Path info from request
  	 * @return string Path info or false when not found
03e52840d   Kload   Init
180
181
182
183
184
185
  	 */
  	public static function getPathInfo() {
  		if (array_key_exists('PATH_INFO', $_SERVER)) {
  			$path_info = $_SERVER['PATH_INFO'];
  		}else{
  			$path_info = self::getRawPathInfo();
6d9380f96   Cédric Dupont   Update sources OC...
186
  			// following is taken from \Sabre\DAV\URLUtil::decodePathSegment
03e52840d   Kload   Init
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
  			$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;
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
202
203
204
  	 * get Path info from request, not urldecoded
  	 * @throws Exception
  	 * @return string Path info or false when not found
03e52840d   Kload   Init
205
206
  	 */
  	public static function getRawPathInfo() {
31b7f2792   Kload   Upgrade to ownclo...
207
208
209
210
211
  		$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
212
  		// Remove the query string from REQUEST_URI
31b7f2792   Kload   Upgrade to ownclo...
213
214
215
216
217
218
219
220
  		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
6d9380f96   Cédric Dupont   Update sources OC...
221
  		list($path, $name) = \Sabre\DAV\URLUtil::splitPath($scriptName);
31b7f2792   Kload   Upgrade to ownclo...
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
  		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
239
  		}
03e52840d   Kload   Init
240
241
242
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
243
244
  	 * Check if the requester sent along an mtime
  	 * @return false or an mtime
03e52840d   Kload   Init
245
246
247
248
249
250
251
252
  	 */
  	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...
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  
  	/**
  	 * 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
271
  }