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 pathValidator.php
More file actions
executable file
·166 lines (137 loc) · 4.72 KB
/
Validator.php
File metadata and controls
executable file
·166 lines (137 loc) · 4.72 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
<?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\Period;
use Piwik\Piwik;
use Piwik\Plugins\API\ProcessedReport;
use Piwik\Plugins\MobileMessaging\API as APIMobileMessaging;
use Exception;
/**
*
* @package Piwik_Alerts
*/
class Validator
{
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 = APIMobileMessaging::getInstance()->getActivatedPhoneNumbers();
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 (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
* @return boolean
*/
public function checkApiMethodAndMetric($idSite, $apiMethodUniqueId, $metric)
{
if (empty($apiMethodUniqueId) || false === strpos($apiMethodUniqueId, '_')) {
throw new Exception(Piwik::translate('CustomAlerts_InvalidReport'));
}
$processedReport = new ProcessedReport();
if (!$processedReport->isValidReportForSite($idSite, $apiMethodUniqueId)) {
throw new Exception(Piwik::translate('CustomAlerts_InvalidReport'));
}
if (!$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']));
}
}
}