Ticket #45: UsePeclExtension.patch
| File UsePeclExtension.patch, 4.2 KB (added by manuelstofer, 19 months ago) |
|---|
-
.php
old new 6 6 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later 7 7 * @version $Id: $ 8 8 */ 9 9 10 // check if geoip is installed as a PHP Module 11 define('USE_GEOIP_MODULE', function_exists("geoip_record_by_name") ? true : false); 12 13 if( USE_GEOIP_MODULE ) 14 { 15 class geoiprecord 16 { 17 var $country_code; 18 var $country_code3; 19 var $country_name; 20 var $region; 21 var $city; 22 var $postal_code; 23 var $latitude; 24 var $longitude; 25 var $area_code; 26 var $dma_code; 27 } 28 } 29 10 30 /** 11 31 * @package Piwik_GeoIP 12 32 */ 13 33 class Piwik_GeoIP extends Piwik_Plugin 14 { 34 { 35 const USE_GEOIP_MODULE = USE_GEOIP_MODULE; 36 15 37 public function getInformation() 16 38 { 17 39 $info = array( … … 30 52 31 53 public function __destruct() 32 54 { 33 if(! is_null($this->geoIpDb))55 if(!self::USE_GEOIP_MODULE && !is_null($this->geoIpDb)) 34 56 { 35 57 geoip_close($this->geoIpDb); 36 58 } … … 276 298 $locationInfo['continent'] = Piwik_Common::getContinent($locationInfo['country_code']); 277 299 return $locationInfo; 278 300 } 279 301 302 303 304 280 305 /** 281 306 * Returns various location information (continent, country, city, latitude, longitude) 282 307 * given the IP … … 287 312 */ 288 313 protected function getLocationInfo($ip) 289 314 { 290 $record = GeoIP_record_by_addr($this->geoIpDb, long2ip($ip)); 315 if( self::USE_GEOIP_MODULE ) 316 { 317 $record = $this->getGeoIpRecordFromModule($ip); 318 } 319 else 320 { 321 $record = GeoIP_record_by_addr($this->geoIpDb, long2ip($ip)); 322 } 323 291 324 $locationInfo = self::$defaultLocationInfo; 292 325 if( empty($record) ) 293 326 { … … 300 333 if(isset($record->longitude)) $locationInfo['longitude'] = round($record->longitude,4); 301 334 return $locationInfo; 302 335 } 336 337 338 protected function getGeoIpRecordFromModule($hostname) 339 { 340 $record = new geoiprecord(); 341 $data = geoip_record_by_name($hostname); 342 $record->area_code = $data['area_code']; 343 $record->city = $data['city']; 344 $record->country_code = $data['country_code']; 345 $record->country_code3 = $data['country_code3']; 346 $record->country_name = $data['country_name']; 347 $record->dma_code = $data['dma_code']; 348 $record->latitude = $data['latitude']; 349 $record->longitude = $data['longitude']; 350 $record->postal_code = $data['postal_code']; 351 $record->region = $data['region']; 352 353 return $record; 354 } 303 355 304 356 protected function initGeoIpDatabase() 305 357 { 306 if( $this->geoIpDb)358 if( !self::USE_GEOIP_MODULE ) 307 359 { 308 return; 309 } 310 require_once PIWIK_INCLUDE_PATH .'/plugins/GeoIP/libs/geoipcity.inc'; 311 $geoIPDataFile = PIWIK_INCLUDE_PATH . '/plugins/GeoIP/libs/GeoLiteCity.dat'; 312 // backward compatibility, old instructions asked to rename to GeoIP.dat 313 if(!is_file($geoIPDataFile)) 314 { 315 $geoIPDataFile = PIWIK_INCLUDE_PATH . '/plugins/GeoIP/libs/GeoIP.dat'; 316 } 317 if(!is_file($geoIPDataFile)) 318 { 319 echo "ERROR: GeoIp file ".$geoIPDataFile." could not be found! 320 Make sure you downloaded and extract the GeoIp city database as explained on http://dev.piwik.org/trac/ticket/45"; 321 exit; 360 if($this->geoIpDb) 361 { 362 return; 363 } 364 require_once PIWIK_INCLUDE_PATH .'/plugins/GeoIP/libs/geoipcity.inc'; 365 $geoIPDataFile = PIWIK_INCLUDE_PATH . '/plugins/GeoIP/libs/GeoLiteCity.dat'; 366 // backward compatibility, old instructions asked to rename to GeoIP.dat 367 if(!is_file($geoIPDataFile)) 368 { 369 $geoIPDataFile = PIWIK_INCLUDE_PATH . '/plugins/GeoIP/libs/GeoIP.dat'; 370 } 371 if(!is_file($geoIPDataFile)) 372 { 373 echo "ERROR: GeoIp file ".$geoIPDataFile." could not be found! 374 Make sure you downloaded and extract the GeoIp city database as explained on http://dev.piwik.org/trac/ticket/45"; 375 exit; 376 } 377 378 $this->geoIpDb = geoip_open($geoIPDataFile, GEOIP_MEMORY_CACHE); 322 379 } 323 $this->geoIpDb = geoip_open($geoIPDataFile, GEOIP_MEMORY_CACHE);324 380 } 325 381 326 382 public function footerUserCountry($notification) … … 341 397 $start = 0; 342 398 $count = $row['cnt']; 343 399 $limit = 1000; 400 401 if( !self::USE_GEOIP_MODULE ) 402 { 403 $this->initGeoIpDatabase(); 404 } 344 405 345 $this->initGeoIpDatabase();346 406 Piwik::log("$count rows to process in ".Piwik_Common::prefixTable('log_visit')."..."); 347 407 flush(); 348 408 while( $start < $count )
