This repository was archived by the owner on Sep 4, 2025. It is now read-only.
forked from matomo-org/plugin-CustomAlerts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.php
More file actions
executable file
·259 lines (212 loc) · 7.73 KB
/
API.php
File metadata and controls
executable file
·259 lines (212 loc) · 7.73 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
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
* @version $Id$
*
* @category Piwik_Plugins
* @package Piwik_Alerts
*/
namespace Piwik\Plugins\CustomAlerts;
use Piwik\Common;
use Piwik\Piwik;
use Piwik\Site;
use Piwik\Translate;
use Piwik\Plugins\MobileMessaging\API as APIMobileMessaging;
use Exception;
/**
*
* @package Piwik_Alerts
* @method static \Piwik\Plugins\CustomAlerts\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
private $validator;
protected function __construct()
{
parent::__construct();
$this->validator = new Validator();
}
/**
* Returns a single alert.
*
* @param int $idAlert
*
* @throws \Exception In case alert does not exist or user has no permission to access alert.
*
* @return array
*/
public function getAlert($idAlert)
{
$alert = $this->getModel()->getAlert($idAlert);
if (empty($alert)) {
throw new Exception(Piwik::translate('CustomAlerts_AlertDoesNotExist', $idAlert));
}
$this->validator->checkUserHasPermissionForAlert($alert);
return $alert;
}
/**
* Returns the Alerts that are defined on the idSites given.
*
* @param array $idSites
* @param bool $ifSuperUserReturnAllAlerts
*
* @return array
*/
public function getAlerts($idSites, $ifSuperUserReturnAllAlerts = false)
{
if (empty($idSites)) {
return array();
}
$idSites = Site::getIdSitesFromIdSitesString($idSites);
Piwik::checkUserHasViewAccess($idSites);
if (Piwik::isUserIsSuperUser() && $ifSuperUserReturnAllAlerts) {
$login = false;
} else {
$login = Piwik::getCurrentUserLogin();
}
$alerts = $this->getModel()->getAlerts($idSites, $login);
return $alerts;
}
/**
* Creates an Alert for given website(s).
*
* @param string $name
* @param mixed $idSites
* @param string $period
* @param bool $emailMe
* @param array $additionalEmails
* @param array $phoneNumbers
* @param string $metric (nb_uniq_visits, sum_visit_length, ..)
* @param string $metricCondition
* @param float $metricValue
* @param string $reportUniqueId
* @param int $comparedTo
* @param bool|string $reportCondition
* @param bool|string $reportValue
* @internal param bool $enableEmail
* @return int ID of new Alert
*/
public function addAlert($name, $idSites, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition = false, $reportValue = false)
{
$idSites = Site::getIdSitesFromIdSitesString($idSites);
$additionalEmails = $this->filterAdditionalEmails($additionalEmails);
$phoneNumbers = $this->filterPhoneNumbers($phoneNumbers);
$this->checkAlert($idSites, $name, $period, $additionalEmails, $metricCondition, $metric, $comparedTo, $reportCondition, $reportUniqueId);
$name = Common::unsanitizeInputValue($name);
$login = Piwik::getCurrentUserLogin();
if (empty($reportCondition) || empty($reportValue)) {
$reportCondition = null;
$reportValue = null;
}
return $this->getModel()->createAlert($name, $idSites, $login, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition, $reportValue);
}
/**
* Edits an Alert for given website(s).
*
* @param $idAlert
* @param string $name Name of Alert
* @param mixed $idSites Single int or array of ints of idSites.
* @param string $period Period the alert is defined on.
* @param bool $emailMe
* @param array $additionalEmails
* @param array $phoneNumbers
* @param string $metric (nb_uniq_visits, sum_visit_length, ..)
* @param string $metricCondition
* @param float $metricValue
* @param string $reportUniqueId
* @param int $comparedTo
* @param bool|string $reportCondition
* @param bool|string $reportValue
*
* @internal param bool $enableEmail
* @return boolean
*/
public function editAlert($idAlert, $name, $idSites, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition = false, $reportValue = false)
{
// make sure alert exists and user has permission to read
$this->getAlert($idAlert);
$idSites = Site::getIdSitesFromIdSitesString($idSites);
$additionalEmails = $this->filterAdditionalEmails($additionalEmails);
$phoneNumbers = $this->filterPhoneNumbers($phoneNumbers);
$this->checkAlert($idSites, $name, $period, $additionalEmails, $metricCondition, $metric, $comparedTo, $reportCondition, $reportUniqueId);
$name = Common::unsanitizeInputValue($name);
if (empty($reportCondition) || empty($reportValue)) {
$reportCondition = null;
$reportValue = null;
}
return $this->getModel()->updateAlert($idAlert, $name, $idSites, $period, $emailMe, $additionalEmails, $phoneNumbers, $metric, $metricCondition, $metricValue, $comparedTo, $reportUniqueId, $reportCondition, $reportValue);
}
/**
* Delete alert by id.
*
* @param int $idAlert
* @throws \Exception
*/
public function deleteAlert($idAlert)
{
// make sure alert exists and user has permission to read
$this->getAlert($idAlert);
$this->getModel()->deleteAlert($idAlert);
}
/**
* Get triggered alerts.
*
* @param int[] idSites
*
* @return array
*/
public function getTriggeredAlerts($idSites)
{
if (empty($idSites)) {
return array();
}
$idSites = Site::getIdSitesFromIdSitesString($idSites);
Piwik::checkUserHasViewAccess($idSites);
$login = Piwik::getCurrentUserLogin();
return $this->getModel()->getTriggeredAlerts($idSites, $login);
}
private function getModel()
{
return new Model();
}
private function filterAdditionalEmails($additionalEmails)
{
if (empty($additionalEmails)) {
return array();
}
foreach ($additionalEmails as &$email) {
$email = trim($email);
if (empty($email)) {
$email = false;
}
}
return array_filter($additionalEmails);
}
private function filterPhoneNumbers($phoneNumbers)
{
$availablePhoneNumbers = APIMobileMessaging::getInstance()->getActivatedPhoneNumbers();
foreach ($phoneNumbers as $key => &$phoneNumber) {
$phoneNumber = trim($phoneNumber);
if (!in_array($phoneNumber, $availablePhoneNumbers)) {
unset($phoneNumbers[$key]);
}
}
return array_values($phoneNumbers);
}
private function checkAlert($idSites, $name, $period, $additionalEmails, $metricCondition, $metricValue, $comparedTo, $reportCondition, $reportUniqueId)
{
Piwik::checkUserHasViewAccess($idSites);
$this->validator->checkName($name);
$this->validator->checkPeriod($period);
$this->validator->checkComparedTo($period, $comparedTo);
$this->validator->checkMetricCondition($metricCondition);
$this->validator->checkReportCondition($reportCondition);
foreach ($idSites as $idSite) {
$this->validator->checkApiMethodAndMetric($idSite, $reportUniqueId, $metricValue);
}
$this->validator->checkAdditionalEmails($additionalEmails);
}
}