-
Notifications
You must be signed in to change notification settings - Fork 20
/
Validator.php
executable file
·168 lines (140 loc) · 4.92 KB
/
Validator.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
<?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\CustomAlerts;
use Exception;
use Piwik\Common;
use Piwik\Context;
use Piwik\Piwik;
use Piwik\Plugins\API\ProcessedReport;
/**
*
*/
class Validator
{
/**
* @var ProcessedReport
*/
private $processedReport;
public function __construct(ProcessedReport $processedReport)
{
$this->processedReport = $processedReport;
}
public function checkAdditionalEmails($additionalEmails)
{
foreach ($additionalEmails as $email) {
if (!Piwik::isValidEmailString($email)) {
throw new \Exception(Piwik::translate('UsersManager_ExceptionInvalidEmail') . ' (' . $email . ')');
}
}
}
public function filterPhoneNumbers($phoneNumbers)
{
$availablePhoneNumbers = (new \Piwik\Plugins\MobileMessaging\Model())->getActivatedPhoneNumbers(Piwik::getCurrentUserLogin());
foreach ($phoneNumbers as $key => &$phoneNumber) {
$phoneNumber = trim($phoneNumber);
if (!in_array($phoneNumber, $availablePhoneNumbers)) {
unset($phoneNumbers[$key]);
}
}
return array_values($phoneNumbers);
}
public function checkPeriod($period)
{
if (!$this->isValidPeriod($period)) {
throw new Exception(Piwik::translate('CustomAlerts_InvalidPeriod'));
}
}
public function isValidPeriod($period)
{
return in_array($period, array('day', 'week', 'month'));
}
public function checkName($name)
{
if (empty($name)) {
throw new Exception(Piwik::translate("General_PleaseSpecifyValue", "name"));
}
if (Common::mb_strlen($name) > 100) {
throw new Exception(Piwik::translate("CustomAlerts_ParmeterIsTooLong", array(Piwik::translate('General_Name'), 100)));
}
}
public function isValidComparableDate($period, $comparedToDate)
{
$dates = Processor::getComparablesDates();
if (!array_key_exists($period, $dates)) {
return false;
}
return in_array($comparedToDate, array_values($dates[$period]));
}
public function isValidGroupCondition($condition)
{
$conditions = Processor::getGroupConditions();
$conditions = array_values($conditions);
return in_array($condition, $conditions);
}
public function isValidMetricCondition($condition)
{
$conditions = Processor::getMetricConditions();
$conditions = array_values($conditions);
return in_array($condition, $conditions);
}
public function checkMetricCondition($condition)
{
if (empty($condition)) {
throw new Exception(Piwik::translate('CustomAlerts_InvalidMetricCondition'));
}
if (!self::isValidMetricCondition($condition)) {
throw new Exception(Piwik::translate('CustomAlerts_InvalidMetricCondition'));
}
}
public function checkReportCondition($condition)
{
if (!empty($condition) && !self::isValidGroupCondition($condition)) {
throw new Exception(Piwik::translate('CustomAlerts_InvalidReportCondition'));
}
}
public function checkComparedTo($period, $comparedTo)
{
if (!self::isValidComparableDate($period, $comparedTo)) {
throw new Exception(Piwik::translate('CustomAlerts_InvalidComparableDate'));
}
}
/**
* Checks whether a report + metric exists for
* the given idSites and if the a dimension is
* given (requires report_condition, report_matched)
*
* @param int $idSite
* @param string $apiMethodUniqueId for example MultiSites.getAll
* @param string $metric
* @throws \Exception
*/
public function checkApiMethodAndMetric($idSite, $apiMethodUniqueId, $metric)
{
Context::changeIdSite($idSite, function () use ($idSite, $apiMethodUniqueId, $metric) {
if (empty($apiMethodUniqueId) || false === strpos($apiMethodUniqueId, '_')) {
throw new Exception(Piwik::translate('CustomAlerts_InvalidReport'));
}
if (!$this->processedReport->isValidReportForSite($idSite, $apiMethodUniqueId)) {
throw new Exception(Piwik::translate('CustomAlerts_InvalidReport'));
}
if (!$this->processedReport->isValidMetricForReport($metric, $idSite, $apiMethodUniqueId)) {
throw new Exception(Piwik::translate('CustomAlerts_InvalidMetric'));
}
});
}
/**
* @param $alert
* @throws \Exception
*/
public function checkUserHasPermissionForAlert($alert)
{
if (Piwik::getCurrentUserLogin() != $alert['login']) {
throw new Exception(Piwik::translate('CustomAlerts_AccessException', $alert['idalert']));
}
}
}