Issues (1061)

Sources/QueryString.php (4 issues)

1
<?php
2
3
/**
4
 * This file does a lot of important stuff.  Mainly, this means it handles
5
 * the query string, request variables, and session management.
6
 *
7
 * Simple Machines Forum (SMF)
8
 *
9
 * @package SMF
10
 * @author Simple Machines https://www.simplemachines.org
11
 * @copyright 2020 Simple Machines and individual contributors
12
 * @license https://www.simplemachines.org/about/smf/license.php BSD
13
 *
14
 * @version 2.1 RC2
15
 */
16
17
if (!defined('SMF'))
18
	die('No direct access...');
19
20
/**
21
 * Clean the request variables - add html entities to GET and slashes if magic_quotes_gpc is Off.
22
 *
23
 * What it does:
24
 * - cleans the request variables (ENV, GET, POST, COOKIE, SERVER) and
25
 * - makes sure the query string was parsed correctly.
26
 * - handles the URLs passed by the queryless URLs option.
27
 * - makes sure, regardless of php.ini, everything has slashes.
28
 * - sets up $board, $topic, and $scripturl and $_REQUEST['start'].
29
 * - determines, or rather tries to determine, the client's IP.
30
 */
31
32
function cleanRequest()
33
{
34
	global $board, $topic, $boardurl, $scripturl, $modSettings, $smcFunc;
35
36
	// Makes it easier to refer to things this way.
37
	$scripturl = $boardurl . '/index.php';
38
39
	// What function to use to reverse magic quotes - if sybase is on we assume that the database sensibly has the right unescape function!
40
	$removeMagicQuoteFunction = ini_get('magic_quotes_sybase') || strtolower(ini_get('magic_quotes_sybase')) == 'on' ? 'unescapestring__recursive' : 'stripslashes__recursive';
41
	$magicQuotesEnabled = version_compare(PHP_VERSION, '7.4.0') == -1 && function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc() != 0 && empty($modSettings['integrate_magic_quotes']);
42
43
	// Save some memory.. (since we don't use these anyway.)
44
	unset($GLOBALS['HTTP_POST_VARS'], $GLOBALS['HTTP_POST_VARS']);
45
	unset($GLOBALS['HTTP_POST_FILES'], $GLOBALS['HTTP_POST_FILES']);
46
47
	// These keys shouldn't be set...ever.
48
	if (isset($_REQUEST['GLOBALS']) || isset($_COOKIE['GLOBALS']))
49
		die('Invalid request variable.');
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
50
51
	// Same goes for numeric keys.
52
	foreach (array_merge(array_keys($_POST), array_keys($_GET), array_keys($_FILES)) as $key)
53
		if (is_numeric($key))
54
			die('Numeric request keys are invalid.');
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
55
56
	// Numeric keys in cookies are less of a problem. Just unset those.
57
	foreach ($_COOKIE as $key => $value)
58
		if (is_numeric($key))
59
			unset($_COOKIE[$key]);
60
61
	// Get the correct query string.  It may be in an environment variable...
62
	if (!isset($_SERVER['QUERY_STRING']))
63
		$_SERVER['QUERY_STRING'] = getenv('QUERY_STRING');
64
65
	// It seems that sticking a URL after the query string is mighty common, well, it's evil - don't.
66
	if (strpos($_SERVER['QUERY_STRING'], 'http') === 0)
67
	{
68
		send_http_status(400);
69
		die;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
70
	}
71
72
	// Are we going to need to parse the ; out?
73
	if (strpos(ini_get('arg_separator.input'), ';') === false && !empty($_SERVER['QUERY_STRING']))
74
	{
75
		// Get rid of the old one! You don't know where it's been!
76
		$_GET = array();
77
78
		// Was this redirected? If so, get the REDIRECT_QUERY_STRING.
79
		// Do not urldecode() the querystring.
80
		$_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'], 0, 5) === 'url=/' ? $_SERVER['REDIRECT_QUERY_STRING'] : $_SERVER['QUERY_STRING'];
81
82
		// Replace ';' with '&' and '&something&' with '&something=&'.  (this is done for compatibility...)
83
		// @todo smflib
84
		parse_str(preg_replace('/&(\w+)(?=&|$)/', '&$1=', strtr($_SERVER['QUERY_STRING'], array(';?' => '&', ';' => '&', '%00' => '', "\0" => ''))), $_GET);
85
86
		// Magic quotes still applies with parse_str - so clean it up.
87
		if ($magicQuotesEnabled)
88
			$_GET = $removeMagicQuoteFunction($_GET);
89
	}
90
	elseif (strpos(ini_get('arg_separator.input'), ';') !== false)
91
	{
92
		if ($magicQuotesEnabled)
93
			$_GET = $removeMagicQuoteFunction($_GET);
94
95
		// Search engines will send action=profile%3Bu=1, which confuses PHP.
96
		foreach ($_GET as $k => $v)
97
		{
98
			if ((string) $v === $v && strpos($k, ';') !== false)
99
			{
100
				$temp = explode(';', $v);
101
				$_GET[$k] = $temp[0];
102
103
				for ($i = 1, $n = count($temp); $i < $n; $i++)
104
				{
105
					@list ($key, $val) = @explode('=', $temp[$i], 2);
106
					if (!isset($_GET[$key]))
107
						$_GET[$key] = $val;
108
				}
109
			}
110
111
			// This helps a lot with integration!
112
			if (strpos($k, '?') === 0)
113
			{
114
				$_GET[substr($k, 1)] = $v;
115
				unset($_GET[$k]);
116
			}
117
		}
118
	}
119
120
	// There's no query string, but there is a URL... try to get the data from there.
121
	if (!empty($_SERVER['REQUEST_URI']))
122
	{
123
		// Remove the .html, assuming there is one.
124
		if (substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '.'), 4) == '.htm')
