From de85e310edad4172fee20dd5cc20dc680258bbbc Mon Sep 17 00:00:00 2001 From: benakamoorthi Date: Tue, 20 Nov 2012 07:56:56 +0000 Subject: Fixes #3534, add UserCountry.getLocationFromIP method as proxy for GeoIP functionality. git-svn-id: http://dev.piwik.org/svn/trunk@7494 59fd770c-687e-43c8-a1e3-f5a4ff64c105 --- plugins/UserCountry/API.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'plugins/UserCountry') diff --git a/plugins/UserCountry/API.php b/plugins/UserCountry/API.php index b684ebdb41..ab7016eee6 100644 --- a/plugins/UserCountry/API.php +++ b/plugins/UserCountry/API.php @@ -156,6 +156,40 @@ class Piwik_UserCountry_API return $dataTable; } + /** + * Uses a location provider to find/guess the location of an IP address. + * + * See Piwik_UserCountry_LocationProvider::getLocation to see the details + * of the result of this function. + * + * @param string $ip The IP address. + * @param string|false $provider The ID of the provider to use or false to use the + * currently configured one. + */ + public function getLocationFromIP( $ip, $provider = false ) + { + Piwik::checkUserHasSomeViewAccess(); + + if ($provider === false) + { + $provider = Piwik_UserCountry_LocationProvider::getCurrentProviderId(); + } + + $oProvider = Piwik_UserCountry_LocationProvider::getProviderById($provider); + if ($oProvider === false) + { + throw new Exception("Cannot find the '$provider' provider. It is either an invalid provider " + . "ID or the ID of a provider that is not working."); + } + + $location = $oProvider->getLocation(array('ip' => $ip)); + if (empty($location)) + { + throw new Exception("Could not geolocate '$ip'!"); + } + return $location; + } + protected function getDataTable($name, $idSite, $period, $date, $segment) { Piwik::checkUserHasViewAccess( $idSite ); -- cgit v1.2.3