diff options
Diffstat (limited to 'plugins/UserCountry/LocationProvider/DefaultProvider.php')
-rw-r--r-- | plugins/UserCountry/LocationProvider/DefaultProvider.php | 90 |
1 files changed, 77 insertions, 13 deletions
diff --git a/plugins/UserCountry/LocationProvider/DefaultProvider.php b/plugins/UserCountry/LocationProvider/DefaultProvider.php index 4c426eef6b..c5483122a7 100644 --- a/plugins/UserCountry/LocationProvider/DefaultProvider.php +++ b/plugins/UserCountry/LocationProvider/DefaultProvider.php @@ -1,4 +1,5 @@ <?php + /** * Matomo - free/libre analytics platform * @@ -6,11 +7,18 @@ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later * */ + namespace Piwik\Plugins\UserCountry\LocationProvider; +use Matomo\Network\IP; use Piwik\Common; use Piwik\Config; +use Piwik\Container\StaticContainer; +use Piwik\Intl\Data\Provider\RegionDataProvider; use Piwik\Piwik; +use Piwik\Plugin\Manager; +use Piwik\Plugins\PrivacyManager\Config as PrivacyManagerConfig; +use Piwik\Plugins\Provider\Provider as ProviderProvider; use Piwik\Plugins\UserCountry\LocationProvider; use Piwik\Tracker\TrackerConfig; @@ -32,19 +40,74 @@ class DefaultProvider extends LocationProvider */ public function getLocation($info) { - $enableLanguageToCountryGuess = Config::getInstance()->Tracker['enable_language_to_country_guess']; + $country = $this->getCountryUsingProviderExtensionIfAvailable($info['ip']); - if (empty($info['lang'])) { - $info['lang'] = Common::getBrowserLanguage(); + if (empty($country)) { + $enableLanguageToCountryGuess = Config::getInstance()->Tracker['enable_language_to_country_guess']; + + if (empty($info['lang'])) { + $info['lang'] = Common::getBrowserLanguage(); + } + $country = Common::getCountry($info['lang'], $enableLanguageToCountryGuess, $info['ip']); } - $country = Common::getCountry($info['lang'], $enableLanguageToCountryGuess, $info['ip']); - $location = array(parent::COUNTRY_CODE_KEY => $country); + $location = [parent::COUNTRY_CODE_KEY => $country]; $this->completeLocationResult($location); return $location; } + + private function getCountryUsingProviderExtensionIfAvailable($ipAddress) + { + if ( + !Manager::getInstance()->isPluginInstalled('Provider') + || Common::getRequestVar('dp', 0, 'int') === 1 + ) { + return false; + } + + $privacyConfig = new PrivacyManagerConfig(); + + // when using anonymized ip for enrichment we skip this check + if ($privacyConfig->useAnonymizedIpForVisitEnrichment) { + return false; + } + + $hostname = $this->getHost($ipAddress); + $hostnameExtension = ProviderProvider::getCleanHostname($hostname); + + $hostnameDomain = substr($hostnameExtension, 1 + strrpos($hostnameExtension, '.')); + if ($hostnameDomain == 'uk') { + $hostnameDomain = 'gb'; + } + + /** @var RegionDataProvider $regionDataProvider */ + $regionDataProvider = StaticContainer::get('Piwik\Intl\Data\Provider\RegionDataProvider'); + + if (array_key_exists($hostnameDomain, $regionDataProvider->getCountryList())) { + return $hostnameDomain; + } + + return false; + } + + /** + * Returns the hostname given the IP address string + * + * @param string $ipStr IP Address + * @return string hostname (or human-readable IP address) + */ + protected function getHost($ipStr) + { + $ip = IP::fromStringIP($ipStr); + + $host = $ip->getHostname(); + $host = ($host === null ? $ipStr : $host); + + return trim(strtolower($host)); + } + /** * Returns whether this location provider is available. * @@ -92,10 +155,10 @@ class DefaultProvider extends LocationProvider */ public function getSupportedLocationInfo() { - return array(self::CONTINENT_CODE_KEY => true, + return [self::CONTINENT_CODE_KEY => true, self::CONTINENT_NAME_KEY => true, self::COUNTRY_CODE_KEY => true, - self::COUNTRY_NAME_KEY => true); + self::COUNTRY_NAME_KEY => true]; } /** @@ -112,22 +175,23 @@ class DefaultProvider extends LocationProvider public function getInfo() { $desc = '<p>' . Piwik::translate('UserCountry_DefaultLocationProviderDesc1') . ' ' - . Piwik::translate('UserCountry_DefaultLocationProviderDesc2', - array('<strong>', '', '', '</strong>')) + . Piwik::translate( + 'UserCountry_DefaultLocationProviderDesc2', + ['<strong>', '', '', '</strong>'] + ) . '</p><p><a href="https://matomo.org/faq/how-to/#faq_163" rel="noreferrer noopener" target="_blank">' . Piwik::translate('UserCountry_HowToInstallGeoIPDatabases') . '</a></p>'; - return array('id' => self::ID, 'title' => self::TITLE, 'description' => $desc, 'order' => 1); + return ['id' => self::ID, 'title' => self::TITLE, 'description' => $desc, 'order' => 1]; } public function getUsageWarning(): ?string { $comment = Piwik::translate('UserCountry_DefaultLocationProviderDesc1') . ' '; - $comment .= Piwik::translate('UserCountry_DefaultLocationProviderDesc2', array( + $comment .= Piwik::translate('UserCountry_DefaultLocationProviderDesc2', [ '<a href="https://matomo.org/docs/geo-locate/" rel="noreferrer noopener" target="_blank">', '', '', '</a>' - )); + ]); return $comment; } } - |