125
			$request = substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '.'));
126
		else
127
			$request = $_SERVER['REQUEST_URI'];
128
129
		// @todo smflib.
130
		// Replace 'index.php/a,b,c/d/e,f' with 'a=b,c&d=&e=f' and parse it into $_GET.
131
		if (strpos($request, basename($scripturl) . '/') !== false)
132
		{
133
			parse_str(substr(preg_replace('/&(\w+)(?=&|$)/', '&$1=', strtr(preg_replace('~/([^,/]+),~', '/$1=', substr($request, strpos($request, basename($scripturl)) + strlen(basename($scripturl)))), '/', '&')), 1), $temp);
134
			if (function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc() != 0 && empty($modSettings['integrate_magic_quotes']))
135
				$temp = $removeMagicQuoteFunction($temp);
136
			$_GET += $temp;
137
		}
138
	}
139
140
	// If magic quotes is on we have some work...
141
	if ($magicQuotesEnabled)
142
	{
143
		$_ENV = $removeMagicQuoteFunction($_ENV);
144
		$_POST = $removeMagicQuoteFunction($_POST);
145
		$_COOKIE = $removeMagicQuoteFunction($_COOKIE);
146
		foreach ($_FILES as $k => $dummy)
147
			if (isset($_FILES[$k]['name']))
148
				$_FILES[$k]['name'] = $removeMagicQuoteFunction($_FILES[$k]['name']);
149
	}
150
151
	// Add entities to GET.  This is kinda like the slashes on everything else.
152
	$_GET = htmlspecialchars__recursive($_GET);
153
154
	// Let's not depend on the ini settings... why even have COOKIE in there, anyway?
155
	$_REQUEST = $_POST + $_GET;
156
157
	// Make sure $board and $topic are numbers.
158
	if (isset($_REQUEST['board']))
