-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDisableTracking.php
239 lines (202 loc) · 6.59 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
<?php
/**
* Piwik - free/libre analytics platform
*
* @link 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\Common;
use Piwik\Db;
use Piwik\Plugin;
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 = [];
// 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
),
]
);
// 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
);
}
}
/**
* Register the events to listen on in this plugin.
*
* @return array
*/
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();
}
}
}
/**
* @param integer $siteId The site id to check.
*
* @return bool Whether new tracking requests are ok or not.
* @throws \Exception
*/
public static function isSiteTrackingDisabled($siteId) {
$sql = '
SELECT
count(*) AS `disabled`
FROM `' . Common::prefixTable(self::TABLEDISABLETRACKINGMAP) . '`
WHERE
siteId = ? AND
deleted_at IS NULL;
';
$state = Db::fetchAll(
$sql,
$siteId
);
return boolval($state[0]['disabled']);
}
/**
* Generate table to store disable states while install plugin.
*
* @throws \Exception
*/
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);
}
}