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