159
	{
160
		// Make sure its a string and not something else like an array
161
		$_REQUEST['board'] = (string) $_REQUEST['board'];
162
163
		// If there's a slash in it, we've got a start value! (old, compatible links.)
164
		if (strpos($_REQUEST['board'], '/') !== false)
165
			list ($_REQUEST['board'], $_REQUEST['start']) = explode('/', $_REQUEST['board']);
166
		// Same idea, but dots.  This is the currently used format - ?board=1.0...
167
		elseif (strpos($_REQUEST['board'], '.') !== false)
168
			list ($_REQUEST['board'], $_REQUEST['start']) = explode('.', $_REQUEST['board']);
169
		// Now make absolutely sure it's a number.
170
		$board = (int) $_REQUEST['board'];
171
		$_REQUEST['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
172
173
		// This is for "Who's Online" because it might come via POST - and it should be an int here.
174
		$_GET['board'] = $board;
175
	}
176
	// Well, $board is going to be a number no matter what.
177
	else
178
		$board = 0;
179
180
	// If there's a threadid, it's probably an old YaBB SE link.  Flow with it.
181
	if (isset($_REQUEST['threadid']) && !isset($_REQUEST['topic']))
182
		$_REQUEST['topic'] = $_REQUEST['threadid'];
183
184
	// We've got topic!
185
	if (isset($_REQUEST['topic']))
186
	{
187
		// Make sure its a string and not something else like an array
188
		$_REQUEST['topic'] = (string) $_REQUEST['topic'];
189
190
		// Slash means old, beta style, formatting.  That's okay though, the link should still work.
191
		if (strpos($_REQUEST['topic'], '/') !== false)
192
			list ($_REQUEST['topic'], $_REQUEST['start']) = explode('/', $_REQUEST['topic']);
193
		// Dots are useful and fun ;).  This is ?topic=1.15.
194
		elseif (strpos($_REQUEST['topic'], '.') !== false)
195
			list ($_REQUEST['topic'], $_REQUEST['start']) = explode('.', $_REQUEST['topic']);
196
197
		// Topic should always be an integer
198
		$topic = $_GET['topic'] = $_REQUEST['topic'] = (int) $_REQUEST['topic'];
199
200
		// Start could be a lot of things...
201
		// ... empty ...
202
		if (empty($_REQUEST['start']))
203
		{
204
			$_REQUEST['start'] = 0;
205
		}
206
		// ... a simple number ...
207
		elseif (is_numeric($_REQUEST['start']))
208
		{
209
			$_REQUEST['start'] = (int) $_REQUEST['start'];
210
		}
211
		// ... or a specific message ...
212
		elseif (strpos($_REQUEST['start'], 'msg') === 0)
213
		{
214
			$virtual_msg = (int) substr($_REQUEST['start'], 3);
215
			$_REQUEST['start'] = $virtual_msg === 0 ? 0 : 'msg' . $virtual_msg;
216
		}
217
		// ... or whatever is new ...
218
		elseif (strpos($_REQUEST['start'], 'new') === 0)
219
		{
220
			$_REQUEST['start'] = 'new';
221
		}
222
		// ... or since a certain time ...
223
		elseif (strpos($_REQUEST['start'], 'from') === 0)
224
		{
225
			$timestamp = (int) substr($_REQUEST['start'], 4);
226
			$_REQUEST['start'] = $timestamp === 0 ? 0 : 'from' . $timestamp;
227
		}
228
		// ... or something invalid, in which case we reset it to 0.
229
		else
230
			$_REQUEST['start'] = 0;
231
	}
232
	else
233
		$topic = 0;
234
235
	// There should be a $_REQUEST['start'], some at least.  If you need to default to other than 0, use $_GET['start'].
236
	if (empty($_REQUEST['start']) || $_REQUEST['start'] < 0 || (int) $_REQUEST['start'] > 2147473647)
237
		$_REQUEST['start'] = 0;
238
239
	// The action needs to be a string and not an array or anything else
240
	if (isset($_REQUEST['action']))
241
		$_REQUEST['action'] = (string) $_REQUEST['action'];
242
	if (isset($_GET['action']))
243
		$_GET['action'] = (string) $_GET['action'];
244
245
	// Some mail providers like to encode semicolons in activation URLs...
246
	if (!empty($_REQUEST['action']) && substr($_SERVER['QUERY_STRING'], 0, 18) == 'action=activate%3b')
247
	{
248
		header('location: ' . $scripturl . '?' . str_replace('%3b', ';', $_SERVER['QUERY_STRING']));
249
		exit;
250
	}
251
252
	// Make sure we have a valid REMOTE_ADDR.
253
	if (!isset($_SERVER['REMOTE_ADDR']))
254
	{
255
		$_SERVER['REMOTE_ADDR'] = '';
256
		// A new magic variable to indicate we think this is command line.
257
		$_SERVER['is_cli'] = true;
258
	}
259
	// Perhaps we have a IPv6 address.
260
	elseif (isValidIP($_SERVER['REMOTE_ADDR']))
261
	{
262
		$_SERVER['REMOTE_ADDR'] = preg_replace('~^::ffff:(\d+\.\d+\.\d+\.\d+)~', '\1', $_SERVER['REMOTE_ADDR']);
263
	}
264
265
	// Try to calculate their most likely IP for those people behind proxies (And the like).
266
	$_SERVER['BAN_CHECK_IP'] = $_SERVER['REMOTE_ADDR'];
267
268
	// If we haven't specified how to handle Reverse Proxy IP headers, lets do what we always used to do.
269
	if (!isset($modSettings['proxy_ip_header']))
270
		$modSettings['proxy_ip_header'] = 'autodetect';
271
272
	// Which headers are we going to check for Reverse Proxy IP headers?
273
	if ($modSettings['proxy_ip_header'] == 'disabled')
274
		$reverseIPheaders = array();
275
	elseif ($modSettings['proxy_ip_header'] == 'autodetect')
276
		$reverseIPheaders = array('HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP');
277
	else
278
		$reverseIPheaders = array($modSettings['proxy_ip_header']);
279
280
	// Find the user's IP address. (but don't let it give you 'unknown'!)
281
	foreach ($reverseIPheaders as $proxyIPheader)
282
	{
283
		// Ignore if this is not set.
284
		if (!isset($_SERVER[$proxyIPheader]))
285
			continue;
286
287
		if (!empty($modSettings['proxy_ip_servers']))
288
		{
289
			foreach (explode(',', $modSettings['proxy_ip_servers']) as $proxy)
290
				if ($proxy == $_SERVER['REMOTE_ADDR'] || matchIPtoCIDR($_SERVER['REMOTE_ADDR'], $proxy))
291
					continue;
292
		}
293
294
		// If there are commas, get the last one.. probably.
295
		if (strpos($_SERVER[$proxyIPheader], ',') !== false)
296
		{
297
			$ips = array_reverse(explode(', ', $_SERVER[$proxyIPheader]));
298
299
			// Go through each IP...
300
			foreach ($ips as $i => $ip)
301
			{
302
				// Make sure it's in a valid range...
303
				if (preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown|::1|fe80::|fc00::)~', $ip) != 0 && preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown|::1|fe80::|fc00::)~', $_SERVER['REMOTE_ADDR']) == 0)
304
				{
305
					if (!isValidIPv6($_SERVER[$proxyIPheader]) || preg_match('~::ffff:\d+\.\d+\.\d+\.\d+~', $_SERVER[$proxyIPheader]) !== 0)
306
					{
307
						$_SERVER[$proxyIPheader] = preg_replace('~^::ffff:(\d+\.\d+\.\d+\.\d+)~', '\1', $_SERVER[$proxyIPheader]);
308
309
						// Just incase we have a legacy IPv4 address.
310
						// @ TODO: Convert to IPv6.
311
						if (preg_match('~^((([1]?\d)?\d|2[0-4]\d|25[0-5])\.){3}(([1]?\d)?\d|2[0-4]\d|25[0-5])$~', $_SERVER[$proxyIPheader]) === 0)
312
							continue;
313
					}
314
315
					continue;
316
				}
317
318
				// Otherwise, we've got an IP!
319
				$_SERVER['BAN_CHECK_IP'] = trim($ip);
320
				break;
321
			}
322
		}
