Returns whether or not IP address is on a local network

@param string $ipAddr IP address

@return boolean
766582
public static function isIpLocal($ipAddr)
	{
		// $this->debug->groupCollapsed(__CLASS__.'->'.__FUNCTION__, $ip);
		$return = false;
		if ($ipAddr == '::1') {
			$ipAddr = '127.0.0.1';
		}
		$net_n_masks = array(
			array('127.0.0.0',		'255.0.0.0'),
			array('10.0.0.0',		'255.0.0.0'),
			array('192.168.0.0',	'255.255.0.0'),
			array('172.16.0.0',		'255.240.0.0'),
			array('169.254.0.0',	'255.255.0.0'),
		);
		foreach ($net_n_masks as $nma) {
			if (self::isIpIn($ipAddr, $nma[0], $nma[1])) {
				$return = true;
				break;
			}
		}
		// $this->debug->groupEnd();
		return $return;
	}
538462
private function isAllowed($ip) {
        // Look for a wildcard
        if (in_array('*', $this->params['allow'])) {
            // There is a wildcard in the list, all IP's are allowed
            return true;
        }

        // Remove IP's which is not of the same type as $ip before matching
        if ($this->isIPv6($ip)) {
            $list = array_filter($this->params['allow'], [$this, 'isIPv6']);
        } else {
            $list = array_filter($this->params['allow'], [$this, 'isIPv4']);
        }

        foreach ($list as $range) {
            if ((strpos($range, '/') !== false && $this->cidrMatch($ip, $range)) || $ip === $range) {
                return true;
            }
        }

        return false;
    }
332840
public static function isLocal($ip)
    {
        $localIpv4Ranges = array(
            '10.*.*.*',
            '127.*.*.*',
            '192.168.*.*',
            '169.254.*.*',
            '172.16.0.0-172.31.255.255',
            '224.*.*.*',
        );

        $localIpv6Ranges = array(
            'fe80::/10',
            '::1/128',
            'fc00::/7'
        );

        if (self::isValidv4($ip)) {
            return self::match($ip, $localIpv4Ranges);
        }

        if (self::isValidv6($ip)) {
            return self::match($ip, $localIpv6Ranges);
        }

        return false;
    }
4934
public function isIPAddressInSubnet($ip_address_string)
    {
        $ip_address = ip2long($ip_address_string);
        list($start_ip, $end_ip) = $this->getIPAddressRangeAsInts();

        return $ip_address >= $start_ip && $ip_address <= $end_ip
            ? true
            : false;
    }
336213
public static function guessIpAddress()
	{
		foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key)
		{
			if (array_key_exists($key, $_SERVER) === true)
			{
				foreach (explode(',', $_SERVER[$key]) as $ipAddress)
				{
					$ipAddress = trim($ipAddress);

					if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false)
					{
						return $ipAddress;
					}
				}
			}
		}
	}
890229
function ipAddress() {
		if ($this->_ipAddress !== FALSE) {
			return $this->_ipAddress;
		}
		
		$proxyIps = Cfg::get('proxy_ips');
		if ($proxyIps != '' && $_SERVER['HTTP_X_FORWARDED_FOR'] && $_SERVER['REMOTE_ADDR']) {
			$proxies = preg_split('/[\s,]/', $proxyIps, -1, PREG_SPLIT_NO_EMPTY);
			$proxies = is_array($proxies) ? $proxies : array($proxies);
			
			$this->_ipAddress = in_array($_SERVER['REMOTE_ADDR'], $proxies) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
		} elseif ($_SERVER['REMOTE_ADDR'] && $_SERVER['HTTP_CLIENT_IP']) {
			$this->_ipAddress = $_SERVER['HTTP_CLIENT_IP'];
		} elseif ($_SERVER['REMOTE_ADDR']) {
			$this->_ipAddress = $_SERVER['REMOTE_ADDR'];
		} elseif ($_SERVER['HTTP_CLIENT_IP']) {
			$this->_ipAddress = $_SERVER['HTTP_CLIENT_IP'];
		} elseif ($_SERVER['HTTP_X_FORWARDED_FOR']) {
			$this->_ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
		}
		
		if ($this->_ipAddress === FALSE) {
			$this->_ipAddress = '0.0.0.0';
			return $this->_ipAddress;
		}
		
		if (strpos($this->_ipAddress, ',') !== FALSE) {
			$x = explode(',', $this->_ipAddress);
			$this->_ipAddress = trim(end($x));
		}
		
		// if (!$this->_validIp($this->_ipAddress)) {
		// 	$this->_ipAddress = '0.0.0.0';
		// }
		
		return $this->_ipAddress;
	}
