From f66ab8dae2f3d0418759f81a3a64dc9517a62449 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 15:17:31 +0200 Subject: Adding upstream version 1.10.2. Signed-off-by: Daniel Baumann --- library/Director/Web/Widget/ShowConfigFile.php | 106 +++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 library/Director/Web/Widget/ShowConfigFile.php (limited to 'library/Director/Web/Widget/ShowConfigFile.php') diff --git a/library/Director/Web/Widget/ShowConfigFile.php b/library/Director/Web/Widget/ShowConfigFile.php new file mode 100644 index 0000000..77d32cf --- /dev/null +++ b/library/Director/Web/Widget/ShowConfigFile.php @@ -0,0 +1,106 @@ +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 "%s" {', + $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"(.+?)"\s{/m'; + + return preg_replace_callback( + $pattern, + [$this, 'linkObject'], + $config + ); + } + + protected function highlight($what, $line, $severity) + { + $lines = explode("\n", $what); + $lines[$line - 1] = '' . $lines[$line - 1] . ''; + return implode("\n", $lines); + } +} -- cgit v1.2.3