323
		// Otherwise just use the only one.
324
		elseif (preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown|::1|fe80::|fc00::)~', $_SERVER[$proxyIPheader]) == 0 || preg_match('~^((0|10|172\.(1[6-9]|2[0-9]|3[01])|192\.168|255|127)\.|unknown|::1|fe80::|fc00::)~', $_SERVER['REMOTE_ADDR']) != 0)
325
			$_SERVER['BAN_CHECK_IP'] = $_SERVER[$proxyIPheader];
326
		elseif (!isValidIPv6($_SERVER[$proxyIPheader]) || preg_match('~::ffff:\d+\.\d+\.\d+\.\d+~', $_SERVER[$proxyIPheader]) !== 0)
327
		{
328
			$_SERVER[$proxyIPheader] = preg_replace('~^::ffff:(\d+\.\d+\.\d+\.\d+)~', '\1', $_SERVER[$proxyIPheader]);
329
330
			// Just incase we have a legacy IPv4 address.
331
			// @ TODO: Convert to IPv6.
332
			if (preg_match('~^((([1]?\d)?\d|2[0-4]\d|25[0-5])\.){3}(([1]?\d)?\d|2[0-4]\d|25[0-5])$~', $_SERVER[$proxyIPheader]) === 0)
333
				continue;
334
		}
