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 started");
42 $this->insertLocation($location);
44 $this->log("Insert Block data end");
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 $continent = $this->processContinent($row['CONTINENT_CODE'], $row['CONTINENT_NAME']);
161 $country = $this->processCountry($row['COUNTRY_ISO_CODE'], $row['COUNTRY_NAME'], $continent);
163 $division = $this->processDivision($row['SUBDIVISION_ISO_CODE'], $row['SUBDIVISION_NAME']);
165 $city = $this->processCity($row['CITY_NAME'], $row['METRO_CODE'], $row['TIME_ZONE'], $country, $division);
167 if(!empty($city) && !empty($city->id)){
168 $this->id_mapping[$row['GEONAME_ID']] = $city->id;
172 function processContinent($code, $name)
178 $continent = DB_DataObject::factory('core_geoip_continent');
179 if(!$continent->get('code', $code)){
180 $continent->setFrom(array(
182 'name' => (!empty($name)) ? $name : $code
185 $continent->insert();
191 function processCountry($code, $name, $continent)
197 $country = DB_DataObject::factory('core_geoip_country');
198 if(!$country->get('code', $code)){
199 $country->setFrom(array(
201 'name' => (!empty($name)) ? $name : $code,
202 'continent_id' => (!empty($continent) && !empty($continent->id)) ? $continent->id : 0
211 function processDivision($code, $name)
217 $division = DB_DataObject::factory('core_geoip_division');
218 if(!$division->get('code', $code)){
219 $division->setFrom(array(
221 'name' => (!empty($name)) ? $name : $code
230 function processCity($name, $metro_code, $time_zone, $country, $division)
236 $city = DB_DataObject::factory('core_geoip_city');
238 if($city->get('name', $name)){
242 $city->setFrom(array(
244 'metro_code' => $metro_code,
245 'time_zone' => $time_zone,
246 'country_id' => (!empty($country) && !empty($country->id)) ? $country->id : 0,
247 'division_id' => (!empty($division) && !empty($division->id)) ? $division->id : 0
256 function processBlock($row)
258 if(empty($this->id_mapping[$row['GEONAME_ID']])){
259 $this->log("Missing mapping for {$row['GEONAME_ID']}");
260 $this->log("IP : {$row['NETWORK_START_IP']}");
264 $network_mapping = DB_DataObject::factory('core_geoip_network_mapping');
266 $start_ip = array_pop(explode(":", $row['NETWORK_START_IP']));
268 $network_mapping->setFrom(array(
269 'start_ip' => $start_ip,
270 'mask_length' => pow(2, (128 - $row['NETWORK_MASK_LENGTH'])),
271 'city_id' => $this->id_mapping[$row['GEONAME_ID']]
274 if(!$network_mapping->find(true)){
275 $network_mapping->insert();
278 $location = DB_DataObject::factory('core_geoip_location');
279 if(!$location->get('city_id', $network_mapping->city_id)){
280 $location->setFrom(array(
281 'latitude' => $row['LATITUDE'],
282 'longitude' => $row['LONGITUDE'],
283 'city_id' => $network_mapping->city_id
288 if(!empty($row['POSTAL_CODE'])){
289 $city = DB_DataObject::factory('core_geoip_city');
290 if($city->get($network_mapping->city_id)){
295 $city->postal_code = $row['POSTAL_CODE'];