4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation;
15 * Http utility functions.
17 * @author Fabien Potencier <fabien@symfony.com>
22 * This class should not be instantiated.
24 private function __construct()
29 * Checks if an IPv4 or IPv6 address is contained in the list of given IPs or subnets.
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)
34 * @return bool Whether the IP is valid
36 public static function checkIp($requestIp, $ips)
38 if (!is_array($ips)) {
42 $method = substr_count($requestIp, ':') > 1 ? 'checkIp6' : 'checkIp4';
44 foreach ($ips as $ip) {
45 if (self::$method($requestIp, $ip)) {
54 * Compares two IPv4 addresses.
55 * In case a subnet is given, it checks if it contains the request IP.
57 * @param string $requestIp IPv4 address to check
58 * @param string $ip IPv4 address or subnet in CIDR notation
60 * @return bool Whether the request IP matches the IP, or whether the request IP is within the CIDR subnet
62 public static function checkIp4($requestIp, $ip)
64 if (!filter_var($requestIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
68 if (false !== strpos($ip, '/')) {
69 list($address, $netmask) = explode('/', $ip, 2);
71 if ($netmask === '0') {
72 return filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
75 if ($netmask < 0 || $netmask > 32) {
83 return 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
87 * Compares two IPv6 addresses.
88 * In case a subnet is given, it checks if it contains the request IP.
90 * @author David Soria Parra <dsp at php dot net>
92 * @see https://github.com/dsp/v6tools
94 * @param string $requestIp IPv6 address to check
95 * @param string $ip IPv6 address or subnet in CIDR notation
97 * @return bool Whether the IP is valid
99 * @throws \RuntimeException When IPV6 support is not enabled
101 public static function checkIp6($requestIp, $ip)
103 if (!((extension_loaded('sockets') && defined('AF_INET6')) || @inet_pton('::1'))) {
104 throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
107 if (false !== strpos($ip, '/')) {
108 list($address, $netmask) = explode('/', $ip, 2);
110 if ($netmask < 1 || $netmask > 128) {
118 $bytesAddr = unpack('n*', @inet_pton($address));
119 $bytesTest = unpack('n*', @inet_pton($requestIp));
121 if (!$bytesAddr || !$bytesTest) {
125 for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; ++$i) {
126 $left = $netmask - 16 * ($i - 1);
127 $left = ($left <= 16) ? $left : 16;
128 $mask = ~(0xffff >> $left) & 0xffff;
129 if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) {