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 flexible intervals.
24 */
25class CFlexibleIntervalParser extends CParser {
26
27	private $simple_interval_parser;
28	private $time_period_parser;
29	private $update_interval;
30	private $time_period;
31
32	private $options = [
33		'usermacros' => false,
34		'lldmacros' => false
35	];
36
37	public function __construct($options = []) {
38		if (array_key_exists('usermacros', $options)) {
39			$this->options['usermacros'] = $options['usermacros'];
40		}
41		if (array_key_exists('lldmacros', $options)) {
42			$this->options['lldmacros'] = $options['lldmacros'];
43		}
44
45		$this->simple_interval_parser = new CSimpleIntervalParser([
46			'usermacros' => $this->options['usermacros'],
47			'lldmacros' => $this->options['lldmacros']
48		]);
49		$this->time_period_parser = new CTimePeriodParser([
50			'usermacros' => $this->options['usermacros'],
51			'lldmacros' => $this->options['lldmacros']
52		]);
53	}
54
55	/**
56	 * Parse the given flexible interval. The source string can contain macros separated by a forward slash.
57	 *
58	 * (simple_interval|{$M}|{#M})/(time_period|{$M}|{#M})
59	 *
60	 * @param string $source  Source string that needs to be parsed.
61	 * @param int    $pos     Position offset.
62	 */
63	public function parse($source, $pos = 0) {
64		$this->length = 0;
65		$this->match = '';
66		$this->update_interval = '';
67		$this->time_period = '';
68
69		$p = $pos;
70
71		if ($this->simple_interval_parser->parse($source, $p) == self::PARSE_FAIL) {
72			return self::PARSE_FAIL;
73		}
74		$update_interval = $this->simple_interval_parser->getMatch();
75		$p += $this->simple_interval_parser->getLength();
76
77		if (!isset($source[$p]) || $source[$p] !== '/') {
78			return self::PARSE_FAIL;
79		}
80		$p++;
81
82		if ($this->time_period_parser->parse($source, $p) == self::PARSE_FAIL) {
83			return self::PARSE_FAIL;
84		}
85		$this->update_interval = $update_interval;
86		$this->time_period = $this->time_period_parser->getMatch();
87		$p += $this->time_period_parser->getLength();
88
89		$this->length = $p - $pos;
90		$this->match = substr($source, $pos, $this->length);
91
92		return isset($source[$p]) ? self::PARSE_SUCCESS_CONT : self::PARSE_SUCCESS;
93	}
94
95	/**
96	 * Returns matched update interval. Can contain macro.
97	 *
98	 * @return string
99	 */
100	public function getUpdateInterval() {
101		return $this->update_interval;
102	}
103
104	/**
105	 * Returns matched time period. Can contain macro.
106	 *
107	 * @return string
108	 */
109	public function getTimePeriod() {
110		return $this->time_period;
111	}
112}
113