From 92d165a6c3fc825f08e38ad846bf57ac5814f1ca Mon Sep 17 00:00:00 2001 From: benakamoorthi Date: Tue, 23 Oct 2012 04:33:19 +0000 Subject: Fixes #3463, allow serverbased geoip provider to work w/ anonymized IPs & add description about this issue in provider description. git-svn-id: http://dev.piwik.org/svn/trunk@7285 59fd770c-687e-43c8-a1e3-f5a4ff64c105 --- .../LocationProvider/GeoIp/ServerBased.php | 44 +++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'plugins/UserCountry') diff --git a/plugins/UserCountry/LocationProvider/GeoIp/ServerBased.php b/plugins/UserCountry/LocationProvider/GeoIp/ServerBased.php index 105d19e077..b5c92ce110 100755 --- a/plugins/UserCountry/LocationProvider/GeoIp/ServerBased.php +++ b/plugins/UserCountry/LocationProvider/GeoIp/ServerBased.php @@ -64,7 +64,7 @@ class Piwik_UserCountry_LocationProvider_GeoIp_ServerBased extends Piwik_UserCou // geoip modules that are built into servers can't use a forced IP. in this case we try // to fallback to another version. $myIP = Piwik_IP::getIpFromHeader(); - if ($info['ip'] != $myIP + if (!self::isSameOrAnonymizedIp($info['ip'], $myIP) && (!isset($info['disable_fallbacks']) || !$info['disable_fallbacks'])) { @@ -196,6 +196,8 @@ class Piwik_UserCountry_LocationProvider_GeoIp_ServerBased extends Piwik_UserCou $title = sprintf(self::TITLE, $serverDesc); $desc = Piwik_Translate('UserCountry_GeoIpLocationProviderDesc_ServerBased1', array('', '')) + . '

' + . ''.Piwik_Translate('UserCountry_GeoIpLocationProviderDesc_ServerBasedAnonWarn').'' . '

' . Piwik_Translate('UserCountry_GeoIpLocationProviderDesc_ServerBased2', array('', '', '', '')); @@ -213,4 +215,44 @@ class Piwik_UserCountry_LocationProvider_GeoIp_ServerBased extends Piwik_UserCou 'order' => 4, 'install_docs' => $installDocs); } + + /** + * 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 + */ + 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; + } } -- cgit v1.2.3