-
Notifications
You must be signed in to change notification settings - Fork 7
/
TrackingSpamPrevention.php
172 lines (147 loc) · 5.53 KB
/
TrackingSpamPrevention.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
<?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\Plugins\TrackingSpamPrevention;
use Matomo\Network\IP;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Date;
use Piwik\Tracker\Request;
use Piwik\Tracker\VisitExcluded;
class TrackingSpamPrevention extends \Piwik\Plugin
{
private $isInstalledInThisRequest = false;
public function registerEvents()
{
return [
'Tracker.isExcludedVisit' => 'isExcludedVisit',
'Tracker.setTrackerCacheGeneral' => 'setTrackerCacheGeneral',
'TrackingSpamPrevention.banIp' => 'onBanIp',
];
}
public function install()
{
$this->isInstalledInThisRequest = true;
$config = new Configuration();
$config->install();
}
public function activate()
{
$this->isInstalledInThisRequest = true;
}
public function uninstall()
{
$config = new Configuration();
$config->uninstall();
}
public function onBanIp($ipRange, $ip)
{
$settings = $this->getSystemSettings();
$email = $settings->notification_email->getValue();
$maxActions = $settings->max_actions->getValue();
$locationData = $this->getBlockGeoIp()->detectLocation($ip, Common::getBrowserLanguage());
$now = Date::now()->getDatetime();
$banIpMail = new BanIpNotificationEmail();
$banIpMail->send($ipRange, $ip, $email, $maxActions, $locationData, $now);
}
public function setTrackerCacheGeneral(&$cache)
{
$isTestMode = defined('PIWIK_TEST_MODE') && PIWIK_TEST_MODE;
if ($this->isInstalledInThisRequest && !$isTestMode) {
// dont do anything when plugin gets loaded as DI config would not be loaded yet and it would
// cause an issue with activity log since it does a geolocation which uses the tracker cache
return;
}
$ranges = $this->getBlockedIpRanges();
$cache[BlockedIpRanges::OPTION_KEY] = $ranges->getBlockedRanges();
}
public function isExcludedVisit(&$excluded, Request $request)
{
if ($excluded) {
return; // already excluded, not needed to check
}
$visitExcluded = new VisitExcluded($request);
$ipString = $request->getIpString();
$ip = IP::fromStringIP($ipString);
if (is_callable(array($visitExcluded, 'isChromeDataSaverUsed')) && $visitExcluded->isChromeDataSaverUsed($ip)) {
Common::printDebug("Not excluding visit as chrome data saver is used");
return;
}
if (StaticContainer::get(AllowListIpRange::class)->isAllowed($ipString)) {
Common::printDebug("Not excluding visit as it matches an IP range that is always allowed");
return;
}
$settings = $this->getSystemSettings();
$blockGeoIp = $this->getBlockGeoIp();
$browserLang = $request->getBrowserLanguage();
$browserDetection = new BrowserDetection();
$clientHints = json_encode($request->getClientHints());
if (
$settings->blockHeadless->getValue() &&
(
$browserDetection->isHeadlessBrowser($request->getUserAgent()) ||
$browserDetection->isHeadlessBrowser($clientHints)
)
) {
// note above user agent could have been overwritten with UA parameter but that's fine since it's easy to change useragent anyway
Common::printDebug("Excluding visit as headless browser detected");
$excluded = 'excluded: headless browser';
return;
}
if (
$settings->block_clouds->getValue()
&& $blockGeoIp->isExcludedProvider($ipString, $browserLang)
) {
// only needs to be done when cloud providers are blocked specifically
Common::printDebug("Excluding visit as geoip detects a cloud provider");
$excluded = 'excluded: geoip cloud provider';
return;
}
if ($this->getBlockedIpRanges()->isExcluded($ipString)) {
// we also execute this when block clouds disabled because it might contain banned ips
Common::printDebug("Excluding visit as IP originates from a cloud provider");
$excluded = 'excluded: ip cloud provider';
return;
}
if (
$blockGeoIp->isExcludedCountry(
$ipString,
$browserLang,
$settings->getExcludedCountryCodes(),
$settings->getIncludedCountryCodes()
)
) {
Common::printDebug("Excluding visit as geoip detects an excluded (or not included) country");
$excluded = 'excluded: country';
return;
}
if (
$settings->blockServerSideLibraries->getValue() &&
$browserDetection->isLibrary($request->getUserAgent())
) {
Common::printDebug("Excluding visit as Server Side Library detected");
$excluded = 'excluded: ServerSideLibraries-';
return;
}
}
private function getSystemSettings()
{
return StaticContainer::get(SystemSettings::class);
}
private function getBlockedIpRanges()
{
return StaticContainer::get(BlockedIpRanges::class);
}
private function getBlockGeoIp()
{
return StaticContainer::get(BlockedGeoIp::class);
}
public function isTrackerPlugin()
{
return true;
}
}