Ticket #45: UsePeclExtension.patch

File UsePeclExtension.patch, 4.2 KB (added by manuelstofer, 19 months ago)

Makes the plugin work when GEO ip is installed as PECL Extension

  • .php

    old new  
    66 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later 
    77 * @version $Id:  $ 
    88 */ 
    9          
     9 
     10// check if geoip is installed as a PHP Module 
     11define('USE_GEOIP_MODULE', function_exists("geoip_record_by_name") ? true : false); 
     12 
     13if( 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 
    1030/** 
    1131 * @package Piwik_GeoIP 
    1232 */ 
    1333class Piwik_GeoIP extends Piwik_Plugin 
    14 {        
     34{ 
     35        const USE_GEOIP_MODULE = USE_GEOIP_MODULE; 
     36 
    1537        public function getInformation() 
    1638        { 
    1739                $info = array( 
     
    3052         
    3153        public function __destruct() 
    3254        { 
    33                 if(!is_null($this->geoIpDb)) 
     55                if(!self::USE_GEOIP_MODULE && !is_null($this->geoIpDb)) 
    3456                { 
    3557                        geoip_close($this->geoIpDb); 
    3658                } 
     
    276298                $locationInfo['continent'] = Piwik_Common::getContinent($locationInfo['country_code']); 
    277299                return $locationInfo; 
    278300        } 
    279          
     301 
     302 
     303 
     304 
    280305        /** 
    281306         * Returns various location information (continent, country, city, latitude, longitude) 
    282307         * given the IP 
     
    287312         */ 
    288313        protected function getLocationInfo($ip) 
    289314        { 
    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 
    291324                $locationInfo = self::$defaultLocationInfo; 
    292325                if( empty($record) ) 
    293326                { 
     
    300333                if(isset($record->longitude))           $locationInfo['longitude'] = round($record->longitude,4); 
    301334                return $locationInfo; 
    302335        } 
     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        } 
    303355         
    304356        protected function initGeoIpDatabase() 
    305357        { 
    306                 if($this->geoIpDb) 
     358                if( !self::USE_GEOIP_MODULE ) 
    307359                { 
    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); 
    322379                } 
    323                 $this->geoIpDb = geoip_open($geoIPDataFile, GEOIP_MEMORY_CACHE); 
    324380        } 
    325381         
    326382        public function footerUserCountry($notification) 
     
    341397                $start = 0; 
    342398                $count = $row['cnt']; 
    343399                $limit = 1000; 
     400 
     401                if( !self::USE_GEOIP_MODULE ) 
     402                { 
     403                        $this->initGeoIpDatabase(); 
     404                } 
    344405                 
    345                 $this->initGeoIpDatabase(); 
    346406                Piwik::log("$count rows to process in ".Piwik_Common::prefixTable('log_visit')."..."); 
    347407                flush(); 
    348408                while( $start < $count )