From 451be1e97f9bdbe45a153fb3346eccd9a21ff7e8 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Fri, 31 Jul 2015 15:31:41 +0200 Subject: [PATCH 01/10] 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; + } } From 6481e9756553b972d994646f1ab2961ccf00997e Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 3 Aug 2015 10:39:42 +0200 Subject: [PATCH 02/10] Add createTicketLinks() view helper refs #9611 --- .../views/helpers/CreateTicketLinks.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 application/views/helpers/CreateTicketLinks.php diff --git a/application/views/helpers/CreateTicketLinks.php b/application/views/helpers/CreateTicketLinks.php new file mode 100644 index 000000000..be92c1f26 --- /dev/null +++ b/application/views/helpers/CreateTicketLinks.php @@ -0,0 +1,23 @@ +view->tickets; + /** @var \Icinga\Web\Hook\TicketHook $tickets */ + return isset($tickets) ? $tickets->createLinks($text) : $text; + } +} From 498585d8af0b9c35116d74a6225e17a0c1562ca6 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 3 Aug 2015 10:43:42 +0200 Subject: [PATCH 03/10] monitoring: Use createTicketLinks helper when showing a host's history refs #9611 --- .../application/views/scripts/host/history.phtml | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/modules/monitoring/application/views/scripts/host/history.phtml b/modules/monitoring/application/views/scripts/host/history.phtml index 36d43ee95..2ad6f428c 100644 --- a/modules/monitoring/application/views/scripts/host/history.phtml +++ b/modules/monitoring/application/views/scripts/host/history.phtml @@ -116,15 +116,7 @@ function contactsLink($match, $view) {
timestamp); ?> - tickets ? preg_replace_callback( - $this->tickets->getPattern(), - array($this->tickets, 'createLink'), - $msg -) : $msg; - -?> + translate('%s on %s', 'Service running on host'), @@ -148,7 +140,7 @@ $output = $this->tickets ? preg_replace_callback(
- icon($icon, $title); ?> + icon($icon, $title); ?> createTicketLinks($msg) ?>
From e555e3e3b22752296048f92e7f8ef7778a920be1 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 3 Aug 2015 10:44:53 +0200 Subject: [PATCH 04/10] monitoring: Use createTicketLinks helper when showing a service's history refs #9611 --- .../views/scripts/service/history.phtml | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/modules/monitoring/application/views/scripts/service/history.phtml b/modules/monitoring/application/views/scripts/service/history.phtml index ac443c370..b600e9fb9 100644 --- a/modules/monitoring/application/views/scripts/service/history.phtml +++ b/modules/monitoring/application/views/scripts/service/history.phtml @@ -114,15 +114,7 @@ function contactsLink($match, $view) {
timestamp); ?> - tickets ? preg_replace_callback( - $this->tickets->getPattern(), - array($this->tickets, 'createLink'), - $msg -) : $msg; - -?> + translate('%s on %s', 'Service running on host'), $this->escape($event->service_display_name), @@ -130,11 +122,11 @@ $output = $this->tickets ? preg_replace_callback( ) ?>
- icon($icon, $title); ?> + icon($icon, $title); ?> createTicketLinks($msg) ?>
- \ No newline at end of file + From b6caeb6c179bab82a4054e426cb2f2e387154ea7 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 3 Aug 2015 10:53:51 +0200 Subject: [PATCH 05/10] monitoring: Use createTicketLinks helper when showing comments of an object refs #9611 --- .../views/scripts/show/components/comments.phtml | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/modules/monitoring/application/views/scripts/show/components/comments.phtml b/modules/monitoring/application/views/scripts/show/components/comments.phtml index ded9d3836..3db9de338 100644 --- a/modules/monitoring/application/views/scripts/show/components/comments.phtml +++ b/modules/monitoring/application/views/scripts/show/components/comments.phtml @@ -52,16 +52,8 @@ if (empty($object->comments) && ! $addLink) { comments as $comment): ?> tickets ? preg_replace_callback( - $this->tickets->getPattern(), - array($this->tickets, 'createLink'), - $this->escape($comment->comment) -) : $this->escape($comment->comment); - - // Form is unset if the current user lacks the respective permission -if (isset($delCommentForm)) { +if (isset($delCommentForm)) { $deleteButton = clone($delCommentForm); $deleteButton->populate( array( @@ -80,7 +72,7 @@ if (isset($delCommentForm)) { timeAgo($comment->timestamp) ?> -

(translate('Comment'); ?>): ', $commentText) ?>

+

(translate('Comment'); ?>): ', $this->createTicketLinks($comment->comment)) ?>

From a379a448d9f412781b91f655c0210afc9411459e Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 3 Aug 2015 10:58:57 +0200 Subject: [PATCH 06/10] monitoring: Fix code style in the show/components/comments.phtml view script refs #9611 --- .../scripts/show/components/comments.phtml | 72 +++++++++---------- 1 file changed, 32 insertions(+), 40 deletions(-) diff --git a/modules/monitoring/application/views/scripts/show/components/comments.phtml b/modules/monitoring/application/views/scripts/show/components/comments.phtml index 3db9de338..37a832358 100644 --- a/modules/monitoring/application/views/scripts/show/components/comments.phtml +++ b/modules/monitoring/application/views/scripts/show/components/comments.phtml @@ -1,7 +1,5 @@ hasPermission('monitoring/command/comment/add')) { /** @var \Icinga\Module\Monitoring\Object\MonitoredObject $object */ if ($object->getType() === $object::TYPE_HOST) { @@ -28,55 +26,49 @@ if ($this->hasPermission('monitoring/command/comment/add')) { ); } } - if (empty($object->comments) && ! $addLink) { return; } - ?> translate('Comments'); - if (! empty($object->comments) && $addLink) { echo '
' . $addLink; } - ?> -comments)): ?> - - -
    - comments as $comment): ?> - populate( - array( - 'comment_id' => $comment->id, - 'comment_is_service' => isset($comment->service_description) - ) - ); -} else { - $deleteButton = ''; -} - - ?> -
  • -

    - escape($comment->author) ?> - timeAgo($comment->timestamp) ?> + comments)): + echo $addLink; + else: ?> +
      + comments as $comment): + // Form is unset if the current user lacks the respective permission + if (isset($delCommentForm)) { + $deleteButton = clone($delCommentForm); + $deleteButton->populate( + array( + 'comment_id' => $comment->id, + 'comment_is_service' => isset($comment->service_description) + ) + ); + } else { + $deleteButton = ''; + } + ?> +
    • +

      + escape($comment->author) ?> + timeAgo($comment->timestamp) ?> -

      -

      (translate('Comment'); ?>): ', $this->createTicketLinks($comment->comment)) ?>

      -
    • - -
    - - +

    +

    + (translate('Comment') ?>): + ', $this->createTicketLinks($comment->comment)) ?> +

    +
  • + +
+ From d5e2f92aa9b9b36cd1368108c9f40f7bb98e7442 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 3 Aug 2015 11:00:46 +0200 Subject: [PATCH 07/10] monitoring: Use createTicketLinks helper when showing downtimes of an object refs #9611 --- .../views/scripts/show/components/downtime.phtml | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/modules/monitoring/application/views/scripts/show/components/downtime.phtml b/modules/monitoring/application/views/scripts/show/components/downtime.phtml index 46b0f5a3a..70f16bb18 100644 --- a/modules/monitoring/application/views/scripts/show/components/downtime.phtml +++ b/modules/monitoring/application/views/scripts/show/components/downtime.phtml @@ -58,13 +58,6 @@ if (empty($object->comments) && ! $addLink) { downtimes as $downtime): ?> tickets ? preg_replace_callback( - $this->tickets->getPattern(), - array($this->tickets, 'createLink'), - $this->escape($downtime->comment) - ) : $this->escape($downtime->comment); - if ((bool) $downtime->is_in_effect) { $state = sprintf( $this->translate('in downtime %s', 'Last format parameter represents the time in downtime'), @@ -86,7 +79,7 @@ if (empty($object->comments) && ! $addLink) { // Form is unset if the current user lacks the respective permission -if (isset($delDowntimeForm)) { +if (isset($delDowntimeForm)) { $deleteButton = clone($delDowntimeForm); $deleteButton->populate( array( @@ -106,7 +99,7 @@ if (isset($delDowntimeForm)) { timeAgo($downtime->entry_time) ?> -

