Source
xxxxxxxxxx
* @param array $parameters [IN] Function parameters - an array containing search list and replacement list.
<?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
class CMacroFunction {
/**
* Calculates regular expression substitution. Returns UNRESOLVED_MACRO_STRING in case of incorrect function
* parameters or regular expression.
*
* @param string $value [IN] The input value.
* @param array $parameters [IN] The function parameters.
* @param bool $insensitive [IN] Case insensitive match.
*
* @return string
*/
private static function macrofuncRegsub(string $value, array $parameters, bool $insensitive): string {
if (count($parameters) != 2) {
return UNRESOLVED_MACRO_STRING;
}
set_error_handler(function ($errno, $errstr) {});
$rc = preg_match('/'.$parameters[0].'/'.($insensitive ? 'i' : ''), $value, $matches);
restore_error_handler();
if ($rc === false) {
return UNRESOLVED_MACRO_STRING;
}
$macro_values = [];
for ($i = 0; $i <= 9; $i++) {
$macro_values['\\'.$i] = array_key_exists($i, $matches) ? $matches[$i] : '';
}
return strtr($parameters[1], $macro_values);
}
/**
* Calculates number formatting macro function. Returns UNRESOLVED_MACRO_STRING in case of incorrect function
* parameters or value. Formatting is not applied to integer values.
*
* @param string $value [IN] The input value.
* @param array $parameters [IN] The function parameters.
*
* @return string
*/
private static function macrofuncFmtnum(string $value, array $parameters): string {
if (count($parameters) != 1 || $parameters[0] == '') {
return UNRESOLVED_MACRO_STRING;
}
$parser = new CNumberParser(['with_float' => false]);
if ($parser->parse($value) == CParser::PARSE_SUCCESS) {
return $value;
}
$parser = new CNumberParser();
if ($parser->parse($value) != CParser::PARSE_SUCCESS) {
return UNRESOLVED_MACRO_STRING;
}
if (!ctype_digit($parameters[0]) || (int) $parameters[0] > 20) {
return UNRESOLVED_MACRO_STRING;
}
return sprintf('%.'.$parameters[0].'f', (float) $value);
}
/**
* Calculates time formatting macro function. Returns UNRESOLVED_MACRO_STRING in case of incorrect function
* parameters or value.
*
* @param string $value [IN] The input value.
* @param array $parameters [IN] The function parameters.
*
* @return string
*/