335
	}
336
337
	// Make sure we know the URL of the current request.
338
	if (empty($_SERVER['REQUEST_URI']))
339
		$_SERVER['REQUEST_URL'] = $scripturl . (!empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '');
340
	elseif (preg_match('~^([^/]+//[^/]+)~', $scripturl, $match) == 1)
341
		$_SERVER['REQUEST_URL'] = $match[1] . $_SERVER['REQUEST_URI'];
342
	else
343
		$_SERVER['REQUEST_URL'] = $_SERVER['REQUEST_URI'];
344
345
	// And make sure HTTP_USER_AGENT is set.
346
	$_SERVER['HTTP_USER_AGENT'] = isset($_SERVER['HTTP_USER_AGENT']) ? (isset($smcFunc['htmlspecialchars']) ? $smcFunc['htmlspecialchars']($smcFunc['db_unescape_string']($_SERVER['HTTP_USER_AGENT']), ENT_QUOTES) : htmlspecialchars($smcFunc['db_unescape_string']($_SERVER['HTTP_USER_AGENT']), ENT_QUOTES)) : '';
347
348
	// Some final checking.
349
	if (!isValidIP($_SERVER['BAN_CHECK_IP']))
350
		$_SERVER['BAN_CHECK_IP'] = '';
351
	if ($_SERVER['REMOTE_ADDR'] == 'unknown')
352
		$_SERVER['REMOTE_ADDR'] = '';
353
}
354
355
/**
356
 * Validates a IPv6 address. returns true if it is ipv6.
357
 *
358
 * @param string $ip The ip address to be validated
359
 * @return boolean Whether the specified IP is a valid IPv6 address
360
 */
361
function isValidIPv6($ip)
362
{
363
	//looking for :
364
	if (strpos($ip, ':') === false)
365
		return false;
366
367
	//check valid address
368
	return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
369
}
370
371
/**
372
 * Expands a IPv6 address to its full form.
373
 *
374
 * @param string $addr The IPv6 address
375
 * @param bool $strict_check Whether to check the length of the expanded address for compliance
376
 * @return string|bool The expanded IPv6 address or false if $strict_check is true and the result isn't valid
377
 */
378
function expandIPv6($addr, $strict_check = true)
379
{
380
	static $converted = array();
381
382
	// Check if we have done this already.
383
	if (isset($converted[$addr]))
384
		return $converted[$addr];
385
386
	// Check if there are segments missing, insert if necessary.
387
	if (strpos($addr, '::') !== false)
388
	{
389
		$part = explode('::', $addr);
390
		$part[0] = explode(':', $part[0]);
391
		$part[1] = explode(':', $part[1]);
392
		$missing = array();
393
394
		for ($i = 0; $i < (8 - (count($part[0]) + count($part[1]))); $i++)
395
			array_push($missing, '0000');
396
397
		$part = array_merge($part[0], $missing, $part[1]);
398
	}
399
	else
400
		$part = explode(':', $addr);
401
402
	// Pad each segment until it has 4 digits.
403
	foreach ($part as &$p)
404
		while (strlen($p) < 4)
405
			$p = '0' . $p;
406
407
	unset($p);
408
409
	// Join segments.
410
	$result = implode(':', $part);
411
412
	// Save this incase of repeated use.
413
	$converted[$addr] = $result;
414
415
	// Quick check to make sure the length is as expected.
416
	if (!$strict_check || strlen($result) == 39)
417
		return $result;
418
	else
419
		return false;
420
}
421
422
/**
423
 * Detect if a IP is in a CIDR address
424
 * - returns true or false
425
 *
426
 * @param string $ip_address IP address to check
427
 * @param string $cidr_address CIDR address to verify
428
 * @return bool Whether the IP matches the CIDR
429
 */
