summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Widget/ShowConfigFile.php
blob: 77d32cf3e0517428f700c13400bbcb925e5962e9 (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
<?php

namespace Icinga\Module\Director\Web\Widget;

use ipl\Html\HtmlDocument;
use Icinga\Module\Director\IcingaConfig\IcingaConfigFile;
use ipl\Html\Html;
use ipl\Html\HtmlString;
use gipfl\IcingaWeb2\Link;
use gipfl\Translation\TranslationHelper;

class ShowConfigFile extends HtmlDocument
{
    use TranslationHelper;

    protected $file;

    protected $highlight;

    protected $highlightSeverity;

    public function __construct(
        IcingaConfigFile $file,
        $highlight = null,
        $highlightSeverity = null
    ) {
        $this->file = $file;
        $this->highlight         = $highlight;
        $this->highlightSeverity = $highlightSeverity;
    }

    /**
     * @throws \Icinga\Exception\IcingaException
     */
    protected function assemble()
    {
        $source = $this->linkObjects(Html::escape($this->file->getContent()));
        if ($this->highlight) {
            $source = $this->highlight(
                $source,
                $this->highlight,
                $this->highlightSeverity
            );
        }

        $this->add(Html::tag(
            'pre',
            ['class' => 'generated-config'],
            new HtmlString($source)
        ));
    }

    /**
     * @param $match
     * @return string
     * @throws \Icinga\Exception\IcingaException
     * @throws \Icinga\Exception\ProgrammingError
     */
    protected function linkObject($match)
    {
        if ($match[2] === 'Service') {
            return $match[0];
        }
        $controller = $match[2];

        if ($match[2] === 'CheckCommand') {
            $controller = 'command';
        }

        $name = $this->decode($match[3]);
        return sprintf(
            '%s %s &quot;%s&quot; {',
            $match[1],
            $match[2],
            Link::create(
                $name,
                'director/' . $controller,
                ['name' => $name],
                ['data-base-target' => '_next']
            )
        );
    }

    protected function decode($str)
    {
        return htmlspecialchars_decode($str, ENT_COMPAT | ENT_SUBSTITUTE | ENT_HTML5);
    }

    protected function linkObjects($config)
    {
        $pattern = '/^(object|template)\s([A-Z][A-Za-z]*?)\s&quot;(.+?)&quot;\s{/m';

        return preg_replace_callback(
            $pattern,
            [$this, 'linkObject'],
            $config
        );
    }

    protected function highlight($what, $line, $severity)
    {
        $lines = explode("\n", $what);
        $lines[$line - 1] = '<span class="highlight ' . $severity . '">' . $lines[$line - 1] . '</span>';
        return implode("\n", $lines);
    }
}