You are here

function geoip_requirements in GeoIP API 8.2

Same name and namespace in other branches
  1. 5 geoip.module \geoip_requirements()
  2. 6 geoip.module \geoip_requirements()
  3. 7.2 geoip.install \geoip_requirements()
  4. 7 geoip.module \geoip_requirements()

Implements hook_requirements().

1 call to geoip_requirements()
RequirementsTest::testRequirements in tests/src/Kernel/RequirementsTest.php
Tests the requirements.

File

./geoip.install, line 10
Installation and update functions.

Code

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;
}