diff options
author | Stefan Giehl <stefan@matomo.org> | 2022-06-28 10:33:00 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-28 10:33:00 +0300 |
commit | d1e15c23dc07f87fb959a29013a519898302b182 (patch) | |
tree | e4f68856e398dbf425bea7061c13f0fde1b99797 /plugins/GeoIp2 | |
parent | df9b36323662b0865380739ea6da50e4e595f02c (diff) |
Try to handle incorrectly prefixed region codes (#19327)
* Try to handle incorrectly prefixed region codes
* Adds test for prefix region codes
Diffstat (limited to 'plugins/GeoIp2')
-rw-r--r-- | plugins/GeoIp2/LocationProvider/GeoIp2/Php.php | 11 | ||||
-rw-r--r-- | plugins/GeoIp2/tests/Integration/LocationProviderTest.php | 20 |
2 files changed, 30 insertions, 1 deletions
diff --git a/plugins/GeoIp2/LocationProvider/GeoIp2/Php.php b/plugins/GeoIp2/LocationProvider/GeoIp2/Php.php index 44423661e8..cf10439ab4 100644 --- a/plugins/GeoIp2/LocationProvider/GeoIp2/Php.php +++ b/plugins/GeoIp2/LocationProvider/GeoIp2/Php.php @@ -251,7 +251,16 @@ class Php extends GeoIp2 if (is_array($lookupResult->subdivisions) && count($lookupResult->subdivisions) > 0) { $subdivisions = $lookupResult->subdivisions; $subdivision = $this->determinSubdivision($subdivisions, $result[self::COUNTRY_CODE_KEY]); - $result[self::REGION_CODE_KEY] = $subdivision->isoCode ? strtoupper($subdivision->isoCode) : $this->determineRegionIsoCodeByNameAndCountryCode($subdivision->name, $result[self::COUNTRY_CODE_KEY]); + $subdivisionIsoCode = $subdivision->isoCode ? strtoupper($subdivision->isoCode) : ''; + + // In some cases the region code might be returned including the country code + // e.g. AE-DU instead of only DU. In that case we remove the prefix + // see https://github.com/matomo-org/matomo/issues/19323 + if (0 === strpos($subdivisionIsoCode, $result[self::COUNTRY_CODE_KEY] . '-')) { + $subdivisionIsoCode = substr($subdivisionIsoCode, strlen($result[self::COUNTRY_CODE_KEY]) + 1); + } + + $result[self::REGION_CODE_KEY] = $subdivisionIsoCode ? : $this->determineRegionIsoCodeByNameAndCountryCode($subdivision->name, $result[self::COUNTRY_CODE_KEY]); $result[self::REGION_NAME_KEY] = $subdivision->name; } } diff --git a/plugins/GeoIp2/tests/Integration/LocationProviderTest.php b/plugins/GeoIp2/tests/Integration/LocationProviderTest.php index ea1fc20927..3798092694 100644 --- a/plugins/GeoIp2/tests/Integration/LocationProviderTest.php +++ b/plugins/GeoIp2/tests/Integration/LocationProviderTest.php @@ -58,6 +58,26 @@ class LocationProviderTest extends \PHPUnit\Framework\TestCase ], $result); } + public function testGeoIP2CityWithIncorrectlyPrefixedRegionIsoCode() + { + // The IP 88.88.88.88 will return a region code that is prefixed with the country code, e.g. US-NJ instead of NJ + $locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-City.mmdb'], 'isp' => []]); + $result = $locationProvider->getLocation(['ip' => '88.88.88.88']); + + $this->assertEquals([ + 'continent_name' => 'North America', + 'continent_code' => 'NA', + 'country_code' => 'US', + 'country_name' => 'United States', + 'city_name' => 'Englewood Cliffs', + 'lat' => 40.892, + 'long' => -73.947, + 'postal_code' => null, + 'region_code' => 'NJ', + 'region_name' => 'New Jersey', + ], $result); + } + public function testGeoIP2Country() { $locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-Country.mmdb'], 'isp' => []]); |