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