From c376b45e534b959effe9c4ab68b77fd26b65bf5d Mon Sep 17 00:00:00 2001 From: benakamoorthi Date: Wed, 14 Nov 2012 07:43:58 +0000 Subject: Fixes #3458, fixes #3515, improved geoip diagnostics/help & allowed IPv4-mapped IP addresses to be geo-located. git-svn-id: http://dev.piwik.org/svn/trunk@7469 59fd770c-687e-43c8-a1e3-f5a4ff64c105 --- plugins/UserCountry/Controller.php | 2 + plugins/UserCountry/LocationProvider.php | 46 +++++++++++++---- .../UserCountry/LocationProvider/GeoIp/Pecl.php | 45 ++++++++++++++++- plugins/UserCountry/LocationProvider/GeoIp/Php.php | 30 ++++++++++- .../LocationProvider/GeoIp/ServerBased.php | 59 ++++++++++++++++++++-- plugins/UserCountry/templates/adminIndex.tpl | 12 ++++- 6 files changed, 176 insertions(+), 18 deletions(-) (limited to 'plugins/UserCountry') diff --git a/plugins/UserCountry/Controller.php b/plugins/UserCountry/Controller.php index 8d477d7f01..e7ed5c458a 100644 --- a/plugins/UserCountry/Controller.php +++ b/plugins/UserCountry/Controller.php @@ -229,6 +229,8 @@ class Piwik_UserCountry_Controller extends Piwik_Controller $footerMessage = Piwik_Translate('UserCountry_NoDataForGeoIPReport', array( '', '', + '', + '', '', '' )); diff --git a/plugins/UserCountry/LocationProvider.php b/plugins/UserCountry/LocationProvider.php index 744bc24fe1..f2cc40cd2d 100755 --- a/plugins/UserCountry/LocationProvider.php +++ b/plugins/UserCountry/LocationProvider.php @@ -213,9 +213,14 @@ abstract class Piwik_UserCountry_LocationProvider $location = false; $statusMessage = false; - if (!$provider->isAvailable()) + $availableOrMessage = $provider->isAvailable(); + if ($availableOrMessage !== true) { $status = self::NOT_INSTALLED; + if (is_string($availableOrMessage)) + { + $statusMessage = $availableOrMessage; + } } else { @@ -446,18 +451,41 @@ abstract class Piwik_UserCountry_LocationProvider { $lines[] = ''; - if (!empty($locationInfo[self::ORG_KEY])) - { - $lines[] = "Org: ".$locationInfo[self::ORG_KEY]; - } + $unknown = Piwik_Translate('General_Unknown'); - if (!empty($locationInfo[self::ISP_KEY])) - { - $lines[] = "ISP: ".$locationInfo[self::ISP_KEY]; - } + $org = !empty($locationInfo[self::ORG_KEY]) ? $locationInfo[self::ORG_KEY] : $unknown; + $lines[] = "Org: $org"; + + $isp = !empty($locationInfo[self::ISP_KEY]) ? $locationInfo[self::ISP_KEY] : $unknown; + $lines[] = "ISP: $isp"; } return implode($newline, $lines); } + + /** + * Returns an IP address from an array that was passed into getLocation. This + * will return an IPv4 address or false if the address is IPv6 (IPv6 is not + * supported yet). + * + * @param array $ip Must have 'ip' key. + * @return string|bool + */ + protected function getIpFromInfo( $info ) + { + $ip = $info['ip']; + if (Piwik_IP::isMappedIPv4($ip)) + { + return Piwik_IP::getIPv4FromMappedIPv6($ip); + } + else if (Piwik_IP::isIPv6($ip)) // IPv6 is not supported (yet) + { + return false; + } + else + { + return $ip; + } + } } diff --git a/plugins/UserCountry/LocationProvider/GeoIp/Pecl.php b/plugins/UserCountry/LocationProvider/GeoIp/Pecl.php index 06789ce8f1..0ddf8e2c1d 100755 --- a/plugins/UserCountry/LocationProvider/GeoIp/Pecl.php +++ b/plugins/UserCountry/LocationProvider/GeoIp/Pecl.php @@ -47,7 +47,7 @@ class Piwik_UserCountry_LocationProvider_GeoIp_Pecl extends Piwik_UserCountry_Lo */ public function getLocation( $info ) { - $ip = $info['ip']; + $ip = $this->getIpFromInfo($info); $result = array(); @@ -237,10 +237,53 @@ class Piwik_UserCountry_LocationProvider_GeoIp_Pecl extends Piwik_UserCountry_Lo . Piwik_Translate('UserCountry_HowToInstallGeoIpPecl') . '' . ''; + + $extraMessage = false; + if ($this->isAvailable()) + { + $peclDir = ini_get('geoip.custom_directory'); + if ($peclDir === false) + { + $extraMessage = Piwik_Translate('UserCountry_GeoIPPeclCustomDirNotSet', "'geoip.custom_directory'"); + } + else + { + $extraMessage = 'The \'geoip.custom_directory\' PHP ini option is set to \''.$peclDir.'\'.'; + } + + $availableDatabaseTypes = array(); + if (self::isCityDatabaseAvailable()) + { + $availableDatabaseTypes[] = Piwik_Translate('UserCountry_City'); + } + if (self::isRegionDatabaseAvailable()) + { + $availableDatabaseTypes[] = Piwik_Translate('UserCountry_Region'); + } + if (self::isCountryDatabaseAvailable()) + { + $availableDatabaseTypes[] = Piwik_Translate('UserCountry_Country'); + } + if (self::isISPDatabaseAvailable()) + { + $availableDatabaseTypes[] = 'ISP'; + } + if (self::isOrgDatabaseAvailable()) + { + $availableDatabaseTypes[] = Piwik_Translate('UserCountry_Organization'); + } + + $extraMessage .= '

