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