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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?
  2. ini_set('memory_limit', '1G');
  3. set_time_limit(0);
  4. $LIMIT = 1000;
  5. if (!check_perms('site_debug')) {
  6. error(403);
  7. }
  8. View::show_header();
  9. chdir('/tmp');
  10. // requires wget, unzip, gunzip commands to be installed
  11. // Country section
  12. shell_exec('wget http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip');
  13. shell_exec('wget http://geolite.maxmind.com/download/geoip/database/GeoIPv6.csv.gz');
  14. shell_exec('unzip GeoIPCountryCSV.zip');
  15. shell_exec('gunzip GeoIPv6.csv.gz');
  16. shell_exec('cut -d , -f 3-5 GeoIPCountryWhois.csv > GeoIPCountry.csv');
  17. shell_exec('cut -d , -f 3-5 GeoIPv6.csv | tr -d " " >> GeoIPCountry.csv');
  18. if (($fd = fopen('GeoIPCountry.csv', 'r')) !== false) {
  19. $DB->query("TRUNCATE TABLE geoip_country");
  20. $Values = array();
  21. $Count = 0;
  22. while (($Data = fgetcsv($fd)) !== false) {
  23. list($StartIP, $EndIP, $CountryID) = $Data;
  24. $Values[] = "($StartIP, $EndIP, '$CountryID')";
  25. $Count++;
  26. if ($Count % $LIMIT == 0) {
  27. $DB->query("
  28. INSERT INTO geoip_country (StartIP, EndIP, Code)
  29. VALUES ".implode(', ', $Values));
  30. $Values = array();
  31. }
  32. }
  33. if (count($Values) > 0) {
  34. $DB->query("
  35. INSERT INTO geoip_country (StartIP, EndIP, Code)
  36. VALUES ".implode(', ', $Values));
  37. }
  38. echo 'GeoIP_Country: There are '.($Count+count($Values)).' entries <br />';
  39. } else {
  40. echo 'Country Error';
  41. }
  42. shell_exec('rm GeoIPCountryCSV.zip GeoIPv6.csv.gz GeoIPCountryWhois.csv GeoIPv6.csv GeoIPCountry.csv');
  43. // ASN (v4) section
  44. shell_exec('wget http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum2.zip');
  45. shell_exec('unzip GeoIPASNum2.zip');
  46. if (($fd = fopen('GeoIPASNum2.csv', 'r')) !== false) {
  47. $DB->query("TRUNCATE TABLE geoip_asn");
  48. $Values = array();
  49. $Count = 0;
  50. while (($Data = fgetcsv($fd)) !== false) {
  51. list($StartIP, $EndIP, $ASN) = $Data;
  52. $ASN = substr($ASN, 2, strpos($ASN, ' ') ? strpos($ASN, ' ')-2 : strlen($ASN)-2);
  53. $Values[] = "(INET6_ATON(INET_NTOA($StartIP)), INET6_ATON(INET_NTOA($EndIP)), $ASN)";
  54. $Count++;
  55. if ($Count % $LIMIT == 0) {
  56. $DB->query("
  57. INSERT INTO geoip_asn (StartIP, EndIP, ASN)
  58. VALUES ".implode(', ', $Values));
  59. $Values = array();
  60. }
  61. }
  62. if (count($Values) > 0) {
  63. $DB->query("
  64. INSERT INTO geoip_asn (StartIP, EndIP, ASN)
  65. VALUES ".implode(', ', $Values));
  66. }
  67. echo 'GeoIP_ASN (v4): There are '.($Count+count($Values)).' entries <br />';
  68. } else {
  69. echo 'ASNv4 Error';
  70. }
  71. shell_exec('rm GeoIPASNum2.zip GeoIPASNum2.csv');
  72. // ASN (v6) section
  73. shell_exec('wget http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum2v6.zip');
  74. shell_exec('unzip GeoIPASNum2v6.zip');
  75. if (($fd = fopen('GeoIPASNum2v6.csv', 'r')) !== false) {
  76. $Values = array();
  77. $Count = 0;
  78. while (($Data = fgetcsv($fd)) !== false) {
  79. list($ASN, $StartIP, $EndIP) = $Data;
  80. $ASN = substr($ASN, 2, strpos($ASN, ' ') ? strpos($ASN, ' ')-2 : strlen($ASN)-2);
  81. $Values[] = "(INET6_ATON('$StartIP'), INET6_ATON('$EndIP'), $ASN)";
  82. $Count++;
  83. if ($Count % $LIMIT == 0) {
  84. $DB->query("
  85. INSERT INTO geoip_asn (StartIP, EndIP, ASN)
  86. VALUES ".implode(', ', $Values));
  87. $Values = array();
  88. }
  89. }
  90. if (count($Values) > 0) {
  91. $DB->query("
  92. INSERT INTO geoip_asn (StartIP, EndIP, ASN)
  93. VALUES ".implode(', ', $Values));
  94. }
  95. echo 'GeoIP_ASN (v6): There are '.($Count+count($Values)).' entries <br />';
  96. } else {
  97. echo 'ASNv6 Error';
  98. }
  99. shell_exec('rm GeoIPASNum2v6.zip GeoIPASNum2v6.tmp GeoIPASNum2v6.csv');
  100. View::show_footer();