'.Piwik_Translate('UserCountry_GeoIPImplHasAccessTo').': ' + . implode(', ', $availableDatabaseTypes).'.'; + + $extraMessage = ''.Piwik_Translate('General_Note').': '.$extraMessage; + } + return array('id' => self::ID, 'title' => self::TITLE, 'description' => $desc, 'install_docs' => $installDocs, + 'extra_message' => $extraMessage, 'order' => 3); } diff --git a/plugins/UserCountry/LocationProvider/GeoIp/Php.php b/plugins/UserCountry/LocationProvider/GeoIp/Php.php index 5696029ad4..63c9e1eb1a 100755 --- a/plugins/UserCountry/LocationProvider/GeoIp/Php.php +++ b/plugins/UserCountry/LocationProvider/GeoIp/Php.php @@ -57,7 +57,7 @@ class Piwik_UserCountry_LocationProvider_GeoIp_Php extends Piwik_UserCountry_Loc */ public function getLocation( $info ) { - $ip = $info['ip']; + $ip = $this->getIpFromInfo($info); $result = array(); @@ -253,10 +253,38 @@ class Piwik_UserCountry_LocationProvider_GeoIp_Php extends Piwik_UserCountry_Loc $installDocs = '' . Piwik_Translate('UserCountry_HowToInstallGeoIPDatabases') . ''; + + $availableDatabaseTypes = array(); + if (self::getPathToGeoIpDatabase(array('GeoIPCity.dat', 'GeoLiteCity.dat')) !== false) + { + $availableDatabaseTypes[] = Piwik_Translate('UserCountry_City'); + } + if (self::getPathToGeoIpDatabase(array('GeoIPRegion.dat')) !== false) + { + $availableDatabaseTypes[] = Piwik_Translate('UserCountry_Region'); + } + if (self::getPathToGeoIpDatabase(array('GeoIPCountry.dat')) !== false) + { + $availableDatabaseTypes[] = Piwik_Translate('UserCountry_Country'); + } + if (self::getPathToGeoIpDatabase(array('GeoIPISP.dat')) !== false) + { + $availableDatabaseTypes[] = 'ISP'; + } + if (self::getPathToGeoIpDatabase(array('GeoIPOrg.dat')) !== false) + { + $availableDatabaseTypes[] = Piwik_Translate('UserCountry_Organization'); + } + + $extraMessage = ''.Piwik_Translate('General_Note').': ' + . Piwik_Translate('UserCountry_GeoIPImplHasAccessTo').': ' + . implode(', ', $availableDatabaseTypes).'.'; + return array('id' => self::ID, 'title' => self::TITLE, 'description' => $desc, 'install_docs' => $installDocs, + 'extra_message' => $extraMessage, 'order' => 2); } diff --git a/plugins/UserCountry/LocationProvider/GeoIp/ServerBased.php b/plugins/UserCountry/LocationProvider/GeoIp/ServerBased.php index b5c92ce110..a5f580530c 100755 --- a/plugins/UserCountry/LocationProvider/GeoIp/ServerBased.php +++ b/plugins/UserCountry/LocationProvider/GeoIp/ServerBased.php @@ -61,10 +61,12 @@ class Piwik_UserCountry_LocationProvider_GeoIp_ServerBased extends Piwik_UserCou */ public function getLocation( $info ) { + $ip = $this->getIpFromInfo($info); + // 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 (!self::isSameOrAnonymizedIp($info['ip'], $myIP) + if (!self::isSameOrAnonymizedIp($ip, $myIP) && (!isset($info['disable_fallbacks']) || !$info['disable_fallbacks'])) { @@ -136,7 +138,7 @@ class Piwik_UserCountry_LocationProvider_GeoIp_ServerBased extends Piwik_UserCou * There's a special check for the Apache module, but we can't check specifically * for anything else. * - * @return bool + * @return bool|string */ public function isAvailable() { @@ -152,8 +154,30 @@ class Piwik_UserCountry_LocationProvider_GeoIp_ServerBased extends Piwik_UserCou } } - return !empty($_SERVER[self::TEST_SERVER_VAR]) - || !empty($_SERVER[self::TEST_SERVER_VAR_ALT]); + $available = !empty($_SERVER[self::TEST_SERVER_VAR]) + || !empty($_SERVER[self::TEST_SERVER_VAR_ALT]); + + if ($available) + { + return true; + } + else // if not available return message w/ extra info + { + if (!function_exists('apache_get_modules')) + { + return Piwik_Translate('General_Note').': '.Piwik_Translate('UserCountry_AssumingNonApache'); + } + + $message = "".Piwik_Translate('General_Note').': ' + . Piwik_Translate('UserCountry_FoundApacheModules') + . ":