955574
public static function detect()
    {
        foreach (static::$remotes as $remote) {
            if ($address = getenv($remote)) {
                foreach (explode(',', $address) as $ipAddress) {
                    if (IpValidator::validate($ipAddress)) return $ipAddress;
                }
            }
        }

        return static::$default;
    }
1330646
protected function allowedIpAddress()
    {
        if ($this->config->allowIpAddresses && is_array($this->getAllowedIpAddresses())) {
            if (in_array($_SERVER['REMOTE_ADDR'], $this->getAllowedIpAddresses())) {
                return true;
            }
        }

        return false;
    }
481119
public function containsIp($ipAddress)
    {
        $ipLong = ip2long($ipAddress);
        return ($this->getLowerInt() <= $ipLong) && ($this->getUpperInt() >= $ipLong);
    }
190552
private function isAddressValid(string $address): bool
    {
        $position = \strrpos($address, ':');
        if ($position === false) {
            return ($address[0] ?? '') === "\0"; // udg socket address.
        }

        $ip = \trim(\substr($address, 0, $position), '[]');
        $port = (int) \substr($address, $position + 1);

        return \inet_pton($ip) !== false && $port > 0 && $port < 65536;
    }
1008406
static function isIPInNet( $ipaddress, $network, $mask = 24 )
    {
        $lnet = ip2long( $network );
        $lip = ip2long( $ipaddress );
        $binnet = str_pad( decbin( $lnet ), 32, '0', STR_PAD_LEFT );
        $firstpart = substr( $binnet, 0, $mask );
        $binip = str_pad( decbin( $lip ), 32, '0', STR_PAD_LEFT );
        $firstip = substr( $binip, 0, $mask );
        return( strcmp( $firstpart, $firstip ) == 0 );
    }
577876
public function isWorking()
    {
        if (empty($_SERVER[self::TEST_SERVER_VAR])
            && empty($_SERVER[self::TEST_SERVER_VAR_ALT])
            && empty($_SERVER[self::TEST_SERVER_VAR_ALT_IPV6])
        ) {
            return Piwik::translate("UserCountry_CannotFindGeoIPServerVar", self::TEST_SERVER_VAR . ' $_SERVER');
        }

        return true; // can't check for another IP
    }
1223273
public static function isIpIn($ipAddr, $net, $mask)
	{
		// doesn't check for the return value of ip2long
		$ipAddr	= ip2long($ipAddr);
		$rede	= ip2long($net);
		$mask	= ip2long($mask);
		$res	= $ipAddr & $mask;	// AND
		return ($res == $rede);
	}
931864
public static boolean isLocalAddress(String addr)
	{
		try
		{
			InetAddress iAddr = InetAddress.getByName(addr);
			return isLocalAddress(iAddr);
		}
		catch (UnknownHostException e)
		{
			return false;
		}
	}
585039
public function isIpAddress($flags = 0)
    {
        $this->isString();

        return $this->doCheck(function ($value) use ($flags) {
            $this->throwExceptionIfFalse(
                filter_var($value, FILTER_VALIDATE_IP, $flags),
                '"%s" is not a valid IP address.', $value
            );
        });
    }
593355
public static function _getUserIpAddr ()
    {
        $ip = 'unknown.ip.address';
        if(isset($_SERVER)){
	        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) // If server is behind a load balancer
			{
	            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
	        } elseif (!empty($_SERVER['REMOTE_ADDR'])) {
	            $ip = $_SERVER['REMOTE_ADDR'];
	        }
	    }
        return $ip;
    }
177795
public static function isAdminIP() {
        if (!isset($_SERVER['REMOTE_ADDR']))
            return false;

        static $whiteList; if (!$whiteList) {
            $ips = ['127.0.0.1', $_SERVER['SERVER_ADDR']];

            if (!self::$defaultConfig)
                return in_array($_SERVER['REMOTE_ADDR'], $ips);

            $values = self::$defaultConfig->get('admin.ip.whitelist', []);
            if (!is_array($values)) $values = [$values];
            $whiteList = \array_keys(\array_flip(\array_merge($ips, $values)));
        }
        return in_array($_SERVER['REMOTE_ADDR'], $whiteList);
    }