430
function matchIPtoCIDR($ip_address, $cidr_address)
431
{
432
	list ($cidr_network, $cidr_subnetmask) = preg_split('/', $cidr_address);
433
434
	//v6?
435
	if ((strpos($cidr_network, ':') !== false))
436
	{
437
		if (!filter_var($ip_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || !filter_var($cidr_network, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))
438
			return false;
439
440
		$ip_address = inet_pton($ip_address);
441
		$cidr_network = inet_pton($cidr_network);
442
		$binMask = str_repeat("f", $cidr_subnetmask / 4);
443
		switch ($cidr_subnetmask % 4)
444
		{
445
			case 0:
446
				break;
447
			case 1:
448
				$binMask .= "8";
449
				break;
450
			case 2:
451
				$binMask .= "c";
452
				break;
453
			case 3:
454
				$binMask .= "e";
455
				break;
456
		}
457
		$binMask = str_pad($binMask, 32, '0');
458
		$binMask = pack("H*", $binMask);
459
460
		return ($ip_address & $binMask) == $cidr_network;
0 ignored issues
show
Are you sure you want to use the bitwise & or did you mean &&?
Loading history...
461
	}
462
	else
463
		return (ip2long($ip_address) & (~((1 << (32 - $cidr_subnetmask)) - 1))) == ip2long($cidr_network);
464
}
465
466
/**
467
 * Adds slashes to the array/variable.
468
 * What it does:
469
 * - returns the var, as an array or string, with escapes as required.
470
 * - importantly escapes all keys and values!
471
 * - calls itself recursively if necessary.
472
 *
473
 * @param array|string $var A string or array of strings to escape
474
 * @return array|string The escaped string or array of escaped strings
475
 */
476
function escapestring__recursive($var)
477
{
478
	global $smcFunc;
479
480
	if (!is_array($var))
481
		return $smcFunc['db_escape_string']($var);
482
483
	// Reindex the array with slashes.
484
	$new_var = array();
485
486
	// Add slashes to every element, even the indexes!
487
	foreach ($var as $k => $v)
488
		$new_var[$smcFunc['db_escape_string']($k)] = escapestring__recursive($v);
489
490
	return $new_var;
491
}
492
493
/**
494
 * Adds html entities to the array/variable.  Uses two underscores to guard against overloading.
495
 * What it does:
496
 * - adds entities (&quot;, &lt;, &gt;) to the array or string var.
497
 * - importantly, does not effect keys, only values.
498
 * - calls itself recursively if necessary.
499
 *
500
 * @param array|string $var The string or array of strings to add entites to
501
 * @param int $level Which level we're at within the array (if called recursively)
502
 * @return array|string The string or array of strings with entities added
503
 */
504
function htmlspecialchars__recursive($var, $level = 0)
505
{
506
	global $smcFunc;
507
508
	if (!is_array($var))
509
		return isset($smcFunc['htmlspecialchars']) ? $smcFunc['htmlspecialchars']($var, ENT_QUOTES) : htmlspecialchars($var, ENT_QUOTES);
510
511
	// Add the htmlspecialchars to every element.
512
	foreach ($var as $k => $v)
513
		$var[$k] = $level > 25 ? null : htmlspecialchars__recursive($v, $level + 1);
514
515
	return $var;
516
}
517
518
/**
519
 * Removes url stuff from the array/variable.  Uses two underscores to guard against overloading.
520
 * What it does:
521
 * - takes off url encoding (%20, etc.) from the array or string var.
522
 * - importantly, does it to keys too!
523
 * - calls itself recursively if there are any sub arrays.
524
 *
525
 * @param array|string $var The string or array of strings to decode
526
 * @param int $level Which level we're at within the array (if called recursively)
527
 * @return array|string The decoded string or array of decoded strings
528
 */
529
function urldecode__recursive($var, $level = 0)
530
{
531
	if (!is_array($var))
532
		return urldecode($var);
533
534
	// Reindex the array...
535
	$new_var = array();
536
537
	// Add the htmlspecialchars to every element.
538
	foreach ($var as $k => $v)
539
		$new_var[urldecode($k)] = $level > 25 ? null : urldecode__recursive($v, $level + 1);
540
541
	return $new_var;
542
}
543
544
/**
545
 * Unescapes any array or variable.  Uses two underscores to guard against overloading.
546
 * What it does:
547
 * - unescapes, recursively, from the array or string var.
548
 * - effects both keys and values of arrays.
549
 * - calls itself recursively to handle arrays of arrays.
550
 *
551
 * @param array|string $var The string or array of strings to unescape
552
 * @return array|string The unescaped string or array of unescaped strings
553
 */
554
function unescapestring__recursive($var)
555
{
556
	global $smcFunc;
557
558
	if (!is_array($var))
559
		return $smcFunc['db_unescape_string']($var);
560
561
	// Reindex the array without slashes, this time.
562
	$new_var = array();
563
564
	// Strip the slashes from every element.
565
	foreach ($var as $k => $v)
566
		$new_var[$smcFunc['db_unescape_string']($k)] = unescapestring__recursive($v);
567
568
	return $new_var;
569
}
570
571
/**
572
 * Remove slashes recursively.  Uses two underscores to guard against overloading.
573
 * What it does:
574
 * - removes slashes, recursively, from the array or string var.
575
 * - effects both keys and values of arrays.
576
 * - calls itself recursively to handle arrays of arrays.
577
 *
578
 * @param array|string $var The string or array of strings to strip slashes from
579
 * @param int $level = 0 What level we're at within the array (if called recursively)
580
 * @return array|string The string or array of strings with slashes stripped
581
 */
582
function stripslashes__recursive($var, $level = 0)
583
{
584
	if (!is_array($var))
585
		return stripslashes($var);
586
587
	// Reindex the array without slashes, this time.
588
	$new_var = array();
589
590
	// Strip the slashes from every element.
591
	foreach ($var as $k => $v)
592
		$new_var[stripslashes($k)] = $level > 25 ? null : stripslashes__recursive($v, $level + 1);
593
594
	return $new_var;
595
}
596
597
/**
598
 * Trim a string including the HTML space, character 160.  Uses two underscores to guard against overloading.
599
 * What it does:
600
 * - trims a string or an the var array using html characters as well.
601
 * - does not effect keys, only values.
602
 * - may call itself recursively if needed.
603
 *
604
 * @param array|string $var The string or array of strings to trim
605
 * @param int $level = 0 How deep we're at within the array (if called recursively)
606
 * @return array|string The trimmed string or array of trimmed strings
607
 */
608
function htmltrim__recursive($var, $level = 0)
609
{
610
	global $smcFunc;
611
612
	// Remove spaces (32), tabs (9), returns (13, 10, and 11), nulls (0), and hard spaces. (160)
613
	if (!is_array($var))
614
		return isset($smcFunc) ? $smcFunc['htmltrim']($var) : trim($var, ' ' . "\t\n\r\x0B" . '\0' . "\xA0");
615
616
	// Go through all the elements and remove the whitespace.
617
	foreach ($var as $k => $v)
618
		$var[$k] = $level > 25 ? null : htmltrim__recursive($v, $level + 1);
619
620
	return $var;
621
}
622
623
/**
624
 * Clean up the XML to make sure it doesn't contain invalid characters.
625
 * What it does:
626
 * - removes invalid XML characters to assure the input string being
627
 * - parsed properly.
628
 *
629
 * @param string $string The string to clean
630
 * @return string The cleaned string
631
 */
632
function cleanXml($string)
633
{
634
	global $context;
635
636
	// https://www.w3.org/TR/2000/REC-xml-20001006#NT-Char
637
	return preg_replace('~[\x00-\x08\x0B\x0C\x0E-\x19' . ($context['utf8'] ? '\x{FFFE}\x{FFFF}' : '') . ']~' . ($context['utf8'] ? 'u' : ''), '', $string);
638
}
639
640
/**
641
 * Escapes (replaces) characters in strings to make them safe for use in javascript
642
 *
643
 * @param string $string The string to escape
644
 * @return string The escaped string
645
 */
646
function JavaScriptEscape($string)
647
{
648
	global $scripturl;
649
650
	return '\'' . strtr($string, array(
651
		"\r" => '',
652
		"\n" => '\\n',
653
		"\t" => '\\t',
654
		'\\' => '\\\\',
655
		'\'' => '\\\'',
656
		'</' => '<\' + \'/',
657
		'<script' => '<scri\'+\'pt',
658
		'<body>' => '<bo\'+\'dy>',
659
		'<a href' => '<a hr\'+\'ef',
660
		$scripturl => '\' + smf_scripturl + \'',
661
	)) . '\'';
662
}
663
664
/**
665
 * Rewrite URLs to include the session ID.
666
 * What it does:
667
 * - rewrites the URLs outputted to have the session ID, if the user
668
 *   is not accepting cookies and is using a standard web browser.
669
 * - handles rewriting URLs for the queryless URLs option.
670
 * - can be turned off entirely by setting $scripturl to an empty
671
 *   string, ''. (it wouldn't work well like that anyway.)
672
 * - because of bugs in certain builds of PHP, does not function in
673
 *   versions lower than 4.3.0 - please upgrade if this hurts you.
674
 *
675
 * @param string $buffer The unmodified output buffer
676
 * @return string The modified buffer
677
 */
678
function ob_sessrewrite($buffer)
679
{
680
	global $scripturl, $modSettings, $context;
681
682
	// If $scripturl is set to nothing, or the SID is not defined (SSI?) just quit.
683
	if ($scripturl == '' || !defined('SID'))
684
		return $buffer;
685
686
	// Do nothing if the session is cookied, or they are a crawler - guests are caught by redirectexit().  This doesn't work below PHP 4.3.0, because it makes the output buffer bigger.
687
	// @todo smflib
688
	if (empty($_COOKIE) && SID != '' && !isBrowser('possibly_robot'))
689
		$buffer = preg_replace('/(?<!<link rel="canonical" href=)"' . preg_quote($scripturl, '/') . '(?!\?' . preg_quote(SID, '/') . ')\\??/', '"' . $scripturl . '?' . SID . '&amp;', $buffer);
690
	// Debugging templates, are we?
691
	elseif (isset($_GET['debug']))
692
		$buffer = preg_replace('/(?<!<link rel="canonical" href=)"' . preg_quote($scripturl, '/') . '\\??/', '"' . $scripturl . '?debug;', $buffer);
693
694
	// This should work even in 4.2.x, just not CGI without cgi.fix_pathinfo.
695
	if (!empty($modSettings['queryless_urls']) && (!$context['server']['is_cgi'] || ini_get('cgi.fix_pathinfo') == 1 || @get_cfg_var('cgi.fix_pathinfo') == 1) && ($context['server']['is_apache'] || $context['server']['is_lighttpd'] || $context['server']['is_litespeed']))
696
	{
697
		// Let's do something special for session ids!
698
		if (defined('SID') && SID != '')
699
			$buffer = preg_replace_callback('~"' . preg_quote($scripturl, '~') . '\?(?:' . SID . '(?:;|&|&amp;))((?:board|topic)=[^#"]+?)(#[^"]*?)?"~', function($m)
700
			{
701
				global $scripturl;
702
				return '"' . $scripturl . "/" . strtr("$m[1]", '&;=', '//,') . ".html?" . SID . (isset($m[2]) ? $m[2] : "") . '"';
703
			}, $buffer);
704
		else
705
			$buffer = preg_replace_callback('~"' . preg_quote($scripturl, '~') . '\?((?:board|topic)=[^#"]+?)(#[^"]*?)?"~', function($m)
706
			{
707
				global $scripturl;
708
				return '"' . $scripturl . '/' . strtr("$m[1]", '&;=', '//,') . '.html' . (isset($m[2]) ? $m[2] : "") . '"';
709
			}, $buffer);
710
	}
711
712
	// Return the changed buffer.
713
	return $buffer;
714
}
715
716
?>