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);
59 $linecount = count(file($csv));
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',
78 while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
79 if(!array_filter($n)){ // empty row
86 $cols[] = strtoupper(trim($k));
93 if (!in_array($r,$cols)) {
103 foreach($cols as $i=>$k) {
104 $row[$k] = trim($n[$i]);
107 $this->processLocation($row);
109 echo "\033[K"; // Erase to end of line:
114 echo "\033[".strlen($echo)."D"; // Move $length characters backward
117 $echo = str_pad(ROUND(($this->processed / $this->total),2) * 100, 3, ' ', STR_PAD_LEFT) .
118 " % (" . str_pad(($this->processed), strlen($this->total), ' ', STR_PAD_LEFT) .
119 " / {$this->total}) - out {$this->out_count}";
124 if($this->processed == $this->total){
131 function insertBlock($csv)
133 ini_set("auto_detect_line_endings", true);
135 $fh = fopen($csv, 'r');
137 $this->jerr("invalid location file");
141 'NETWORK_START_IP', 'NETWORK_MASK_LENGTH', 'GEONAME_ID',
142 'REGISTERED_COUNTRY_GEONAME_ID', 'REPRESENTED_COUNTRY_GEONAME_ID', 'POSTAL_CODE',
143 'LATITUDE', 'LONGITUDE', 'IS_ANONYMOUS_PROXY',
144 'IS_SATELLITE_PROVIDER'
149 while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
150 if(!array_filter($n)){ // empty row
157 $cols[] = strtoupper(trim($k));
163 foreach($req as $r) {
164 if (!in_array($r,$cols)) {
174 foreach($cols as $i=>$k) {
175 $row[$k] = trim($n[$i]);
178 $this->processBlock($row);
185 function processLocation($row)
187 $continent = $this->processContinent($row['CONTINENT_CODE'], $row['CONTINENT_NAME']);
189 $country = $this->processCountry($row['COUNTRY_ISO_CODE'], $row['COUNTRY_NAME'], $continent);
191 $division = $this->processDivision($row['SUBDIVISION_ISO_CODE'], $row['SUBDIVISION_NAME']);
193 $city = $this->processCity($row['CITY_NAME'], $row['METRO_CODE'], $row['TIME_ZONE'], $country, $division);
195 if(!empty($city) && !empty($city->id)){
196 $this->id_mapping[$row['GEONAME_ID']] = $city->id;
200 function processContinent($code, $name)
206 $continent = DB_DataObject::factory('core_geoip_continent');
207 if(!$continent->get('code', $code)){
208 $continent->setFrom(array(
210 'name' => (!empty($name)) ? $name : $code
213 $continent->insert();
219 function processCountry($code, $name, $continent)
225 $country = DB_DataObject::factory('core_geoip_country');
226 if(!$country->get('code', $code)){
227 $country->setFrom(array(
229 'name' => (!empty($name)) ? $name : $code,
230 'continent_id' => (!empty($continent) && !empty($continent->id)) ? $continent->id : 0
239 function processDivision($code, $name)
245 $division = DB_DataObject::factory('core_geoip_division');
246 if(!$division->get('code', $code)){
247 $division->setFrom(array(
249 'name' => (!empty($name)) ? $name : $code
258 function processCity($name, $metro_code, $time_zone, $country, $division)
264 $city = DB_DataObject::factory('core_geoip_city');
266 if($city->get('name', $name)){
270 $city->setFrom(array(
272 'metro_code' => $metro_code,
273 'time_zone' => $time_zone,
274 'country_id' => (!empty($country) && !empty($country->id)) ? $country->id : 0,
275 'division_id' => (!empty($division) && !empty($division->id)) ? $division->id : 0
284 function processBlock($row)
286 if(empty($this->id_mapping[$row['GEONAME_ID']])){
287 $this->log("Missing mapping for {$row['GEONAME_ID']}");
288 $this->log("IP : {$row['NETWORK_START_IP']}");
292 $network_mapping = DB_DataObject::factory('core_geoip_network_mapping');
294 $start_ip = array_pop(explode(":", $row['NETWORK_START_IP']));
296 $network_mapping->setFrom(array(
297 'start_ip' => $start_ip,
298 'mask_length' => pow(2, (128 - $row['NETWORK_MASK_LENGTH'])),
299 'city_id' => $this->id_mapping[$row['GEONAME_ID']]
302 if(!$network_mapping->find(true)){
303 $network_mapping->insert();
306 $location = DB_DataObject::factory('core_geoip_location');
307 if(!$location->get('city_id', $network_mapping->city_id)){
308 $location->setFrom(array(
309 'latitude' => $row['LATITUDE'],
310 'longitude' => $row['LONGITUDE'],
311 'city_id' => $network_mapping->city_id
316 if(!empty($row['POSTAL_CODE'])){
317 $city = DB_DataObject::factory('core_geoip_city');
318 if($city->get($network_mapping->city_id)){
323 $city->postal_code = $row['POSTAL_CODE'];