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: $tmp = function ($n) { return sprintf('%032b', $n); };
29: $ip = implode('', array_map($tmp, unpack('N*', inet_pton($ip))));
30: $mask = implode('', array_map($tmp, unpack('N*', inet_pton($mask))));
31: $max = strlen($ip);
32: if (!$max || $max !== strlen($mask) || $size < 0 || $size > $max) {
33: return FALSE;
34: }
35: return strncmp($ip, $mask, $size === '' ? $max : $size) === 0;
36: }
37:
38: }
39: