Issues (1065)

Sources/QueryString.php (1 issue)

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 2025 Simple Machines and individual contributors
12
 * @license https://www.simplemachines.org/about/smf/license.php BSD
13
 *
14
 * @version 2.1.6
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.');
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.');
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;
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', 'HTTP_X_REAL_IP', 'HTTP_CF_CONNECTING_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
			$valid_sender = false;
290
291
			foreach (explode(',', $modSettings['proxy_ip_servers']) as $proxy)
292
			{
293
				if ($proxy == $_SERVER['REMOTE_ADDR'] || matchIPtoCIDR($_SERVER['REMOTE_ADDR'], $proxy))
294
				{
295
					$valid_sender = true;
296
					break;
297
				}
298
			}
299
300
			if (!$valid_sender)
301
				continue;
302
		}
303
304
		// If there are commas, get the last one.. probably.
305
		if (strpos($_SERVER[$proxyIPheader], ',') !== false)
306
		{
307
			$ips = array_reverse(explode(', ', $_SERVER[$proxyIPheader]));
308
309
			// Go through each IP...
310
			foreach ($ips as $i => $ip)
311
			{
312
				// Make sure it's in a valid range...
313
				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)
314
				{
315
					if (!isValidIPv6($_SERVER[$proxyIPheader]) || preg_match('~::ffff:\d+\.\d+\.\d+\.\d+~', $_SERVER[$proxyIPheader]) !== 0)
316
					{
317
						$_SERVER[$proxyIPheader] = preg_replace('~^::ffff:(\d+\.\d+\.\d+\.\d+)~', '\1', $_SERVER[$proxyIPheader]);
318
319
						// Just incase we have a legacy IPv4 address.
320
						// @ TODO: Convert to IPv6.
321
						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)
322
							continue;
323
					}
324
325
					continue;
326
				}
327
328
				// Otherwise, we've got an IP!
329
				$_SERVER['REMOTE_ADDR'] = trim($ip);
330
				break;
331
			}
332
		}
333
		// Otherwise just use the only one.
334
		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)
335
		{
336
			$_SERVER['REMOTE_ADDR'] = $_SERVER[$proxyIPheader];
337
		}
338
		elseif (!isValidIPv6($_SERVER[$proxyIPheader]) || preg_match('~::ffff:\d+\.\d+\.\d+\.\d+~', $_SERVER[$proxyIPheader]) !== 0)
339
		{
340
			$_SERVER[$proxyIPheader] = preg_replace('~^::ffff:(\d+\.\d+\.\d+\.\d+)~', '\1', $_SERVER[$proxyIPheader]);
341
342
			// Just incase we have a legacy IPv4 address.
343
			// @ TODO: Convert to IPv6.
344
			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)
345
				continue;
346
		}
347
	}
348
349
	// Make sure we know the URL of the current request.
350
	if (empty($_SERVER['REQUEST_URI']))
351
		$_SERVER['REQUEST_URL'] = $scripturl . (!empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '');
352
	elseif (preg_match('~^([^/]+//[^/]+)~', $scripturl, $match) == 1)
353
		$_SERVER['REQUEST_URL'] = $match[1] . $_SERVER['REQUEST_URI'];
354
	else
355
		$_SERVER['REQUEST_URL'] = $_SERVER['REQUEST_URI'];
356
357
	// And make sure HTTP_USER_AGENT is set.
358
	$_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)) : '';
359
360
	// Some final checking.
361
	if (!isValidIP($_SERVER['BAN_CHECK_IP']))
362
		$_SERVER['BAN_CHECK_IP'] = '';
363
	if ($_SERVER['REMOTE_ADDR'] == 'unknown')
364
		$_SERVER['REMOTE_ADDR'] = '';
365
}
366
367
/**
368
 * Validates a IPv6 address. returns true if it is ipv6.
369
 *
370
 * @param string $ip The ip address to be validated
371
 * @return boolean Whether the specified IP is a valid IPv6 address
372
 */
373
function isValidIPv6($ip)
374
{
375
	//looking for :
376
	if (strpos($ip, ':') === false)
377
		return false;
378
379
	//check valid address
380
	return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
381
}
382
383
/**
384
 * Expands a IPv6 address to its full form.
385
 *
386
 * @param string $addr The IPv6 address
387
 * @param bool $strict_check Whether to check the length of the expanded address for compliance
388
 * @return string|bool The expanded IPv6 address or false if $strict_check is true and the result isn't valid
389
 */
390
function expandIPv6($addr, $strict_check = true)
391
{
392
	static $converted = array();
393
394
	// Check if we have done this already.
395
	if (isset($converted[$addr]))
396
		return $converted[$addr];
397
398
	// Check if there are segments missing, insert if necessary.
399
	if (strpos($addr, '::') !== false)
400
	{
401
		$part = explode('::', $addr);
402
		$part[0] = explode(':', $part[0]);
403
		$part[1] = explode(':', $part[1]);
404
		$missing = array();
405
406
		for ($i = 0; $i < (8 - (count($part[0]) + count($part[1]))); $i++)
407
			array_push($missing, '0000');
408
409
		$part = array_merge($part[0], $missing, $part[1]);
410
	}
411
	else
412
		$part = explode(':', $addr);
413
414
	// Pad each segment until it has 4 digits.
415
	foreach ($part as &$p)
416
		while (strlen($p) < 4)
417
			$p = '0' . $p;
418
419
	unset($p);
420
421
	// Join segments.
422
	$result = implode(':', $part);
423
424
	// Save this incase of repeated use.
425
	$converted[$addr] = $result;
426
427
	// Quick check to make sure the length is as expected.
428
	if (!$strict_check || strlen($result) == 39)
429
		return $result;
430
	else
431
		return false;
432
}
433
434
/**
435
 * Detect if a IP is in a CIDR address
436
 * - returns true or false
437
 *
438
 * @param string $ip_address IP address to check
439
 * @param string $cidr_address CIDR address to verify
440
 * @return bool Whether the IP matches the CIDR
441
 */
