Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpFoundation; Chris@0: Chris@0: /** Chris@0: * Http utility functions. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class IpUtils Chris@0: { Chris@17: private static $checkedIps = []; Chris@12: Chris@0: /** Chris@0: * This class should not be instantiated. Chris@0: */ Chris@0: private function __construct() Chris@0: { Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks if an IPv4 or IPv6 address is contained in the list of given IPs or subnets. Chris@0: * Chris@0: * @param string $requestIp IP to check Chris@0: * @param string|array $ips List of IPs or subnets (can be a string if only a single one) Chris@0: * Chris@0: * @return bool Whether the IP is valid Chris@0: */ Chris@0: public static function checkIp($requestIp, $ips) Chris@0: { Chris@17: if (!\is_array($ips)) { Chris@17: $ips = [$ips]; Chris@0: } Chris@0: Chris@0: $method = substr_count($requestIp, ':') > 1 ? 'checkIp6' : 'checkIp4'; Chris@0: Chris@0: foreach ($ips as $ip) { Chris@0: if (self::$method($requestIp, $ip)) { Chris@0: return true; Chris@0: } Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Compares two IPv4 addresses. Chris@0: * In case a subnet is given, it checks if it contains the request IP. Chris@0: * Chris@0: * @param string $requestIp IPv4 address to check Chris@0: * @param string $ip IPv4 address or subnet in CIDR notation Chris@0: * Chris@0: * @return bool Whether the request IP matches the IP, or whether the request IP is within the CIDR subnet Chris@0: */ Chris@0: public static function checkIp4($requestIp, $ip) Chris@0: { Chris@12: $cacheKey = $requestIp.'-'.$ip; Chris@12: if (isset(self::$checkedIps[$cacheKey])) { Chris@12: return self::$checkedIps[$cacheKey]; Chris@12: } Chris@12: Chris@0: if (!filter_var($requestIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { Chris@12: return self::$checkedIps[$cacheKey] = false; Chris@0: } Chris@0: Chris@0: if (false !== strpos($ip, '/')) { Chris@0: list($address, $netmask) = explode('/', $ip, 2); Chris@0: Chris@14: if ('0' === $netmask) { Chris@12: return self::$checkedIps[$cacheKey] = filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); Chris@0: } Chris@0: Chris@0: if ($netmask < 0 || $netmask > 32) { Chris@12: return self::$checkedIps[$cacheKey] = false; Chris@0: } Chris@0: } else { Chris@0: $address = $ip; Chris@0: $netmask = 32; Chris@0: } Chris@0: Chris@14: if (false === ip2long($address)) { Chris@14: return self::$checkedIps[$cacheKey] = false; Chris@14: } Chris@14: Chris@12: return self::$checkedIps[$cacheKey] = 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Compares two IPv6 addresses. Chris@0: * In case a subnet is given, it checks if it contains the request IP. Chris@0: * Chris@0: * @author David Soria Parra Chris@0: * Chris@0: * @see https://github.com/dsp/v6tools Chris@0: * Chris@0: * @param string $requestIp IPv6 address to check Chris@0: * @param string $ip IPv6 address or subnet in CIDR notation Chris@0: * Chris@0: * @return bool Whether the IP is valid Chris@0: * Chris@0: * @throws \RuntimeException When IPV6 support is not enabled Chris@0: */ Chris@0: public static function checkIp6($requestIp, $ip) Chris@0: { Chris@12: $cacheKey = $requestIp.'-'.$ip; Chris@12: if (isset(self::$checkedIps[$cacheKey])) { Chris@12: return self::$checkedIps[$cacheKey]; Chris@12: } Chris@12: Chris@17: if (!((\extension_loaded('sockets') && \defined('AF_INET6')) || @inet_pton('::1'))) { Chris@0: throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".'); Chris@0: } Chris@0: Chris@0: if (false !== strpos($ip, '/')) { Chris@0: list($address, $netmask) = explode('/', $ip, 2); Chris@0: Chris@14: if ('0' === $netmask) { Chris@14: return (bool) unpack('n*', @inet_pton($address)); Chris@14: } Chris@14: Chris@0: if ($netmask < 1 || $netmask > 128) { Chris@12: return self::$checkedIps[$cacheKey] = false; Chris@0: } Chris@0: } else { Chris@0: $address = $ip; Chris@0: $netmask = 128; Chris@0: } Chris@0: Chris@0: $bytesAddr = unpack('n*', @inet_pton($address)); Chris@0: $bytesTest = unpack('n*', @inet_pton($requestIp)); Chris@0: Chris@0: if (!$bytesAddr || !$bytesTest) { Chris@12: return self::$checkedIps[$cacheKey] = false; Chris@0: } Chris@0: Chris@0: for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; ++$i) { Chris@0: $left = $netmask - 16 * ($i - 1); Chris@0: $left = ($left <= 16) ? $left : 16; Chris@0: $mask = ~(0xffff >> $left) & 0xffff; Chris@0: if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) { Chris@12: return self::$checkedIps[$cacheKey] = false; Chris@0: } Chris@0: } Chris@0: Chris@12: return self::$checkedIps[$cacheKey] = true; Chris@0: } Chris@0: }