Highlight state in HTML plugin output too

fixes #12223
This commit is contained in:
Eric Lippmann 2016-12-06 15:04:26 +01:00
parent eef982f3c3
commit a6bbc68bcb
1 changed files with 47 additions and 17 deletions

View File

@ -1,6 +1,8 @@
<?php <?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */ /* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
use Icinga\Web\Dom\DomNodeIterator;
/** /**
* Plugin output renderer * Plugin output renderer
*/ */
@ -81,7 +83,7 @@ class Zend_View_Helper_PluginOutput extends Zend_View_Helper_Abstract
$output = preg_replace('/,(?=[^\s])/', ', ', $output); $output = preg_replace('/,(?=[^\s])/', ', ', $output);
if (! $raw) { if (! $raw) {
if ($isHtml) { if ($isHtml) {
$output = $this->fixLinks($output); $output = $this->processHtml($output);
$output = '<div class="plugin-output">' . $output . '</div>'; $output = '<div class="plugin-output">' . $output . '</div>';
} else { } else {
$output = '<div class="plugin-output preformatted">' . $output . '</div>'; $output = '<div class="plugin-output preformatted">' . $output . '</div>';
@ -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 * @param string $html
* *
* @return string * @return string
*/ */
protected function fixLinks($html) protected function processHtml($html)
{ {
$dom = new DOMDocument(); $pattern = '/[([](OK|WARNING|CRITICAL|UNKNOWN)[)\]]/';
$dom->loadXML('<div>' . $html . '</div>', LIBXML_NOERROR | LIBXML_NOWARNING); $doc = new DOMDocument();
$dom->preserveWhiteSpace = false; $doc->loadXML('<div>' . $html . '</div>', LIBXML_NOERROR | LIBXML_NOWARNING);
$links = $dom->getElementsByTagName('a'); $dom = new RecursiveIteratorIterator(new DomNodeIterator($doc), RecursiveIteratorIterator::SELF_FIRST);
foreach ($links as $link) { $nodesToRemove = array();
/** @var \DOMElement $link */ foreach ($dom as $node) {
$href = $link->getAttribute('href'); /** @var \DOMNode $node */
if (preg_match('~^/cgi\-bin/status\.cgi\?(.+)$~', $href, $m)) { if ($node->nodeType === XML_TEXT_NODE) {
parse_str($m[1], $params); $start = 0;
if (isset($params['host'])) { while (preg_match($pattern, $node->nodeValue, $match, PREG_OFFSET_CAPTURE, $start)) {
$link->setAttribute('href', $this->view->baseUrl( $offsetLeft = $match[0][1];
'/monitoring/host/show?host=' . urlencode($params['host'] $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);
} }
/** /**