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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\PrivacyManager;
use Piwik\Piwik;
use Piwik\Config as PiwikConfig;
use Piwik\Plugins\PrivacyManager\Model\DataSubjects;
use Piwik\Plugins\PrivacyManager\Dao\LogDataAnonymizer;
use Piwik\Plugins\PrivacyManager\Model\LogDataAnonymizations;
use Piwik\Plugins\PrivacyManager\Validators\VisitsDataSubject;
use Piwik\Site;
use Piwik\Validators\BaseValidator;
/**
* API for plugin PrivacyManager
*
* @method static \Piwik\Plugins\PrivacyManager\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
/**
* @var DataSubjects
*/
private $gdpr;
/**
* @var LogDataAnonymizations
*/
private $logDataAnonymizations;
/**
* @var LogDataAnonymizer
*/
private $logDataAnonymizer;
public function __construct(DataSubjects $gdpr, LogDataAnonymizations $logDataAnonymizations, LogDataAnonymizer $logDataAnonymizer)
{
$this->gdpr = $gdpr;
$this->logDataAnonymizations = $logDataAnonymizations;
$this->logDataAnonymizer = $logDataAnonymizer;
}
private function checkDataSubjectVisits($visits)
{
BaseValidator::check('visits', $visits, [new VisitsDataSubject()]);
$idSites = array();
foreach ($visits as $index => $visit) {
$idSites[] = $visit['idsite'];
}
Piwik::checkUserHasAdminAccess($idSites);
}
public function deleteDataSubjects($visits)
{
Piwik::checkUserHasSomeAdminAccess();
$this->checkDataSubjectVisits($visits);
return $this->gdpr->deleteDataSubjects($visits);
}
public function exportDataSubjects($visits)
{
Piwik::checkUserHasSomeAdminAccess();
$this->checkDataSubjectVisits($visits);
return $this->gdpr->exportDataSubjects($visits);
}
public function anonymizeSomeRawData($idSites, $date, $anonymizeIp = false, $anonymizeLocation = false, $anonymizeUserId = false, $unsetVisitColumns = [], $unsetLinkVisitActionColumns = [])
{
Piwik::checkUserHasSuperUserAccess();
if ($idSites === 'all' || empty($idSites)) {
$idSites = null; // all websites
} else {
$idSites = Site::getIdSitesFromIdSitesString($idSites);
}
$requester = Piwik::getCurrentUserLogin();
$this->logDataAnonymizations->scheduleEntry($requester, $idSites, $date, $anonymizeIp, $anonymizeLocation, $anonymizeUserId, $unsetVisitColumns, $unsetLinkVisitActionColumns);
}
public function getAvailableVisitColumnsToAnonymize()
{
Piwik::checkUserHasSuperUserAccess();
$columns = $this->logDataAnonymizer->getAvailableVisitColumnsToAnonymize();
return $this->formatAvailableColumnsToAnonymize($columns);
}
public function getAvailableLinkVisitActionColumnsToAnonymize()
{
Piwik::checkUserHasSuperUserAccess();
$columns = $this->logDataAnonymizer->getAvailableLinkVisitActionColumnsToAnonymize();
return $this->formatAvailableColumnsToAnonymize($columns);
}
private function formatAvailableColumnsToAnonymize($columns)
{
ksort($columns);
$formatted = array();
foreach ($columns as $column => $default) {
$formatted[] = array(
'column_name' => $column,
'default_value' => $default
);
}
return $formatted;
}
/**
* @internal
*/
public function setAnonymizeIpSettings($anonymizeIPEnable, $maskLength, $useAnonymizedIpForVisitEnrichment, $anonymizeUserId = false, $anonymizeOrderId = false)
{
Piwik::checkUserHasSuperUserAccess();
if ($anonymizeIPEnable == '1') {
IPAnonymizer::activate();
} else if ($anonymizeIPEnable == '0') {
IPAnonymizer::deactivate();
} else {
// pass
}
$privacyConfig = new Config();
$privacyConfig->ipAddressMaskLength = (int) $maskLength;
$privacyConfig->useAnonymizedIpForVisitEnrichment = (bool) $useAnonymizedIpForVisitEnrichment;
if (false !== $anonymizeUserId) {
$privacyConfig->anonymizeUserId = (bool) $anonymizeUserId;
}
if (false !== $anonymizeOrderId) {
$privacyConfig->anonymizeOrderId = (bool) $anonymizeOrderId;
}
return true;
}
/**
* @internal
*/
public function deactivateDoNotTrack()
{
Piwik::checkUserHasSuperUserAccess();
$dntChecker = new DoNotTrackHeaderChecker();
$dntChecker->deactivate();
return true;
}
/**
* @internal
*/
public function activateDoNotTrack()
{
Piwik::checkUserHasSuperUserAccess();
$dntChecker = new DoNotTrackHeaderChecker();
$dntChecker->activate();
return true;
}
/**
* @internal
*/
public function setScheduleReportDeletionSettings($deleteLowestInterval = 7)
{
return $this->savePurgeDataSettings(array(
'delete_logs_schedule_lowest_interval' => (int) $deleteLowestInterval
));
}
/**
* @internal
*/
public function setDeleteLogsSettings($enableDeleteLogs = '0', $deleteLogsOlderThan = 180)
{
$deleteLogsOlderThan = (int) $deleteLogsOlderThan;
if ($deleteLogsOlderThan < 1) {
$deleteLogsOlderThan = 1;
}
return $this->savePurgeDataSettings(array(
'delete_logs_enable' => !empty($enableDeleteLogs),
'delete_logs_older_than' => $deleteLogsOlderThan,
));
}
/**
* @internal
*/
public function setDeleteReportsSettings($enableDeleteReports = 0, $deleteReportsOlderThan = 3,
$keepBasic = 0, $keepDay = 0, $keepWeek = 0, $keepMonth = 0,
$keepYear = 0, $keepRange = 0, $keepSegments = 0)
{
$settings = array();
// delete reports settings
$settings['delete_reports_enable'] = !empty($enableDeleteReports);
$deleteReportsOlderThan = (int) $deleteReportsOlderThan;
if ($deleteReportsOlderThan < 2) {
$deleteReportsOlderThan = 2;
}
$settings['delete_reports_older_than'] = $deleteReportsOlderThan;
$settings['delete_reports_keep_basic_metrics'] = (int) $keepBasic;
$settings['delete_reports_keep_day_reports'] = (int) $keepDay;
$settings['delete_reports_keep_week_reports'] = (int) $keepWeek;
$settings['delete_reports_keep_month_reports'] = (int) $keepMonth;
$settings['delete_reports_keep_year_reports'] = (int) $keepYear;
$settings['delete_reports_keep_range_reports'] = (int) $keepRange;
$settings['delete_reports_keep_segment_reports'] = (int) $keepSegments;
$settings['delete_logs_max_rows_per_query'] = PiwikConfig::getInstance()->Deletelogs['delete_logs_max_rows_per_query'];
return $this->savePurgeDataSettings($settings);
}
private function savePurgeDataSettings($settings)
{
Piwik::checkUserHasSuperUserAccess();
$this->checkDataPurgeAdminSettingsIsEnabled();
PrivacyManager::savePurgeDataSettings($settings);
return true;
}
private function checkDataPurgeAdminSettingsIsEnabled()
{
if (!Controller::isDataPurgeSettingsEnabled()) {
throw new \Exception("Configuring deleting raw data and report data has been disabled by Matomo admins.");
}
}
}
|