1: <?php
2:
3: /*
4: * This file is part of the Symfony package.
5: *
6: * (c) Fabien Potencier <fabien@symfony.com>
7: *
8: * For the full copyright and license information, please view the LICENSE
9: * file that was distributed with this source code.
10: */
11:
12: namespace Symfony\Component\HttpFoundation;
13:
14: /**
15: * Http utility functions.
16: *
17: * @author Fabien Potencier <fabien@symfony.com>
18: */
19: class IpUtils
20: {
21: /**
22: * This class should not be instantiated.
23: */
24: private function __construct()
25: {
26: }
27:
28: /**
29: * Checks if an IPv4 or IPv6 address is contained in the list of given IPs or subnets.
30: *
31: * @param string $requestIp IP to check
32: * @param string|array $ips List of IPs or subnets (can be a string if only a single one)
33: *
34: * @return bool Whether the IP is valid
35: */
36: public static function checkIp($requestIp, $ips)
37: {
38: if (!is_array($ips)) {
39: $ips = array($ips);
40: }
41:
42: $method = substr_count($requestIp, ':') > 1 ? 'checkIp6' : 'checkIp4';
43:
44: foreach ($ips as $ip) {
45: if (self::$method($requestIp, $ip)) {
46: return true;
47: }
48: }
49:
50: return false;
51: }
52:
53: /**
54: * Compares two IPv4 addresses.
55: * In case a subnet is given, it checks if it contains the request IP.
56: *
57: * @param string $requestIp IPv4 address to check
58: * @param string $ip IPv4 address or subnet in CIDR notation
59: *
60: * @return bool Whether the IP is valid
61: */
62: public static function checkIp4($requestIp, $ip)
63: {
64: if (false !== strpos($ip, '/')) {
65: list($address, $netmask) = explode('/', $ip, 2);
66:
67: if ($netmask < 1 || $netmask > 32) {
68: return false;
69: }
70: } else {
71: $address = $ip;
72: $netmask = 32;
73: }
74:
75: return 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
76: }
77:
78: /**
79: * Compares two IPv6 addresses.
80: * In case a subnet is given, it checks if it contains the request IP.
81: *
82: * @author David Soria Parra <dsp at php dot net>
83: *
84: * @see https://github.com/dsp/v6tools
85: *
86: * @param string $requestIp IPv6 address to check
87: * @param string $ip IPv6 address or subnet in CIDR notation
88: *
89: * @return bool Whether the IP is valid
90: *
91: * @throws \RuntimeException When IPV6 support is not enabled
92: */
93: public static function checkIp6($requestIp, $ip)
94: {
95: if (!((extension_loaded('sockets') && defined('AF_INET6')) || @inet_pton('::1'))) {
96: throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
97: }
98:
99: if (false !== strpos($ip, '/')) {
100: list($address, $netmask) = explode('/', $ip, 2);
101:
102: if ($netmask < 1 || $netmask > 128) {
103: return false;
104: }
105: } else {
106: $address = $ip;
107: $netmask = 128;
108: }
109:
110: $bytesAddr = unpack("n*", inet_pton($address));
111: $bytesTest = unpack("n*", inet_pton($requestIp));
112:
113: for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; $i++) {
114: $left = $netmask - 16 * ($i-1);
115: $left = ($left <= 16) ? $left : 16;
116: $mask = ~(0xffff >> $left) & 0xffff;
117: if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) {
118: return false;
119: }
120: }
121:
122: return true;
123: }
124: }
125: