forked from lippoliv/piwik-plugin-disabletracking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisableTracking.php
330 lines (283 loc) · 8.63 KB
/
DisableTracking.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/**
* Piwik - free/libre analytics platform.
*
* @see http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\DisableTracking;
use Exception;
use Piwik\API\Request;
use Piwik\Cache;
use Piwik\Common;
use Piwik\Db;
use Piwik\Piwik;
use Piwik\Plugin;
use Piwik\Log;
class DisableTracking extends
Plugin {
const TABLEDISABLETRACKINGMAP = 'disable_site_tracking';
/**
* @return array The information for each tracked site if it is disabled or not.
* @throws \Exception
*/
public static function getSitesStates() {
$ret = array();
$sql = '
SELECT
`idsite` as `id`,
`name`,
`main_url`
FROM
`' . Common::prefixTable('site') . '`
ORDER BY
`name` ASC
';
$rows = Db::query($sql);
while (($row = $rows->fetch()) !== FALSE) {
$ret[] = array(
'id' => $row['id'],
'label' => $row['name'],
'url' => $row['main_url'],
'disabled' => FALSE,
);
}
// Get disabled states seperately to not destroy our db query resultset.
for ($i = 0; $i < count($ret); $i++) {
$ret[$i]['disabled'] = self::isSiteTrackingDisabled($ret[$i]['id']);
}
return $ret;
}
/**
* Enables tracking for all sites except the given siteIds.
*
* @param array $siteIds The sites to exclude from process.
*
* @throws \Exception
*/
public static function setDisabledSiteTracking($siteIds = array()) {
$allExistingIds = [];
$cache = Cache::getEagerCache();
// Get all site ids in our "disabled tracking map"
$sql = '
SELECT
`siteId` as `id`
FROM
`' . Common::prefixTable(self::TABLEDISABLETRACKINGMAP) . '`
';
$rows = Db::query($sql);
while (($row = $rows->fetch()) !== FALSE) {
$allExistingIds[] = $row['id'];
}
// Remove ids, which shouldn't be disabled any longer
$idsToDelete = array_diff(
$allExistingIds,
$siteIds
);
$sql = '
DELETE FROM
`' . Common::prefixTable(self::TABLEDISABLETRACKINGMAP) . '`
WHERE
siteId in (?)
';
Db::query(
$sql,
[
join(
",",
$idsToDelete
),
]
);
foreach ($idsToDelete as $siteId) {
$cache->delete('DisableTracking_' . $siteId);
}
// Remove ids, which now should be disabled
$idsToAdd = array_diff(
$siteIds,
$allExistingIds
);
$sql = '
INSERT INTO `' . Common::prefixTable(self::TABLEDISABLETRACKINGMAP) . '`
(siteId, created_at)
VALUES
(?, NOW())
';
foreach ($idsToAdd as $siteId) {
Db::query(
$sql,
$siteId
);
$cache->delete('DisableTracking_' . $siteId);
}
}
/**
* Register the events to listen on in this plugin.
*
* @return array the array of events and related listener
*/
public function registerEvents() {
return array(
'Tracker.initRequestSet' => 'newTrackingRequest',
);
}
/**
* Event-Handler for a new tracking request.
*/
public function newTrackingRequest() {
if (isset($_GET['idsite']) === TRUE) {
$siteId = intval($_GET['idsite']);
if ($this->isSiteTrackingDisabled($siteId) === TRUE) {
// End tracking here, as of tracking for this page should be disabled, admin sais.
die();
}
}
}
/**
* Check if site tracking is disabled.
*
* @return bool Whether new tracking requests are ok or not.
* @throws \Exception
*/
public static function isSiteTrackingDisabled($siteId) {
$cache = Cache::getEagerCache();
if ($cache->contains('DisableTracking_' . $siteId)) {
return $cache->fetch('DisableTracking_' . $siteId);
} else {
$sql = '
SELECT
count(*) AS `disabled`
FROM `' . Common::prefixTable(self::TABLEDISABLETRACKINGMAP) . '`
WHERE
siteId = ? AND
deleted_at IS NULL;
';
$state = Db::fetchAll(
$sql,
$siteId
);
$isSiteTrackingDisabled = boolval($state[0]['disabled']);
$cache->save('DisableTracking_' . $siteId, $isSiteTrackingDisabled);
return $isSiteTrackingDisabled;
}
}
/**
* Generate table to store disable states while install plugin.
*
* @throws \Exception if an error occurred
*/
public function install() {
try {
$sql = 'CREATE TABLE `' . Common::prefixTable(self::TABLEDISABLETRACKINGMAP) . '` (
id INT NOT NULL AUTO_INCREMENT,
siteId INT NOT NULL,
created_at DATETIME NOT NULL,
deleted_at DATETIME,
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8';
Db::exec($sql);
} catch (Exception $e) {
// ignore error if table already exists (1050 code is for 'table already exists')
if (Db::get()
->isErrNo(
$e,
'1050'
) === FALSE) {
throw $e;
}
}
}
/**
* Remove plugins table, while uninstall the plugin.
*/
public function uninstall() {
Db::dropTables(Common::prefixTable(self::TABLEDISABLETRACKINGMAP));
}
/**
* Save new input.
*/
public static function save() {
$disabled = array();
foreach ($_POST as $key => $state) {
if (strpos(
$key,
'-'
) !== FALSE) {
$id = preg_split(
"/-/",
$key
);
$id = $id[1];
if ($state === 'on') {
$disabled[] = $id;
}
}
}
self::setDisabledSiteTracking($disabled);
}
/**
* Change disabled status for the websites.
*
* @param array $idSites the list of websites
* @param string $disabled 'on' to archive, 'off' to re-enable
*
* @throws \Exception if an error occurred
*/
public static function changeDisableState($idSites, $disabled)
{
Piwik::checkUserHasAdminAccess($idSites);
foreach ($idSites as $idSite) {
if ('on' === $disabled) {
self::disableSiteTracking($idSite);
} else {
self::enableSiteTracking($idSite);
}
$cache = Cache::getEagerCache();
$cache->delete('DisableTracking_' . $idSite);
}
}
/**
* Disables tracking for the given site.
*
* @param int $siteId the site to disable tracking for
*
* @throws Exception if an error occurred
*/
protected static function disableSiteTracking($siteId)
{
if (empty(Request::processRequest('SitesManager.getSiteFromId', ['idSite' => $siteId]))) {
throw new Exception('Invalid site ID');
}
if (!self::isSiteTrackingDisabled($siteId)) {
$sql = '
INSERT INTO `' . Common::prefixTable(self::TABLEDISABLETRACKINGMAP) . '`
(siteId, created_at)
VALUES
(?, NOW())
';
Db::query($sql, $siteId);
}
}
/**
* Enables tracking for the given site.
*
* @param int $siteId the site to enable tracking for
*
* @throws Exception if an error occurred
*/
protected static function enableSiteTracking($siteId)
{
if (empty(Request::processRequest('SitesManager.getSiteFromId', ['idSite' => $siteId]))) {
throw new Exception('Invalid site ID');
}
if (self::isSiteTrackingDisabled($siteId)) {
$sql = '
DELETE FROM
`' . Common::prefixTable(self::TABLEDISABLETRACKINGMAP) . '`
WHERE
`siteId` = ?
';
Db::query($sql, $siteId);
}
}
}