Db
2 years ago
Handler
2 years ago
Visit
2 years ago
Action.php
2 years ago
ActionPageview.php
2 years ago
Cache.php
2 years ago
Db.php
2 years ago
Failures.php
2 years ago
FingerprintSalt.php
2 years ago
GoalManager.php
2 years ago
Handler.php
2 years ago
IgnoreCookie.php
2 years ago
LogTable.php
2 years ago
Model.php
2 years ago
PageUrl.php
2 years ago
Request.php
2 years ago
RequestProcessor.php
2 years ago
RequestSet.php
2 years ago
Response.php
2 years ago
ScheduledTasksRunner.php
2 years ago
Settings.php
2 years ago
TableLogAction.php
2 years ago
TrackerCodeGenerator.php
2 years ago
TrackerConfig.php
2 years ago
Visit.php
2 years ago
VisitExcluded.php
2 years ago
VisitInterface.php
2 years ago
Visitor.php
2 years ago
VisitorNotFoundInDb.php
2 years ago
VisitorRecognizer.php
2 years ago
FingerprintSalt.php
73 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | * |
| 9 | */ |
| 10 | namespace Piwik\Tracker; |
| 11 | |
| 12 | use Piwik\Common; |
| 13 | use Piwik\Date; |
| 14 | use Piwik\Option; |
| 15 | class FingerprintSalt |
| 16 | { |
| 17 | const OPTION_PREFIX = 'fingerprint_salt_'; |
| 18 | const DELETE_FINGERPRINT_OLDER_THAN_SECONDS = 432000; |
| 19 | // 5 days in seconds |
| 20 | public function generateSalt() |
| 21 | { |
| 22 | return Common::getRandomString(32); |
| 23 | } |
| 24 | public function deleteOldSalts() |
| 25 | { |
| 26 | // we want to make sure to delete salts that were created more than three days ago as they are likely not in |
| 27 | // use anymore. We should delete them to ensure the fingerprint is truly random for each day because if we used |
| 28 | // eg the regular salt then it would technically still be possible to try and regenerate the fingerprint based |
| 29 | // on certain information. |
| 30 | // Typically, only the salts for today and yesterday are used. However, if someone was to import historical data |
| 31 | // for the same day and this takes more than five days, then it could technically happen that we delete a |
| 32 | // fingerprint that is still in use now and as such after deletion a few visitors would have a new configId |
| 33 | // within one visit and such a new visit would be created. That should be very much edge case though. |
| 34 | $deleteSaltsCreatedBefore = Date::getNowTimestamp() - self::DELETE_FINGERPRINT_OLDER_THAN_SECONDS; |
| 35 | $options = Option::getLike(self::OPTION_PREFIX . '%'); |
| 36 | $deleted = array(); |
| 37 | foreach ($options as $name => $value) { |
| 38 | $value = $this->decode($value); |
| 39 | if (empty($value['time']) || $value['time'] < $deleteSaltsCreatedBefore) { |
| 40 | Option::delete($name); |
| 41 | $deleted[] = $name; |
| 42 | } |
| 43 | } |
| 44 | return $deleted; |
| 45 | } |
| 46 | public function getDateString(Date $date, $timezone) |
| 47 | { |
| 48 | $dateString = Date::factory($date->getTimestampUTC(), $timezone)->toString(); |
| 49 | return $dateString; |
| 50 | } |
| 51 | private function encode($value) |
| 52 | { |
| 53 | return json_encode($value); |
| 54 | } |
| 55 | private function decode($value) |
| 56 | { |
| 57 | return @json_decode($value, true); |
| 58 | } |
| 59 | public function getSalt($dateString, $idSite) |
| 60 | { |
| 61 | $fingerprintSaltKey = self::OPTION_PREFIX . (int) $idSite . '_' . $dateString; |
| 62 | $salt = Option::get($fingerprintSaltKey); |
| 63 | if (!empty($salt)) { |
| 64 | $salt = $this->decode($salt); |
| 65 | } |
| 66 | if (empty($salt['value'])) { |
| 67 | $salt = array('value' => $this->generateSalt(), 'time' => Date::getNowTimestamp()); |
| 68 | Option::set($fingerprintSaltKey, $this->encode($salt)); |
| 69 | } |
| 70 | return $salt['value']; |
| 71 | } |
| 72 | } |
| 73 |