From a6bbc68bcb54a0a0a95c048045ff0142c99b0f30 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Tue, 6 Dec 2016 15:04:26 +0100 Subject: [PATCH] Highlight state in HTML plugin output too fixes #12223 --- .../views/helpers/PluginOutput.php | 64 ++++++++++++++----- 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/modules/monitoring/application/views/helpers/PluginOutput.php b/modules/monitoring/application/views/helpers/PluginOutput.php index 0de6cf14c..92500e4e2 100644 --- a/modules/monitoring/application/views/helpers/PluginOutput.php +++ b/modules/monitoring/application/views/helpers/PluginOutput.php @@ -1,6 +1,8 @@ fixLinks($output); + $output = $this->processHtml($output); $output = '
' . $output . '
'; } else { $output = '
' . $output . '
'; @@ -91,32 +93,60 @@ class Zend_View_Helper_PluginOutput extends Zend_View_Helper_Abstract } /** - * Replace classic Icinga CGI links with Icinga Web 2 links + * Replace classic Icinga CGI links with Icinga Web 2 links and color state information, if any * * @param string $html * * @return string */ - protected function fixLinks($html) + protected function processHtml($html) { - $dom = new DOMDocument(); - $dom->loadXML('
' . $html . '
', LIBXML_NOERROR | LIBXML_NOWARNING); - $dom->preserveWhiteSpace = false; - $links = $dom->getElementsByTagName('a'); - foreach ($links as $link) { - /** @var \DOMElement $link */ - $href = $link->getAttribute('href'); - if (preg_match('~^/cgi\-bin/status\.cgi\?(.+)$~', $href, $m)) { - parse_str($m[1], $params); - if (isset($params['host'])) { - $link->setAttribute('href', $this->view->baseUrl( - '/monitoring/host/show?host=' . urlencode($params['host'] - ))); + $pattern = '/[([](OK|WARNING|CRITICAL|UNKNOWN)[)\]]/'; + $doc = new DOMDocument(); + $doc->loadXML('
' . $html . '
', LIBXML_NOERROR | LIBXML_NOWARNING); + $dom = new RecursiveIteratorIterator(new DomNodeIterator($doc), RecursiveIteratorIterator::SELF_FIRST); + $nodesToRemove = array(); + foreach ($dom as $node) { + /** @var \DOMNode $node */ + if ($node->nodeType === XML_TEXT_NODE) { + $start = 0; + while (preg_match($pattern, $node->nodeValue, $match, PREG_OFFSET_CAPTURE, $start)) { + $offsetLeft = $match[0][1]; + $matchLength = strlen($match[0][0]); + $leftLength = $offsetLeft - $start; + if ($leftLength) { + $text = new DOMText(substr($node->nodeValue, $start, $leftLength)); + $node->parentNode->insertBefore($text, $node); + } + $span = $doc->createElement('span', $match[0][0]); + $span->setAttribute('class', 'state-' . strtolower($match[1][0])); + $node->parentNode->insertBefore($span, $node); + $start = $offsetLeft + $matchLength; + } + if ($start) { + $nodesToRemove[] = $node; + } + } elseif ($node->nodeType === XML_ELEMENT_NODE) { + /** @var \DOMElement $node */ + if ($node->tagName === 'a' + && preg_match('~^/cgi\-bin/status\.cgi\?(.+)$~', $node->getAttribute('href'), $match) + ) { + parse_str($match[1], $params); + if (isset($params['host'])) { + $node->setAttribute( + 'href', + $this->view->baseUrl('/monitoring/host/show?host=' . urlencode($params['host'])) + ); + } } } } + foreach ($nodesToRemove as $node) { + /** @var \DOMNode $node */ + $node->parentNode->removeChild($node); + } - return substr($dom->saveHTML(), 5, -7); + return substr($doc->saveHTML(), 5, -7); } /**