| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Tracker; |
|
|
| use Piwik\Config; |
| use Piwik\Container\StaticContainer; |
| use Piwik\Date; |
| use Piwik\Tracker; |
| use Piwik\DeviceDetector\DeviceDetectorFactory; |
| use Piwik\SettingsPiwik; |
|
|
| class Settings // TODO: merge w/ visitor recognizer or make it it's own service. the class name is required for BC. |
| { |
| public const OS_BOT = 'BOT'; |
|
|
| |
| |
| |
| |
| |
| |
| private $isSameFingerprintsAcrossWebsites; |
|
|
| public function __construct($isSameFingerprintsAcrossWebsites) |
| { |
| $this->isSameFingerprintsAcrossWebsites = $isSameFingerprintsAcrossWebsites; |
| } |
|
|
| public function getConfigId(Request $request, $ipAddress): string |
| { |
| list($plugin_Flash, $plugin_Java, $plugin_Quicktime, $plugin_RealPlayer, $plugin_PDF, |
| $plugin_WindowsMedia, $plugin_Silverlight, $plugin_Cookie) = $request->getPlugins(); |
|
|
| $userAgent = $request->getUserAgent(); |
|
|
| $deviceDetector = StaticContainer::get(DeviceDetectorFactory::class)->makeInstance($userAgent, $request->getClientHints()); |
| $aBrowserInfo = $deviceDetector->getClient(); |
|
|
| if (empty($aBrowserInfo['type']) || 'browser' !== $aBrowserInfo['type']) { |
| |
| unset($aBrowserInfo); |
| } |
|
|
| $browserName = !empty($aBrowserInfo['short_name']) ? $aBrowserInfo['short_name'] : 'UNK'; |
| $browserVersion = !empty($aBrowserInfo['version']) ? $aBrowserInfo['version'] : ''; |
|
|
| if ($deviceDetector->isBot()) { |
| $os = self::OS_BOT; |
| } else { |
| $os = $deviceDetector->getOS(); |
| $os = empty($os['short_name']) ? 'UNK' : $os['short_name']; |
| } |
|
|
| $client = $deviceDetector->getClient(); |
| if (!empty($client['name']) && $client['name'] === 'Internet Explorer') { |
| |
| |
| |
| |
| |
| |
| $plugin_Cookie = '0'; |
| } |
|
|
| $browserLang = substr($request->getBrowserLanguage(), 0, 20); |
| $trackerConfig = Config::getInstance()->Tracker; |
|
|
| $fingerprintSalt = ''; |
|
|
| |
| |
| if (!$this->isSameFingerprintsAcrossWebsites && !empty($trackerConfig['create_new_visit_after_midnight'])) { |
| $cache = Cache::getCacheWebsiteAttributes($request->getIdSite()); |
| $date = Date::factory((int) $request->getCurrentTimestamp()); |
| $fingerprintSaltKey = new FingerprintSalt(); |
| $dateString = $fingerprintSaltKey->getDateString($date, $cache['timezone']); |
|
|
| if (!empty($cache[FingerprintSalt::OPTION_PREFIX . $dateString])) { |
| $fingerprintSalt = $cache[FingerprintSalt::OPTION_PREFIX . $dateString]; |
| } else { |
| |
| $fingerprintSalt = $fingerprintSaltKey->getSalt($dateString, $request->getIdSite()); |
| } |
|
|
| $fingerprintSalt .= $dateString; |
|
|
| if (defined('PIWIK_TEST_MODE') && PIWIK_TEST_MODE) { |
| $fingerprintSalt = ''; |
| } |
| } |
|
|
| return $this->getConfigHash( |
| $request, |
| $os, |
| $browserName, |
| $browserVersion, |
| $plugin_Flash, |
| $plugin_Java, |
| $plugin_Quicktime, |
| $plugin_RealPlayer, |
| $plugin_PDF, |
| $plugin_WindowsMedia, |
| $plugin_Silverlight, |
| $plugin_Cookie, |
| $ipAddress, |
| $browserLang, |
| $fingerprintSalt |
| ); |
| } |
|
|
| public function getRandomConfigId(): string |
| { |
| return $this->getRandomConfigHash(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected function getConfigHash( |
| Request $request, |
| $os, |
| $browserName, |
| $browserVersion, |
| $plugin_Flash, |
| $plugin_Java, |
| $plugin_Quicktime, |
| $plugin_RealPlayer, |
| $plugin_PDF, |
| $plugin_WindowsMedia, |
| $plugin_Silverlight, |
| $plugin_Cookie, |
| $ip, |
| $browserLang, |
| $fingerprintHash |
| ): string { |
| |
| |
| $salt = SettingsPiwik::getSalt(); |
|
|
| $configString = |
| $os |
| . $browserName . $browserVersion |
| . $plugin_Flash . $plugin_Java . '0' . $plugin_Quicktime . $plugin_RealPlayer . $plugin_PDF |
| . $plugin_WindowsMedia . '0' . $plugin_Silverlight . $plugin_Cookie |
| . $ip |
| . $browserLang |
| . $salt |
| . $fingerprintHash; |
|
|
| if (!$this->isSameFingerprintsAcrossWebsites) { |
| $configString .= $request->getIdSite(); |
| } |
|
|
| return $this->createHashOfConfigString($configString); |
| } |
|
|
| protected function getRandomConfigHash(): string |
| { |
| return $this->createHashOfConfigString(random_bytes(64)); |
| } |
|
|
| private function createHashOfConfigString(string $configString): string |
| { |
| $hash = md5($configString, $raw_output = true); |
|
|
| return substr($hash, 0, Tracker::LENGTH_BINARY_ID); |
| } |
| } |
|
|