--- GeoIP-original.php	2010-11-28 12:20:58.000000000 +0100
+++ GeoIP.php	2010-10-27 10:51:05.000000000 +0200
@@ -6,12 +6,34 @@
  * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
  * @version $Id:  $
  */
-	
+
+// check if geoip is installed as a PHP Module
+define('USE_GEOIP_MODULE', function_exists("geoip_record_by_name") ? true : false);
+
+if( USE_GEOIP_MODULE )
+{
+	class geoiprecord
+	{
+		  var $country_code;
+		  var $country_code3;
+		  var $country_name;
+		  var $region;
+		  var $city;
+		  var $postal_code;
+		  var $latitude;
+		  var $longitude;
+		  var $area_code;
+		  var $dma_code;
+	}
+}
+
 /**
  * @package Piwik_GeoIP
  */
 class Piwik_GeoIP extends Piwik_Plugin
-{	
+{
+	const USE_GEOIP_MODULE = USE_GEOIP_MODULE;
+
 	public function getInformation()
 	{
 		$info = array(
@@ -30,7 +52,7 @@
 	
 	public function __destruct()
 	{
-		if(!is_null($this->geoIpDb))
+		if(!self::USE_GEOIP_MODULE && !is_null($this->geoIpDb))
 		{
 			geoip_close($this->geoIpDb);
 		}
@@ -276,7 +298,10 @@
 		$locationInfo['continent'] = Piwik_Common::getContinent($locationInfo['country_code']);
 		return $locationInfo;
 	}
-	
+
+
+
+
 	/**
 	 * Returns various location information (continent, country, city, latitude, longitude)
 	 * given the IP
@@ -287,7 +312,15 @@
 	 */
 	protected function getLocationInfo($ip)
 	{
-		$record = GeoIP_record_by_addr($this->geoIpDb, long2ip($ip));
+		if( self::USE_GEOIP_MODULE )
+		{
+			$record = $this->getGeoIpRecordFromModule($ip);
+		}
+		else
+		{
+			$record = GeoIP_record_by_addr($this->geoIpDb, long2ip($ip));
+		}
+
 		$locationInfo = self::$defaultLocationInfo;
 		if( empty($record) )
 		{
@@ -300,27 +333,50 @@
 		if(isset($record->longitude)) 		$locationInfo['longitude'] = round($record->longitude,4);
 		return $locationInfo;
 	}
+
+	
+	protected function getGeoIpRecordFromModule($hostname)
+	{
+		$record = new geoiprecord();
+		$data = geoip_record_by_name($hostname);
+		$record->area_code = $data['area_code'];
+		$record->city = $data['city'];
+		$record->country_code = $data['country_code'];
+		$record->country_code3 = $data['country_code3'];
+		$record->country_name = $data['country_name'];
+		$record->dma_code = $data['dma_code'];
+		$record->latitude = $data['latitude'];
+		$record->longitude = $data['longitude'];
+		$record->postal_code = $data['postal_code'];
+		$record->region = $data['region'];
+
+		return $record;
+	}
 	
 	protected function initGeoIpDatabase()
 	{
-		if($this->geoIpDb)
+		if( !self::USE_GEOIP_MODULE )
 		{
-			return;
-		}
-		require_once PIWIK_INCLUDE_PATH .'/plugins/GeoIP/libs/geoipcity.inc';
-		$geoIPDataFile = PIWIK_INCLUDE_PATH . '/plugins/GeoIP/libs/GeoLiteCity.dat';
-		// backward compatibility, old instructions asked to rename to GeoIP.dat
-		if(!is_file($geoIPDataFile))
-		{
-			$geoIPDataFile = PIWIK_INCLUDE_PATH . '/plugins/GeoIP/libs/GeoIP.dat';
-		}
-		if(!is_file($geoIPDataFile))
-		{
-			echo "ERROR: GeoIp file ".$geoIPDataFile." could not be found! 
-								Make sure you downloaded and extract the GeoIp city database as explained on http://dev.piwik.org/trac/ticket/45";
-			exit;
+			if($this->geoIpDb)
+			{
+				return;
+			}
+			require_once PIWIK_INCLUDE_PATH .'/plugins/GeoIP/libs/geoipcity.inc';
+			$geoIPDataFile = PIWIK_INCLUDE_PATH . '/plugins/GeoIP/libs/GeoLiteCity.dat';
+			// backward compatibility, old instructions asked to rename to GeoIP.dat
+			if(!is_file($geoIPDataFile))
+			{
+				$geoIPDataFile = PIWIK_INCLUDE_PATH . '/plugins/GeoIP/libs/GeoIP.dat';
+			}
+			if(!is_file($geoIPDataFile))
+			{
+				echo "ERROR: GeoIp file ".$geoIPDataFile." could not be found!
+									Make sure you downloaded and extract the GeoIp city database as explained on http://dev.piwik.org/trac/ticket/45";
+				exit;
+			}
+
+			$this->geoIpDb = geoip_open($geoIPDataFile, GEOIP_MEMORY_CACHE);
 		}
-		$this->geoIpDb = geoip_open($geoIPDataFile, GEOIP_MEMORY_CACHE);
 	}
 	
 	public function footerUserCountry($notification)
@@ -341,8 +397,12 @@
 		$start = 0;
 		$count = $row['cnt'];
 		$limit = 1000;
+
+		if( !self::USE_GEOIP_MODULE )
+		{
+			$this->initGeoIpDatabase();
+		}
 		
-		$this->initGeoIpDatabase();
 		Piwik::log("$count rows to process in ".Piwik_Common::prefixTable('log_visit')."...");
 		flush();
 		while( $start < $count )
