Import/Core_geoip.php
[Pman.Core] / Import / Core_geoip.php
1 <?php
2
3 require_once 'Pman/Roo.php';
4
5 class Pman_Core_Import_Core_geoip extends Pman_Roo
6 {
7     static $cli_desc = "Insert the geoip database";
8     
9     static $cli_opts = array();
10     
11     var $id_mapping = array();
12
13     function getAuth()
14     {
15         $ff = HTML_FlexyFramework::get();
16         if (!$ff->cli) {
17             die("access denied");
18         }
19         HTML_FlexyFramework::ensureSingle(__FILE__, $this);
20         return true;
21     }
22     
23     function post()
24     {
25         $this->get();
26     }
27     
28     function get()
29     {
30         
31         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
32         
33         $location = '/tmp/GeoLite2-City-Locations.csv';
34         $block = '/tmp/GeoLite2-City-Blocks.csv';
35         
36         if(!file_exists($location) || !file_exists($block)){
37             $this->jerr('GeoLite2-City-Locations.csv OR GeoLite2-City-Blocks.csv does not exists?!');
38         }
39         
40         $this->log("Insert location data start");
41         
42         $this->insertLocation($location);
43         
44         $this->log("Insert Block data start");
45         
46         $this->insertBlock($block);
47         
48         $this->jok("DONE");
49     }
50     
51     function insertLocation($csv)
52     {
53         ini_set("auto_detect_line_endings", true);
54         
55         $fh = fopen($csv, 'r');
56         
57         if (!$fh) {
58             $this->jerr("invalid location file");
59         }
60         
61         $linecount = count(file($csv));
62         
63         print_r($linecount);exit;
64         
65         $req = array(
66             'GEONAME_ID', 'CONTINENT_CODE', 'CONTINENT_NAME',
67             'COUNTRY_ISO_CODE', 'COUNTRY_NAME', 'SUBDIVISION_ISO_CODE',
68             'SUBDIVISION_NAME', 'CITY_NAME', 'METRO_CODE',
69             'TIME_ZONE'
70         );
71         
72         $cols = false;
73         
74         while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
75             if(!array_filter($n)){ // empty row
76                 continue;
77             }
78             
79             if (!$cols) {
80                 $cols = array();
81                 foreach($n as $k) {
82                     $cols[] = strtoupper(trim($k));
83                 }
84                 
85                 if (empty($cols)) {
86                     continue;
87                 }
88                 foreach($req as $r) {
89                     if (!in_array($r,$cols)) {
90                         $cols = false;
91                         break;
92                     }
93                 }
94                 continue;
95             }
96             
97             $row = array();
98             
99             foreach($cols as $i=>$k) {
100                 $row[$k] = trim($n[$i]);
101             }
102             
103             $this->processLocation($row);
104         }
105         
106     }
107     
108     function insertBlock($csv)
109     {
110         ini_set("auto_detect_line_endings", true);
111         
112         $fh = fopen($csv, 'r');
113         if (!$fh) {
114             $this->jerr("invalid location file");
115         }
116         
117         $req = array(
118             'NETWORK_START_IP', 'NETWORK_MASK_LENGTH', 'GEONAME_ID',
119             'REGISTERED_COUNTRY_GEONAME_ID', 'REPRESENTED_COUNTRY_GEONAME_ID', 'POSTAL_CODE',
120             'LATITUDE', 'LONGITUDE', 'IS_ANONYMOUS_PROXY',
121             'IS_SATELLITE_PROVIDER'
122         );
123         
124         $cols = false;
125         
126         while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
127             if(!array_filter($n)){ // empty row
128                 continue;
129             }
130             
131             if (!$cols) {
132                 $cols = array();
133                 foreach($n as $k) {
134                     $cols[] = strtoupper(trim($k));
135                 }
136                 
137                 if (empty($cols)) {
138                     continue;
139                 }
140                 foreach($req as $r) {
141                     if (!in_array($r,$cols)) {
142                         $cols = false;
143                         break;
144                     }
145                 }
146                 continue;
147             }
148             
149             $row = array();
150             
151             foreach($cols as $i=>$k) {
152                 $row[$k] = trim($n[$i]);
153             }
154             
155             $this->processBlock($row);
156         }
157         
158     }
159     
160     
161     
162     function processLocation($row)
163     {   
164         $continent = $this->processContinent($row['CONTINENT_CODE'], $row['CONTINENT_NAME']);
165         
166         $country = $this->processCountry($row['COUNTRY_ISO_CODE'], $row['COUNTRY_NAME'], $continent);
167         
168         $division = $this->processDivision($row['SUBDIVISION_ISO_CODE'], $row['SUBDIVISION_NAME']);
169         
170         $city = $this->processCity($row['CITY_NAME'], $row['METRO_CODE'], $row['TIME_ZONE'], $country, $division);
171         
172         if(!empty($city) && !empty($city->id)){
173             $this->id_mapping[$row['GEONAME_ID']] = $city->id;
174         }
175     }
176     
177     function processContinent($code, $name)
178     {
179         if(empty($code)){
180             return false;
181         }
182         
183         $continent = DB_DataObject::factory('core_geoip_continent');
184         if(!$continent->get('code', $code)){
185             $continent->setFrom(array(
186                 'code' => $code,
187                 'name' => (!empty($name)) ? $name : $code
188             ));
189
190             $continent->insert();
191         }
192         
193         return $continent;
194     }
195     
196     function processCountry($code, $name, $continent)
197     {
198         if(empty($code)){
199             return false;
200         }
201         
202         $country = DB_DataObject::factory('core_geoip_country');
203         if(!$country->get('code', $code)){
204             $country->setFrom(array(
205                 'code' => $code,
206                 'name' => (!empty($name)) ? $name : $code,
207                 'continent_id' => (!empty($continent) && !empty($continent->id)) ? $continent->id : 0
208             ));
209
210             $country->insert();
211         }
212         
213         return $country;
214     }
215     
216     function processDivision($code, $name)
217     {
218         if(empty($code)){
219             return false;
220         }
221         
222         $division = DB_DataObject::factory('core_geoip_division');
223         if(!$division->get('code', $code)){
224             $division->setFrom(array(
225                 'code' => $code,
226                 'name' => (!empty($name)) ? $name : $code
227             ));
228
229             $division->insert();
230         }
231         
232         return $division;
233     }
234     
235     function processCity($name, $metro_code, $time_zone, $country, $division)
236     {
237         if(empty($name)){
238             return false;
239         }
240         
241         $city = DB_DataObject::factory('core_geoip_city');
242         
243         if($city->get('name', $name)){
244             return $city;
245         }
246         
247         $city->setFrom(array(
248             'name' => $name,
249             'metro_code' => $metro_code,
250             'time_zone' => $time_zone,
251             'country_id' => (!empty($country) && !empty($country->id)) ? $country->id : 0,
252             'division_id' => (!empty($division) && !empty($division->id)) ? $division->id : 0
253         ));
254         
255         $city->insert();
256         
257         return $city;
258         
259     }
260     
261     function processBlock($row)
262     {
263         if(empty($this->id_mapping[$row['GEONAME_ID']])){
264             $this->log("Missing mapping for {$row['GEONAME_ID']}");
265             $this->log("IP : {$row['NETWORK_START_IP']}");
266             return;
267         }
268         
269         $network_mapping = DB_DataObject::factory('core_geoip_network_mapping');
270         
271         $start_ip = array_pop(explode(":", $row['NETWORK_START_IP']));
272         
273         $network_mapping->setFrom(array(
274             'start_ip' => $start_ip,
275             'mask_length' => pow(2, (128 - $row['NETWORK_MASK_LENGTH'])),
276             'city_id' => $this->id_mapping[$row['GEONAME_ID']]
277         ));
278         
279         if(!$network_mapping->find(true)){
280             $network_mapping->insert();
281         }
282         
283         $location = DB_DataObject::factory('core_geoip_location');
284         if(!$location->get('city_id', $network_mapping->city_id)){
285             $location->setFrom(array(
286                 'latitude' => $row['LATITUDE'],
287                 'longitude' => $row['LONGITUDE'],
288                 'city_id' => $network_mapping->city_id
289             ));
290         }
291         
292         
293         if(!empty($row['POSTAL_CODE'])){
294             $city = DB_DataObject::factory('core_geoip_city');
295             if($city->get($network_mapping->city_id)){
296                 return;
297             }
298
299             $oc = clone($city);
300             $city->postal_code = $row['POSTAL_CODE'];
301             
302             $city->update($oc);
303         }
304         
305     }
306     
307     function log($str)
308     {
309         echo "$str \n";
310     }
311 }