-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathboot.php
More file actions
287 lines (222 loc) · 11 KB
/
Copy pathboot.php
File metadata and controls
287 lines (222 loc) · 11 KB
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
<?php
use AndiLeni\Statistics\MediaRequest;
use AndiLeni\Statistics\Visit;
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
use Jaybizzle\CrawlerDetect\CrawlerDetect;
if (rex::isBackend()) {
$addon = rex_addon::get('statistics');
$currentPage = (string) rex_be_controller::getCurrentPage();
// Hide campaign page unless campaign tracking is explicitly enabled.
$page = $addon->getProperty('page');
if (is_array($page) && isset($page['subpages']) && is_array($page['subpages'])) {
$campaignTrackingEnabled = (bool) $addon->getConfig('statistics_google_campaigns_enable', false);
if (!$campaignTrackingEnabled && isset($page['subpages']['google_campaigns'])) {
unset($page['subpages']['google_campaigns']);
$addon->setProperty('page', $page);
}
}
// permissions
rex_perm::register('statistics[]', null);
rex_perm::register('statistics[settings]', null, rex_perm::OPTIONS);
rex_view::addCssFile($addon->getAssetsUrl('datatables.min.css'));
rex_view::addCssFile($addon->getAssetsUrl('statistics.css'));
$echartsAddon = rex_addon::get('echarts');
if ($echartsAddon->isAvailable()) {
rex_view::addJsFile($echartsAddon->getAssetsUrl('echarts.min.js'));
} else {
rex_view::addJsFile($addon->getAssetsUrl('echarts.min.js'));
}
rex_view::addJsFile($addon->getAssetsUrl('dark.js'));
rex_view::addJsFile($addon->getAssetsUrl('shine.js'));
rex_view::addJsFile($addon->getAssetsUrl('datatables.min.js'));
rex_view::addJsFile($addon->getAssetsUrl('statistics.js'));
if ('statistics/reports' === $currentPage || str_starts_with($currentPage, 'statistics/reports/')) {
$assetVersion = rawurlencode((string) $addon->getVersion());
rex_view::addCssFile($addon->getAssetsUrl('reports.css') . '?v=' . $assetVersion);
rex_view::addJsFile($addon->getAssetsUrl('reports.js') . '?v=' . $assetVersion);
}
if (rex_addon::get('cronjob')->isAvailable() && !rex::isSafeMode()) {
rex_cronjob_manager::registerType('rex_statistics_hashremove_cronjob');
rex_cronjob_manager::registerType('rex_statistics_maintenance_cronjob');
}
$pagination_scroll = $addon->getConfig('statistics_scroll_pagination');
if ($pagination_scroll == 'panel') {
rex_view::addJsFile($addon->getAssetsUrl('statistics_scroll_container.js'));
} elseif ($pagination_scroll == 'table') {
rex_view::addJsFile($addon->getAssetsUrl('statistics_scroll_table.js'));
}
}
// set variable to check in EP whether the visit is coming from a logged-in user or not
if (rex::isFrontend()) {
$addon = rex_addon::get('statistics');
$ignore_backend_loggedin = $addon->getConfig('statistics_ignore_backend_loggedin');
$identityMode = (string) $addon->getConfig('statistics_identity_mode', 'stateless');
if (!in_array($identityMode, ['stateless', 'session'], true)) {
$identityMode = 'stateless';
}
if ($ignore_backend_loggedin) {
$statistics_has_backend_login = rex_backend_login::hasSession();
// Release possible session lock from backend-login probing immediately.
if (PHP_SESSION_ACTIVE === session_status()) {
session_write_close();
}
} else {
$statistics_has_backend_login = false;
}
if ('session' === $identityMode) {
rex_login::startSession();
$token = rex_session('statistics_token', 'string', null);
if (null === $token || '' === $token) {
$token = bin2hex(random_bytes(20));
rex_set_session('statistics_token', $token);
}
// Keep lock duration as short as possible.
if (PHP_SESSION_ACTIVE === session_status()) {
session_write_close();
}
} else {
$instancePepper = (string) rex::getProperty('instname');
$clientAddress = rex::getRequest()->getClientIp();
$clientAddress = $clientAddress ? $clientAddress : '0.0.0.0';
$anonymizedClientAddress = Visit::anonymizeIpAddress($clientAddress);
$tokenRotationHours = (int) $addon->getConfig('statistics_token_rotation_hours', 24);
if ($tokenRotationHours < 1) {
$tokenRotationHours = 1;
} elseif ($tokenRotationHours > 24) {
$tokenRotationHours = 24;
}
$rotationBucket = (string) floor(time() / ($tokenRotationHours * 3600));
$userAgent = rex_server('HTTP_USER_AGENT', 'string', '');
$acceptLanguage = rex_server('HTTP_ACCEPT_LANGUAGE', 'string', '');
$token = hash(
'sha256',
implode('|', [
'statistics_stateless_token_v3',
$instancePepper,
$rotationBucket,
$anonymizedClientAddress,
$userAgent,
$acceptLanguage,
])
);
}
} else {
$statistics_has_backend_login = true;
$token = "";
}
// NOTICE: EP 'RESPONSE_SHUTDOWN' is not called on madia request
// do actions after content is delivered
rex_extension::register('RESPONSE_SHUTDOWN', function () use ($statistics_has_backend_login, $token) {
if (rex::isFrontend()) {
$addon = rex_addon::get('statistics');
$trackingPaused = (bool) $addon->getConfig('statistics_pause_tracking_manual', false)
|| (bool) $addon->getConfig('statistics_pause_tracking_runtime', false)
|| (bool) $addon->getConfig('statistics_pause_tracking', false);
if ($trackingPaused) {
return;
}
$log_all = $addon->getConfig('statistics_log_all');
$ignore_backend_loggedin = $addon->getConfig('statistics_ignore_backend_loggedin');
// return and do not save when visit is coming from a logged-in user
if ($ignore_backend_loggedin && $statistics_has_backend_login) {
return;
}
// domain
try {
$domain = rex::getRequest()->getHost();
} catch (SuspiciousOperationException $e) {
$domain = 'undefined';
}
// page url (raw URL including parameters for ignore checks)
$url = $domain . rex::getRequest()->getRequestUri();
// request response code
$response_code = rex_response::getStatus();
// get ip from visitor, set to 0.0.0.0 when ip can not be determined
$clientAddress = rex::getRequest()->getClientIp();
$clientAddress = $clientAddress ? $clientAddress : '0.0.0.0';
// user agent
$userAgent = rex_server('HTTP_USER_AGENT', 'string', '');
$visit = new Visit($clientAddress, $url, $userAgent, $domain, $token, $response_code);
// Track only frontend requests if page url should not be ignored
// ignore requests with empty user agent
if (!rex::isBackend() && $userAgent != '' && !$visit->shouldIgnore()) {
// visit is not a media request, hence either bot or human visitor
// parse useragent
$visit->parseUA();
if ($visit->isBot()) {
// visitor is a bot
$visit->saveBot();
} else {
// if matomo was not able to detect a bot try CrawlerDetect
$CrawlerDetect = new CrawlerDetect;
if ($CrawlerDetect->isCrawler($userAgent)) {
// true if crawler user agent detected
$crawlerName = $CrawlerDetect->getMatches() ?? "unknown crawler";
$visit->saveCrawlerDetect($crawlerName);
} else {
if ($visit->shouldSaveVisit() && !$visit->DeviceDetector->isLibrary()) {
$recordOnlyOk = (bool) $addon->getConfig('statistics_rec_onlyok', false);
$shouldRecordResponse = !$recordOnlyOk || $response_code === rex_response::HTTP_OK;
if ($shouldRecordResponse) {
// optionally ignore url parameters after ignore checks were done on the raw URL
if ((bool) $addon->getConfig('statistics_ignore_url_params')) {
$visit->setUrl(Visit::removeUrlParameters($visit->getUrl()));
}
// visitduration, number pages visited, last visited page
if (rex::getRequest()->getRequestUri() !== '/favicon.ico') {
$sql = rex_sql::factory();
$sql->setQuery(
'INSERT INTO ' . rex::getTable('pagestats_sessionstats') . ' (token, lastpage, lastvisit, visitduration, pagecount) VALUES (:token, :lastpage, NOW(), 0, 1) ON DUPLICATE KEY UPDATE lastpage = VALUES(lastpage), visitduration = visitduration + (NOW() - lastvisit), lastvisit = NOW(), pagecount = pagecount + 1',
[':token' => $token, ':lastpage' => $visit->getUrl()]
);
}
$visit->updateVisitsPerUrl();
if ((bool) $addon->getConfig('statistics_pages_visitors_enabled', false)) {
$visit->{'persistVisitorPerUrl'}();
}
// visitor is human
// check hash with save_visit, if true then save visit
// check if referer exists, if yes safe it
$referer = rex_server('HTTP_REFERER', 'string', '');
if ($referer != '') {
$referer = urldecode($referer);
if (!str_starts_with($referer, rex::getServer())) {
$visit->saveReferer($referer);
}
}
// check if unique visitor
if ($visit->shouldSaveVisitor()) {
// save visitor
$visit->persistVisitor();
}
$visit->persist();
}
}
}
}
}
}
});
// media
if (rex::isBackend()) {
if (rex_addon::get('media_manager')->isAvailable()) {
rex_media_manager::addEffect(rex_effect_stats_mm::class);
}
} else {
rex_extension::register('MEDIA_MANAGER_AFTER_SEND', function () {
$addon = rex_addon::get('statistics');
$trackingPaused = (bool) $addon->getConfig('statistics_pause_tracking_manual', false)
|| (bool) $addon->getConfig('statistics_pause_tracking_runtime', false)
|| (bool) $addon->getConfig('statistics_pause_tracking', false);
if ($trackingPaused) {
return;
}
if ($addon->getConfig('statistics_media_log_all') == true) {
$url = rex_server('REQUEST_URI', 'string', '');
$media_request = new MediaRequest($url);
if ($media_request->isMedia()) {
$media_request->save();
}
}
});
}