You are here

class DatabaseFileUtility 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
  2. 8.3 modules/smart_ip_ip2location_bin_db/src/DatabaseFileUtility.php \Drupal\smart_ip_ip2location_bin_db\DatabaseFileUtility
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

Utility methods class wrapper.

@package Drupal\smart_ip_ip2location_bin_db

Hierarchy

Expanded class hierarchy of DatabaseFileUtility

1 file declares its use of DatabaseFileUtility
SmartIpEventSubscriber.php in modules/smart_ip_ip2location_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php
Contains \Drupal\smart_ip_ip2location_bin_db\EventSubscriber\SmartIpEventSubscriber.

File

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

Namespace

Drupal\smart_ip_ip2location_bin_db
View source
class DatabaseFileUtility extends DatabaseFileUtilityBase {

  /**
   * Get IP2Location binary database filename.
   *
   * @param string $version
   *   Type of subscription: licensed or lite version.
   * @param string $edition
   *   IP2Location product code.
   * @param string $ipVersion
   *   IP version: IPv4 or IPv6.
   * @return string
   *   Returns the IP2Location binary database filename.
   */
  public static function getFilename($version = Ip2locationBinDb::LITE_VERSION, $edition = Ip2locationBinDb::DEFAULT_EDITION, $ipVersion = Ip2locationBinDb::IPV4_VERSION) {
    if ($version == Ip2locationBinDb::LINCENSED_VERSION) {
      $productName = Ip2locationBinDb::products($version, $edition);
      if ($ipVersion == Ip2locationBinDb::IPV6_VERSION) {
        $filename = 'IPV6-' . Unicode::strtoupper($productName) . '.BIN';
      }
      else {
        $filename = 'IP-' . Unicode::strtoupper($productName) . '.BIN';
      }
    }
    else {
      if ($ipVersion == Ip2locationBinDb::IPV6_VERSION) {
        $filename = "IP2LOCATION-LITE-{$edition}.IPV6.BIN";
      }
      else {
        $filename = "IP2LOCATION-LITE-{$edition}.BIN";
      }
    }
    return $filename;
  }

  /**
   * Download IP2Location binary database file and extract it.
   * Only perform this action when the database is out of date or under specific
   * direction.
   * @param int $ipVersion
   *   The IP address version: 4 or 6.
   */
  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);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DatabaseFileUtility::downloadDatabaseFile public static function Download IP2Location binary database file and extract it. Only perform this action when the database is out of date or under specific direction.
DatabaseFileUtility::getFilename public static function Get IP2Location binary database filename. Overrides DatabaseFileUtilityInterface::getFilename
DatabaseFileUtilityBase::DOWNLOAD_MONTHLY constant Download Smart IP's data source module's database file monthly.
DatabaseFileUtilityBase::DOWNLOAD_TIMEOUT constant Download Smart IP's data source module's database timout.
DatabaseFileUtilityBase::DOWNLOAD_WEEKLY constant Download Smart IP's data source module's database file weekly.
DatabaseFileUtilityBase::DRUPAL_FOLDER constant Fixed Drupal folder path of Smart IP data source module's database file is stored.
DatabaseFileUtilityBase::DRUPAL_TEMP_FOLDER constant Drupal temporary folder path.
DatabaseFileUtilityBase::getPath public static function Get Smart IP's data source module's database file's path. This should return the fixed Drupal folder if auto update is on or if custom path is empty with auto update off. Overrides DatabaseFileUtilityInterface::getPath
DatabaseFileUtilityBase::needsUpdate public static function Checks if Smart IP's data source module's database file needs update. Overrides DatabaseFileUtilityInterface::needsUpdate
DatabaseFileUtilityBase::requestDatabaseFile protected static function Download Smart IP's data source module's database file and extract it.