123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace Symfony\Component\HttpFoundation;
- class IpUtils
- {
-
- private function __construct()
- {
- }
-
- public static function checkIp($requestIp, $ips)
- {
- if (!is_array($ips)) {
- $ips = array($ips);
- }
- $method = substr_count($requestIp, ':') > 1 ? 'checkIp6' : 'checkIp4';
- foreach ($ips as $ip) {
- if (self::$method($requestIp, $ip)) {
- return true;
- }
- }
- return false;
- }
-
- public static function checkIp4($requestIp, $ip)
- {
- if (false !== strpos($ip, '/')) {
- list($address, $netmask) = explode('/', $ip, 2);
- if ($netmask === '0') {
-
- return filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
- }
- if ($netmask < 0 || $netmask > 32) {
- return false;
- }
- } else {
- $address = $ip;
- $netmask = 32;
- }
- return 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
- }
-
- public static function checkIp6($requestIp, $ip)
- {
- if (!((extension_loaded('sockets') && defined('AF_INET6')) || @inet_pton('::1'))) {
- throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
- }
- if (false !== strpos($ip, '/')) {
- list($address, $netmask) = explode('/', $ip, 2);
- if ($netmask < 1 || $netmask > 128) {
- return false;
- }
- } else {
- $address = $ip;
- $netmask = 128;
- }
- $bytesAddr = unpack('n*', inet_pton($address));
- $bytesTest = unpack('n*', inet_pton($requestIp));
- for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; ++$i) {
- $left = $netmask - 16 * ($i - 1);
- $left = ($left <= 16) ? $left : 16;
- $mask = ~(0xffff >> $left) & 0xffff;
- if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) {
- return false;
- }
- }
- return true;
- }
- }
|