translate('Downtime'); ?> - ', $commentText); ?>

+

translate('Downtime'); ?> - ', $this->createTicketLinks($downtime->comment)); ?>

From 177fafaa56a2c97894e75504eeacd2181671a16a Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 3 Aug 2015 11:05:48 +0200 Subject: [PATCH 08/10] lib: Fix TicketHook's PHPDoc refs #9611 --- library/Icinga/Web/Hook/TicketHook.php | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/library/Icinga/Web/Hook/TicketHook.php b/library/Icinga/Web/Hook/TicketHook.php index 265a0641e..74af1a91f 100644 --- a/library/Icinga/Web/Hook/TicketHook.php +++ b/library/Icinga/Web/Hook/TicketHook.php @@ -5,19 +5,13 @@ 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 + * Base class for ticket hooks * - * Extend this class if you want to integrate your ticketing solution nicely into - * Icinga Web - * - * @copyright Copyright (c) 2013 Icinga-Web Team - * @author Icinga-Web Team - * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License + * Extend this class if you want to integrate your ticketing solution Icinga Web 2 */ abstract class TicketHook { @@ -29,10 +23,9 @@ abstract class TicketHook protected $lastError; /** - * Constructor must live without arguments right now + * Create a new ticket hook * - * Therefore the constructor is final, we might change our opinion about - * this one far day + * @see init() For hook initialization. */ final public function __construct() { @@ -40,7 +33,7 @@ abstract class TicketHook } /** - * Overwrite this function if you want to do some initialization stuff + * Overwrite this function for hook initialization, e.g. loading the hook's config */ protected function init() { From 0809caa86b4986692f5983be2add711fbc54cafd Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 3 Aug 2015 15:03:02 +0200 Subject: [PATCH 09/10] monitoring: Fix indent in the show/components/comments.phtml view script refs #9611 --- .../scripts/show/components/comments.phtml | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/modules/monitoring/application/views/scripts/show/components/comments.phtml b/modules/monitoring/application/views/scripts/show/components/comments.phtml index 37a832358..f2df26aef 100644 --- a/modules/monitoring/application/views/scripts/show/components/comments.phtml +++ b/modules/monitoring/application/views/scripts/show/components/comments.phtml @@ -43,30 +43,30 @@ if (empty($object->comments) && ! $addLink) { else: ?>
    comments as $comment): - // Form is unset if the current user lacks the respective permission - if (isset($delCommentForm)) { - $deleteButton = clone($delCommentForm); - $deleteButton->populate( - array( - 'comment_id' => $comment->id, - 'comment_is_service' => isset($comment->service_description) - ) - ); - } else { - $deleteButton = ''; - } - ?> -
  • -

    - escape($comment->author) ?> - timeAgo($comment->timestamp) ?> - -

    -

    - (translate('Comment') ?>): - ', $this->createTicketLinks($comment->comment)) ?> -

    -
  • + // Form is unset if the current user lacks the respective permission + if (isset($delCommentForm)) { + $deleteButton = clone($delCommentForm); + $deleteButton->populate( + array( + 'comment_id' => $comment->id, + 'comment_is_service' => isset($comment->service_description) + ) + ); + } else { + $deleteButton = ''; + } + ?> +
  • +

    + escape($comment->author) ?> + timeAgo($comment->timestamp) ?> + +

    +

    + (translate('Comment') ?>): + ', $this->createTicketLinks($comment->comment)) ?> +

    +
