You are here

public function SmartIpEventSubscriber::processQuery in Smart IP 8.3

Same name in this branch
  1. 8.3 modules/device_geolocation/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\device_geolocation\EventSubscriber\SmartIpEventSubscriber::processQuery()
  2. 8.3 modules/smart_ip_maxmind_geoip2_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_maxmind_geoip2_bin_db\EventSubscriber\SmartIpEventSubscriber::processQuery()
  3. 8.3 modules/smart_ip_ipinfodb_web_service/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_ipinfodb_web_service\EventSubscriber\SmartIpEventSubscriber::processQuery()
  4. 8.3 modules/smart_ip_ip2location_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_ip2location_bin_db\EventSubscriber\SmartIpEventSubscriber::processQuery()
  5. 8.3 modules/smart_ip_abstract_web_service/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_abstract_web_service\EventSubscriber\SmartIpEventSubscriber::processQuery()
  6. 8.3 modules/smart_ip_maxmind_geoip2_web_service/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_maxmind_geoip2_web_service\EventSubscriber\SmartIpEventSubscriber::processQuery()
Same name and namespace in other branches
  1. 8.4 modules/smart_ip_ip2location_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_ip2location_bin_db\EventSubscriber\SmartIpEventSubscriber::processQuery()

Act on \Drupal\smart_ip\SmartIp::query() when executed and if selected as Smart IP data source, query the IP address against its database.

Parameters

\Drupal\smart_ip\GetLocationEvent $event: Smart IP query location override event for event listeners.

Overrides SmartIpDataSourceInterface::processQuery

File

modules/smart_ip_ip2location_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php, line 46
Contains \Drupal\smart_ip_ip2location_bin_db\EventSubscriber\SmartIpEventSubscriber.

Class

SmartIpEventSubscriber
Core functionality of this Smart IP data source module. Listens to Smart IP override events.

Namespace

Drupal\smart_ip_ip2location_bin_db\EventSubscriber

Code

public function processQuery(GetLocationEvent $event) {
  if ($event
    ->getDataSource() == self::sourceId()) {
    $config = \Drupal::config(self::configName());
    $version = $config
      ->get('version');
    $edition = $config
      ->get('edition');
    $autoUpdate = $config
      ->get('db_auto_update');
    $customPath = $config
      ->get('bin_file_custom_path');
    $folder = DatabaseFileUtility::getPath($autoUpdate, $customPath);
    $location = $event
      ->getLocation();
    $ipAddress = $location
      ->get('ipAddress');
    $ipVersion = $location
      ->get('ipVersion');
    $file = DatabaseFileUtility::getFilename($version, $edition, $ipVersion);
    $dbFile = "{$folder}/{$file}";
    $error = $this
      ->checkBinFile($dbFile, $ipVersion);
    if ($error['code']) {
      \Drupal::logger('smart_ip')
        ->error(t('The database file %file does not exist or has been moved.', [
        '%file' => $dbFile,
      ]));
      return;
    }
    if ($config
      ->get('caching_method') == Ip2locationBinDb::NO_CACHE) {
      $cachingMethod = \IP2Location\Database::FILE_IO;
    }
    elseif ($config
      ->get('caching_method') == Ip2locationBinDb::MEMORY_CACHE) {
      $cachingMethod = \IP2Location\Database::MEMORY_CACHE;
    }
    elseif ($config
      ->get('caching_method') == Ip2locationBinDb::SHARED_MEMORY) {
      $cachingMethod = \IP2Location\Database::SHARED_MEMORY;
    }
    if (!empty($dbFile) && !empty($cachingMethod)) {
      $reader = new \IP2Location\Database($dbFile, $cachingMethod);
      $record = $reader
        ->lookup($ipAddress, \IP2Location\Database::ALL);
      foreach ($record as &$item) {
        if (strpos($item, 'Please upgrade') !== FALSE || strpos($item, 'Invalid IP address') !== FALSE || $item == '-') {

          // Make the value "This parameter is unavailable in selected .BIN
          // data file. Please upgrade." or "Invalid IP address" or "-" as
          // NULL.
          $item = NULL;
        }
      }
      $country = isset($record['countryName']) ? $record['countryName'] : '';
      $countryCode = isset($record['countryCode']) ? $record['countryCode'] : '';
      $region = isset($record['regionName']) ? $record['regionName'] : '';
      $regionCode = isset($record['regionCode']) ? $record['regionCode'] : '';
      $city = isset($record['cityName']) ? $record['cityName'] : '';
      $zip = isset($record['zipCode']) ? $record['zipCode'] : '';
      $latitude = isset($record['latitude']) ? $record['latitude'] : '';
      $longitude = isset($record['longitude']) ? $record['longitude'] : '';
      $timeZone = isset($record['timeZone']) ? $record['timeZone'] : '';
      $isEuCountry = isset($record['isEuCountry']) ? $record['isEuCountry'] : '';
      $isGdprCountry = isset($record['isGdprCountry']) ? $record['isGdprCountry'] : '';
      $location
        ->set('originalData', $record)
        ->set('country', $country)
        ->set('countryCode', Unicode::strtoupper($countryCode))
        ->set('region', $region)
        ->set('regionCode', $regionCode)
        ->set('city', $city)
        ->set('zip', $zip)
        ->set('latitude', $latitude)
        ->set('longitude', $longitude)
        ->set('timeZone', $timeZone)
        ->set('isEuCountry', $isEuCountry)
        ->set('isGdprCountry', $isGdprCountry);
    }
  }
}