Local.php in GeoIP API 8.2
File
src/Plugin/GeoLocator/Local.php
View source
<?php
namespace Drupal\geoip\Plugin\GeoLocator;
use GeoIp2\Database\Reader;
use GeoIp2\Exception\AddressNotFoundException;
use MaxMind\Db\Reader\InvalidDatabaseException;
class Local extends GeoLocatorBase {
const GEOLITE_CITY_DB = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz';
const GEOLITE_COUNTRY_DB = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz';
protected $scheme = 'public';
public function geolocate($ip_address) {
$reader = $this
->getReader();
if (!$reader) {
return NULL;
}
try {
$record = $reader
->country($ip_address);
if ($this->geoIpConfig
->get('debug')) {
$this->logger
->notice($this
->t('Discovered %ip_address in the Maxmind local database', [
'%ip_address' => $ip_address,
]));
}
return $record->country->isoCode;
} catch (AddressNotFoundException $e) {
if ($this->geoIpConfig
->get('debug')) {
$this->logger
->notice($this
->t('Unable to look up %ip_address in the Maxmind local database', [
'%ip_address' => $ip_address,
]));
}
return NULL;
} catch (InvalidDatabaseException $e) {
$this->logger
->error($this
->t('The Maxmind database reader reported an invalid or corrupt database.'));
return NULL;
}
}
protected function getReader() {
$city_uri = $this
->getScheme() . '://GeoLite2-City.mmdb';
$country_uri = $this
->getScheme() . '://GeoLite2-Country.mmdb';
if (file_exists($city_uri)) {
$reader = new Reader($city_uri);
}
elseif (file_exists($country_uri)) {
$reader = new Reader($country_uri);
}
else {
return NULL;
}
return $reader;
}
public function getScheme() {
return $this->scheme;
}
}
Classes
Name |
Description |
Local |
Local geolocation provider. |