-
Notifications
You must be signed in to change notification settings - Fork 12
/
SystemSettings.php
158 lines (132 loc) · 5.74 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
<?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\AnonymousPiwikUsageMeasurement;
use Piwik\API\Request;
use Piwik\Common;
use Exception;
use Piwik\Settings\Setting;
use Piwik\Settings\FieldConfig;
use Piwik\UrlHelper;
/**
* Defines Settings for AnonymousPiwikUsageMeasurement.
*/
class SystemSettings extends \Piwik\Settings\Plugin\SystemSettings
{
/** @var Setting */
public $canUserOptOut;
/** @var Setting */
public $ownPiwikSiteId;
/** @var Setting */
public $customPiwikSiteId;
/** @var Setting */
public $customPiwikSiteUrl;
/** @var Setting */
public $anonymizeCustomPiwik;
/** @var Setting */
public $anonymizeSelfPiwik;
protected function init()
{
$this->canUserOptOut = $this->createLetUsersOptOutSetting();
$this->ownPiwikSiteId = $this->createTrackToOwnPiwikSetting();
$this->anonymizeSelfPiwik = $this->anonymizationOption('anonymizeSelfPiwik');
$this->customPiwikSiteUrl = $this->createTrackToCustomSiteUrlSetting();
$this->customPiwikSiteId = $this->createTrackToCustomSiteIdSetting();
$this->anonymizeCustomPiwik = $this->anonymizationOption('anonymizeCustomPiwik');
}
private function createLetUsersOptOutSetting()
{
return $this->makeSetting('canUserOptOut', $default = true, FieldConfig::TYPE_BOOL, function (FieldConfig $field) {
$field->title = 'Let users disable anonymous tracking';
$field->uiControl = FieldConfig::UI_CONTROL_CHECKBOX;
$field->description = 'If enabled, logged in users can opt out in their plugin settings. Anonymous users cannot opt out.';
});
}
private function createTrackToOwnPiwikSetting()
{
return $this->makeSetting('ownPiwikSiteId', $default = 0, FieldConfig::TYPE_INT, function (FieldConfig $field) {
$field->title = 'Site Id';
// ideally we would use a SELECT control and let user choose an existing site but this would make performance slow
// since we'd always have to get all site ids in each request
$field->uiControl = FieldConfig::UI_CONTROL_TEXT;
$field->introduction = 'Send usage data to this Matomo';
$field->description = 'If specified, anonymized usage data will be sent to the specified site in this Matomo. This lets you analyze how you and your colleagues are using Matomo.';
$field->validate = function ($idSite) {
if (empty($idSite)) {
return;
}
if (!is_numeric($idSite)) {
throw new Exception("Site Id '$idSite' should be a number");
}
$idSite = (int) $idSite;
try {
$siteExists = Request::processRequest('SitesManager.getSiteFromId', array('idSite' => $idSite));
} catch (Exception $e) {
$siteExists = false;
}
if (!$siteExists) {
throw new Exception("The specified idSite '$idSite' does not exist");
}
};
});
}
private function createTrackToCustomSiteUrlSetting()
{
return $this->makeSetting('customSiteUrl', $default = '', FieldConfig::TYPE_STRING, function (FieldConfig $field) {
$field->title = 'Matomo Url';
$field->uiControl = FieldConfig::UI_CONTROL_TEXT;
$field->uiControlAttributes = array('placeHolder' => 'eg. http://example.com/matomo');
$field->introduction = 'Send usage data to a custom Matomo';
$field->description = '';
$field->validate = function ($value, $setting) {
if (empty($value)) {
return;
}
if (!UrlHelper::isLookLikeUrl($value)) {
throw new Exception("URL '$value' seems to be not a valid URL");
}
// TODO should we check if URL exists and is valid?!? might not work if instance is not connected to internet
};
$field->transform = function ($value) {
if (empty($value)) {
return '';
}
if (!Common::stringEndsWith($value, '/piwik.php') && !Common::stringEndsWith($value, '/matomo.php')) {
if (!Common::stringEndsWith($value, '/')) {
$value .= '/';
}
$value .= 'matomo.php';
}
return $value;
};
});
}
private function createTrackToCustomSiteIdSetting()
{
return $this->makeSetting('customSiteId', $default = 0, FieldConfig::TYPE_INT, function (FieldConfig $field) {
$field->title = 'Site Id';
$field->uiControl = FieldConfig::UI_CONTROL_TEXT;
$field->uiControlAttributes = array('placeHolder' => 'eg. "2"');
$field->description = 'If a URL and Site Id is specified, usage data will be sent to the custom Matomo instance.';
$field->validate = function ($idSite) {
if (empty($idSite)) {
return;
}
if (!is_numeric($idSite)) {
throw new Exception("Site Id '$idSite' should be a number");
}
};
});
}
private function anonymizationOption($optionName)
{
return $this->makeSetting($optionName, $default = true, FieldConfig::TYPE_BOOL, function (FieldConfig $field) {
$field->title = 'Anonymize tracking requests';
$field->uiControl = FieldConfig::UI_CONTROL_CHECKBOX;
});
}
}