/
SiteContentDetector.php
444 lines (380 loc) · 15.8 KB
/
SiteContentDetector.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik;
use Matomo\Cache\Lazy;
use Piwik\Config\GeneralConfig;
use Piwik\Plugins\SitesManager\GtmSiteTypeGuesser;
use Piwik\Plugins\SitesManager\SitesManager;
/**
* This class provides detection functions for specific content on a site. It can be used to easily detect the
* presence of known third party code.
*
* Note: Calling the detect() method will create a HTTP request to the site to retrieve data, only the main site URL
* will be checked
*
* Usage:
*
* $contentDetector = new SiteContentDetector();
* $contentDetector->detectContent([SiteContentDetector::GA3]);
* if ($contentDetector->ga3) {
* // site is using GA3
* }
*
* @api
*/
class SiteContentDetector
{
// Content types
const ALL_CONTENT = 1;
const CONSENT_MANAGER = 2;
const GA3 = 3;
const GA4 = 4;
const GTM = 5;
const CMS = 6;
// Detection detail
public $consentManagerId; // Id of the detected consent manager, eg. 'osano'
public $consentManagerName; // Display name of the detected consent manager, eg. 'Osano'
public $consentManagerUrl; // Url for the configuration guide for the detected consent manager
public $isConnected = false; // True if the detected consent manager is already connected with Matomo
public $ga3; // True if GA3 was detected on the site
public $ga4; // True if GA4 was detected on the site
public $gtm; // True if GTM was detected on the site
public $cms; // The CMS that was detected on the site
public $cloudflare; // true if website is hosted on cloudflare
private $siteResponse = [
'data' => '',
'headers' => []
];
/** @var Lazy */
private $cache;
/**
* @var GtmSiteTypeGuesser
*/
private $siteGuesser;
public function __construct(?Lazy $cache = null)
{
if ($cache === null) {
$this->cache = Cache::getLazyCache();
} else {
$this->cache = $cache;
}
$this->siteGuesser = new GtmSiteTypeGuesser();
}
/**
* Reset the detection properties
*
* @return void
*/
private function resetDetectionProperties(): void
{
$this->consentManagerId = null;
$this->consentManagerUrl = null;
$this->consentManagerName = null;
$this->isConnected = false;
$this->ga3 = false;
$this->ga4 = false;
$this->gtm = false;
$this->cms = SitesManager::SITE_TYPE_UNKNOWN;
$this->cloudflare = false;
}
/**
* This will query the site and populate the class properties with
* the details of the detected content
*
* @param array $detectContent Array of content type for which to check, defaults to all, limiting this list
* will speed up the detection check
* @param ?int $idSite Override the site ID, will use the site from the current request if null
* @param ?array $siteResponse String containing the site data to search, if blank then data will be retrieved
* from the current request site via an http request
* @param int $timeOut How long to wait for the site to response, defaults to 5 seconds
* @return void
*/
public function detectContent(array $detectContent = [SiteContentDetector::ALL_CONTENT],
?int $idSite = null, ?array $siteResponse = null, int $timeOut = 5): void
{
$this->resetDetectionProperties();
// If site data was passed in, then just run the detection checks against it and return.
if ($siteResponse) {
$this->siteResponse = $siteResponse;
$this->detectionChecks($detectContent);
return;
}
// Get the site id from the request object if not explicitly passed
if ($idSite === null) {
if (!isset($_REQUEST['idSite'])) {
return;
}
$idSite = Common::getRequestVar('idSite', null, 'int');
if (!$idSite) {
return;
}
}
$url = Site::getMainUrlFor($idSite);
// Check and load previously cached site content detection data if it exists
$cacheKey = 'SiteContentDetector_' . md5($url);
$requiredProperties = $this->getRequiredProperties($detectContent);
$siteContentDetectionCache = $this->cache->fetch($cacheKey);
if ($siteContentDetectionCache !== false) {
if ($this->checkCacheHasRequiredProperties($requiredProperties, $siteContentDetectionCache)) {
$this->loadRequiredPropertiesFromCache($requiredProperties, $siteContentDetectionCache);
return;
}
}
// No cache hit, no passed data, so make a request for the site content
$siteResponse = $this->requestSiteResponse($url, $timeOut);
// Abort if still no site data
if (empty($siteResponse['data'])) {
return;
}
$this->siteResponse = $siteResponse;
// We now have site data to analyze, so run the detection checks
$this->detectionChecks($detectContent);
// A request was made to get this data and it isn't currently cached, so write it to the cache now
$cacheLife = (60 * 60 * 24 * 7);
$this->savePropertiesToCache($cacheKey, $requiredProperties, $cacheLife);
}
/**
* Returns an array of properties required by the detect content array
*
* @param array $detectContent
*
* @return array
*/
private function getRequiredProperties(array $detectContent): array
{
$requiredProperties = [];
if (in_array(SiteContentDetector::CONSENT_MANAGER, $detectContent) || in_array(SiteContentDetector::ALL_CONTENT, $detectContent)) {
$requiredProperties = array_merge($requiredProperties, ['consentManagerId', 'consentManagerName', 'consentManagerUrl', 'isConnected']);
}
if (in_array(SiteContentDetector::GA3, $detectContent) || in_array(SiteContentDetector::ALL_CONTENT, $detectContent)) {
$requiredProperties[] = 'ga3';
}
if (in_array(SiteContentDetector::GA4, $detectContent) || in_array(SiteContentDetector::ALL_CONTENT, $detectContent)) {
$requiredProperties[] = 'ga4';
}
if (in_array(SiteContentDetector::GTM, $detectContent) || in_array(SiteContentDetector::ALL_CONTENT, $detectContent)) {
$requiredProperties[] = 'gtm';
}
if (in_array(SiteContentDetector::CMS, $detectContent) || in_array(SiteContentDetector::ALL_CONTENT, $detectContent)) {
$requiredProperties[] = 'cms';
}
return $requiredProperties;
}
/**
* Checks that all required properties are in the cache array
*
* @param array $properties
* @param array $cache
*
* @return bool
*/
private function checkCacheHasRequiredProperties(array $properties, array $cache): bool
{
foreach ($properties as $prop) {
if (!array_key_exists($prop, $cache)) {
return false;
}
}
return true;
}
/**
* Load object properties from the cache array
*
* @param array $properties
* @param array $cache
*
* @return void
*/
private function loadRequiredPropertiesFromCache(array $properties, array $cache): void
{
foreach ($properties as $prop) {
if (!array_key_exists($prop, $cache)) {
continue;
}
$this->{$prop} = $cache[$prop];
}
}
/**
* Save properties to the cache
*
* @param string $cacheKey
* @param array $properties
* @param int $cacheLife
*
* @return void
*/
private function savePropertiesToCache(string $cacheKey, array $properties, int $cacheLife): void
{
$cacheData = [];
// Load any existing cached values
$siteContentDetectionCache = $this->cache->fetch($cacheKey);
if (is_array($siteContentDetectionCache)) {
$cacheData = $siteContentDetectionCache;
}
foreach ($properties as $prop) {
$cacheData[$prop] = $this->{$prop};
}
$this->cache->save($cacheKey, $cacheData, $cacheLife);
}
/**
* Run various detection checks for site content
*
* @param array $detectContent Array of detection types used to filter the checks that are run
*
* @return void
*/
private function detectionChecks($detectContent): void
{
if (in_array(SiteContentDetector::CONSENT_MANAGER, $detectContent) || in_array(SiteContentDetector::ALL_CONTENT, $detectContent)) {
$this->detectConsentManager();
}
if (in_array(SiteContentDetector::GA3, $detectContent) || in_array(SiteContentDetector::ALL_CONTENT, $detectContent)) {
$this->ga3 = $this->siteGuesser->detectGA3FromResponse($this->siteResponse);
}
if (in_array(SiteContentDetector::GA4, $detectContent) || in_array(SiteContentDetector::ALL_CONTENT, $detectContent)) {
$this->ga4 = $this->siteGuesser->detectGA4FromResponse($this->siteResponse);
}
if (in_array(SiteContentDetector::GTM, $detectContent) || in_array(SiteContentDetector::ALL_CONTENT, $detectContent)) {
$this->gtm = $this->siteGuesser->guessGtmFromResponse($this->siteResponse);
}
if (in_array(SiteContentDetector::CMS, $detectContent) || in_array(SiteContentDetector::ALL_CONTENT, $detectContent)) {
$this->cms = $this->siteGuesser->guessSiteTypeFromResponse($this->siteResponse);
}
if (
(!empty($this->siteResponse['headers']['server']) && stripos($this->siteResponse['headers']['server'], 'cloudflare') !== false) ||
(!empty($this->siteResponse['headers']['Server']) && stripos($this->siteResponse['headers']['Server'], 'cloudflare') !== false) ||
(!empty($this->siteResponse['headers']['SERVER']) && stripos($this->siteResponse['headers']['SERVER'], 'cloudflare') !== false) ||
!empty($this->siteResponse['headers']['cf-ray']) ||
!empty($this->siteResponse['headers']['Cf-Ray']) ||
!empty($this->siteResponse['headers']['CF-RAY'])
) {
$this->cloudflare = true;
}
}
/**
* Retrieve data from the specified site using an HTTP request
*
* @param string $url
* @param int $timeOut
*
* @return array
*/
private function requestSiteResponse(string $url, int $timeOut): array
{
if (!$url) {
return [];
}
// If internet features are disabled, we don't try to fetch any site content
if (0 === (int) GeneralConfig::getConfigValue('enable_internet_features')) {
return [];
}
$siteData = [];
try {
$siteData = Http::sendHttpRequestBy(Http::getTransportMethod(), $url, $timeOut, null, null,
null, 0, false, true, false, true);
} catch (\Exception $e) {
}
return $siteData;
}
/**
* Detect known consent managers in the site data
*
* Populate this object's properties with the results
*
* @return void
*/
private function detectConsentManager(): void
{
$defs = self::getConsentManagerDefinitions();
if (!$defs) {
return;
}
if (empty($this->siteResponse['data'])) {
return;
}
foreach ($defs as $consentManagerId => $consentManagerDef) {
foreach ($consentManagerDef['detectStrings'] as $dStr) {
if (empty($dStr)) {
continue; // skip empty detections
}
if (strpos($this->siteResponse['data'], $dStr) !== false && array_key_exists($consentManagerId, $defs)) {
$this->consentManagerId = $consentManagerId;
$this->consentManagerName = $consentManagerDef['name'];
$this->consentManagerUrl = $consentManagerDef['url'];
break 2;
}
}
}
if (!isset($defs[$this->consentManagerId]['connectedStrings'])) {
return;
}
// If a consent manager was detected then perform an additional check to see if it has been connected to Matomo
foreach ($defs[$this->consentManagerId]['connectedStrings'] as $cStr) {
if (strpos($this->siteResponse['data'], $cStr) !== false) {
$this->isConnected = true;
break;
}
}
}
/**
* Return an array of consent manager definitions which can be used to detect their presence on the site and show
* the associated guide links
*
* Note: This list is also used to display the known / supported consent managers on the "Ask for Consent" page
* For adding a new consent manager to this page, it needs to be added here. If a consent manager can't be detected
* automatically, simply leave the detections empty.
*
* @return array[]
*/
public static function getConsentManagerDefinitions(): array
{
return [
'osano' => [
'name' => 'Osano',
'detectStrings' => ['osano.com'],
'connectedStrings' => ["Osano.cm.addEventListener('osano-cm-consent-changed', (change) => { console.log('cm-change'); consentSet(change); });"],
'url' => 'https://matomo.org/faq/how-to/using-osano-consent-manager-with-matomo',
],
'cookiebot' => [
'name' => 'Cookiebot',
'detectStrings' => ['cookiebot.com'],
'connectedStrings' => ["typeof _paq === 'undefined' || typeof Cookiebot === 'undefined'"],
'url' => 'https://matomo.org/faq/how-to/using-cookiebot-consent-manager-with-matomo',
],
'cookieyes' => [
'name' => 'CookieYes',
'detectStrings' => ['cookieyes.com'],
'connectedStrings' => ['document.addEventListener("cookieyes_consent_update", function (eventData)'],
'url' => 'https://matomo.org/faq/how-to/using-cookieyes-consent-manager-with-matomo',
],
// Note: tarte au citron pro is configured server side so we cannot tell if it has been connected by
// crawling the website, however setup of Matomo with the pro version is automatic, so displaying the guide
// link for pro isn't necessary. Only the open source version is detected by this definition.
'tarteaucitron' => [
'name' => 'Tarte au Citron',
'detectStrings' => ['tarteaucitron.js'],
'connectedStrings' => ['tarteaucitron.user.matomoHost'],
'url' => 'https://matomo.org/faq/how-to/using-tarte-au-citron-consent-manager-with-matomo',
],
'klaro' => [
'name' => 'Klaro',
'detectStrings' => ['klaro.js', 'kiprotect.com'],
'connectedStrings' => ['KlaroWatcher()', "title: 'Matomo',"],
'url' => 'https://matomo.org/faq/how-to/using-klaro-consent-manager-with-matomo',
],
'complianz' => [
'name' => 'Complianz',
'detectStrings' => ['complianz-gdpr'],
'connectedStrings' => ["if (!cmplz_in_array( 'statistics', consentedCategories )) {
_paq.push(['forgetCookieConsentGiven']);"],
'url' => 'https://matomo.org/faq/how-to/using-complianz-for-wordpress-consent-manager-with-matomo',
],
];
}
}