Checks if two IP addresses are the same or if the first is the anonymized
version of the other.

@param string $ip
@param string $currentIp This IP should not be anonymized.
@return bool
320150
public static function isSameOrAnonymizedIp($ip, $currentIp)
    {
        $ip = array_reverse(explode('.', $ip));
        $currentIp = array_reverse(explode('.', $currentIp));

        if (count($ip) != count($currentIp)) {
            return false;
        }

        foreach ($ip as $i => $byte) {
            if ($byte == 0) {
                $currentIp[$i] = 0;
            } else {
                break;
            }
        }

        foreach ($ip as $i => $byte) {
            if ($byte != $currentIp[$i]) {
                return false;
            }
        }
        return true;
    }
660101
public function isBlacklisted(AddressInterface $ip = null)
    {
        if ($ip === null) {
            $ip = $this->getRequestIPAddress();
        }
        $rangeType = $this->getRangeType($ip);

        return $rangeType !== null && ($rangeType & static::IPRANGEFLAG_BLACKLIST) === static::IPRANGEFLAG_BLACKLIST;
    }
329989
function blacklisted($ip) {
		if ($this->hive['DNSBL'] &&
			!in_array($ip,
				is_array($this->hive['EXEMPT'])?
					$this->hive['EXEMPT']:
					$this->split($this->hive['EXEMPT']))) {
			// Reverse IPv4 dotted quad
			$rev=implode('.',array_reverse(explode('.',$ip)));
			foreach (is_array($this->hive['DNSBL'])?
				$this->hive['DNSBL']:
				$this->split($this->hive['DNSBL']) as $server)
				// DNSBL lookup
				if (checkdnsrr($rev.'.'.$server,'A'))
					return TRUE;
		}
		return FALSE;
	}
1192384
private function ip_is_private($ip)
    {
        $privateAddresses = [ '10.0.0.0|10.255.255.255', '172.16.0.0|172.31.255.255', '192.168.0.0|192.168.255.255', '169.254.0.0|169.254.255.255', '127.0.0.0|127.255.255.255' ];
        $long_ip = ip2long($ip);
        if($long_ip != -1)
        {
            foreach($privateAddresses as $pri_addr)
            {
                list($start, $end) = explode('|', $pri_addr);
                // IF IS PRIVATE
                if($long_ip >= ip2long($start) && $long_ip <= ip2long($end))
                {
                        return true;
                }
            }
        }
        return false;
    }
1109998
public static function is_iplocation($ip, $location)
	{
		$iplocation = false;
		if ($ip && $location)
		{
			if (!($gdb = self::getGeoDBO()))
			{
				return $iplocation;
			}

			$sql = "SELECT COUNT(*) FROM iplocation WHERE ipfrom <= INET_ATON(" . $gdb->quote($ip) . ") AND ipto >= INET_ATON(" . $gdb->quote($ip) . ") AND LOWER(location) = LOWER(" . $gdb->quote($location) . ")";
			$gdb->setQuery($sql);
			$c = $gdb->loadResult();
			if ($c > 0)
			{
				$iplocation = true;
			}
		}
		return $iplocation;
	}
96942
protected function checkIPVersion($UserIP = false)
	{
		// Test for IPv4
		if (ip2long($UserIP) !== false)
		{
			$this->IP_Version = "IPv4";
			return $this->IP_Version;
		}
		
		// Test for IPv6
		if (substr_count($UserIP, ":") < 2)
		{
			$this->IP_Version = false;
			return false; 
		}
		// ::1 or 2001::0db8
		if (substr_count($UserIP, "::") > 1)
		{
			$this->IP_Version = false;
			return false; // one allowed
		}
		$groups = explode(':', $UserIP);
		$num_groups = count($groups);
		if (($num_groups > 8) || ($num_groups < 3))
		{
			$this->IP_Version = false;
			return false;
		}
		$empty_groups = 0;
		foreach ($groups as $group)
		{
			$group = trim($group);
			if (! empty($group) && ! (is_numeric($group) && ($group == 0)))
			{
				if (! preg_match('#([a-fA-F0-9]{0,4})#', $group))
				{
					$this->IP_Version = false;
					return false;
				}
			}
			else
			{
				++ $empty_groups;
			}
		}
		if ($empty_groups < $num_groups)
		{
			$this->IP_Version = "IPv6";
			return $this->IP_Version;
		}
		$this->IP_Version = false;
		return false; // no (valid) IP Address
	}
