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
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2016 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
class CTimePeriodValidator extends CValidator {
/**
* Assume that period string contains multiple periods separated by ';'.
*
* @var bool
*/
public $allowMultiple = true;
/**
* Validate multiple time periods.
* Time periods is a string with format:
* 'day1-day2,time1-time2;interval2;interval3;...' (day2 and last ';' are optional)
* Examples:
* 5-7,00:00-09:00;1,10:00-20:00;
* 5,0:0-9:0
*
* @param string $periods
*
* @throws InvalidArgumentException
* @return bool
*/
public function validate($periods) {
if (zbx_empty($periods)) {
$this->setError(_('Empty time period.'));
return false;
}
if ($this->allowMultiple) {
// remove one last ';'
if ($periods[strlen($periods) - 1] === ';') {
$periods = substr($periods, 0, strlen($periods) - 1);
}
$periods = explode(';', $periods);
}
else {
$periods = [$periods];
}
foreach ($periods as $period) {
if (!$this->validateSinglePeriod($period)) {
return false;
}
}
return true;
}
/**
* Validate single time period.
* Time period is a string with format:
* 'day1-day2,time1-time2;' (day2 and ';' are optional)
* Examples:
* 5-7,00:00-09:00
* 5,0:00-9:00
*
* @param string $period
*
* @return bool
*/
protected function validateSinglePeriod($period) {
$daysRegExp = '(?P<day1>[1-7])(-(?P<day2>[1-7]))?';
$time1RegExp = '(?P<hour1>20|21|22|23|24|[0-1]\d|\d):(?P<min1>[0-5]\d)';
$time2RegExp = '(?P<hour2>20|21|22|23|24|[0-1]\d|\d):(?P<min2>[0-5]\d)';
if (!preg_match('#^'.$daysRegExp.','.$time1RegExp.'-'.$time2RegExp.'$#', $period, $matches)) {
$this->setError(_s('Incorrect time period "%1$s".', $period));
return false;
}
if ($matches['hour2'] == '24' && $matches['min2'] != 0) {
$this->setError(_s('Incorrect time period "%1$s".', $period));
return false;
}
if (!zbx_empty($matches['day2']) && ($matches['day1'] > $matches['day2'])) {
$this->setError(_s('Incorrect time period "%1$s" start day must be less or equal to end day.', $period));
return false;
}
if (($matches['hour1'] > $matches['hour2'])
|| (($matches['hour1'] == $matches['hour2']) && ($matches['min1'] >= $matches['min2']))) {
$this->setError(_s('Incorrect time period "%1$s" start time must be less than end time.', $period));
return false;
}
return true;
}
}
|