You are here

geoip.install in GeoIP API 8.2

Same filename and directory in other branches
  1. 6 geoip.install
  2. 7.2 geoip.install
  3. 7 geoip.install

Installation and update functions.

File

geoip.install
View source
<?php

/**
 * @file
 * Installation and update functions.
 */

/**
 * Implements hook_requirements().
 */
function geoip_requirements($phase) {
  $requirements = [];

  // Test for a valid GeoIP database.
  $requirements['geoip_local_database'] = [
    'title' => t('GeoIP Local Database'),
  ];
  if ($phase === 'install') {
    if (!class_exists('\\GeoIp2\\Database\\Reader')) {
      $requirements['geoip_local_database'] += [
        'description' => t("GeoIP requires the geoip2/geoip2 library."),
        'severity' => REQUIREMENT_ERROR,
      ];
    }
  }
  if ($phase === 'runtime') {

    /** @var \Drupal\Core\Plugin\DefaultPluginManager $geolocator_manager */
    $geolocator_manager = \Drupal::service('plugin.manager.geolocator');

    /** @var \Drupal\geoip\Plugin\GeoLocator\Local $local_geolocator */
    $local_geolocator = $geolocator_manager
      ->createInstance('local');
    $city_uri = $local_geolocator
      ->getScheme() . '://GeoLite2-City.mmdb';
    $country_uri = $local_geolocator
      ->getScheme() . '://GeoLite2-Country.mmdb';
    if (file_exists($city_uri)) {
      $requirements['geoip_local_database'] += [
        'description' => t('GeoIP local database plugin will use the Maxmind City GeoIP database.'),
        'severity' => REQUIREMENT_OK,
      ];
      $active_uri = $city_uri;
    }
    elseif (file_exists($country_uri)) {
      $requirements['geoip_local_database'] += [
        'description' => t('GeoIP local database plugin will use the Maxmind Country GeoIP database.'),
        'severity' => REQUIREMENT_OK,
      ];
      $active_uri = $country_uri;
    }
    else {
      $requirements['geoip_local_database'] += [
        'description' => t('GeoIP local database plugin could not find a Maxmind GeoIP database.'),
        'severity' => REQUIREMENT_WARNING,
      ];

      // Return requirements, since we can't report on the age of the database.
      return $requirements;
    }
    $requirements['geoip_local_database_age'] = [
      'title' => t('GeoIP Local Database Age'),
    ];
    $mtime = filemtime($active_uri);
    if ($mtime <= strtotime('1 months ago')) {
      $database_link = 'http://dev.maxmind.com/geoip/geoip2/geolite2/#Downloads';
      $requirements['geoip_local_database_age']['value'] = t('Out of date!');
      $requirements['geoip_local_database_age']['description'] = t('The GeoIP database file is more than a month old. Download the latest file at <a href="@url">Maxmind.com</a>.', [
        '@url' => $database_link,
      ]);
      $requirements['geoip_local_database_age']['severity'] = REQUIREMENT_WARNING;
    }
    else {
      $requirements['geoip_local_database_age']['value'] = t('Installed and up to date.');
      $requirements['geoip_local_database_age']['severity'] = REQUIREMENT_OK;
    }
  }
  return $requirements;
}

Functions

Namesort descending Description
geoip_requirements Implements hook_requirements().