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);
31 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
33 $location = '/tmp/GeoLite2-City-Locations.csv';
34 $block = '/tmp/GeoLite2-City-Blocks.csv';
36 if(!file_exists($location) || !file_exists($block)){
37 $this->jerr('GeoLite2-City-Locations.csv OR GeoLite2-City-Blocks.csv does not exists?!');
40 $this->log("Insert location data start");
42 $this->insertLocation($location);
44 $this->log("Insert Block data start");
46 $this->insertBlock($block);
51 function insertLocation($csv)
53 ini_set("auto_detect_line_endings", true);
55 $fh = fopen($csv, 'r');
57 $this->jerr("invalid location file");
61 'GEONAME_ID', 'CONTINENT_CODE', 'CONTINENT_NAME',
62 'COUNTRY_ISO_CODE', 'COUNTRY_NAME', 'SUBDIVISION_ISO_CODE',
63 'SUBDIVISION_NAME', 'CITY_NAME', 'METRO_CODE',
69 while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
70 if(!array_filter($n)){ // empty row
77 $cols[] = strtoupper(trim($k));
84 if (!in_array($r,$cols)) {
94 foreach($cols as $i=>$k) {
95 $row[$k] = trim($n[$i]);
98 $this->processLocation($row);
103 function insertBlock($csv)
105 ini_set("auto_detect_line_endings", true);
107 $fh = fopen($csv, 'r');
109 $this->jerr("invalid location file");
113 'NETWORK_START_IP', 'NETWORK_MASK_LENGTH', 'GEONAME_ID',
114 'REGISTERED_COUNTRY_GEONAME_ID', 'REPRESENTED_COUNTRY_GEONAME_ID', 'POSTAL_CODE',
115 'LATITUDE', 'LONGITUDE', 'IS_ANONYMOUS_PROXY',
116 'IS_SATELLITE_PROVIDER'
121 while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
122 if(!array_filter($n)){ // empty row
129 $cols[] = strtoupper(trim($k));
135 foreach($req as $r) {
136 if (!in_array($r,$cols)) {
146 foreach($cols as $i=>$k) {
147 $row[$k] = trim($n[$i]);
150 $this->processBlock($row);
157 function processLocation($row)
159 $this->log("Processing Location : Continent - {$row['CONTINENT_NAME']}({$row['CONTINENT_CODE']}) | Country - {$row['COUNTRY_NAME']}({$row['COUNTRY_ISO_CODE']}) | Division - {$row['SUBDIVISION_ISO_CODE']}({$row['SUBDIVISION_NAME']}) | City - {$row['CITY_NAME']}");
161 $continent = $this->processContinent($row['CONTINENT_CODE'], $row['CONTINENT_NAME']);
163 $country = $this->processCountry($row['COUNTRY_ISO_CODE'], $row['COUNTRY_NAME'], $continent);
165 $division = $this->processDivision($row['SUBDIVISION_ISO_CODE'], $row['SUBDIVISION_NAME']);
167 $city = $this->processCity($row['CITY_NAME'], $row['METRO_CODE'], $row['TIME_ZONE'], $country, $division);
169 if(!empty($city) && !empty($city->id)){
170 $this->id_mapping[$row['GEONAME_ID']] = $city->id;
174 function processContinent($code, $name)
180 $continent = DB_DataObject::factory('core_geoip_continent');
181 if(!$continent->get('code', $code)){
182 $continent->setFrom(array(
184 'name' => (!empty($name)) ? $name : $code
187 $continent->insert();
193 function processCountry($code, $name, $continent)
199 $country = DB_DataObject::factory('core_geoip_country');
200 if(!$country->get('code', $code)){
201 $country->setFrom(array(
203 'name' => (!empty($name)) ? $name : $code,
204 'continent_id' => (!empty($continent) && !empty($continent->id)) ? $continent->id : 0
213 function processDivision($code, $name)
219 $division = DB_DataObject::factory('core_geoip_division');
220 if(!$division->get('code', $code)){
221 $division->setFrom(array(
223 'name' => (!empty($name)) ? $name : $code
232 function processCity($name, $metro_code, $time_zone, $country, $division)
238 $city = DB_DataObject::factory('core_geoip_city');
240 if($city->get('name', $name)){
244 $city->setFrom(array(
246 'metro_code' => $metro_code,
247 'time_zone' => $time_zone,
248 'country_id' => (!empty($country) && !empty($country->id)) ? $country->id : 0,
249 'division_id' => (!empty($division) && !empty($division->id)) ? $division->id : 0
258 function processBlock($row)
260 if(empty($this->id_mapping[$row['GEONAME_ID']])){
261 $this->log("Missing mapping for {$row['GEONAME_ID']}");
262 $this->log("IP : {$row['NETWORK_START_IP']}");
266 $this->log("Processing : Continent - {$row['CONTINENT_NAME']}({$row['CONTINENT_CODE']}) | Country - {$row['COUNTRY_NAME']}({$row['COUNTRY_ISO_CODE']}) | Division - {$row['SUBDIVISION_ISO_CODE']}({$row['SUBDIVISION_NAME']}) | City - {$row['CITY_NAME']}");
268 $network_mapping = DB_DataObject::factory('core_geoip_network_mapping');
270 $start_ip = array_pop(explode(":", $row['NETWORK_START_IP']));
272 $network_mapping->setFrom(array(
273 'start_ip' => $start_ip,
274 'mask_length' => pow(2, (128 - $row['NETWORK_MASK_LENGTH'])),
275 'city_id' => $this->id_mapping[$row['GEONAME_ID']]
278 if(!$network_mapping->find(true)){
279 $network_mapping->insert();
282 $location = DB_DataObject::factory('core_geoip_location');
283 if(!$location->get('city_id', $network_mapping->city_id)){
284 $location->setFrom(array(
285 'latitude' => $row['LATITUDE'],
286 'longitude' => $row['LONGITUDE'],
287 'city_id' => $network_mapping->city_id
292 if(!empty($row['POSTAL_CODE'])){
293 $city = DB_DataObject::factory('core_geoip_city');
294 if($city->get($network_mapping->city_id)){
299 $city->postal_code = $row['POSTAL_CODE'];