1084760
public static function checkIP($UserIP = false)
    {
        // Check if IP present
        if ($UserIP === false)
        {
            if (\Environment::get('ip'))
            {
                if (strpos(\Environment::get('ip'), ',') !== false) //first IP
                {
                    $UserIP =  trim(substr(\Environment::get('ip'), 0, strpos(\Environment::get('ip'), ',')));
                }
                else
                {
                    $UserIP = trim(\Environment::get('ip'));
                }
            }
            else
            {
                return false; // No IP, no search.
            }
            //Test for private IPs
            if ( true === static::checkPrivateIP($UserIP) &&
                false === empty($_SERVER['HTTP_X_FORWARDED_FOR'])
            )
            {
                //second try
                $HTTPXFF = $_SERVER['HTTP_X_FORWARDED_FOR'];
                $_SERVER['HTTP_X_FORWARDED_FOR'] = '';
            
                $UserIP = \Environment::get('ip');
                if (strpos($UserIP, ',') !== false) //first IP
                {
                    $UserIP = trim( substr($UserIP, 0, strpos($UserIP, ',') ) );
                }
                $_SERVER['HTTP_X_FORWARDED_FOR'] = $HTTPXFF;
            }
        }
        // IPv4 or IPv6 ?
        switch (static::getIpVersion($UserIP))
        {
        	case "IPv4":
        	    if ( static::checkBotIPv4($UserIP, static::getBotIpv4List()) === true) { return true; }
        	    break;
        	case "IPv6":
        	    if ( static::checkBotIPv6($UserIP, static::getBotIpv6List()) === true) { return true; }
        	    break;
        	default:
        	    return false;
        	    break;
        }
        return false;
    }
1000954
public function setVisitorIpAddress(&$ip)
    {
        $ipObject = IP::fromBinaryIP($ip);

        if (!$this->isActive()) {
            Common::printDebug("Visitor IP was _not_ anonymized: ". $ipObject->toString());
            return;
        }

        $privacyConfig = new Config();

        $newIpObject = self::applyIPMask($ipObject, $privacyConfig->ipAddressMaskLength);
        $ip = $newIpObject->toBinary();

        Common::printDebug("Visitor IP (was: ". $ipObject->toString() .") has been anonymized: ". $newIpObject->toString());
    }
1001166
public function checkIP($ip)
    {
        $result = array();
        foreach ($this->getBlServers() as $blServer) {
            if ($blServer->supportIPv4()) {
                $result[] = $blServer->getResolver()->execute($ip);
            }
        }

        return $result;
    }
1182680
public function ip2longV6($ip)
    {
        if (substr_count($ip, '::')) {
            $ip = str_replace('::', str_repeat(':0000', 8 - substr_count($ip, ':')) . ':', $ip);
        }
        $ip   = explode(':', $ip);
        $r_ip = '';
        foreach ($ip as $v) {
            $r_ip .= str_pad(base_convert($v, 16, 2), 16, 0, STR_PAD_LEFT);
        }

        return base_convert($r_ip, 2, 10);
    }
1256198
public static function isValid(string $ip = null)
    {
        if (is_null($ip)) {
            $ip = self::get();
        }

        return filter_var($ip, FILTER_VALIDATE_IP) == $ip;
    }
207436
private function isIpv4InUnroutableRange(IPv4 $ip): bool
    {
        foreach ($this->unrouteableRanges as $ipRange) {
            if ($ip->matches(new Subnet($ipRange))) {
                return true;
            }
        }

        return false;
    }
572146
public function isIPBlocked($ip){
        return ($this->isIPBlockedList($ip) || $this->isIPBlockedRange($ip) || $this->isISOBlocked($ip));
    }
466630
public function checkPermission($ip, $context)
    {
        if (!is_string($ip)) {
            throw new \TypeError('The ip parameter must be a string.');
        }
        if (!$ip) {
            throw new \InvalidArgumentException('The ip parameter cannot be empty.');
        }

        $currentRequest = $this->requestStack->getCurrentRequest();

        if (!$currentRequest) {
            return false;
        }

        return IpUtils::checkIp($currentRequest->getClientIp(), $ip);
    }
598239
public static synchronized boolean denyIP(String ip) {
		boolean rv = false;
		if(deniedIP==null) {
			deniedIP = new HashMap<String,Counter>();
			deniedIP.put(ip, new Counter(ip)); // Noted duplicated for minimum time spent
			rv= true;
		} else if(deniedIP.get(ip)==null) {
			deniedIP.put(ip, new Counter(ip));
			rv = true;
		}
		if(rv) {
			writeIP();
		}
		return rv;
	}
234735
public function ipValidateV6($ip)
    {
        $this->debug->info(__FUNCTION__, 'IP: ' . json_encode($ip));
        if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === FALSE) {
            $this->debug->info(__FUNCTION__, 'Valid is FALSE');

            return FALSE;
        }

        $this->debug->info(__FUNCTION__, 'Valid is TRUE');

        return TRUE;
    }
683407
protected function checkIps($requestedIp, $ips)
    {
        $ips = (array) $ips;

        $valid = false;
        $i = 0;

        while ($i<count($ips) && !$valid) {
            $valid = IpUtils::checkIp($requestedIp, $ips[$i]);
            $i++;
        }

        return $valid;
    }