forked from joubertredrat/Matomo-ProtectTrackID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProtectTrackID.php
139 lines (122 loc) · 3.34 KB
/
ProtectTrackID.php
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
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* ProtectTrackID Main class, responsible for hash and unhash idSite
*
* @copyright (c) 2016 Joubert RedRat
* @author Joubert RedRat <[email protected]>
* @license http://www.gnu.org/licenses/gpl-3.0.html GPLv3
* @category Piwik_Plugins
* @package ProtectTrackID
*/
namespace Piwik\Plugins\ProtectTrackID;
use Piwik\Container\StaticContainer;
class ProtectTrackID extends \Piwik\Plugin
{
/**
* Register event observers
*
* @return array
*/
public function registerEvents()
{
return [
'Tracker.getJavascriptCode' => 'hashIdJavaScript',
'SitesManager.getImageTrackingCode' => 'hashIdImage',
'Tracker.Request.getIdSite' => 'unhashId'
];
}
/**
* Get plugin settings
*
* @return array
*/
private function getSettings()
{
$settings = StaticContainer::get('Piwik\Plugins\ProtectTrackID\SystemSettings');
return [
'base' => $settings->base->getValue(),
'salt' => $settings->salt->getValue(),
'length' => $settings->length->getValue()
];
}
/**
* Creates a hash from a integer id
*
* @param int $idSite
* @return int|string
*/
private function hashId($idSite)
{
extract($this->getSettings());
if (is_null($base) || empty($base) ||
is_null($salt) || empty($salt) ||
is_null($length) || empty($length)
) {
return $idSite;
}
require_once(__DIR__.'/vendor/autoload.php');
$Hashid = new \Hashids\Hashids($salt, $length, $base);
return $Hashid->encode($idSite);
}
/**
* Hash id site for JavaScript Tracking Code
*
* @param array &$codeImpl
* @param array $parameters
* @return void
*/
public function hashIdJavaScript(&$codeImpl, $parameters)
{
$codeImpl['idSite'] = $this->hashId($codeImpl['idSite']);
}
/**
* Hash id site for Image Tracking Link
*
* @param array &$piwikUrl
* @param array &$urlParams
* @return void
*/
public function hashIdImage(&$piwikUrl, &$urlParams)
{
$urlParams['idsite'] = $this->hashId($urlParams['idsite']);
}
/**
* Unhash id site
*
* @param int &$idSite
* @param array $params
* @return void
*/
public function unhashId(&$idSite, $params)
{
if ($this->validateHash($params['idsite'])) {
require_once(__DIR__.'/vendor/autoload.php');
extract($this->getSettings());
$Hashid = new \Hashids\Hashids($salt, $length, $base);
$idSite = $Hashid->decode($params['idsite'])[0];
}
}
/**
* Verify if hash is valid from settings
*
* @param string $hash
* @return bool
*/
public function validateHash($hash)
{
extract($this->getSettings());
if (is_null($base) || empty($base) ||
is_null($salt) || empty($salt) ||
is_null($length) || empty($length)
) {
return false;
}
$regex = '/^('.implode('|', str_split($base)).'){'.$length.'}$/';
return (bool) preg_match($regex, $hash);
}
}