442
function matchIPtoCIDR($ip_address, $cidr_address)
443
{
444
	list ($cidr_network, $cidr_subnetmask) = preg_split('/', $cidr_address);
445
446
	//v6?
447
	if ((strpos($cidr_network, ':') !== false))
448
	{
449
		if (!filter_var($ip_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || !filter_var($cidr_network, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))
450
			return false;
451
452
		$ip_address = inet_pton($ip_address);
453
		$cidr_network = inet_pton($cidr_network);
454
		$binMask = str_repeat("f", $cidr_subnetmask / 4);
455
		switch ($cidr_subnetmask % 4)
456
		{
457
			case 0:
458
				break;
459
			case 1:
460
				$binMask .= "8";
461
				break;
462
			case 2:
463
				$binMask .= "c";
464
				break;
465
			case 3:
466
				$binMask .= "e";
467
				break;
468
		}
469
		$binMask = str_pad($binMask, 32, '0');
470
		$binMask = pack("H*", $binMask);
471
472
		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...
473
	}
474
	else
475
		return (ip2long($ip_address) & (~((1 << (32 - $cidr_subnetmask)) - 1))) == ip2long($cidr_network);
476
}
477
478
/**
479
 * Adds slashes to the array/variable.
480
 * What it does:
481
 * - returns the var, as an array or string, with escapes as required.
482
 * - importantly escapes all keys and values!
483
 * - calls itself recursively if necessary.
484
 *
485
 * @param array|string $var A string or array of strings to escape
486
 * @return array|string The escaped string or array of escaped strings
487
 */
488
function escapestring__recursive($var)
489
{
490
	global $smcFunc;
491
492
	if (!is_array($var))
493
		return $smcFunc['db_escape_string']($var);
494
495
	// Reindex the array with slashes.
496
	$new_var = array();
497
498
	// Add slashes to every element, even the indexes!
499
	foreach ($var as $k => $v)
500
		$new_var[$smcFunc['db_escape_string']($k)] = escapestring__recursive($v);
501
502
	return $new_var;
503
}
504
505
/**
506
 * Adds html entities to the array/variable.  Uses two underscores to guard against overloading.
507
 * What it does:
508
 * - adds entities (&quot;, &lt;, &gt;) to the array or string var.
509
 * - importantly, does not effect keys, only values.
510
 * - calls itself recursively if necessary.
511
 *
512
 * @param array|string $var The string or array of strings to add entites to
513
 * @param int $level Which level we're at within the array (if called recursively)
514
 * @return array|string The string or array of strings with entities added
515
 */
516
function htmlspecialchars__recursive($var, $level = 0)
517
{
518
	global $smcFunc;
519
520
	if (!is_array($var))
521
		return isset($smcFunc['htmlspecialchars']) ? $smcFunc['htmlspecialchars']($var, ENT_QUOTES) : htmlspecialchars($var, ENT_QUOTES);
522
523
	// Add the htmlspecialchars to every element.
524
	foreach ($var as $k => $v)
525
		$var[$k] = $level > 25 ? null : htmlspecialchars__recursive($v, $level + 1);
526
527
	return $var;
528
}
529
530
/**
531
 * Removes url stuff from the array/variable.  Uses two underscores to guard against overloading.
532
 * What it does:
533
 * - takes off url encoding (%20, etc.) from the array or string var.
534
 * - importantly, does it to keys too!
535
 * - calls itself recursively if there are any sub arrays.
536
 *
537
 * @param array|string $var The string or array of strings to decode
538
 * @param int $level Which level we're at within the array (if called recursively)
539
 * @return array|string The decoded string or array of decoded strings
540
 */
541
function urldecode__recursive($var, $level = 0)
542
{
543
	if (!is_array($var))
544
		return urldecode($var);
545
546
	// Reindex the array...
547
	$new_var = array();
548
549
	// Add the htmlspecialchars to every element.
550
	foreach ($var as $k => $v)
551
		$new_var[urldecode($k)] = $level > 25 ? null : urldecode__recursive($v, $level + 1);
552
553
	return $new_var;
554
}
555
556
/**
557
 * Unescapes any array or variable.  Uses two underscores to guard against overloading.
558
 * What it does:
559
 * - unescapes, recursively, from the array or string var.
560
 * - effects both keys and values of arrays.
561
 * - calls itself recursively to handle arrays of arrays.
562
 *
563
 * @param array|string $var The string or array of strings to unescape
564
 * @return array|string The unescaped string or array of unescaped strings
565
 */
566
function unescapestring__recursive($var)
567
{
568
	global $smcFunc;
569
570
	if (!is_array($var))
571
		return $smcFunc['db_unescape_string']($var);
572
573
	// Reindex the array without slashes, this time.
574
	$new_var = array();
575
576
	// Strip the slashes from every element.
577
	foreach ($var as $k => $v)
578
		$new_var[$smcFunc['db_unescape_string']($k)] = unescapestring__recursive($v);
579
580
	return $new_var;
581
}
582
583
/**
584
 * Remove slashes recursively.  Uses two underscores to guard against overloading.
585
 * What it does:
586
 * - removes slashes, recursively, from the array or string var.
587
 * - effects both keys and values of arrays.
588
 * - calls itself recursively to handle arrays of arrays.
589
 *
590
 * @param array|string $var The string or array of strings to strip slashes from
591
 * @param int $level = 0 What level we're at within the array (if called recursively)
592
 * @return array|string The string or array of strings with slashes stripped
593
 */
594
function stripslashes__recursive($var, $level = 0)
595
{
596
	if (!is_array($var))
597
		return stripslashes($var);
598
599
	// Reindex the array without slashes, this time.
600
	$new_var = array();
601
602
	// Strip the slashes from every element.
603
	foreach ($var as $k => $v)
604
		$new_var[stripslashes($k)] = $level > 25 ? null : stripslashes__recursive($v, $level + 1);
605
606
	return $new_var;
607
}
608
609
/**
610
 * Trim a string including the HTML space, character 160.  Uses two underscores to guard against overloading.
611
 * What it does:
612
 * - trims a string or an the var array using html characters as well.
613
 * - does not effect keys, only values.
614
 * - may call itself recursively if needed.
615
 *
616
 * @param array|string $var The string or array of strings to trim
617
 * @param int $level = 0 How deep we're at within the array (if called recursively)
618
 * @return array|string The trimmed string or array of trimmed strings
619
 */
620
function htmltrim__recursive($var, $level = 0)
621
{
622
	global $smcFunc;
623
624
	// Remove spaces (32), tabs (9), returns (13, 10, and 11), nulls (0), and hard spaces. (160)
625
	if (!is_array($var))
626
		return isset($smcFunc) ? $smcFunc['htmltrim']($var) : trim($var, ' ' . "\t\n\r\x0B" . '\0' . "\xA0");
627
628
	// Go through all the elements and remove the whitespace.
629
	foreach ($var as $k => $v)
630
		$var[$k] = $level > 25 ? null : htmltrim__recursive($v, $level + 1);
631
632
	return $var;
633
}
634
635
/**
636
 * Rewrite URLs to include the session ID.
637
 * What it does:
638
 * - rewrites the URLs outputted to have the session ID, if the user
639
 *   is not accepting cookies and is using a standard web browser.
640
 * - handles rewriting URLs for the queryless URLs option.
641
 * - can be turned off entirely by setting $scripturl to an empty
642
 *   string, ''. (it wouldn't work well like that anyway.)
643
 * - because of bugs in certain builds of PHP, does not function in
644
 *   versions lower than 4.3.0 - please upgrade if this hurts you.
645
 *
646
 * @param string $buffer The unmodified output buffer
647
 * @return string The modified buffer
648
 */
649
function ob_sessrewrite($buffer)
650
{
651
	global $scripturl, $modSettings, $context;
652
653
	// PHP 8.4 deprecated SID. A better long-term solution is needed, but this works for now.
654
	$sid = defined('SID') ? @constant('SID') : null;
655
656
	// If $scripturl is set to nothing, or the SID is not defined (SSI?) just quit.
657
	if ($scripturl == '' || !isset($sid))
658
		return $buffer;
659
660
	// 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.
661
	// @todo smflib
662
	if (empty($_COOKIE) && $sid != '' && !isBrowser('possibly_robot'))
663
		$buffer = preg_replace('/(?<!<link rel="canonical" href=)"' . preg_quote($scripturl, '/') . '(?!\?' . preg_quote($sid, '/') . ')\\??/', '"' . $scripturl . '?' . $sid . '&amp;', $buffer);
664
	// Debugging templates, are we?
665
	elseif (isset($_GET['debug']))
666
		$buffer = preg_replace('/(?<!<link rel="canonical" href=)"' . preg_quote($scripturl, '/') . '\\??/', '"' . $scripturl . '?debug;', $buffer);
667
668
	// This should work even in 4.2.x, just not CGI without cgi.fix_pathinfo.
669
	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']))
670
	{
671
		// Let's do something special for session ids!
672
		if (isset($sid) && $sid != '')
673
			$buffer = preg_replace_callback(
674
				'~"' . preg_quote($scripturl, '~') . '\?(?:' . $sid . '(?:;|&|&amp;))((?:board|topic)=[^#"]+?)(#[^"]*?)?"~',
675
				function($m) use ($scripturl, $sid)
676
				{
677
					return '"' . $scripturl . "/" . strtr("$m[1]", '&;=', '//,') . ".html?" . $sid . (isset($m[2]) ? $m[2] : "") . '"';
678
				},
679
				$buffer
680
			);
681
		else
682
			$buffer = preg_replace_callback(
683
				'~"' . preg_quote($scripturl, '~') . '\?((?:board|topic)=[^#"]+?)(#[^"]*?)?"~',
684
				function($m)
685
				{
686
					global $scripturl;
687
688
					return '"' . $scripturl . '/' . strtr("$m[1]", '&;=', '//,') . '.html' . (isset($m[2]) ? $m[2] : "") . '"';
689
				},
690
				$buffer
691
			);
692
	}
693
694
	// Return the changed buffer.
695
	return $buffer;
696
}
697
698
?>