3 require_once 'Pman/Roo.php';
5 class Pman_Core_Import_Core_geoip extends Pman_Roo
7 static $cli_desc = "Insert the geoip database";
9 static $cli_opts = array();
11 var $id_mapping = array();
15 $ff = HTML_FlexyFramework::get();
19 HTML_FlexyFramework::ensureSingle(__FILE__, $this);
35 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
37 $location = '/tmp/GeoLite2-City-Locations.csv';
38 $block = '/tmp/GeoLite2-City-Blocks.csv';
40 if(!file_exists($location) || !file_exists($block)){
41 $this->jerr('GeoLite2-City-Locations.csv OR GeoLite2-City-Blocks.csv does not exists?!');
44 $this->log("Insert location data start");
46 $this->insertLocation($location);
48 $this->log("Insert Block data start");
50 $this->insertBlock($block);
55 function insertLocation($csv)
57 ini_set("auto_detect_line_endings", true);
61 $fh = fopen($csv, 'r');
64 $this->jerr("invalid location file");
68 'GEONAME_ID', 'CONTINENT_CODE', 'CONTINENT_NAME',
69 'COUNTRY_ISO_CODE', 'COUNTRY_NAME', 'SUBDIVISION_ISO_CODE',
70 'SUBDIVISION_NAME', 'CITY_NAME', 'METRO_CODE',
77 $this->total = count(file($csv));
80 while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
81 if(!array_filter($n)){ // empty row
88 $cols[] = strtoupper(trim($k));
95 if (!in_array($r,$cols)) {
105 foreach($cols as $i=>$k) {
106 $row[$k] = trim($n[$i]);
109 $this->processLocation($row);
113 $this->processStatus();
118 function insertBlock($csv)
120 ini_set("auto_detect_line_endings", true);
122 $fh = fopen($csv, 'r');
124 $this->jerr("invalid location file");
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'
136 $this->processed = 0;
137 $this->total = count(file($csv));
140 while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
141 if(!array_filter($n)){ // empty row
148 $cols[] = strtoupper(trim($k));
154 foreach($req as $r) {
155 if (!in_array($r,$cols)) {
165 foreach($cols as $i=>$k) {
166 $row[$k] = trim($n[$i]);
169 $this->processBlock($row);
177 function processLocation($row)
179 $continent = $this->processContinent($row['CONTINENT_CODE'], $row['CONTINENT_NAME']);
181 $country = $this->processCountry($row['COUNTRY_ISO_CODE'], $row['COUNTRY_NAME'], $continent);
183 $division = $this->processDivision($row['SUBDIVISION_ISO_CODE'], $row['SUBDIVISION_NAME']);
185 $city = $this->processCity($row['CITY_NAME'], $row['METRO_CODE'], $row['TIME_ZONE'], $country, $division);
187 if(!empty($city) && !empty($city->id)){
188 $this->id_mapping[$row['GEONAME_ID']] = $city->id;
192 function processContinent($code, $name)
198 $continent = DB_DataObject::factory('core_geoip_continent');
199 if(!$continent->get('code', $code)){
200 $continent->setFrom(array(
202 'name' => (!empty($name)) ? $name : $code
205 $continent->insert();
211 function processCountry($code, $name, $continent)
217 $country = DB_DataObject::factory('core_geoip_country');
218 if(!$country->get('code', $code)){
219 $country->setFrom(array(
221 'name' => (!empty($name)) ? $name : $code,
222 'continent_id' => (!empty($continent) && !empty($continent->id)) ? $continent->id : 0
231 function processDivision($code, $name)
237 $division = DB_DataObject::factory('core_geoip_division');
238 if(!$division->get('code', $code)){
239 $division->setFrom(array(
241 'name' => (!empty($name)) ? $name : $code
250 function processCity($name, $metro_code, $time_zone, $country, $division)
256 $city = DB_DataObject::factory('core_geoip_city');
258 if($city->get('name', $name)){
262 $city->setFrom(array(
264 'metro_code' => $metro_code,
265 'time_zone' => $time_zone,
266 'country_id' => (!empty($country) && !empty($country->id)) ? $country->id : 0,
267 'division_id' => (!empty($division) && !empty($division->id)) ? $division->id : 0
276 function processBlock($row)
278 if(empty($this->id_mapping[$row['GEONAME_ID']])){
279 $this->log("Missing mapping for {$row['GEONAME_ID']}");
280 $this->log("IP : {$row['NETWORK_START_IP']}");
284 $network_mapping = DB_DataObject::factory('core_geoip_network_mapping');
286 $start_ip = array_pop(explode(":", $row['NETWORK_START_IP']));
288 $network_mapping->setFrom(array(
289 'start_ip' => $start_ip,
290 'mask_length' => pow(2, (128 - $row['NETWORK_MASK_LENGTH'])),
291 'city_id' => $this->id_mapping[$row['GEONAME_ID']]
294 if(!$network_mapping->find(true)){
295 $network_mapping->insert();
298 $location = DB_DataObject::factory('core_geoip_location');
299 if(!$location->get('city_id', $network_mapping->city_id)){
300 $location->setFrom(array(
301 'latitude' => $row['LATITUDE'],
302 'longitude' => $row['LONGITUDE'],
303 'city_id' => $network_mapping->city_id
308 if(!empty($row['POSTAL_CODE'])){
309 $city = DB_DataObject::factory('core_geoip_city');
310 if($city->get($network_mapping->city_id)){
315 $city->postal_code = $row['POSTAL_CODE'];
322 function processStatus()
324 echo "\033[K"; // Erase to end of line:
326 if (strlen($this->echo)) {
327 echo "\033[".strlen($this->echo)."D"; // Move $length characters backward
330 $this->echo = str_pad(ROUND(($this->processed / $this->total),2) * 100, 3, ' ', STR_PAD_LEFT) .
331 " % (" . str_pad(($this->processed), strlen($this->total), ' ', STR_PAD_LEFT) .
332 " / {$this->total})";
337 if($this->processed == $this->total){