Oppaitime's version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

update_geoip.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?
  2. ini_set('memory_limit', '1G');
  3. set_time_limit(0);
  4. if (!check_perms('site_debug')) {
  5. error(403);
  6. }
  7. View::show_header();
  8. chdir('/tmp');
  9. //requires wget, unzip, gunzip commands to be installed
  10. shell_exec('wget http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip');
  11. shell_exec('wget http://geolite.maxmind.com/download/geoip/database/GeoIPv6.csv.gz');
  12. shell_exec('unzip GeoIPCountryCSV.zip');
  13. shell_exec('gunzip GeoIPv6.csv.gz');
  14. shell_exec('cut -d , -f 3-5 GeoIPCountryWhois.csv > GeoIPCountry.csv');
  15. shell_exec('cut -d , -f 3-5 GeoIPv6.csv | tr -d " " >> GeoIPCountry.csv');
  16. shell_exec('rm GeoIPCountryCSV.zip GeoIPv6.csv.gz GeoIPCountryWhois.csv GeoIPv6.csv');
  17. if (($Blocks = file("GeoIPCountry.csv", FILE_IGNORE_NEW_LINES)) === false) {
  18. echo 'Error';
  19. }
  20. echo 'There are '.count($Blocks).' blocks';
  21. echo '<br />';
  22. //Because the old code reading a 2mil line database had splitting, we're just gonna keep using it with this much more reasonable 140k line db
  23. $SplitOn = 1000;
  24. $DB->query("TRUNCATE TABLE geoip_country");
  25. $Values = array();
  26. foreach ($Blocks as $Index => $Block) {
  27. list($StartIP, $EndIP, $CountryID) = explode(",", $Block);
  28. $StartIP = trim($StartIP, '"');
  29. $EndIP = trim($EndIP, '"');
  30. $CountryID = trim($CountryID, '"');
  31. $Values[] = "('$StartIP', '$EndIP', '".$CountryID."')";
  32. if ($Index % $SplitOn == 0) {
  33. $DB->query('
  34. INSERT INTO geoip_country (StartIP, EndIP, Code)
  35. VALUES '.implode(', ', $Values));
  36. $Values = array();
  37. }
  38. }
  39. if (count($Values) > 0) {
  40. $DB->query("
  41. INSERT INTO geoip_country (StartIP, EndIP, Code)
  42. VALUES ".implode(', ', $Values));
  43. }
  44. View::show_footer();
  45. /*
  46. The following way works perfectly fine, we just foung the APNIC data to be to outdated for us.
  47. */
  48. /*
  49. if (!check_perms('admin_update_geoip')) {
  50. die();
  51. }
  52. enforce_login();
  53. ini_set('memory_limit', 1024 * 1024 * 1024);
  54. ini_set('max_execution_time', 3600);
  55. header('Content-type: text/plain');
  56. ob_end_clean();
  57. restore_error_handler();
  58. $Registries[] = 'http://ftp.apnic.net/stats/afrinic/delegated-afrinic-latest'; //Africa
  59. $Registries[] = 'http://ftp.apnic.net/stats/apnic/delegated-apnic-latest'; //Asia & Pacific
  60. $Registries[] = 'http://ftp.apnic.net/stats/arin/delegated-arin-latest'; //North America
  61. $Registries[] = 'http://ftp.apnic.net/stats/lacnic/delegated-lacnic-latest'; //South America
  62. $Registries[] = 'http://ftp.apnic.net/stats/ripe-ncc/delegated-ripencc-latest'; //Europe
  63. $Registries[] = 'ftp://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-latest'; //Africa
  64. $Registries[] = 'ftp://ftp.apnic.net/pub/stats/apnic/delegated-apnic-latest'; //Asia & Pacific
  65. $Registries[] = 'ftp://ftp.arin.net/pub/stats/arin/delegated-arin-latest'; //North America
  66. $Registries[] = 'ftp://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest'; //South America
  67. $Registries[] = 'ftp://ftp.ripe.net/ripe/stats/delegated-ripencc-latest'; //Europe
  68. $Query = array();
  69. foreach ($Registries as $Registry) {
  70. $CountryData = explode("\n",file_get_contents($Registry));
  71. foreach ($CountryData as $Country) {
  72. if (preg_match('/\|([A-Z]{2})\|ipv4\|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\|(\d+)\|/', $Country, $Matches)) {
  73. $Start = Tools::ip_to_unsigned($Matches[2]);
  74. if ($Start == 2147483647) { continue; }
  75. if (!isset($Current)) {
  76. $Current = array('StartIP' => $Start, 'EndIP' => $Start + $Matches[3],'Code' => $Matches[1]);
  77. } elseif ($Current['Code'] == $Matches[1] && $Current['EndIP'] == $Start) {
  78. $Current['EndIP'] = $Current['EndIP'] + $Matches[3];
  79. } else {
  80. $Query[] = "('".$Current['StartIP']."','".$Current['EndIP']."','".$Current['Code']."')";
  81. $Current = array('StartIP' => $Start, 'EndIP' => $Start + $Matches[3],'Code' => $Matches[1]);
  82. }
  83. }
  84. }
  85. }
  86. $Query[] = "('".$Current['StartIP']."','".$Current['EndIP']."','".$Current['Code']."')";
  87. $DB->query("TRUNCATE TABLE geoip_country");
  88. $DB->query("INSERT INTO geoip_country (StartIP, EndIP, Code) VALUES ".implode(',', $Query));
  89. echo $DB->affected_rows();
  90. */