summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Common/Macros.php
blob: 4842c27187d93274294723be2b7ecdab25f70705 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php

/* Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Common;

use Icinga\Application\Logger;
use Icinga\Module\Icingadb\Compat\CompatHost;
use Icinga\Module\Icingadb\Compat\CompatService;
use Icinga\Module\Icingadb\Model\Host;
use ipl\Orm\Model;
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 Model|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 Model|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) {
            $value = null;
            Logger::debug('Unable to resolve macro "%s". An error occurred: %s', $macro, $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;
        }

        return $value !== null ? $value : $macro;
    }
}