Last active
December 2, 2020 10:04
-
-
Save mhdhamouday/d92f397fc747cb302453b948a5db4893 to your computer and use it in GitHub Desktop.
autoupdate Maxmind (geoip) database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// To use it update $license_key, $db_filenamea and $dist_db_path then call: | |
\geoip_db::update(); | |
/** | |
* Update Maxmind DB | |
* by Mohamad Hamouday | |
*/ | |
class geoip_db | |
{ | |
static private $license_key = "YOUR License_Key"; | |
static private $db_filename = "GeoLite2-Country.mmdb"; | |
static private $dist_db_path = __DIR__ . "/database/"; | |
static public function update() | |
{ | |
$min_db_size = 100000; | |
$tmp_folder_name = uniqid(); | |
$temp_folder = __DIR__ . "/{$tmp_folder_name}"; | |
$tar_path = $temp_folder . "/geoip.tar.gz"; | |
self::create_full_directory($temp_folder); | |
self::create_full_directory(self::$dist_db_path); | |
$result = self::download_remote_file("https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=". self::$license_key ."&suffix=tar.gz", $tar_path); | |
if($result) | |
{ | |
$result = self::extract_tar_file($tar_path, $temp_folder); | |
if($result) | |
{ | |
$files = preg_grep('/^GeoLite2/', scandir($temp_folder)); | |
if($files) | |
{ | |
$db_folder_name = end($files); | |
if($db_folder_name) | |
{ | |
$old_db_path = "{$temp_folder}/{$db_folder_name}/". self::$db_filename; | |
if(file_exists($old_db_path) && filesize($old_db_path) > $min_db_size) | |
{ | |
if(rename($old_db_path, self::$dist_db_path . "/". self::$db_filename)) | |
{ | |
self::delete_directory($temp_folder); | |
return true; | |
} | |
} | |
} | |
} | |
} | |
else | |
{ | |
throw new \Exception("Can't extract maxmind db file"); | |
} | |
} | |
else | |
{ | |
throw new \Exception("Can't download maxmind db"); | |
} | |
return false; | |
} | |
static private function download_remote_file($remote_file, $dist) | |
{ | |
$file_size = file_put_contents($dist, fopen($remote_file, 'r')); | |
if($file_size){ | |
return true; | |
} | |
return false; | |
} | |
static private function extract_tar_file($file_name, $dist) | |
{ | |
if(file_exists($file_name)) | |
{ | |
$result = self::create_full_directory($dist); | |
if(file_exists($file_name)) | |
{ | |
try{ | |
$phar = new \PharData($file_name); | |
$phar->extractTo($dist, null, true); // extract all files, and overwrite | |
} | |
catch (\Throwable $e) | |
{ | |
throw new \Exception("Can't extract tar file",['err' => \logging::get_error_info($e)]); | |
return false; | |
} | |
if(file_exists($dist)){ | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
static private function create_full_directory($path) | |
{ | |
if (is_dir($path)) return true; | |
if(!file_exists($path)) | |
{ | |
$pos = strrpos($path, '/', -2); | |
if($pos === false){ | |
$path = "./$path"; | |
$pos = strrpos($path, '/', -2); | |
} | |
$prev_path = substr($path, 0, $pos + 1 ); | |
$return = self::create_full_directory($prev_path); | |
if($return && is_writable($prev_path)) | |
{ | |
if(!file_exists($path)) | |
{ | |
return mkdir($path); | |
} | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
static private function delete_directory($dir) | |
{ | |
if(is_dir($dir)) | |
{ | |
$files = glob( $dir . '*', GLOB_MARK ); //GLOB_MARK adds a slash to directories returned | |
foreach( $files as $file ) | |
{ | |
self::delete_directory( $file ); | |
} | |
if(file_exists($dir)) | |
{ | |
rmdir($dir); | |
} | |
} | |
elseif(is_file($dir)) | |
{ | |
unlink( $dir ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment