IpHelper.php
12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
<?php
/**
* Part of the Joomla Framework Utilities Package
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Utilities;
/**
* IpHelper is a utility class for processing IP addresses
*
* This class is adapted from the `FOFUtilsIp` class distributed with the Joomla! CMS as part of the FOF library by Akeeba Ltd.
* The original class is copyright of Nicholas K. Dionysopoulos / Akeeba Ltd.
*
* @since 1.6.0
*/
final class IpHelper
{
/**
* The IP address of the current visitor
*
* @var string
* @since 1.6.0
*/
private static $ip = null;
/**
* Should I allow IP overrides through X-Forwarded-For or Client-Ip HTTP headers?
*
* @var boolean
* @since 1.6.0
*/
private static $allowIpOverrides = true;
/**
* Private constructor to prevent instantiation of this class
*
* @since 1.6.0
*/
private function __construct()
{
}
/**
* Get the current visitor's IP address
*
* @return string
*
* @since 1.6.0
*/
public static function getIp()
{
if (self::$ip === null)
{
$ip = static::detectAndCleanIP();
if (!empty($ip) && ($ip != '0.0.0.0') && \function_exists('inet_pton') && \function_exists('inet_ntop'))
{
$myIP = @inet_pton($ip);
if ($myIP !== false)
{
$ip = inet_ntop($myIP);
}
}
static::setIp($ip);
}
return self::$ip;
}
/**
* Set the IP address of the current visitor
*
* @param string $ip The visitor's IP address
*
* @return void
*
* @since 1.6.0
*/
public static function setIp($ip)
{
self::$ip = $ip;
}
/**
* Is it an IPv6 IP address?
*
* @param string $ip An IPv4 or IPv6 address
*
* @return boolean
*
* @since 1.6.0
*/
public static function isIPv6($ip)
{
return strstr($ip, ':');
}
/**
* Checks if an IP is contained in a list of IPs or IP expressions
*
* @param string $ip The IPv4/IPv6 address to check
* @param array|string $ipTable An IP expression (or a comma-separated or array list of IP expressions) to check against
*
* @return boolean
*
* @since 1.6.0
*/
public static function IPinList($ip, $ipTable = '')
{
// No point proceeding with an empty IP list
if (empty($ipTable))
{
return false;
}
// If the IP list is not an array, convert it to an array
if (!\is_array($ipTable))
{
if (strpos($ipTable, ',') !== false)
{
$ipTable = explode(',', $ipTable);
$ipTable = array_map('trim', $ipTable);
}
else
{
$ipTable = trim($ipTable);
$ipTable = array($ipTable);
}
}
// If no IP address is found, return false
if ($ip === '0.0.0.0')
{
return false;
}
// If no IP is given, return false
if (empty($ip))
{
return false;
}
// Sanity check
if (!\function_exists('inet_pton'))
{
return false;
}
// Get the IP's in_adds representation
$myIP = @inet_pton($ip);
// If the IP is in an unrecognisable format, quite
if ($myIP === false)
{
return false;
}
$ipv6 = static::isIPv6($ip);
foreach ($ipTable as $ipExpression)
{
$ipExpression = trim($ipExpression);
// Inclusive IP range, i.e. 123.123.123.123-124.125.126.127
if (strstr($ipExpression, '-'))
{
list($from, $to) = explode('-', $ipExpression, 2);
if ($ipv6 && (!static::isIPv6($from) || !static::isIPv6($to)))
{
// Do not apply IPv4 filtering on an IPv6 address
continue;
}
if (!$ipv6 && (static::isIPv6($from) || static::isIPv6($to)))
{
// Do not apply IPv6 filtering on an IPv4 address
continue;
}
$from = @inet_pton(trim($from));
$to = @inet_pton(trim($to));
// Sanity check
if (($from === false) || ($to === false))
{
continue;
}
// Swap from/to if they're in the wrong order
if ($from > $to)
{
list($from, $to) = array($to, $from);
}
if (($myIP >= $from) && ($myIP <= $to))
{
return true;
}
}
// Netmask or CIDR provided
elseif (strstr($ipExpression, '/'))
{
$binaryip = static::inetToBits($myIP);
list($net, $maskbits) = explode('/', $ipExpression, 2);
if ($ipv6 && !static::isIPv6($net))
{
// Do not apply IPv4 filtering on an IPv6 address
continue;
}
if (!$ipv6 && static::isIPv6($net))
{
// Do not apply IPv6 filtering on an IPv4 address
continue;
}
if ($ipv6 && strstr($maskbits, ':'))
{
// Perform an IPv6 CIDR check
if (static::checkIPv6CIDR($myIP, $ipExpression))
{
return true;
}
// If we didn't match it proceed to the next expression
continue;
}
if (!$ipv6 && strstr($maskbits, '.'))
{
// Convert IPv4 netmask to CIDR
$long = ip2long($maskbits);
$base = ip2long('255.255.255.255');
$maskbits = 32 - log(($long ^ $base) + 1, 2);
}
// Convert network IP to in_addr representation
$net = @inet_pton($net);
// Sanity check
if ($net === false)
{
continue;
}
// Get the network's binary representation
$expectedNumberOfBits = $ipv6 ? 128 : 24;
$binarynet = str_pad(static::inetToBits($net), $expectedNumberOfBits, '0', STR_PAD_RIGHT);
// Check the corresponding bits of the IP and the network
$ipNetBits = substr($binaryip, 0, $maskbits);
$netBits = substr($binarynet, 0, $maskbits);
if ($ipNetBits === $netBits)
{
return true;
}
}
else
{
// IPv6: Only single IPs are supported
if ($ipv6)
{
$ipExpression = trim($ipExpression);
if (!static::isIPv6($ipExpression))
{
continue;
}
$ipCheck = @inet_pton($ipExpression);
if ($ipCheck === false)
{
continue;
}
if ($ipCheck == $myIP)
{
return true;
}
}
else
{
// Standard IPv4 address, i.e. 123.123.123.123 or partial IP address, i.e. 123.[123.][123.][123]
$dots = 0;
if (substr($ipExpression, -1) == '.')
{
// Partial IP address. Convert to CIDR and re-match
foreach (count_chars($ipExpression, 1) as $i => $val)
{
if ($i == 46)
{
$dots = $val;
}
}
switch ($dots)
{
case 1:
$netmask = '255.0.0.0';
$ipExpression .= '0.0.0';
break;
case 2:
$netmask = '255.255.0.0';
$ipExpression .= '0.0';
break;
case 3:
$netmask = '255.255.255.0';
$ipExpression .= '0';
break;
default:
$dots = 0;
}
if ($dots)
{
$binaryip = static::inetToBits($myIP);
// Convert netmask to CIDR
$long = ip2long($netmask);
$base = ip2long('255.255.255.255');
$maskbits = 32 - log(($long ^ $base) + 1, 2);
$net = @inet_pton($ipExpression);
// Sanity check
if ($net === false)
{
continue;
}
// Get the network's binary representation
$expectedNumberOfBits = $ipv6 ? 128 : 24;
$binarynet = str_pad(static::inetToBits($net), $expectedNumberOfBits, '0', STR_PAD_RIGHT);
// Check the corresponding bits of the IP and the network
$ipNetBits = substr($binaryip, 0, $maskbits);
$netBits = substr($binarynet, 0, $maskbits);
if ($ipNetBits === $netBits)
{
return true;
}
}
}
if (!$dots)
{
$ip = @inet_pton(trim($ipExpression));
if ($ip == $myIP)
{
return true;
}
}
}
}
}
return false;
}
/**
* Works around the REMOTE_ADDR not containing the user's IP
*
* @return void
*
* @since 1.6.0
*/
public static function workaroundIPIssues()
{
$ip = static::getIp();
if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] === $ip)
{
return;
}
if (isset($_SERVER['REMOTE_ADDR']))
{
$_SERVER['JOOMLA_REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR'];
}
elseif (\function_exists('getenv'))
{
if (getenv('REMOTE_ADDR'))
{
$_SERVER['JOOMLA_REMOTE_ADDR'] = getenv('REMOTE_ADDR');
}
}
$_SERVER['REMOTE_ADDR'] = $ip;
}
/**
* Should I allow the remote client's IP to be overridden by an X-Forwarded-For or Client-Ip HTTP header?
*
* @param boolean $newState True to allow the override
*
* @return void
*
* @since 1.6.0
*/
public static function setAllowIpOverrides($newState)
{
self::$allowIpOverrides = $newState ? true : false;
}
/**
* Gets the visitor's IP address.
*
* Automatically handles reverse proxies reporting the IPs of intermediate devices, like load balancers. Examples:
*
* - https://www.akeebabackup.com/support/admin-tools/13743-double-ip-adresses-in-security-exception-log-warnings.html
* - https://stackoverflow.com/questions/2422395/why-is-request-envremote-addr-returning-two-ips
*
* The solution used is assuming that the last IP address is the external one.
*
* @return string
*
* @since 1.6.0
*/
protected static function detectAndCleanIP()
{
$ip = static::detectIP();
if (strstr($ip, ',') !== false || strstr($ip, ' ') !== false)
{
$ip = str_replace(' ', ',', $ip);
$ip = str_replace(',,', ',', $ip);
$ips = explode(',', $ip);
$ip = '';
while (empty($ip) && !empty($ips))
{
$ip = array_pop($ips);
$ip = trim($ip);
}
}
else
{
$ip = trim($ip);
}
return $ip;
}
/**
* Gets the visitor's IP address
*
* @return string
*
* @since 1.6.0
*/
protected static function detectIP()
{
// Normally the $_SERVER superglobal is set
if (isset($_SERVER))
{
// Do we have an x-forwarded-for HTTP header (e.g. NginX)?
if (self::$allowIpOverrides && isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
// Do we have a client-ip header (e.g. non-transparent proxy)?
if (self::$allowIpOverrides && isset($_SERVER['HTTP_CLIENT_IP']))
{
return $_SERVER['HTTP_CLIENT_IP'];
}
// Normal, non-proxied server or server behind a transparent proxy
if (isset($_SERVER['REMOTE_ADDR']))
{
return $_SERVER['REMOTE_ADDR'];
}
}
/*
* This part is executed on PHP running as CGI, or on SAPIs which do not set the $_SERVER superglobal
* If getenv() is disabled, you're screwed
*/
if (!\function_exists('getenv'))
{
return '';
}
// Do we have an x-forwarded-for HTTP header?
if (self::$allowIpOverrides && getenv('HTTP_X_FORWARDED_FOR'))
{
return getenv('HTTP_X_FORWARDED_FOR');
}
// Do we have a client-ip header?
if (self::$allowIpOverrides && getenv('HTTP_CLIENT_IP'))
{
return getenv('HTTP_CLIENT_IP');
}
// Normal, non-proxied server or server behind a transparent proxy
if (getenv('REMOTE_ADDR'))
{
return getenv('REMOTE_ADDR');
}
// Catch-all case for broken servers, apparently
return '';
}
/**
* Converts inet_pton output to bits string
*
* @param string $inet The in_addr representation of an IPv4 or IPv6 address
*
* @return string
*
* @since 1.6.0
*/
protected static function inetToBits($inet)
{
if (\strlen($inet) == 4)
{
$unpacked = unpack('A4', $inet);
}
else
{
$unpacked = unpack('A16', $inet);
}
$unpacked = str_split($unpacked[1]);
$binaryip = '';
foreach ($unpacked as $char)
{
$binaryip .= str_pad(decbin(\ord($char)), 8, '0', STR_PAD_LEFT);
}
return $binaryip;
}
/**
* Checks if an IPv6 address $ip is part of the IPv6 CIDR block $cidrnet
*
* @param string $ip The IPv6 address to check, e.g. 21DA:00D3:0000:2F3B:02AC:00FF:FE28:9C5A
* @param string $cidrnet The IPv6 CIDR block, e.g. 21DA:00D3:0000:2F3B::/64
*
* @return boolean
*
* @since 1.6.0
*/
protected static function checkIPv6CIDR($ip, $cidrnet)
{
$ip = inet_pton($ip);
$binaryip = static::inetToBits($ip);
list($net, $maskbits) = explode('/', $cidrnet);
$net = inet_pton($net);
$binarynet = static::inetToBits($net);
$ipNetBits = substr($binaryip, 0, $maskbits);
$netBits = substr($binarynet, 0, $maskbits);
return $ipNetBits === $netBits;
}
}