-
Notifications
You must be signed in to change notification settings - Fork 7
/
SystemSettings.php
243 lines (209 loc) · 9.17 KB
/
SystemSettings.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
<?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 Piwik\Container\StaticContainer;
use Piwik\Intl\Data\Provider\RegionDataProvider;
use Piwik\Piwik;
use Piwik\Plugins\TrackingSpamPrevention\Settings\BlockCloudsSetting;
use Piwik\Settings\Setting;
use Piwik\Settings\FieldConfig;
use Piwik\SettingsPiwik;
use Piwik\Tracker\Cache;
use Piwik\Validators\Email;
class SystemSettings extends \Piwik\Settings\Plugin\SystemSettings
{
/** @var Setting */
public $max_actions;
/** @var Setting */
public $notification_email;
/** @var BlockCloudsSetting */
public $block_clouds;
/** @var Setting */
public $excludedCountries;
/** @var Setting */
public $includedCountries;
/** @var Setting */
public $blockHeadless;
/** @var Setting */
public $blockServerSideLibraries;
protected function init()
{
$this->block_clouds = $this->createBlockCloudsSetting();
$this->blockHeadless = $this->createBlockHeadlessSettings();
$this->blockServerSideLibraries = $this->createBlockServerSideLibrariesSetting();
$this->max_actions = $this->createMaxActionsSetting();
$this->notification_email = $this->createNotificationEmail();
$this->excludedCountries = $this->createExcludedCountriesSetting();
$this->includedCountries = $this->createIncludedCountriesSetting();
}
private function createBlockCloudsSetting()
{
$setting = new BlockCloudsSetting('block_clouds', false, FieldConfig::TYPE_BOOL, $this->pluginName);
$setting->setConfigureCallback(function (FieldConfig $field) {
$field->title = Piwik::translate('TrackingSpamPrevention_SettingBlockCloudTitle');
$field->uiControl = FieldConfig::UI_CONTROL_CHECKBOX;
$field->description = Piwik::translate('TrackingSpamPrevention_SettingBlockCloudDescription');
if (!SettingsPiwik::isInternetEnabled()) {
$field->description = Piwik::translate('TrackingSpamPrevention_BlockCloudNoteInternetDisabled') . $field->description;
}
});
$this->addSetting($setting);
return $setting;
}
private function createBlockHeadlessSettings()
{
return $this->makeSetting('block_headless', $default = false, FieldConfig::TYPE_BOOL, function (FieldConfig $field) {
$field->title = Piwik::translate('TrackingSpamPrevention_SettingBlockHeadlessTitle');
$field->description = Piwik::translate('TrackingSpamPrevention_SettingBlockHeadlessDescription');
$field->uiControl = FieldConfig::UI_CONTROL_CHECKBOX;
});
}
private function createMaxActionsSetting()
{
return $this->makeSetting('max_actions_allowed', $default = 0, FieldConfig::TYPE_INT, function (FieldConfig $field) {
$field->title = Piwik::translate('TrackingSpamPrevention_SettingMaxActionsTitle');
$field->description = Piwik::translate('TrackingSpamPrevention_SettingMaxActionsDescription');
$field->uiControl = FieldConfig::UI_CONTROL_TEXT;
});
}
private function createNotificationEmail()
{
return $this->makeSetting('notification_email', $default = '', FieldConfig::TYPE_STRING, function (FieldConfig $field) {
$field->title = Piwik::translate('TrackingSpamPrevention_SettingNotificationEmailTitle');
$field->description = Piwik::translate('TrackingSpamPrevention_SettingNotificationEmailDescription');
$field->uiControl = FieldConfig::UI_CONTROL_TEXT;
$field->condition = 'max_actions_allowed>0';
$field->validators[] = new Email();
});
}
public function save()
{
parent::save();
$ranges = StaticContainer::get(BlockedIpRanges::class);
if ((bool) $this->block_clouds->getValue() !== (bool) $this->block_clouds->getOldValue()) {
if ($this->block_clouds->getValue()) {
// is now enabled, lets sync ip ranges
$ranges->updateBlockedIpRanges();
} else {
// we also unset any IP that was banned recently
$ranges->unsetAllIpRanges();
}
Cache::clearCacheGeneral();
}
}
private function createExcludedCountriesSetting()
{
return $this->makeSetting('excluded_countries', [], FieldConfig::TYPE_ARRAY, function (FieldConfig $field) {
$field->title = Piwik::translate('TrackingSpamPrevention_SettingExcludedCountriesTitle');
$field->description = Piwik::translate('TrackingSpamPrevention_SettingExcludedCountriesDescription');
$field->uiControl = FieldConfig::UI_CONTROL_MULTI_TUPLE;
$field1 = new FieldConfig\MultiPair("Country", 'country', FieldConfig::UI_CONTROL_SINGLE_SELECT);
$field1->availableValues = $this->listCountries();
$field->uiControlAttributes['field1'] = $field1->toArray();
$self = $this;
$field->transform = function ($value) use ($self) {
return $self->transformCountryList($value);
};
$field->validate = function ($value) use ($field1) {
foreach ($value as $country) {
if (empty($country['country'])) {
continue;
}
if ($country['country'] === 'xx') {
continue; // valid, country not detected
}
if (!isset($field1->availableValues[$country['country']])) {
throw new \Exception('Invalid country code');
}
}
};
});
}
public function transformCountryList($value)
{
if (!empty($value) && is_array($value)) {
$newVal = [];
foreach ($value as $index => $val) {
if (empty($val['country'])) {
continue;
}
$newVal[] = ['country' => $val['country']];
}
return $newVal;
}
return $value;
}
private function createIncludedCountriesSetting()
{
return $this->makeSetting('included_countries', [], FieldConfig::TYPE_ARRAY, function (FieldConfig $field) {
$field->title = Piwik::translate('TrackingSpamPrevention_SettingIncludedCountriesTitle');
$field->description = Piwik::translate('TrackingSpamPrevention_SettingIncludedCountriesDescription');
$field->uiControl = FieldConfig::UI_CONTROL_MULTI_TUPLE;
$field1 = new FieldConfig\MultiPair("Country", 'country', FieldConfig::UI_CONTROL_SINGLE_SELECT);
$field1->availableValues = $this->listCountries();
$field->uiControlAttributes['field1'] = $field1->toArray();
$self = $this;
$field->transform = function ($value) use ($self) {
return $self->transformCountryList($value);
};
$field->validate = function ($value) use ($field1) {
foreach ($value as $country) {
if (empty($country['country'])) {
continue;
}
if ($country['country'] === 'xx') {
continue; // valid, country not detected
}
if (!isset($field1->availableValues[$country['country']])) {
throw new \Exception('Invalid country code');
}
}
};
});
}
private function createBlockServerSideLibrariesSetting()
{
return $this->makeSetting('blockServerSideLibraries', false, FieldConfig::TYPE_BOOL, function (FieldConfig $field) {
$field->title = Piwik::translate('TrackingSpamPrevention_SettingBlockServerSideLibrariesTitle');
$field->inlineHelp = Piwik::translate('TrackingSpamPrevention_SettingBlockServerSideLibrariesDescription', array('<strong>','</strong>','<br>'));
$field->uiControl = FieldConfig::UI_CONTROL_CHECKBOX;
});
}
private function listCountries()
{
$regionDataProvider = StaticContainer::get(RegionDataProvider::class);
$countryList = $regionDataProvider->getCountryList();
array_walk($countryList, function (&$item, $key) {
$item = Piwik::translate('Intl_Country_' . strtoupper($key));
});
asort($countryList); //order by localized name
return $countryList;
}
public function getExcludedCountryCodes()
{
return $this->settingToCountryCodes($this->excludedCountries);
}
public function getIncludedCountryCodes()
{
return $this->settingToCountryCodes($this->includedCountries);
}
private function settingToCountryCodes(Setting $setting)
{
$val = $setting->getValue();
if (empty($val) || !is_array($val)) {
return [];
}
$codes = [];
foreach ($val as $value) {
if (!empty($value['country'])) {
$codes[] = $value['country'];
}
}
return $codes;
}
}