| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Plugins\GeoIp2\LocationProvider; |
|
|
| use Exception; |
| use Piwik\Container\StaticContainer; |
| use Piwik\Date; |
| use Piwik\Option; |
| use Piwik\Piwik; |
| use Piwik\Plugins\UserCountry\LocationProvider; |
|
|
| |
| |
| |
| |
| abstract class GeoIp2 extends LocationProvider |
| { |
| public const TEST_IP = '194.57.91.215'; |
| public const SWITCH_TO_ISO_REGIONS_OPTION_NAME = 'usercountry.switchtoisoregions'; |
|
|
| |
| |
| |
| |
| |
| private static $regionNames = null; |
|
|
| |
| |
| |
| |
| |
| |
| public static $dbNames = array( |
| 'loc' => array('GeoIP2-City.mmdb', 'DBIP-City.mmdb', 'DBIP-City-Lite.mmdb', 'DBIP-Country-Lite.mmdb', 'DBIP-Country.mmdb', |
| 'dbip-city-lite-\d{4}-\d{2}.mmdb', 'GeoIP2-City-Africa.mmdb', 'GeoIP2-City-Asia-Pacific.mmdb', 'GeoIP2-City-Europe.mmdb', |
| 'GeoIP2-City-North-America.mmdb', 'GeoIP2-City-South-America.mmdb', 'GeoIP2-Enterprise.mmdb', 'GeoIP2-Country.mmdb', |
| 'dbip-country-lite-\d{4}-\d{2}.mmdb', 'GeoLite2-City.mmdb', 'GeoLite2-Country.mmdb', 'DBIP-Enterprise.mmdb'), |
| 'isp' => array('GeoIP2-ISP.mmdb', 'GeoLite2-ASN.mmdb', 'DBIP-ISP.mmdb', 'GeoIP2-Enterprise.mmdb', 'DBIP-Enterprise.mmdb', |
| 'DBIP-ASN.mmdb', 'dbip-asn-lite-\d{4}-\d{2}.mmdb'), |
| ); |
|
|
| public static function getDbIpLiteUrl($type = 'city') |
| { |
| $today = Date::today(); |
| return "https://download.db-ip.com/free/dbip-{$type}-lite-{$today->toString('Y-m')}.mmdb.gz"; |
| } |
|
|
| |
| |
| |
| |
| |
| public function isWorking() |
| { |
| |
| try { |
| $testIp = self::TEST_IP; |
|
|
| |
| $location = $this->getLocation(array('ip' => $testIp)); |
| $location = $location ? array_filter($location) : $location; |
| $isResultCorrect = !empty($location); |
|
|
| if (!$isResultCorrect) { |
| $bind = array($testIp); |
| return Piwik::translate('UserCountry_TestIPLocatorFailed', $bind); |
| } |
|
|
| return true; |
| } catch (Exception $ex) { |
| return $ex->getMessage(); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public static function getPathToGeoIpDatabase($possibleFileNames) |
| { |
| foreach ($possibleFileNames as $filename) { |
| $path = self::getPathForGeoIpDatabase($filename); |
| if (file_exists($path)) { |
| return $path; |
| } |
| } |
| return false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public static function getPathForGeoIpDatabase($filename) |
| { |
| if (strpos($filename, '/') !== false && file_exists($filename)) { |
| return $filename; |
| } |
|
|
| return StaticContainer::get('path.geoip2') . $filename; |
| } |
|
|
| public function activate() |
| { |
| $option = Option::get(self::SWITCH_TO_ISO_REGIONS_OPTION_NAME); |
| if (empty($option)) { |
| Option::set(self::SWITCH_TO_ISO_REGIONS_OPTION_NAME, time()); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| public static function isDatabaseInstalled() |
| { |
| return self::getPathToGeoIpDatabase(self::$dbNames['loc']) |
| || self::getPathToGeoIpDatabase(self::$dbNames['isp']); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public static function getGeoIPDatabaseTypeFromFilename($filename) |
| { |
| foreach (self::$dbNames as $key => $names) { |
| foreach ($names as $name) { |
| if ($name === $filename || preg_match('/' . $name . '/', $filename)) { |
| return $key; |
| } |
| } |
| } |
| return false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public static function getRegionNameFromCodes($countryCode, $regionCode) |
| { |
| $regionNames = self::getRegions(); |
| $countryCode = strtoupper($countryCode); |
| $regionCode = strtoupper($regionCode); |
|
|
| if (isset($regionNames[$countryCode][$regionCode]['name'])) { |
| return $regionNames[$countryCode][$regionCode]['name']; |
| } else { |
| return Piwik::translate('General_Unknown'); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| public static function getRegionNames() |
| { |
| $regionsByCountry = self::getRegions(); |
|
|
| foreach ($regionsByCountry as $countryCode => &$regions) { |
| foreach ($regions as $regionCode => &$regionData) { |
| $regionData = $regionData['name']; |
| } |
| } |
|
|
| return $regionsByCountry; |
| } |
|
|
| |
| |
| |
| |
| |
| public static function getRegions() |
| { |
| if (is_null(self::$regionNames)) { |
| self::$regionNames = require_once __DIR__ . '/../data/isoRegionNames.php'; |
| } |
|
|
| return self::$regionNames; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public static function convertRegionCodeToIso($countryCode, $fipsRegionCode, $returnOriginalIfNotFound = false) |
| { |
| static $mapping; |
| if (empty($mapping)) { |
| $mapping = include __DIR__ . '/../data/regionMapping.php'; |
| } |
| $countryCode = strtoupper($countryCode); |
| if (empty($countryCode) || in_array($countryCode, ['EU', 'AP', 'A1', 'A2'])) { |
| return ['', '']; |
| } |
| if (in_array($countryCode, ['US', 'CA'])) { |
| return [$countryCode, $fipsRegionCode]; |
| } |
| if ($countryCode == 'TI') { |
| $countryCode = 'CN'; |
| $fipsRegionCode = '14'; |
| } |
| $isoRegionCode = $returnOriginalIfNotFound ? $fipsRegionCode : ''; |
| if (!empty($fipsRegionCode) && !empty($mapping[$countryCode][$fipsRegionCode])) { |
| $isoRegionCode = $mapping[$countryCode][$fipsRegionCode]; |
| } |
| return [$countryCode, $isoRegionCode]; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| protected function getIpFromInfo($info) |
| { |
| $ip = \Matomo\Network\IP::fromStringIP($info['ip']); |
|
|
| return $ip->toString(); |
| } |
|
|
| |
| |
| |
| public function canBeUsedForLocationBasedSecurityChecks(): bool |
| { |
| return true; |
| } |
| } |
|
|