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_maxmind_geoip2_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_maxmind_geoip2_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_maxmind_geoip2_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php, line 47
Contains \Drupal\smart_ip_maxmind_geoip2_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_maxmind_geoip2_bin_db\EventSubscriber

Code

public function processQuery(GetLocationEvent $event) {
  if ($event
    ->getDataSource() == self::sourceId()) {
    $config = \Drupal::config(self::configName());
    $autoUpdate = $config
      ->get('db_auto_update');
    $customPath = $config
      ->get('bin_file_custom_path');
    $version = $config
      ->get('version');
    $edition = $config
      ->get('edition');
    $location = $event
      ->getLocation();
    $ipAddress = $location
      ->get('ipAddress');
    $folder = DatabaseFileUtility::getPath($autoUpdate, $customPath);
    $file = DatabaseFileUtility::getFilename($version, $edition);
    $dbFile = "{$folder}/{$file}";
    if (!file_exists($dbFile)) {
      \Drupal::logger('smart_ip')
        ->error(t('The database file %file does not exist or has been moved.', [
        '%file' => $dbFile,
      ]));
      return;
    }
    if (class_exists('\\MaxMind\\Db\\Reader')) {
      $reader = new \MaxMind\Db\Reader($dbFile);
      $raw = $reader
        ->get($ipAddress);
    }
    else {
      $reader = new \GeoIp2\Database\Reader($dbFile);
      $edition = $config
        ->get('edition');
      if ($edition == MaxmindGeoip2BinDb::COUNTRY_EDITION) {
        $record = $reader
          ->country($ipAddress);
      }
      else {
        $record = $reader
          ->city($ipAddress);
      }
      $raw = $record
        ->jsonSerialize();
    }
    $lang = \Drupal::languageManager()
      ->getCurrentLanguage()
      ->getId();
    if (class_exists('\\MaxMind\\Db\\Reader')) {
      $reader
        ->close();
    }
    if (!isset($raw['country']['names'][$lang])) {

      // The current language is not yet supported by MaxMind, use English as
      // default language.
      $lang = 'en';
    }
    $country = isset($raw['country']['names'][$lang]) ? $raw['country']['names'][$lang] : '';
    $countryCode = isset($raw['country']['iso_code']) ? $raw['country']['iso_code'] : '';
    $region = isset($raw['subdivisions'][0]['names'][$lang]) ? $raw['subdivisions'][0]['names'][$lang] : '';
    $regionCode = isset($raw['subdivisions'][0]['iso_code']) ? $raw['subdivisions'][0]['iso_code'] : '';
    $city = isset($raw['city']['names'][$lang]) ? $raw['city']['names'][$lang] : '';
    $zip = isset($raw['postal']['code']) ? $raw['postal']['code'] : '';
    $latitude = isset($raw['location']['latitude']) ? $raw['location']['latitude'] : '';
    $longitude = isset($raw['location']['longitude']) ? $raw['location']['longitude'] : '';
    $timeZone = isset($raw['location']['time_zone']) ? $raw['location']['time_zone'] : '';
    $isEuCountry = isset($raw['country']['is_in_european_union']) ? $raw['country']['is_in_european_union'] : '';
    $isGdprCountry = isset($record['country']['isGdprCountry']) ? $record['country']['isGdprCountry'] : '';
    $location
      ->set('originalData', $raw)
      ->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);
  }
}