Import/Core_geoip.php
[Pman.Core] / Import / Core_geoip.php
index 9257dea..177cf8d 100644 (file)
@@ -9,7 +9,7 @@ class Pman_Core_Import_Core_geoip extends Pman_Roo
     static $cli_opts = array();
     
     var $id_mapping = array();
-    
+
     function getAuth()
     {
         $ff = HTML_FlexyFramework::get();
@@ -37,17 +37,15 @@ class Pman_Core_Import_Core_geoip extends Pman_Roo
             $this->jerr('GeoLite2-City-Locations.csv OR GeoLite2-City-Blocks.csv does not exists?!');
         }
         
-        print_r($this->id_mapping);exit;
-        
-        ini_set("auto_detect_line_endings", true);
-        
-        $this->insertLocation($location);
+//        $this->insertLocation($location);
         
         $this->insertBlock($block);
     }
     
     function insertLocation($csv)
     {
+        ini_set("auto_detect_line_endings", true);
+        
         $fh = fopen($csv, 'r');
         if (!$fh) {
             $this->jerr("invalid location file");
@@ -96,6 +94,60 @@ class Pman_Core_Import_Core_geoip extends Pman_Roo
         
     }
     
+    function insertBlock($csv)
+    {
+        ini_set("auto_detect_line_endings", true);
+        
+        $fh = fopen($csv, 'r');
+        if (!$fh) {
+            $this->jerr("invalid location file");
+        }
+        
+        $req = array(
+            'NETWORK_START_IP', 'NETWORK_MASK_LENGTH', 'GEONAME_ID',
+            'REGISTERED_COUNTRY_GEONAME_ID', 'REPRESENTED_COUNTRY_GEONAME_ID', 'POSTAL_CODE',
+            'LATITUDE', 'LONGITUDE', 'IS_ANONYMOUS_PROXY',
+            'IS_SATELLITE_PROVIDER'
+        );
+        
+        $cols = false;
+        
+        while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
+            if(!array_filter($n)){ // empty row
+                continue;
+            }
+            
+            if (!$cols) {
+                $cols = array();
+                foreach($n as $k) {
+                    $cols[] = strtoupper(trim($k));
+                }
+                
+                if (empty($cols)) {
+                    continue;
+                }
+                foreach($req as $r) {
+                    if (!in_array($r,$cols)) {
+                        $cols = false;
+                        break;
+                    }
+                }
+                continue;
+            }
+            
+            $row = array();
+            
+            foreach($cols as $i=>$k) {
+                $row[$k] = trim($n[$i]);
+            }
+            
+            $this->processBlock($row);
+        }
+        
+    }
+    
+    
+    
     function processLocation($row)
     {
         $continent = $this->processContinent($row['CONTINENT_CODE'], $row['CONTINENT_NAME']);
@@ -106,6 +158,9 @@ class Pman_Core_Import_Core_geoip extends Pman_Roo
         
         $city = $this->processCity($row['CITY_NAME'], $row['METRO_CODE'], $row['TIME_ZONE'], $country, $division);
         
+        if(!empty($city) && !empty($city->id)){
+            $this->id_mapping[$row['GEONAME_ID']] = $city->id;
+        }
     }
     
     function processContinent($code, $name)
@@ -192,6 +247,46 @@ class Pman_Core_Import_Core_geoip extends Pman_Roo
         
     }
     
+    function processBlock($row)
+    {
+        $network_mapping = DB_DataObject::factory('core_geoip_network_mapping');
+        
+        $start_ip = array_pop(explode(":", $row['NETWORK_START_IP']));
+        
+        $network_mapping->setFrom(array(
+            'start_ip' => $start_ip,
+            'mask_length' => pow(2, (128 - $row['NETWORK_MASK_LENGTH'])),
+            'city_id' => $this->id_mapping[$row['GEONAME_ID']]
+        ));
+        
+        if(!$network_mapping->find(true)){
+            $network_mapping->insert();
+        }
+        
+        $location = DB_DataObject::factory('core_geoip_location');
+        if(!$location->get('city_id', $network_mapping->city_id)){
+            $location->setFrom(array(
+                'latitude' => $row['LATITUDE'],
+                'longitude' => $row['LONGITUDE'],
+                'city_id' => $network_mapping->city_id
+            ));
+        }
+        
+        
+        if(!empty($row['POSTAL_CODE'])){
+            $city = DB_DataObject::factory('core_geoip_city');
+            if($city->get($network_mapping->city_id)){
+                return;
+            }
+
+            $oc = clone($city);
+            $city->postal_code = $row['POSTAL_CODE'];
+            
+            $city->update($oc);
+        }
+        
+        
+    }
     
     
     function log($str)