icingadb-web/library/Icingadb/Common/Macros.php
Alexander A. Klimov 3c8ed68cc6 Upgrade license from GPLv2 to GPLv2+
This was easy because only README.md and doc/01-About.md were redacted manually, everything else via:
git ls-files -z |xargs -0 perl -pi -e 's/Icinga GmbH \| GPLv2/Icinga GmbH | GPLv2+/'

This is legal because we have only merged PRs with label:cla/signed or made by Icinga staff:
https://github.com/Icinga/icingadb-web/pulls?page=1&q=is%3Apr+is%3Aclosed+-label%3Acla%2Fsigned+-author%3Anilmerg

This has no risk for us in people distributing their own version under GPLv3 only.
After all, we won't take their patches anyway, unless they sign our CLA.

This is the cleanest solution for having e.g. these in one address space:

* Icinga Web, GPLv2+
* K8s Web, AGPLv3
* Thirdparty, some LGPLv3 and Apache-2.0

Apropos, K8s Web is even v3-licensed on purpose, to have a stronger protection against cloud ops.
2025-11-21 13:31:24 +01:00

132 lines
4.2 KiB
PHP

<?php
/* Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2+ */
namespace Icinga\Module\Icingadb\Common;
use DateTime;
use Icinga\Application\Logger;
use Icinga\Module\Icingadb\Compat\CompatHost;
use Icinga\Module\Icingadb\Compat\CompatService;
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Model\Service;
use ipl\Orm\Query;
use ipl\Orm\ResultSet;
use function ipl\Stdlib\get_php_type;
trait Macros
{
/**
* Get the given string with macros being resolved
*
* @param string $input The string in which to look for macros
* @param Host|Service|CompatService|CompatHost $object The host or service used to resolve the macros
*
* @return string
*/
public function expandMacros(string $input, $object): string
{
if (preg_match_all('@\$([^\$\s]+)\$@', $input, $matches)) {
foreach ($matches[1] as $key => $value) {
$newValue = $this->resolveMacro($value, $object);
if ($newValue !== $value) {
$input = str_replace($matches[0][$key], $newValue, $input);
}
}
}
return $input;
}
/**
* Resolve a macro based on the given object
*
* @param string $macro The macro to resolve
* @param Host|Service|CompatService|CompatHost $object The host or service used to resolve the macros
*
* @return string
*/
public function resolveMacro(string $macro, $object): string
{
if ($object instanceof Host || (property_exists($object, 'type') && $object->type === 'host')) {
$objectType = 'host';
} else {
$objectType = 'service';
}
$path = null;
$macroType = $objectType;
$isCustomVar = false;
if (preg_match('/^((host|service)\.)?vars\.(.+)/', $macro, $matches)) {
if (! empty($matches[2])) {
$macroType = $matches[2];
}
$path = $matches[3];
$isCustomVar = true;
} elseif (preg_match('/^(\w+)\.(.+)/', $macro, $matches)) {
$macroType = $matches[1];
$path = $matches[2];
}
try {
if ($path !== null) {
if ($macroType !== $objectType) {
$value = $object->$macroType;
} else {
$value = $object;
}
$properties = explode('.', $path);
do {
$column = array_shift($properties);
if ($value instanceof Query || $value instanceof ResultSet || is_array($value)) {
Logger::debug(
'Failed to resolve property "%s" on a "%s" type.',
$isCustomVar ? 'vars' : $column,
get_php_type($value)
);
$value = null;
break;
}
if ($isCustomVar) {
$value = $value->vars[$path];
break;
}
$value = $value->$column;
} while (! empty($properties) && $value !== null);
} else {
$value = $object->$macro;
}
} catch (\Exception $e) {
$objectName = $object->name;
if ($objectType === 'service' && isset($object->host)) {
$objectName = $object->host->name . '!' . $objectName;
}
$value = null;
Logger::debug('Unable to resolve macro "%s" on object "%s". An error occured: %s', $macro, $objectName, $e);
}
if ($value instanceof Query || $value instanceof ResultSet || is_array($value)) {
Logger::debug(
'It is not allowed to use "%s" as a macro which produces a "%s" type as a result.',
$macro,
get_php_type($value)
);
$value = null;
}
if ($value instanceof DateTime) {
$value = $value->format(DateTime::ATOM);
} elseif (is_bool($value)) {
$value = $value ? 'y' : 'n';
}
return (string) ($value ?? $macro);
}
}