1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Http;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17:
18: class Helpers
19: {
20:
21: 22: 23: 24:
25: public static function ipMatch($ip, $mask)
26: {
27: list($mask, $size) = explode('/', $mask . '/');
28: $ipv4 = strpos($ip, '.');
29: $max = $ipv4 ? 32 : 128;
30: if (($ipv4 xor strpos($mask, '.')) || $size < 0 || $size > $max) {
31: return FALSE;
32: } elseif ($ipv4) {
33: $arr = array(ip2long($ip), ip2long($mask));
34: } else {
35: $arr = unpack('N*', inet_pton($ip) . inet_pton($mask));
36: $size = $size === '' ? 0 : $max - $size;
37: }
38: $bits = implode('', array_map(function ($n) {
39: return sprintf('%032b', $n);
40: }, $arr));
41: return substr($bits, 0, $max - $size) === substr($bits, $max, $max - $size);
42: }
43:
44: }
45: