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 function macros like "{{ITEM.VALUE}.func()}". 24 */ 25class CMacroFunctionParser extends CParser { 26 27 /** 28 * Parser for generic macros. 29 * 30 * @var CMacroParser 31 */ 32 private $macro_parser; 33 34 /** 35 * Parser for trigger functions. 36 * 37 * @var C10FunctionParser 38 */ 39 private $function_parser; 40 41 /** 42 * @param array $options Parser options. 43 * @param array|bool $options['macros'] The list of macros, for example ['{ITEM.VALUE}', '{ITEM.LASTVALUE}'] 44 * @param int $options['ref_type'] Reference options. 45 */ 46 public function __construct(array $options) { 47 $this->macro_parser = new CMacroParser($options); 48 $this->function_parser = new C10FunctionParser(); 49 } 50 51 /** 52 * @param string $source 53 * @param int $pos 54 * 55 * @return int 56 */ 57 public function parse($source, $pos = 0) { 58 $this->length = 0; 59 $this->match = ''; 60 61 $p = $pos; 62 63 if (!isset($source[$p]) || $source[$p] !== '{') { 64 return self::PARSE_FAIL; 65 } 66 $p++; 67 68 if ($this->macro_parser->parse($source, $p) == CParser::PARSE_FAIL) { 69 return self::PARSE_FAIL; 70 } 71 $p += $this->macro_parser->getLength(); 72 73 if (!isset($source[$p]) || $source[$p] !== '.') { 74 return self::PARSE_FAIL; 75 } 76 $p++; 77 78 if ($this->function_parser->parse($source, $p) == CParser::PARSE_FAIL) { 79 return self::PARSE_FAIL; 80 } 81 $p += $this->function_parser->getLength(); 82 83 if (!isset($source[$p]) || $source[$p] !== '}') { 84 return self::PARSE_FAIL; 85 } 86 $p++; 87 88 $this->length = $p - $pos; 89 $this->match = substr($source, $pos, $this->length); 90 91 return (isset($source[$pos + $this->length]) ? self::PARSE_SUCCESS_CONT : self::PARSE_SUCCESS); 92 } 93 94 /** 95 * Returns macro parser. 96 * 97 * @return string 98 */ 99 public function getMacroParser() { 100 return $this->macro_parser; 101 } 102 103 /** 104 * Returns function parser. 105 * 106 * @return string 107 */ 108 public function getFunctionParser() { 109 return $this->function_parser; 110 } 111} 112