You are here

public static function DatabaseFileUtility::downloadDatabaseFile in Smart IP 8.3

Same name in this branch
  1. 8.3 modules/smart_ip_maxmind_geoip2_bin_db/src/DatabaseFileUtility.php \Drupal\smart_ip_maxmind_geoip2_bin_db\DatabaseFileUtility::downloadDatabaseFile()
  2. 8.3 modules/smart_ip_ip2location_bin_db/src/DatabaseFileUtility.php \Drupal\smart_ip_ip2location_bin_db\DatabaseFileUtility::downloadDatabaseFile()
Same name and namespace in other branches
  1. 8.4 modules/smart_ip_ip2location_bin_db/src/DatabaseFileUtility.php \Drupal\smart_ip_ip2location_bin_db\DatabaseFileUtility::downloadDatabaseFile()

Download IP2Location binary database file and extract it. Only perform this action when the database is out of date or under specific direction.

Parameters

int $ipVersion: The IP address version: 4 or 6.

2 calls to DatabaseFileUtility::downloadDatabaseFile()
SmartIpEventSubscriber::cronRun in modules/smart_ip_ip2location_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php
Act on Drupal cron run.
SmartIpEventSubscriber::manualUpdate in modules/smart_ip_ip2location_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php
Act on manual database update.

File

modules/smart_ip_ip2location_bin_db/src/DatabaseFileUtility.php, line 63
Contains \Drupal\smart_ip_ip2location_bin_db\DatabaseFileUtility.

Class

DatabaseFileUtility
Utility methods class wrapper.

Namespace

Drupal\smart_ip_ip2location_bin_db

Code

public static function downloadDatabaseFile($ipVersion) {
  $config = \Drupal::config(SmartIpEventSubscriber::configName());
  $version = $config
    ->get('version');
  $edition = $config
    ->get('edition');
  $sourceId = SmartIpEventSubscriber::sourceId();
  $file = self::getFilename($version, $edition, $ipVersion);
  $urlIpv4 = '';
  $urlIpv6 = '';
  $currentIp = \Drupal::state()
    ->get('smart_ip_ip2location_bin_db.current_ip_version_queue');
  if ($version == Ip2locationBinDb::LINCENSED_VERSION) {
    $token = $config
      ->get('token');
    $urlIpv4 = Ip2locationBinDb::LINCENSED_DL_URL;
    $urlIpv4 .= '?' . UrlHelper::buildQuery([
      'token' => $token,
      'file' => $edition . 'BIN',
    ]);
    $urlIpv6 = Ip2locationBinDb::LINCENSED_DL_URL;
    $urlIpv6 .= '?' . UrlHelper::buildQuery([
      'token' => $token,
      'file' => $edition . 'BINIPV6',
    ]);
  }
  elseif ($version == Ip2locationBinDb::LITE_VERSION) {
    $urlIpv4 = Ip2locationBinDb::LITE_DL_URL;
    $urlIpv4 .= '?' . UrlHelper::buildQuery([
      'db' => $edition,
      'type' => 'bin',
      'version' => Ip2locationBinDb::IPV4_VERSION,
    ]);
    $urlIpv6 = Ip2locationBinDb::LITE_DL_URL;
    $urlIpv6 .= '?' . UrlHelper::buildQuery([
      'db' => $edition,
      'type' => 'bin',
      'version' => Ip2locationBinDb::IPV6_VERSION,
    ]);

    // TODO:
    // The IP2Location lite account needs to be logged in first to be able
    // to download the bin files.
    return;
  }
  if ($currentIp == Ip2locationBinDb::IPV4_VERSION && parent::requestDatabaseFile($urlIpv4, $file, $sourceId)) {

    // Next, update IPv6 IP2Location binary database.
    \Drupal::state()
      ->set('smart_ip_ip2location_bin_db.current_ip_version_queue', Ip2locationBinDb::IPV6_VERSION);
  }
  elseif ($currentIp == Ip2locationBinDb::IPV6_VERSION && parent::requestDatabaseFile($urlIpv6, $file, $sourceId)) {

    // All IPv4 and IPv6 IP2Location binary database files are already updated.
    \Drupal::state()
      ->set('smart_ip_ip2location_bin_db.last_update_time', \Drupal::time()
      ->getRequestTime());
    \Drupal::state()
      ->set('smart_ip_ip2location_bin_db.current_ip_version_queue', NULL);
  }
}