2011-03-28 14:21:28 +00:00
|
|
|
<?
|
2011-11-09 08:00:14 +00:00
|
|
|
ini_set('memory_limit', '5G');
|
2011-08-12 21:42:06 +00:00
|
|
|
set_time_limit(0);
|
2011-03-28 14:21:28 +00:00
|
|
|
|
2013-05-14 08:00:34 +00:00
|
|
|
//if (!check_perms('site_debug')) {
|
|
|
|
// error(403);
|
|
|
|
//}
|
2011-03-28 14:21:28 +00:00
|
|
|
|
2012-10-11 08:00:15 +00:00
|
|
|
View::show_header();
|
2011-03-28 14:21:28 +00:00
|
|
|
|
|
|
|
//requires wget, unzip commands to be installed
|
2013-03-08 08:00:45 +00:00
|
|
|
shell_exec('wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity_CSV/GeoLiteCity-latest.zip');
|
|
|
|
shell_exec('unzip GeoLiteCity-latest.zip');
|
|
|
|
shell_exec('rm GeoLiteCity-latest.zip');
|
2011-03-28 14:21:28 +00:00
|
|
|
|
2013-05-10 08:00:18 +00:00
|
|
|
if (($Locations = file("GeoLiteCity_".date('Ym')."07/GeoLiteCity-Location.csv", FILE_IGNORE_NEW_LINES)) === false) {
|
2013-05-14 08:00:34 +00:00
|
|
|
error('Download or extraction of maxmind database failed');
|
2011-03-28 14:21:28 +00:00
|
|
|
}
|
|
|
|
array_shift($Locations);
|
|
|
|
array_shift($Locations);
|
|
|
|
|
2013-05-14 08:00:34 +00:00
|
|
|
echo 'There are '.count($Locations).' locations';
|
|
|
|
echo '<br />';
|
2011-03-28 14:21:28 +00:00
|
|
|
|
|
|
|
$CountryIDs = array();
|
2013-04-07 08:00:41 +00:00
|
|
|
foreach ($Locations as $Location) {
|
2013-05-14 08:00:34 +00:00
|
|
|
$Parts = explode(',', $Location);
|
2011-03-28 14:21:28 +00:00
|
|
|
//CountryIDs[1] = "AP";
|
|
|
|
$CountryIDs[trim($Parts[0], '"')] = trim($Parts[1], '"');
|
|
|
|
}
|
|
|
|
|
2013-05-14 08:00:34 +00:00
|
|
|
echo 'There are '.count($CountryIDs).' CountryIDs';
|
|
|
|
echo '<br />';
|
2011-03-28 14:21:28 +00:00
|
|
|
|
2013-05-10 08:00:18 +00:00
|
|
|
if (($Blocks = file("GeoLiteCity_".date('Ym')."07/GeoLiteCity-Blocks.csv", FILE_IGNORE_NEW_LINES)) === false) {
|
2013-05-14 08:00:34 +00:00
|
|
|
echo 'Error';
|
2011-03-28 14:21:28 +00:00
|
|
|
}
|
|
|
|
array_shift($Blocks);
|
|
|
|
array_shift($Blocks);
|
|
|
|
|
2013-05-14 08:00:34 +00:00
|
|
|
echo 'There are '.count($Blocks).' blocks';
|
|
|
|
echo '<br />';
|
2011-03-28 14:21:28 +00:00
|
|
|
|
|
|
|
//Because 4,000,000 rows is a lot for any server to handle, we split it into manageable groups of 10,000
|
|
|
|
$SplitOn = 10000;
|
|
|
|
$DB->query("TRUNCATE TABLE geoip_country");
|
|
|
|
|
|
|
|
$Values = array();
|
2013-04-07 08:00:41 +00:00
|
|
|
foreach ($Blocks as $Index => $Block) {
|
2011-03-28 14:21:28 +00:00
|
|
|
list($StartIP, $EndIP, $CountryID) = explode(",", $Block);
|
|
|
|
$StartIP = trim($StartIP, '"');
|
|
|
|
$EndIP = trim($EndIP, '"');
|
|
|
|
$CountryID = trim($CountryID, '"');
|
2013-05-14 08:00:34 +00:00
|
|
|
$Values[] = "('$StartIP', '$EndIP', '".$CountryIDs[$CountryID]."')";
|
2013-04-07 08:00:41 +00:00
|
|
|
if ($Index % $SplitOn == 0) {
|
2013-05-14 08:00:34 +00:00
|
|
|
$DB->query('
|
|
|
|
INSERT INTO geoip_country (StartIP, EndIP, Code)
|
|
|
|
VALUES '.implode(', ', $Values));
|
2011-03-28 14:21:28 +00:00
|
|
|
$Values = array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-07 08:00:41 +00:00
|
|
|
if (count($Values) > 0) {
|
2013-05-14 08:00:34 +00:00
|
|
|
$DB->query("
|
|
|
|
INSERT INTO geoip_country (StartIP, EndIP, Code)
|
|
|
|
VALUES ".implode(', ', $Values));
|
2011-03-28 14:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-11 08:00:15 +00:00
|
|
|
View::show_footer();
|
2011-03-28 14:21:28 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
The following way works perfectly fine, we just foung the APNIC data to be to outdated for us.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2013-04-07 08:00:41 +00:00
|
|
|
if (!check_perms('admin_update_geoip')) {
|
|
|
|
die();
|
|
|
|
}
|
2011-03-28 14:21:28 +00:00
|
|
|
enforce_login();
|
|
|
|
|
2013-05-14 08:00:34 +00:00
|
|
|
ini_set('memory_limit', 1024 * 1024 * 1024);
|
2011-03-28 14:21:28 +00:00
|
|
|
ini_set('max_execution_time', 3600);
|
|
|
|
|
|
|
|
header('Content-type: text/plain');
|
|
|
|
ob_end_clean();
|
|
|
|
restore_error_handler();
|
|
|
|
|
|
|
|
$Registries[] = 'http://ftp.apnic.net/stats/afrinic/delegated-afrinic-latest'; //Africa
|
|
|
|
$Registries[] = 'http://ftp.apnic.net/stats/apnic/delegated-apnic-latest'; //Asia & Pacific
|
|
|
|
$Registries[] = 'http://ftp.apnic.net/stats/arin/delegated-arin-latest'; //North America
|
|
|
|
$Registries[] = 'http://ftp.apnic.net/stats/lacnic/delegated-lacnic-latest'; //South America
|
|
|
|
$Registries[] = 'http://ftp.apnic.net/stats/ripe-ncc/delegated-ripencc-latest'; //Europe
|
|
|
|
|
|
|
|
$Registries[] = 'ftp://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-latest'; //Africa
|
|
|
|
$Registries[] = 'ftp://ftp.apnic.net/pub/stats/apnic/delegated-apnic-latest'; //Asia & Pacific
|
|
|
|
$Registries[] = 'ftp://ftp.arin.net/pub/stats/arin/delegated-arin-latest'; //North America
|
|
|
|
$Registries[] = 'ftp://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest'; //South America
|
|
|
|
$Registries[] = 'ftp://ftp.ripe.net/ripe/stats/delegated-ripencc-latest'; //Europe
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$Query = array();
|
|
|
|
|
|
|
|
foreach ($Registries as $Registry) {
|
|
|
|
$CountryData = explode("\n",file_get_contents($Registry));
|
|
|
|
foreach ($CountryData as $Country) {
|
|
|
|
if (preg_match('/\|([A-Z]{2})\|ipv4\|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\|(\d+)\|/', $Country, $Matches)) {
|
|
|
|
|
2012-10-11 08:00:15 +00:00
|
|
|
$Start = Tools::ip_to_unsigned($Matches[2]);
|
2013-04-07 08:00:41 +00:00
|
|
|
if ($Start == 2147483647) { continue; }
|
2013-02-22 08:00:24 +00:00
|
|
|
|
2011-03-28 14:21:28 +00:00
|
|
|
if (!isset($Current)) {
|
|
|
|
$Current = array('StartIP' => $Start, 'EndIP' => $Start + $Matches[3],'Code' => $Matches[1]);
|
|
|
|
} elseif ($Current['Code'] == $Matches[1] && $Current['EndIP'] == $Start) {
|
|
|
|
$Current['EndIP'] = $Current['EndIP'] + $Matches[3];
|
|
|
|
} else {
|
|
|
|
$Query[] = "('".$Current['StartIP']."','".$Current['EndIP']."','".$Current['Code']."')";
|
|
|
|
$Current = array('StartIP' => $Start, 'EndIP' => $Start + $Matches[3],'Code' => $Matches[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$Query[] = "('".$Current['StartIP']."','".$Current['EndIP']."','".$Current['Code']."')";
|
|
|
|
|
|
|
|
$DB->query("TRUNCATE TABLE geoip_country");
|
|
|
|
$DB->query("INSERT INTO geoip_country (StartIP, EndIP, Code) VALUES ".implode(',', $Query));
|
|
|
|
echo $DB->affected_rows();
|
|
|
|
*/
|