isMappedIPv4(); } /** * Returns an IPv4 address from a 'mapped' IPv6 address. * * @param string $ip eg, `'::ffff:192.0.2.128'` * @return string eg, `'192.0.2.128'` * * @deprecated Use Piwik\Network\IP::toIPv4String() instead * @see \Piwik\Network\IP */ public static function getIPv4FromMappedIPv6($ip) { $ip = Network\IP::fromStringIP($ip); return $ip->toIPv4String(); } /** * Get low and high IP addresses for a specified range. * * @param array $ipRange An IP address range in presentation format. * @return array|bool Array `array($lowIp, $highIp)` in network address format, or false on failure. * * @deprecated Use Piwik\Network\IPUtils::getIPRangeBounds() instead * @see \Piwik\Network\IPUtils */ public static function getIpsForRange($ipRange) { $result = IPUtils::getIPRangeBounds($ipRange); return $result === null ? false : $result; } /** * Determines if an IP address is in a specified IP address range. * * An IPv4-mapped address should be range checked with an IPv4-mapped address range. * * @param string $ip IP address in network address format * @param array $ipRanges List of IP address ranges * @return bool True if in any of the specified IP address ranges; else false. * * @deprecated Use Piwik\Network\IP::isInRanges() instead * @see \Piwik\Network\IP */ public static function isIpInRange($ip, $ipRanges) { $ip = Network\IP::fromBinaryIP($ip); return $ip->isInRanges($ipRanges); } /** * Returns the most accurate IP address availble for the current user, in * IPv4 format. This could be the proxy client's IP address. * * @return string IP address in presentation format. */ public static function getIpFromHeader() { $clientHeaders = @Config::getInstance()->General['proxy_client_headers']; if (!is_array($clientHeaders)) { $clientHeaders = array(); } $default = '0.0.0.0'; if (isset($_SERVER['REMOTE_ADDR'])) { $default = $_SERVER['REMOTE_ADDR']; } $ipString = self::getNonProxyIpFromHeader($default, $clientHeaders); return IPUtils::sanitizeIp($ipString); } /** * Returns a non-proxy IP address from header. * * @param string $default Default value to return if there no matching proxy header. * @param array $proxyHeaders List of proxy headers. * @return string */ public static function getNonProxyIpFromHeader($default, $proxyHeaders) { $proxyIps = array(); $config = Config::getInstance()->General; if (isset($config['proxy_ips'])) { $proxyIps = $config['proxy_ips']; } if (!is_array($proxyIps)) { $proxyIps = array(); } $proxyIps[] = $default; // examine proxy headers foreach ($proxyHeaders as $proxyHeader) { if (!empty($_SERVER[$proxyHeader])) { $proxyIp = self::getLastIpFromList($_SERVER[$proxyHeader], $proxyIps); if (strlen($proxyIp) && stripos($proxyIp, 'unknown') === false) { return $proxyIp; } } } return $default; } /** * Returns the last IP address in a comma separated list, subject to an optional exclusion list. * * @param string $csv Comma separated list of elements. * @param array $excludedIps Optional list of excluded IP addresses (or IP address ranges). * @return string Last (non-excluded) IP address in the list. */ public static function getLastIpFromList($csv, $excludedIps = null) { $p = strrpos($csv, ','); if ($p !== false) { $elements = explode(',', $csv); for ($i = count($elements); $i--;) { $element = trim(Common::sanitizeInputValue($elements[$i])); $ip = \Piwik\Network\IP::fromStringIP(IPUtils::sanitizeIp($element)); if (empty($excludedIps) || (!in_array($element, $excludedIps) && !$ip->isInRanges($excludedIps))) { return $element; } } } return trim(Common::sanitizeInputValue($csv)); } /** * Returns the hostname for a given IP address. * * @param string $ipStr Human-readable IP address. * @return string The hostname or unmodified $ipStr on failure. * * @deprecated Use Piwik\Network\IP::getHostname() instead * @see \Piwik\Network\IP */ public static function getHostByAddr($ipStr) { $ip = Network\IP::fromStringIP($ipStr); $host = $ip->getHostname(); return $host === null ? $ipStr : $host; } }