diff options
author | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-10-22 08:39:01 +0400 |
---|---|---|
committer | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-10-29 07:47:51 +0300 |
commit | bbbf9acea29e10b9b9b2da1def68fbebc1efef6c (patch) | |
tree | 411dfffb5b0dcbb1923c54dadd49a6d991248307 /core/IP.php | |
parent | d9aef0c397045484a3848106b57de99f7cdeddbe (diff) |
Extracted the Piwik\IP class into a standalone Network component
Diffstat (limited to 'core/IP.php')
-rw-r--r-- | core/IP.php | 235 |
1 files changed, 76 insertions, 159 deletions
diff --git a/core/IP.php b/core/IP.php index 691bc7442c..10c2c62ef8 100644 --- a/core/IP.php +++ b/core/IP.php @@ -9,6 +9,10 @@ namespace Piwik; +use Piwik\Network\IPUtils; +use Piwik\Network\IPv4; +use Piwik\Network\IPv6; + /** * Contains IP address helper functions (for both IPv4 and IPv6). * @@ -28,47 +32,18 @@ namespace Piwik; */ class IP { - const MAPPED_IPv4_START = '::ffff:'; - /** * Removes the port and the last portion of a CIDR IP address. * * @param string $ipString The IP address to sanitize. * @return string + * + * @deprecated Use IPUtils::sanitizeIp() instead + * @see \Piwik\Network\IPUtils */ public static function sanitizeIp($ipString) { - $ipString = trim($ipString); - - // CIDR notation, A.B.C.D/E - $posSlash = strrpos($ipString, '/'); - if ($posSlash !== false) { - $ipString = substr($ipString, 0, $posSlash); - } - - $posColon = strrpos($ipString, ':'); - $posDot = strrpos($ipString, '.'); - if ($posColon !== false) { - // IPv6 address with port, [A:B:C:D:E:F:G:H]:EEEE - $posRBrac = strrpos($ipString, ']'); - if ($posRBrac !== false && $ipString[0] == '[') { - $ipString = substr($ipString, 1, $posRBrac - 1); - } - - if ($posDot !== false) { - // IPv4 address with port, A.B.C.D:EEEE - if ($posColon > $posDot) { - $ipString = substr($ipString, 0, $posColon); - } - // else: Dotted quad IPv6 address, A:B:C:D:E:F:G.H.I.J - } else if (strpos($ipString, ':') === $posColon) { - $ipString = substr($ipString, 0, $posColon); - } - // else: IPv6 address, A:B:C:D:E:F:G:H - } - // else: IPv4 address, A.B.C.D - - return $ipString; + return IPUtils::sanitizeIp($ipString); } /** @@ -83,43 +58,15 @@ class IP * * @param string $ipRangeString IP address range * @return string|bool IP address range in CIDR notation OR false + * + * @deprecated Use IPUtils::sanitizeIpRange() instead + * @see \Piwik\Network\IPUtils */ public static function sanitizeIpRange($ipRangeString) { - $ipRangeString = trim($ipRangeString); - if (empty($ipRangeString)) { - return false; - } - - // IPv4 address with wildcards '*' - if (strpos($ipRangeString, '*') !== false) { - if (preg_match('~(^|\.)\*\.\d+(\.|$)~D', $ipRangeString)) { - return false; - } - - $bits = 32 - 8 * substr_count($ipRangeString, '*'); - $ipRangeString = str_replace('*', '0', $ipRangeString); - } + $result = IPUtils::sanitizeIpRange($ipRangeString); - // CIDR - if (($pos = strpos($ipRangeString, '/')) !== false) { - $bits = substr($ipRangeString, $pos + 1); - $ipRangeString = substr($ipRangeString, 0, $pos); - } - - // single IP - if (($ip = @inet_pton($ipRangeString)) === false) - return false; - - $maxbits = strlen($ip) * 8; - if (!isset($bits)) - $bits = $maxbits; - - if ($bits < 0 || $bits > $maxbits) { - return false; - } - - return "$ipRangeString/$bits"; + return $result === null ? false : $result; } /** @@ -127,12 +74,13 @@ class IP * * @param string $ipString IP address, either IPv4 or IPv6, e.g., `"127.0.0.1"`. * @return string Binary-safe string, e.g., `"\x7F\x00\x00\x01"`. + * + * @deprecated Use IPUtils::stringToBinaryIP() instead + * @see \Piwik\Network\IPUtils */ public static function P2N($ipString) { - // use @inet_pton() because it throws an exception and E_WARNING on invalid input - $ip = @inet_pton($ipString); - return $ip === false ? "\x00\x00\x00\x00" : $ip; + return IPUtils::stringToBinaryIP($ipString); } /** @@ -142,12 +90,12 @@ class IP * * @param string $ip IP address in network address format. * @return string IP address in presentation format. + * + * @deprecated Use IPUtils::binaryToStringIP() instead */ public static function N2P($ip) { - // use @inet_ntop() because it throws an exception and E_WARNING on invalid input - $ipStr = @inet_ntop($ip); - return $ipStr === false ? '0.0.0.0' : $ipStr; + return IPUtils::binaryToStringIP($ip); } /** @@ -155,10 +103,12 @@ class IP * * @param string $ip IP address in network address format. * @return string IP address in presentation format. + * + * @deprecated Will be removed */ public static function prettyPrint($ip) { - return self::N2P($ip); + return IPUtils::binaryToStringIP($ip); } /** @@ -167,27 +117,15 @@ class IP * * @param string $ip IP address in network address format. * @return bool True if IPv4, else false. + * + * @deprecated Will be removed + * @see \Piwik\Network\IP */ public static function isIPv4($ip) { - // in case mbstring overloads strlen function - $strlen = function_exists('mb_orig_strlen') ? 'mb_orig_strlen' : 'strlen'; - - // IPv4 - if ($strlen($ip) == 4) { - return true; - } + $ip = Network\IP::fromBinaryIP($ip); - // IPv6 - transitional address? - if ($strlen($ip) == 16) { - if (substr_compare($ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff", 0, 12) === 0 - || substr_compare($ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 0, 12) === 0 - ) { - return true; - } - } - - return false; + return $ip instanceof IPv4; } /** @@ -200,12 +138,14 @@ class IP * * @param string $ip IPv4 address in network address format. * @return string IP address in presentation format. + * + * @deprecated This method was kept for backward compatibility and doesn't seem used */ public static function long2ip($ip) { // IPv4 if (strlen($ip) == 4) { - return self::N2P($ip); + return IPUtils::binaryToStringIP($ip); } // IPv6 - transitional address? @@ -214,7 +154,7 @@ class IP || substr_compare($ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 0, 12) === 0 ) { // remap 128-bit IPv4-mapped and IPv4-compat addresses - return self::N2P(substr($ip, 12)); + return IPUtils::binaryToStringIP(substr($ip, 12)); } } @@ -227,10 +167,15 @@ class IP * * @param string $ip * @return bool + * + * @deprecated Will be removed + * @see \Piwik\Network\IP */ public static function isIPv6($ip) { - return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); + $ip = Network\IP::fromBinaryIP($ip); + + return $ip instanceof IPv6; } /** @@ -238,10 +183,19 @@ class IP * * @param string $ip * @return bool + * + * @deprecated Will be removed + * @see \Piwik\Network\IP */ public static function isMappedIPv4($ip) { - return substr($ip, 0, strlen(self::MAPPED_IPv4_START)) === self::MAPPED_IPv4_START; + $ip = Network\IP::fromStringIP($ip); + + if (! $ip instanceof IPv6) { + return false; + } + + return $ip->isMappedIPv4(); } /** @@ -249,10 +203,15 @@ class IP * * @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) { - return substr($ip, strlen(self::MAPPED_IPv4_START)); + $ip = Network\IP::fromStringIP($ip); + + return $ip->toIPv4String(); } /** @@ -260,37 +219,15 @@ class IP * * @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) { - if (strpos($ipRange, '/') === false) { - $ipRange = self::sanitizeIpRange($ipRange); - } - $pos = strpos($ipRange, '/'); - - $bits = substr($ipRange, $pos + 1); - $range = substr($ipRange, 0, $pos); - $high = $low = @inet_pton($range); - if ($low === false) { - return false; - } - - $lowLen = strlen($low); - $i = $lowLen - 1; - $bits = $lowLen * 8 - $bits; + $result = IPUtils::getIPRangeBounds($ipRange); - for ($n = (int)($bits / 8); $n > 0; $n--, $i--) { - $low[$i] = chr(0); - $high[$i] = chr(255); - } - - $n = $bits % 8; - if ($n) { - $low[$i] = chr(ord($low[$i]) & ~((1 << $n) - 1)); - $high[$i] = chr(ord($high[$i]) | ((1 << $n) - 1)); - } - - return array($low, $high); + return $result === null ? false : $result; } /** @@ -301,40 +238,15 @@ class IP * @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) { - $ipLen = strlen($ip); - if (empty($ip) || empty($ipRanges) || ($ipLen != 4 && $ipLen != 16)) { - return false; - } - - foreach ($ipRanges as $range) { - if (is_array($range)) { - // already split into low/high IP addresses - $range[0] = self::P2N($range[0]); - $range[1] = self::P2N($range[1]); - } else { - // expect CIDR format but handle some variations - $range = self::getIpsForRange($range); - } - if ($range === false) { - continue; - } - - $low = $range[0]; - $high = $range[1]; - if (strlen($low) != $ipLen) { - continue; - } + $ip = Network\IP::fromBinaryIP($ip); - // binary-safe string comparison - if ($ip >= $low && $ip <= $high) { - return true; - } - } - - return false; + return $ip->isInRanges($ipRanges); } /** @@ -356,7 +268,7 @@ class IP } $ipString = self::getNonProxyIpFromHeader($default, $clientHeaders); - return self::sanitizeIp($ipString); + return IPUtils::sanitizeIp($ipString); } /** @@ -406,7 +318,8 @@ class IP $elements = explode(',', $csv); for ($i = count($elements); $i--;) { $element = trim(Common::sanitizeInputValue($elements[$i])); - if (empty($excludedIps) || (!in_array($element, $excludedIps) && !self::isIpInRange(self::P2N(self::sanitizeIp($element)), $excludedIps))) { + $ip = \Piwik\Network\IP::fromStringIP(IPUtils::sanitizeIp($element)); + if (empty($excludedIps) || (!in_array($element, $excludedIps) && !$ip->isInRanges($excludedIps))) { return $element; } } @@ -415,16 +328,20 @@ class IP } /** - * Retirms the hostname for a given IP address. + * 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) { - // PHP's reverse lookup supports ipv4 and ipv6 - // except on Windows before PHP 5.3 - $host = strtolower(@gethostbyaddr($ipStr)); - return $host === '' ? $ipStr : $host; + $ip = Network\IP::fromStringIP($ipStr); + + $host = $ip->getHostname(); + + return $host === null ? $ipStr : $host; } } |