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->insertLocation($location);
41         
42         $this->insertBlock($block);
43     }
44     
45     function insertLocation($csv)
46     {
47         ini_set("auto_detect_line_endings", true);
48         
49         $fh = fopen($csv, 'r');
50         if (!$fh) {
51             $this->jerr("invalid location file");
52         }
53         
54         $req = array(
55             'GEONAME_ID', 'CONTINENT_CODE', 'CONTINENT_NAME',
56             'COUNTRY_ISO_CODE', 'COUNTRY_NAME', 'SUBDIVISION_ISO_CODE',
57             'SUBDIVISION_NAME', 'CITY_NAME', 'METRO_CODE',
58             'TIME_ZONE'
59         );
60         
61         $cols = false;
62         
63         while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
64             if(!array_filter($n)){ // empty row
65                 continue;
66             }
67             
68             if (!$cols) {
69                 $cols = array();
70                 foreach($n as $k) {
71                     $cols[] = strtoupper(trim($k));
72                 }
73                 
74                 if (empty($cols)) {
75                     continue;
76                 }
77                 foreach($req as $r) {
78                     if (!in_array($r,$cols)) {
79                         $cols = false;
80                         break;
81                     }
82                 }
83                 continue;
84             }
85             
86             $row = array();
87             
88             foreach($cols as $i=>$k) {
89                 $row[$k] = trim($n[$i]);
90             }
91             
92             $this->processLocation($row);
93         }
94         
95     }
96     
97     function insertBlock($csv)
98     {
99         ini_set("auto_detect_line_endings", true);
100         
101         $fh = fopen($csv, 'r');
102         if (!$fh) {
103             $this->jerr("invalid location file");
104         }
105         
106         $req = array(
107             'NETWORK_START_IP', 'NETWORK_MASK_LENGTH', 'GEONAME_ID',
108             'REGISTERED_COUNTRY_GEONAME_ID', 'REPRESENTED_COUNTRY_GEONAME_ID', 'POSTAL_CODE',
109             'LATITUDE', 'LONGITUDE', 'IS_ANONYMOUS_PROXY',
110             'IS_SATELLITE_PROVIDER'
111         );
112         
113         $cols = false;
114         
115         while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
116             if(!array_filter($n)){ // empty row
117                 continue;
118             }
119             
120             if (!$cols) {
121                 $cols = array();
122                 foreach($n as $k) {
123                     $cols[] = strtoupper(trim($k));
124                 }
125                 
126                 if (empty($cols)) {
127                     continue;
128                 }
129                 foreach($req as $r) {
130                     if (!in_array($r,$cols)) {
131                         $cols = false;
132                         break;
133                     }
134                 }
135                 continue;
136             }
137             
138             $row = array();
139             
140             foreach($cols as $i=>$k) {
141                 $row[$k] = trim($n[$i]);
142             }
143             
144             $this->processBlock($row);
145         }
146         
147     }
148     
149     
150     
151     function processLocation($row)
152     {
153         $continent = $this->processContinent($row['CONTINENT_CODE'], $row['CONTINENT_NAME']);
154         
155         $country = $this->processCountry($row['COUNTRY_ISO_CODE'], $row['COUNTRY_NAME'], $continent);
156         
157         $division = $this->processDivision($row['SUBDIVISION_ISO_CODE'], $row['SUBDIVISION_NAME']);
158         
159         $city = $this->processCity($row['CITY_NAME'], $row['METRO_CODE'], $row['TIME_ZONE'], $country, $division);
160         
161         if(!empty($city) && !empty($city->id)){
162             $this->id_mapping[$row['GEONAME_ID']] = $city->id;
163         }
164     }
165     
166     function processContinent($code, $name)
167     {
168         if(empty($code)){
169             return false;
170         }
171         
172         $continent = DB_DataObject::factory('core_geoip_continent');
173         if(!$continent->get('code', $code)){
174             $continent->setFrom(array(
175                 'code' => $code,
176                 'name' => (!empty($name)) ? $name : $code
177             ));
178
179             $continent->insert();
180         }
181         
182         return $continent;
183     }
184     
185     function processCountry($code, $name, $continent)
186     {
187         if(empty($code)){
188             return false;
189         }
190         
191         $country = DB_DataObject::factory('core_geoip_country');
192         if(!$country->get('code', $code)){
193             $country->setFrom(array(
194                 'code' => $code,
195                 'name' => (!empty($name)) ? $name : $code,
196                 'continent_id' => (!empty($continent) && !empty($continent->id)) ? $continent->id : 0
197             ));
198
199             $country->insert();
200         }
201         
202         return $country;
203     }
204     
205     function processDivision($code, $name)
206     {
207         if(empty($code)){
208             return false;
209         }
210         
211         $division = DB_DataObject::factory('core_geoip_division');
212         if(!$division->get('code', $code)){
213             $division->setFrom(array(
214                 'code' => $code,
215                 'name' => (!empty($name)) ? $name : $code
216             ));
217
218             $division->insert();
219         }
220         
221         return $division;
222     }
223     
224     function processCity($name, $metro_code, $time_zone, $country, $division)
225     {
226         if(empty($name)){
227             return false;
228         }
229         
230         $city = DB_DataObject::factory('core_geoip_city');
231         
232         if($city->get('name', $name)){
233             return $city;
234         }
235         
236         $city->setFrom(array(
237             'name' => $name,
238             'metro_code' => $metro_code,
239             'time_zone' => $time_zone,
240             'country_id' => (!empty($country) && !empty($country->id)) ? $country->id : 0,
241             'division_id' => (!empty($division) && !empty($division->id)) ? $division->id : 0
242         ));
243         
244         $city->insert();
245         
246         return $city;
247         
248     }
249     
250     function processBlock($row)
251     {
252         $network_mapping = DB_DataObject::factory('core_geoip_network_mapping');
253         
254         $start_ip = array_pop(explode(":", $row['NETWORK_START_IP']));
255         print_r($start_ip);exit;
256         $network_mapping->setFrom(array(
257             'start_ip' => $start_ip,
258             'mask_length' => pow(2, (128 - $row['NETWORK_MASK_LENGTH'])),
259             'city_id' => $this->id_mapping[$row['GEONAME_ID']]
260         ));
261         
262         if(!$network_mapping->find(true)){
263             $network_mapping->insert();
264         }
265         
266         $location = DB_DataObject::factory('core_geoip_location');
267         if(!$location->get('city_id', $network_mapping->city_id)){
268             $location->setFrom(array(
269                 'latitude' => $row['LATITUDE'],
270                 'longitude' => $row['LONGITUDE'],
271                 'city_id' => $network_mapping->city_id
272             ));
273         }
274         
275         
276         if(!empty($row['POSTAL_CODE'])){
277             $city = DB_DataObject::factory('core_geoip_city');
278             if($city->get($network_mapping->city_id)){
279                 return;
280             }
281
282             $oc = clone($city);
283             $city->postal_code = $row['POSTAL_CODE'];
284             
285             $city->update($oc);
286         }
287         
288     }
289     
290     function log($str)
291     {
292         echo "$str \n";
293     }
294 }