1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
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;
/**
* Base type for all GeoIP 2 LocationProviders.
*
*/
abstract class GeoIp2 extends LocationProvider
{
public const TEST_IP = '194.57.91.215';
public const SWITCH_TO_ISO_REGIONS_OPTION_NAME = 'usercountry.switchtoisoregions';
/**
* Cached region name array. Data is from geoipregionvars.php.
*
* @var array
*/
private static $regionNames = null;
/**
* Stores possible database file names categorized by the type of information
* GeoIP databases hold.
*
* @var array
*/
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";
}
/**
* Returns true if this provider has been setup correctly, the error message if not.
*
* @return bool|string
*/
public function isWorking()
{
// test with an example IP to make sure the provider is working
try {
$testIp = self::TEST_IP;
// get location using test IP and check that some information was returned
$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();
}
}
/**
* Returns the path of an existing GeoIP 2 database or false if none can be found.
*
* @param array $possibleFileNames The list of possible file names for the GeoIP database.
* @return string|false
*/
public static function getPathToGeoIpDatabase($possibleFileNames)
{
foreach ($possibleFileNames as $filename) {
$path = self::getPathForGeoIpDatabase($filename);
if (file_exists($path)) {
return $path;
}
}
return false;
}
/**
* Returns full path for a GeoIP 2 database managed by Piwik.
*
* @param string $filename Name of the .dat file.
* @return string
*/
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());
}
}
/**
* Returns true if there is a GeoIP 2 database in the 'misc' directory.
*
* @return bool
*/
public static function isDatabaseInstalled()
{
return self::getPathToGeoIpDatabase(self::$dbNames['loc'])
|| self::getPathToGeoIpDatabase(self::$dbNames['isp']);
}
/**
* Returns the type of GeoIP 2 database ('loc' or 'isp') based on the
* filename (eg, 'GeoLite2-City.mmdb', 'GeoIP2-ISP.mmdb', etc).
*
* @param string $filename
* @return string|false 'loc', 'isp' or false if cannot find a database type.
*/
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;
}
/**
* Returns a region name for a country code + region code.
*
* @param string $countryCode
* @param string $regionCode
* @return string The region name or 'Unknown' (translated).
*/
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');
}
}
/**
* Returns an array of region names mapped by country code & region code.
*
* @return array
*/
public static function getRegionNames()
{
$regionsByCountry = self::getRegions();
foreach ($regionsByCountry as $countryCode => &$regions) {
foreach ($regions as $regionCode => &$regionData) {
$regionData = $regionData['name'];
}
}
return $regionsByCountry;
}
/**
* Returns an array of region names mapped by country code & region code.
*
* @return array
*/
public static function getRegions()
{
if (is_null(self::$regionNames)) {
self::$regionNames = require_once __DIR__ . '/../data/isoRegionNames.php';
}
return self::$regionNames;
}
/**
* Converts an old FIPS region code to ISO
*
* @param string $countryCode
* @param string $fipsRegionCode
* @param bool $returnOriginalIfNotFound return given region code if no mapping was found
* @return array
*/
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'])) { // US and CA always haven been iso codes
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];
}
/**
* Returns an IP address from an array that was passed into getLocation. This
* will return an IPv4 address or IPv6 address.
*
* @param array $info Must have 'ip' key.
* @return string|null
*/
protected function getIpFromInfo($info)
{
$ip = \Matomo\Network\IP::fromStringIP($info['ip']);
return $ip->toString();
}
/**
* GeoIP2 providers can be used for location-based security checks
*
*/
public function canBeUsedForLocationBasedSecurityChecks(): bool
{
return true;
}
}
|