\n"; + return $message; + } } /** @@ -209,11 +233,36 @@ class Piwik_UserCountry_LocationProvider_GeoIp_ServerBased extends Piwik_UserCou . Piwik_Translate('UserCountry_HowToInstallNginxModule') . ''; + $geoipServerVars = array(); + foreach ($_SERVER as $key => $value) + { + if (strpos($key, 'GEOIP') === 0) + { + $geoipServerVars[] = $key; + } + } + + if (empty($geoipServerVars)) + { + $extraMessage = ''.Piwik_Translate('UserCountry_GeoIPNoServerVars', '$_SERVER').''; + } + else + { + $extraMessage = ''.Piwik_Translate('UserCountry_GeoIPServerVarsFound', '$_SERVER') + .":

\n'; + } + return array('id' => self::ID, 'title' => $title, 'description' => $desc, 'order' => 4, - 'install_docs' => $installDocs); + 'install_docs' => $installDocs, + 'extra_message' => $extraMessage); } /** diff --git a/plugins/UserCountry/templates/adminIndex.tpl b/plugins/UserCountry/templates/adminIndex.tpl index 4362dae5c7..a4ddcfa9bc 100755 --- a/plugins/UserCountry/templates/adminIndex.tpl +++ b/plugins/UserCountry/templates/adminIndex.tpl @@ -73,12 +73,20 @@ {/if} {/capture} {$currentLocation|inlineHelp} - {elseif $provider.status eq 2} + {/if} + {if isset($provider.statusMessage) && $provider.statusMessage} {capture assign=brokenReason} - {'General_Error'|translate}: {$provider.statusMessage} + {if $provider.status eq 2}{'General_Error'|translate}: {/if}{$provider.statusMessage} {/capture} {$brokenReason|inlineHelp} {/if} + {if isset($provider.extra_message) && $provider.extra_message} + {capture assign=extraMessage} + {$provider.extra_message} + {/capture} +
+ {$extraMessage|inlineHelp} + {/if} {/foreach} -- cgit v1.2.3