/usr/share/php/Icinga/Application/Hook/TicketHook.php is in php-icinga 2.1.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | <?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Application\Hook;
use ErrorException;
use Exception;
use Icinga\Application\Logger;
use Icinga\Exception\IcingaException;
/**
* Base class for ticket hooks
*
* Extend this class if you want to integrate your ticketing solution Icinga Web 2
*/
abstract class TicketHook
{
/**
* Last error, if any
*
* @var string|null
*/
protected $lastError;
/**
* Create a new ticket hook
*
* @see init() For hook initialization.
*/
final public function __construct()
{
$this->init();
}
/**
* Overwrite this function for hook initialization, e.g. loading the hook's config
*/
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;
}
}
|