function smart_ip_maxmind_bin_db_update in Smart IP 6.2
Same name and namespace in other branches
- 7.2 includes/smart_ip.utility.inc \smart_ip_maxmind_bin_db_update()
Download a Maxmind binary database and activate it for use
3 calls to smart_ip_maxmind_bin_db_update()
- smart_ip_admin_settings_submit in includes/
smart_ip.admin.inc - Process Forms submitted by IP to Country administration page
- smart_ip_cron in ./
smart_ip.module - Implements hook_cron().
- _smart_ip_bin_database_update_submit in includes/
smart_ip.admin.inc - Submit handler to update the Maxmind binary file.
File
- includes/
smart_ip.utility.inc, line 398 - Utility routines to load the Smart IP database.
Code
function smart_ip_maxmind_bin_db_update($license = '', $cron_run = FALSE, $force = FALSE) {
$data_source = variable_get('smart_ip_source', 'ipinfodb_service');
if ($data_source == 'maxmind_bin') {
$license = !empty($license) ? $license : variable_get('smart_ip_maxmind_license', '');
$version = variable_get('smart_ip_maxmind_bin_version', SMART_IP_MAXMIND_BIN_LICENSED_VERSION);
$edition = variable_get('smart_ip_maxmind_bin_edition', SMART_IP_MAXMIND_BIN_EDITION_CITY);
$filename = smart_ip_get_bin_source_filename($version, $edition);
$last_update_time = strtotime('midnight', variable_get('smart_ip_maxmind_bin_db_last_update', 0));
}
elseif ($data_source == 'maxmindgeoip2_bin') {
$user_account = variable_get('smart_ip_maxmind_geoip2_bin_user_account', '');
$license = !empty($license) ? $license : variable_get('smart_ip_maxmind_geoip2_license', '');
$version = variable_get('smart_ip_maxmind_geoip2_bin_version', SMART_IP_MAXMIND_BIN_LICENSED_VERSION);
$edition = variable_get('smart_ip_maxmind_geoip2_bin_edition', SMART_IP_MAXMIND_GEOIP2_BIN_EDITION_CITY);
$filename = smart_ip_get_geoip2_bin_source_filename($version, $edition);
$last_update_time = strtotime('midnight', variable_get('smart_ip_maxmind_geoip2_bin_db_last_update', 0));
}
// Only update the binary database when it is out of date or under specific direction
if (!$force && smart_ip_maxmind_bin_db_up_to_date($version, $last_update_time)) {
return FALSE;
}
$path = file_directory_path() . '/smart_ip';
if (!file_check_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
// Private file system path not defined then stop the process
drupal_set_message(t('File directory path is not writable, please check !here.', array(
'!here' => l('here', 'admin/settings/file-system'),
)), 'error');
return FALSE;
}
if ($version == SMART_IP_MAXMIND_BIN_LICENSED_VERSION) {
$url = SMART_IP_MAXMIND_BIN_DOWNLOAD_BASE_URL . '?' . http_build_query(array(
'edition_id' => $edition,
'suffix' => 'tar.gz',
'license_key' => $license,
));
$gz_name = "{$path}/{$filename}.tar.gz";
}
else {
if ($data_source == 'maxmind_bin') {
if ($edition == SMART_IP_MAXMIND_BIN_EDITION_COUNTRY) {
$url = SMART_IP_MAXMIND_LITE_BIN_DOWNLOAD_BASE_URL . "/GeoLiteCountry/{$filename}.dat.gz";
}
else {
$url = SMART_IP_MAXMIND_LITE_BIN_DOWNLOAD_BASE_URL . "/{$filename}.dat.gz";
}
$gz_name = "{$path}/{$filename}.dat.gz";
}
elseif ($data_source == 'maxmindgeoip2_bin') {
if ($edition == SMART_IP_MAXMIND_GEOIP2_BIN_EDITION_CITY) {
$edition = SMART_IP_MAXMIND_GEOIP2_BIN_LITE_EDITION_CITY;
}
else {
$edition = SMART_IP_MAXMIND_GEOIP2_BIN_LITE_EDITION_COUNTRY;
}
$url = SMART_IP_MAXMIND_GEOIP2_LITE_BIN_DOWNLOAD_BASE_URL . '?' . http_build_query(array(
'account_id' => $user_account,
'edition_id' => $edition,
'suffix' => 'tar.gz',
'license_key' => $license,
));
$gz_name = "{$path}/{$filename}.gz";
}
}
$fp = @fopen($url, 'r');
if ($fp === FALSE || file_put_contents($gz_name, $fp) === FALSE) {
if ($data_source == 'maxmind_bin') {
variable_set('smart_ip_maxmind_bin_db_update_error', t('MaxMind GeoIP Legacy binary database download failed'));
watchdog('smart_ip', 'MaxMind GeoIP Legacy bin database download failed', array(), WATCHDOG_WARNING);
}
elseif ($data_source == 'maxmindgeoip2_bin') {
variable_set('smart_ip_maxmind_geoip2_bin_db_update_error', t('MaxMind GeoIP2 binary database download failed'));
watchdog('smart_ip', 'MaxMind GeoIP2 bin database download failed', array(), WATCHDOG_WARNING);
}
return FALSE;
}
try {
if (class_exists('Archive_Tar')) {
$p = new Archive_Tar(drupal_realpath($gz_name));
$p
->extract(drupal_realpath("{$path}/db_tmp"));
}
elseif (class_exists('PharData')) {
$p = new PharData($gz_name);
$p
->extractTo($path . '/db_tmp');
}
else {
throw new Exception('Server does not have Phar extension installed');
}
} catch (Exception $e) {
mkdir("{$path}/db_tmp", 0775);
$source_fp = gzopen($gz_name, 'rb');
if ($data_source == 'maxmind_bin') {
$target_fp = fopen("{$path}/db_tmp/{$filename}.dat", 'w');
}
elseif ($data_source == 'maxmindgeoip2_bin') {
$target_fp = fopen("{$path}/db_tmp/{$filename}", 'w');
}
while (!gzeof($source_fp)) {
$string = gzread($source_fp, 4096);
fwrite($target_fp, $string, strlen($string));
}
gzclose($source_fp);
fclose($target_fp);
}
try {
if ($data_source == 'maxmind_bin') {
$files = file_scan_directory("{$path}/db_tmp", "/^{$filename}.*\\.dat\$/");
$target_filename = "{$filename}.dat";
}
elseif ($data_source == 'maxmindgeoip2_bin') {
$files = file_scan_directory("{$path}/db_tmp", "/^{$filename}\$/");
$target_filename = "{$filename}";
}
if (empty($files) || count($files) !== 1) {
throw new Exception("Unable to determine the contents of the db_tmp directory ({$path}/db_tmp)");
}
foreach ($files as $file) {
if (rename($file->filename, "{$path}/{$target_filename}") === FALSE) {
throw new Exception('Failed to activate the downloaded database');
}
}
} catch (Exception $e) {
if ($data_source == 'maxmind_bin') {
$msg = t('Error during MaxMind GeoIP Legacy binary database download/extraction: %error', array(
'%error' => $e
->getMessage(),
));
variable_set('smart_ip_maxmind_bin_db_update_error', $msg);
}
elseif ($data_source == 'maxmindgeoip2_bin') {
$msg = t('Error during MaxMind GeoIP2 binary database download/extraction: %error', array(
'%error' => $e
->getMessage(),
));
variable_set('smart_ip_maxmind_geoip2_bin_db_update_error', $msg);
}
watchdog('smart_ip', $msg, array(), WATCHDOG_WARNING);
return FALSE;
}
if ($data_source == 'maxmind_bin') {
variable_set('smart_ip_maxmind_bin_db_update_error', NULL);
variable_set('smart_ip_maxmind_bin_db_last_update', (int) $_SERVER['REQUEST_TIME']);
if ($cron_run) {
watchdog('smart_ip', 'The MaxMind GeoIP Legacy binary database has been updated via cron.');
}
else {
watchdog('smart_ip', 'The MaxMind GeoIP Legacy binary database has been manually updated.');
}
}
elseif ($data_source == 'maxmindgeoip2_bin') {
variable_set('smart_ip_maxmind_geoip2_bin_db_update_error', NULL);
variable_set('smart_ip_maxmind_geoip2_bin_db_last_update', (int) $_SERVER['REQUEST_TIME']);
if ($cron_run) {
watchdog('smart_ip', 'The MaxMind GeoIP2 binary database has been updated via cron.');
}
else {
watchdog('smart_ip', 'The MaxMind GeoIP2 binary database has been manually updated.');
}
}
file_delete($gz_name);
rmdir_recurse("{$path}/db_tmp");
return TRUE;
}