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');
58 $this->jerr("invalid location file");
62 'GEONAME_ID', 'CONTINENT_CODE', 'CONTINENT_NAME',
63 'COUNTRY_ISO_CODE', 'COUNTRY_NAME', 'SUBDIVISION_ISO_CODE',
64 'SUBDIVISION_NAME', 'CITY_NAME', 'METRO_CODE',
70 while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
71 if(!array_filter($n)){ // empty row
78 $cols[] = strtoupper(trim($k));
85 if (!in_array($r,$cols)) {
95 foreach($cols as $i=>$k) {
96 $row[$k] = trim($n[$i]);
99 $this->processLocation($row);
104 function insertBlock($csv)
106 ini_set("auto_detect_line_endings", true);
108 $fh = fopen($csv, 'r');
110 $this->jerr("invalid location file");
114 'NETWORK_START_IP', 'NETWORK_MASK_LENGTH', 'GEONAME_ID',
115 'REGISTERED_COUNTRY_GEONAME_ID', 'REPRESENTED_COUNTRY_GEONAME_ID', 'POSTAL_CODE',
116 'LATITUDE', 'LONGITUDE', 'IS_ANONYMOUS_PROXY',
117 'IS_SATELLITE_PROVIDER'
122 while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
123 if(!array_filter($n)){ // empty row
130 $cols[] = strtoupper(trim($k));
136 foreach($req as $r) {
137 if (!in_array($r,$cols)) {
147 foreach($cols as $i=>$k) {
148 $row[$k] = trim($n[$i]);
151 $this->processBlock($row);
158 function processLocation($row)
160 $continent = $this->processContinent($row['CONTINENT_CODE'], $row['CONTINENT_NAME']);
162 $country = $this->processCountry($row['COUNTRY_ISO_CODE'], $row['COUNTRY_NAME'], $continent);
164 $division = $this->processDivision($row['SUBDIVISION_ISO_CODE'], $row['SUBDIVISION_NAME']);
166 $city = $this->processCity($row['CITY_NAME'], $row['METRO_CODE'], $row['TIME_ZONE'], $country, $division);
168 if(!empty($city) && !empty($city->id)){
169 $this->id_mapping[$row['GEONAME_ID']] = $city->id;
173 function processContinent($code, $name)
179 $continent = DB_DataObject::factory('core_geoip_continent');
180 if(!$continent->get('code', $code)){
181 $continent->setFrom(array(
183 'name' => (!empty($name)) ? $name : $code
186 $continent->insert();
192 function processCountry($code, $name, $continent)
198 $country = DB_DataObject::factory('core_geoip_country');
199 if(!$country->get('code', $code)){
200 $country->setFrom(array(
202 'name' => (!empty($name)) ? $name : $code,
203 'continent_id' => (!empty($continent) && !empty($continent->id)) ? $continent->id : 0
212 function processDivision($code, $name)
218 $division = DB_DataObject::factory('core_geoip_division');
219 if(!$division->get('code', $code)){
220 $division->setFrom(array(
222 'name' => (!empty($name)) ? $name : $code
231 function processCity($name, $metro_code, $time_zone, $country, $division)
237 $city = DB_DataObject::factory('core_geoip_city');
239 if($city->get('name', $name)){
243 $city->setFrom(array(
245 'metro_code' => $metro_code,
246 'time_zone' => $time_zone,
247 'country_id' => (!empty($country) && !empty($country->id)) ? $country->id : 0,
248 'division_id' => (!empty($division) && !empty($division->id)) ? $division->id : 0
257 function processBlock($row)
259 if(empty($this->id_mapping[$row['GEONAME_ID']])){
260 $this->log("Missing mapping for {$row['GEONAME_ID']}");
261 $this->log("IP : {$row['NETWORK_START_IP']}");
265 $network_mapping = DB_DataObject::factory('core_geoip_network_mapping');
267 $start_ip = array_pop(explode(":", $row['NETWORK_START_IP']));
269 $network_mapping->setFrom(array(
270 'start_ip' => $start_ip,
271 'mask_length' => pow(2, (128 - $row['NETWORK_MASK_LENGTH'])),
272 'city_id' => $this->id_mapping[$row['GEONAME_ID']]
275 if(!$network_mapping->find(true)){
276 $network_mapping->insert();
279 $location = DB_DataObject::factory('core_geoip_location');
280 if(!$location->get('city_id', $network_mapping->city_id)){
281 $location->setFrom(array(
282 'latitude' => $row['LATITUDE'],
283 'longitude' => $row['LONGITUDE'],
284 'city_id' => $network_mapping->city_id
289 if(!empty($row['POSTAL_CODE'])){
290 $city = DB_DataObject::factory('core_geoip_city');
291 if($city->get($network_mapping->city_id)){
296 $city->postal_code = $row['POSTAL_CODE'];