diff options
-rw-r--r-- | CHANGELOG.md | 14 | ||||
-rw-r--r-- | core/Common.php | 20 | ||||
-rw-r--r-- | core/IP.php | 237 | ||||
-rw-r--r-- | core/Tracker/VisitExcluded.php | 9 | ||||
-rw-r--r-- | plugins/Provider/Columns/Provider.php | 13 | ||||
-rw-r--r-- | plugins/UserCountry/Columns/Country.php | 13 | ||||
-rw-r--r-- | tests/PHPUnit/Unit/IPTest.php | 11 |
7 files changed, 37 insertions, 280 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 43d8996d26..82a9d15f10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,20 @@ This is a changelog for Piwik platform developers. All changes for our HTTP API' * The deprecated methods `MenuAdmin::addEntry()` and `MenuAdmin::removeEntry()` have been removed. Use `Piwik\Plugin\Menu` instead. * The deprecated methods `MenuTop::addEntry()` and `MenuTop::removeEntry()` have been removed. Use `Piwik\Plugin\Menu` instead. * The deprecated method `SettingsPiwik::rewriteTmpPathWithInstanceId()` has been removed. +* The following deprecated methods from the `Piwik\IP` class have been removed, use `Piwik\Network\IP` instead: + * `sanitizeIp()` + * `sanitizeIpRange()` + * `P2N()` + * `N2P()` + * `prettyPrint()` + * `isIPv4()` + * `long2ip()` + * `isIPv6()` + * `isMappedIPv4()` + * `getIPv4FromMappedIPv6()` + * `getIpsForRange()` + * `isIpInRange()` + * `getHostByAddr()` ## Piwik 2.11.0 diff --git a/core/Common.php b/core/Common.php index 7e3bcbc7af..1ed8d48935 100644 --- a/core/Common.php +++ b/core/Common.php @@ -638,26 +638,6 @@ class Common } /** - * Convert IP address (in network address format) to presentation format. - * This is a backward compatibility function for code that only expects - * IPv4 addresses (i.e., doesn't support IPv6). - * - * @see IP::N2P() - * - * This function does not support the long (or its string representation) - * returned by the built-in ip2long() function, from Piwik 1.3 and earlier. - * - * @deprecated 1.4 - * - * @param string $ip IP address in network address format - * @return string - */ - public static function long2ip($ip) - { - return IP::long2ip($ip); - } - - /** * JSON encode wrapper * - missing or broken in some php 5.x versions * diff --git a/core/IP.php b/core/IP.php index e952ddbe27..b6ec2c2d22 100644 --- a/core/IP.php +++ b/core/IP.php @@ -10,8 +10,6 @@ namespace Piwik; use Piwik\Network\IPUtils; -use Piwik\Network\IPv4; -use Piwik\Network\IPv6; /** * Contains IP address helper functions (for both IPv4 and IPv6). @@ -40,223 +38,6 @@ use Piwik\Network\IPv6; class IP { /** - * 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) - { - return IPUtils::sanitizeIp($ipString); - } - - /** - * Sanitize human-readable (user-supplied) IP address range. - * - * Accepts the following formats for $ipRange: - * - single IPv4 address, e.g., 127.0.0.1 - * - single IPv6 address, e.g., ::1/128 - * - IPv4 block using CIDR notation, e.g., 192.168.0.0/22 represents the IPv4 addresses from 192.168.0.0 to 192.168.3.255 - * - IPv6 block using CIDR notation, e.g., 2001:DB8::/48 represents the IPv6 addresses from 2001:DB8:0:0:0:0:0:0 to 2001:DB8:0:FFFF:FFFF:FFFF:FFFF:FFFF - * - wildcards, e.g., 192.168.0.* - * - * @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) - { - $result = IPUtils::sanitizeIpRange($ipRangeString); - - return $result === null ? false : $result; - } - - /** - * Converts an IP address in presentation format to network address format. - * - * @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) - { - return IPUtils::stringToBinaryIP($ipString); - } - - /** - * Convert network address format to presentation format. - * - * See also {@link prettyPrint()}. - * - * @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) - { - return IPUtils::binaryToStringIP($ip); - } - - /** - * Alias for {@link N2P()}. - * - * @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 IPUtils::binaryToStringIP($ip); - } - - /** - * Returns true if `$ip` is an IPv4, IPv4-compat, or IPv4-mapped address, false - * if otherwise. - * - * @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) - { - $ip = Network\IP::fromBinaryIP($ip); - - return $ip instanceof IPv4; - } - - /** - * Converts an IP address (in network address format) to presentation format. - * This is a backward compatibility function for code that only expects - * IPv4 addresses (i.e., doesn't support IPv6). - * - * This function does not support the long (or its string representation) - * returned by the built-in ip2long() function, from Piwik 1.3 and earlier. - * - * @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 IPUtils::binaryToStringIP($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 - ) { - // remap 128-bit IPv4-mapped and IPv4-compat addresses - return IPUtils::binaryToStringIP(substr($ip, 12)); - } - } - - return '0.0.0.0'; - } - - /** - * Returns true if $ip is an IPv6 address, false if otherwise. This function does - * a naive check. It assumes that whatever format $ip is in, it is well-formed. - * - * @param string $ip - * @return bool - * - * @deprecated Will be removed - * @see \Piwik\Network\IP - */ - public static function isIPv6($ip) - { - $ip = Network\IP::fromBinaryIP($ip); - - return $ip instanceof IPv6; - } - - /** - * Returns true if $ip is a IPv4 mapped address, false if otherwise. - * - * @param string $ip - * @return bool - * - * @deprecated Will be removed - * @see \Piwik\Network\IP - */ - public static function isMappedIPv4($ip) - { - $ip = Network\IP::fromStringIP($ip); - - if (! $ip instanceof IPv6) { - return false; - } - - return $ip->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. * @@ -333,22 +114,4 @@ class IP } 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; - } } diff --git a/core/Tracker/VisitExcluded.php b/core/Tracker/VisitExcluded.php index f3d8d0cab2..ae380cac42 100644 --- a/core/Tracker/VisitExcluded.php +++ b/core/Tracker/VisitExcluded.php @@ -11,7 +11,7 @@ namespace Piwik\Tracker; use Piwik\Common; use Piwik\Config; use Piwik\DeviceDetectorFactory; -use Piwik\IP; +use Piwik\Network\IP; use Piwik\Piwik; /** @@ -158,9 +158,10 @@ class VisitExcluded $deviceDetector = DeviceDetectorFactory::getInstance($this->userAgent); + $ip = IP::fromBinaryIP($this->ip); + return !$allowBots - && ($deviceDetector->isBot() - || IP::isIpInRange($this->ip, $this->getBotIpRanges())); + && ($deviceDetector->isBot() || $ip->isInRanges($this->getBotIpRanges())); } protected function getBotIpRanges() @@ -223,7 +224,7 @@ class VisitExcluded $websiteAttributes = Cache::getCacheWebsiteAttributes($this->idSite); if (!empty($websiteAttributes['excluded_ips'])) { - $ip = \Piwik\Network\IP::fromBinaryIP($this->ip); + $ip = IP::fromBinaryIP($this->ip); if ($ip->isInRanges($websiteAttributes['excluded_ips'])) { Common::printDebug('Visitor IP ' . $ip->toString() . ' is excluded from being tracked'); return true; diff --git a/plugins/Provider/Columns/Provider.php b/plugins/Provider/Columns/Provider.php index 24ab6cecd2..b7ca57e9c9 100644 --- a/plugins/Provider/Columns/Provider.php +++ b/plugins/Provider/Columns/Provider.php @@ -9,7 +9,7 @@ namespace Piwik\Plugins\Provider\Columns; use Piwik\Common; -use Piwik\IP; +use Piwik\Network\IP; use Piwik\Network\IPUtils; use Piwik\Piwik; use Piwik\Plugin\Dimension\VisitDimension; @@ -87,12 +87,17 @@ class Provider extends VisitDimension /** * Returns the hostname given the IP address string * - * @param string $ip IP Address + * @param string $ipStr IP Address * @return string hostname (or human-readable IP address) */ - private function getHost($ip) + private function getHost($ipStr) { - return trim(strtolower(@IP::getHostByAddr($ip))); + $ip = IP::fromStringIP($ipStr); + + $host = $ip->getHostname(); + $host = ($host === null ? $ipStr : $host); + + return trim(strtolower($host)); } public function getName() diff --git a/plugins/UserCountry/Columns/Country.php b/plugins/UserCountry/Columns/Country.php index db8026ba6b..458e92b068 100644 --- a/plugins/UserCountry/Columns/Country.php +++ b/plugins/UserCountry/Columns/Country.php @@ -12,7 +12,7 @@ use Piwik\Common; use Piwik\Config; use Piwik\Container\StaticContainer; use Piwik\Intl\Data\Provider\RegionDataProvider; -use Piwik\IP; +use Piwik\Network\IP; use Piwik\Piwik; use Piwik\Plugin\Manager; use Piwik\Plugins\Provider\Provider as ProviderProvider; @@ -107,12 +107,17 @@ class Country extends Base /** * Returns the hostname given the IP address string * - * @param string $ip IP Address + * @param string $ipStr IP Address * @return string hostname (or human-readable IP address) */ - private function getHost($ip) + private function getHost($ipStr) { - return trim(strtolower(@IP::getHostByAddr($ip))); + $ip = IP::fromStringIP($ipStr); + + $host = $ip->getHostname(); + $host = ($host === null ? $ipStr : $host); + + return trim(strtolower($host)); } /** diff --git a/tests/PHPUnit/Unit/IPTest.php b/tests/PHPUnit/Unit/IPTest.php index a78d7ddeca..1fa7ea4a8b 100644 --- a/tests/PHPUnit/Unit/IPTest.php +++ b/tests/PHPUnit/Unit/IPTest.php @@ -64,17 +64,6 @@ class IPTest extends \PHPUnit_Framework_TestCase } /** - * @dataProvider getLong2IPTestData - * @group Core - */ - public function testLong2ip($N, $P) - { - $this->assertEquals($P, IP::long2ip($N), bin2hex($N)); - // this is our compatibility function - $this->assertEquals($P, Common::long2ip($N), bin2hex($N)); - } - - /** * Dataprovider for ip from header tests */ public function getIpFromHeaderTestData() |