1<?php 2/* 3** Zabbix 4** Copyright (C) 2001-2021 Zabbix SIA 5** 6** This program is free software; you can redistribute it and/or modify 7** it under the terms of the GNU General Public License as published by 8** the Free Software Foundation; either version 2 of the License, or 9** (at your option) any later version. 10** 11** This program is distributed in the hope that it will be useful, 12** but WITHOUT ANY WARRANTY; without even the implied warranty of 13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14** GNU General Public License for more details. 15** 16** You should have received a copy of the GNU General Public License 17** along with this program; if not, write to the Free Software 18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19**/ 20 21 22/** 23 * A parser for a list of time periods separated by a semicolon. 24 */ 25class CTimePeriodsParser extends CParser { 26 27 private $time_period_parser; 28 29 private $periods = []; 30 private $options = ['usermacros' => false]; 31 32 public function __construct($options = []) { 33 if (array_key_exists('usermacros', $options)) { 34 $this->options['usermacros'] = $options['usermacros']; 35 } 36 37 $this->time_period_parser = new CTimePeriodParser(['usermacros' => $this->options['usermacros']]); 38 } 39 40 /** 41 * Parse the given periods separated by a semicolon. 42 * 43 * @param string $source Source string that needs to be parsed. 44 * @param int $pos Position offset. 45 */ 46 public function parse($source, $pos = 0) { 47 $this->length = 0; 48 $this->match = ''; 49 $this->periods = []; 50 51 $periods = []; 52 $p = $pos; 53 $offset = 0; 54 55 while (isset($source[$p + $offset])) { 56 if ($this->time_period_parser->parse($source, $p + $offset) == self::PARSE_FAIL) { 57 break; 58 } 59 $p += $offset + $this->time_period_parser->getLength(); 60 $periods[] = $this->time_period_parser->getMatch(); 61 62 if (isset($source[$p]) && $source[$p] !== ';') { 63 break; 64 } 65 $offset = 1; 66 } 67 68 if ($p == $pos || isset($source[$p])) { 69 return self::PARSE_FAIL; 70 } 71 72 $this->length = $p - $pos; 73 $this->match = substr($source, $pos, $this->length); 74 $this->periods = $periods; 75 76 return self::PARSE_SUCCESS; 77 } 78 79 /** 80 * Retrieve the time periods. 81 * 82 * @return array 83 */ 84 public function getPeriods() { 85 return $this->periods; 86 } 87} 88