From 451be1e97f9bdbe45a153fb3346eccd9a21ff7e8 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Fri, 31 Jul 2015 15:31:41 +0200 Subject: [PATCH] monitoring/hooks: Add TicketHook::createLinks() Instead of calling preg_replace_callback w/o error handling in our view scripts, TicketHook::createLinks() will take care of that. refs #9611 --- library/Icinga/Web/Hook/TicketHook.php | 93 +++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 2 deletions(-) diff --git a/library/Icinga/Web/Hook/TicketHook.php b/library/Icinga/Web/Hook/TicketHook.php index 1e1171165..265a0641e 100644 --- a/library/Icinga/Web/Hook/TicketHook.php +++ b/library/Icinga/Web/Hook/TicketHook.php @@ -3,6 +3,12 @@ namespace Icinga\Web\Hook; +use ErrorException; +use Exception; +use Icinga\Application\Icinga; +use Icinga\Application\Logger; +use Icinga\Exception\IcingaException; + /** * Icinga Web Ticket Hook base class * @@ -15,6 +21,13 @@ namespace Icinga\Web\Hook; */ abstract class TicketHook { + /** + * Last error, if any + * + * @var string|null + */ + protected $lastError; + /** * Constructor must live without arguments right now * @@ -28,15 +41,91 @@ abstract class TicketHook /** * Overwrite this function if you want to do some initialization stuff - * - * @return void */ protected function init() { } + /** + * Set the hook as failed w/ the given message + * + * @param string $message Error message or error format string + * @param mixed ...$arg Format string argument + */ + private function fail($message) + { + $args = array_slice(func_get_args(), 1); + $lastError = vsprintf($message, $args); + Logger::debug($lastError); + $this->lastError = $lastError; + } + + /** + * Get the last error, if any + * + * @return string|null + */ + public function getLastError() + { + return $this->lastError; + } + + /** + * Get the pattern + * + * @return string + */ abstract public function getPattern(); + /** + * Create a link for each matched element in the subject text + * + * @param array $match Array of matched elements according to {@link getPattern()} + * + * @return string Replacement string + */ abstract public function createLink($match); + /** + * Create links w/ {@link createLink()} in the given text that matches to the subject from {@link getPattern()} + * + * In case of errors a debug message is recorded to the log and any subsequent call to {@link createLinks()} will + * be a no-op. + * + * @param string $text + * + * @return string + */ + final public function createLinks($text) + { + if ($this->lastError !== null) { + return $text; + } + + try { + $pattern = $this->getPattern(); + } catch (Exception $e) { + $this->fail('Can\'t create ticket links: Retrieving the pattern failed: %s', IcingaException::describe($e)); + return $text; + } + if (empty($pattern)) { + $this->fail('Can\'t create ticket links: Pattern is empty'); + return $text; + } + try { + $text = preg_replace_callback( + $pattern, + array($this, 'createLink'), + $text + ); + } catch (ErrorException $e) { + $this->fail('Can\'t create ticket links: Pattern is invalid: %s', IcingaException::describe($e)); + return $text; + } catch (Exception $e) { + $this->fail('Can\'t create ticket links: %s', IcingaException::describe($e)); + return $text; + } + + return $text; + } }