From d8e6418e94af71b5185e69848ee79883ce1351e4 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 3 Aug 2015 15:03:31 +0200 Subject: [PATCH 10/10] monitoring: Fix code style in the show/components/downtime.phtml view script refs #9611 --- .../scripts/show/components/downtime.phtml | 116 ++++++++---------- 1 file changed, 52 insertions(+), 64 deletions(-) diff --git a/modules/monitoring/application/views/scripts/show/components/downtime.phtml b/modules/monitoring/application/views/scripts/show/components/downtime.phtml index 70f16bb18..e892a71ab 100644 --- a/modules/monitoring/application/views/scripts/show/components/downtime.phtml +++ b/modules/monitoring/application/views/scripts/show/components/downtime.phtml @@ -1,7 +1,5 @@ hasPermission('monitoring/command/downtime/schedule')) { /** @var \Icinga\Module\Monitoring\Object\MonitoredObject $object */ if ($object->getType() === $object::TYPE_HOST) { @@ -32,78 +30,68 @@ if ($this->hasPermission('monitoring/command/downtime/schedule')) { ); } } - if (empty($object->comments) && ! $addLink) { return; } - ?> - translate('Downtimes'); - if (! empty($object->downtimes) && $addLink) { echo '
' . $addLink; } - ?> -downtimes)): ?> - - -
    - -downtimes as $downtime): ?> -is_in_effect) { - $state = sprintf( - $this->translate('in downtime %s', 'Last format parameter represents the time in downtime'), - $this->timeSince($downtime->start) - ); - } else { - if ((bool) $downtime->is_fixed) { - $state = sprintf( - $this->translate('scheduled %s', 'Last format parameter represents the time scheduled'), - $this->timeUntil($downtime->start) - ); - } else { - $state = sprintf( - $this->translate('scheduled flexible %s', 'Last format parameter represents the time scheduled'), - $this->timeUntil($downtime->start) - ); - } - } - - -// Form is unset if the current user lacks the respective permission -if (isset($delDowntimeForm)) { - $deleteButton = clone($delDowntimeForm); - $deleteButton->populate( - array( - 'downtime_id' => $downtime->id, - 'downtime_is_service' => $object->getType() === $object::TYPE_SERVICE - ) - ); -} else { - $deleteButton = ''; -} - -?> - -
  • -

    - escape($downtime->author_name) ?> - timeAgo($downtime->entry_time) ?> - -

    -

    translate('Downtime'); ?> - ', $this->createTicketLinks($downtime->comment)); ?>

    -
  • - -
- - + downtimes)): + echo $addLink; + else: ?> +
    + downtimes as $downtime): + if ((bool) $downtime->is_in_effect) { + $state = sprintf( + $this->translate('in downtime %s', 'Last format parameter represents the time in downtime'), + $this->timeSince($downtime->start) + ); + } else { + if ((bool) $downtime->is_fixed) { + $state = sprintf( + $this->translate('scheduled %s', 'Last format parameter represents the time scheduled'), + $this->timeUntil($downtime->start) + ); + } else { + $state = sprintf( + $this->translate('scheduled flexible %s', 'Last format parameter represents the time scheduled'), + $this->timeUntil($downtime->start) + ); + } + } + // Form is unset if the current user lacks the respective permission + if (isset($delDowntimeForm)) { + $deleteButton = clone($delDowntimeForm); + $deleteButton->populate( + array( + 'downtime_id' => $downtime->id, + 'downtime_is_service' => $object->getType() === $object::TYPE_SERVICE + ) + ); + } else { + $deleteButton = ''; + } + ?> +
  • +

    + escape($downtime->author_name) ?> + timeAgo($downtime->entry_time) ?> + +

    +

    + translate('Downtime') ?> + - + ', $this->createTicketLinks($downtime->comment)) ?> +

    +
  • + +
+