2018-07-21 13:34:59 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace LibreNMS\Alert;
|
|
|
|
|
|
2021-10-06 07:29:47 -05:00
|
|
|
use App\Models\AlertTransport;
|
2021-11-03 13:37:57 -05:00
|
|
|
use App\View\SimpleTemplate;
|
2020-04-17 17:37:56 -05:00
|
|
|
use Illuminate\Support\Str;
|
2019-12-18 11:08:32 +01:00
|
|
|
use LibreNMS\Config;
|
2020-05-24 04:14:36 +02:00
|
|
|
use LibreNMS\Enum\AlertState;
|
2018-07-21 13:34:59 -06:00
|
|
|
use LibreNMS\Interfaces\Alert\Transport as TransportInterface;
|
|
|
|
|
|
|
|
|
|
abstract class Transport implements TransportInterface
|
|
|
|
|
{
|
2023-05-23 09:25:17 -05:00
|
|
|
protected ?array $config;
|
|
|
|
|
protected string $name = '';
|
2021-10-06 07:29:47 -05:00
|
|
|
|
|
|
|
|
public static function make(string $type): TransportInterface
|
|
|
|
|
{
|
|
|
|
|
$class = self::getClass($type);
|
|
|
|
|
|
|
|
|
|
return new $class();
|
|
|
|
|
}
|
2018-07-21 13:34:59 -06:00
|
|
|
|
2021-11-03 13:37:57 -05:00
|
|
|
/**
|
|
|
|
|
* Returns a list of all available transports
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function list(): array
|
|
|
|
|
{
|
|
|
|
|
$list = [];
|
|
|
|
|
foreach (glob(base_path('LibreNMS/Alert/Transport/*.php')) as $file) {
|
|
|
|
|
$transport = strtolower(basename($file, '.php'));
|
|
|
|
|
$class = self::getClass($transport);
|
|
|
|
|
$instance = new $class;
|
|
|
|
|
$list[$transport] = $instance->name();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 09:25:17 -05:00
|
|
|
public function __construct(?AlertTransport $transport = null)
|
2018-07-21 13:34:59 -06:00
|
|
|
{
|
2023-05-23 09:25:17 -05:00
|
|
|
$this->config = $transport ? $transport->transport_config : [];
|
2018-07-21 13:34:59 -06:00
|
|
|
}
|
2019-03-27 07:18:02 -05:00
|
|
|
|
2021-10-06 07:29:47 -05:00
|
|
|
/**
|
|
|
|
|
* @return string The display name of this transport
|
|
|
|
|
*/
|
|
|
|
|
public function name(): string
|
|
|
|
|
{
|
2023-05-23 09:25:17 -05:00
|
|
|
if ($this->name !== '') {
|
2021-10-06 07:29:47 -05:00
|
|
|
return $this->name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$path = explode('\\', get_called_class());
|
|
|
|
|
|
|
|
|
|
return array_pop($path);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 07:18:02 -05:00
|
|
|
/**
|
|
|
|
|
* Helper function to parse free form text box defined in ini style to key value pairs
|
|
|
|
|
*
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param string $input
|
2021-11-03 13:37:57 -05:00
|
|
|
* @param array $replacements for SimpleTemplate if desired
|
2019-03-27 07:18:02 -05:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2021-11-03 13:37:57 -05:00
|
|
|
protected function parseUserOptions(string $input, array $replacements = []): array
|
2019-03-27 07:18:02 -05:00
|
|
|
{
|
|
|
|
|
$options = [];
|
2021-10-29 22:12:20 -05:00
|
|
|
foreach (preg_split('/\\r\\n|\\r|\\n/', $input, -1, PREG_SPLIT_NO_EMPTY) as $option) {
|
2020-04-17 17:37:56 -05:00
|
|
|
if (Str::contains($option, '=')) {
|
2021-10-29 22:12:20 -05:00
|
|
|
[$k, $v] = explode('=', $option, 2);
|
2021-11-03 13:37:57 -05:00
|
|
|
$options[$k] = empty($replacements) ? trim($v) : SimpleTemplate::parse(trim($v), $replacements);
|
2019-03-27 07:18:02 -05:00
|
|
|
}
|
|
|
|
|
}
|
2020-09-21 14:54:51 +02:00
|
|
|
|
2019-03-27 07:18:02 -05:00
|
|
|
return $options;
|
|
|
|
|
}
|
2019-12-17 22:27:08 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the hex color string for a particular state
|
2021-09-10 20:09:53 +02:00
|
|
|
*
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param int $state State code from alert
|
2019-12-17 22:27:08 +01:00
|
|
|
* @return string Hex color, default to #337AB7 blue if state unrecognised
|
|
|
|
|
*/
|
|
|
|
|
public static function getColorForState($state)
|
|
|
|
|
{
|
|
|
|
|
$colors = [
|
2024-01-05 05:39:12 +01:00
|
|
|
AlertState::CLEAR => Config::get('alert_colour.ok'),
|
|
|
|
|
AlertState::ACTIVE => Config::get('alert_colour.bad'),
|
2020-05-24 04:14:36 +02:00
|
|
|
AlertState::ACKNOWLEDGED => Config::get('alert_colour.acknowledged'),
|
2024-01-05 05:39:12 +01:00
|
|
|
AlertState::WORSE => Config::get('alert_colour.worse'),
|
|
|
|
|
AlertState::BETTER => Config::get('alert_colour.better'),
|
2019-12-17 22:27:08 +01:00
|
|
|
];
|
2019-12-18 11:08:32 +01:00
|
|
|
|
2019-12-17 22:27:08 +01:00
|
|
|
return isset($colors[$state]) ? $colors[$state] : '#337AB7';
|
|
|
|
|
}
|
2021-10-06 07:29:47 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display the configuration details of this alert transport
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function displayDetails(): string
|
|
|
|
|
{
|
|
|
|
|
$output = '';
|
|
|
|
|
|
|
|
|
|
// Iterate through transport config template to display config details
|
|
|
|
|
$config = static::configTemplate();
|
|
|
|
|
foreach ($config['config'] as $item) {
|
|
|
|
|
if ($item['type'] == 'oauth') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$val = $this->config[$item['name']];
|
|
|
|
|
if ($item['type'] == 'password') {
|
|
|
|
|
$val = '<b>••••••••</b>';
|
|
|
|
|
} elseif ($item['type'] == 'select') {
|
|
|
|
|
// Match value to key name for select inputs
|
|
|
|
|
$val = array_search($val, $item['options']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$output .= $item['title'] . ': ' . $val . PHP_EOL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the alert transport class from transport type.
|
|
|
|
|
*
|
|
|
|
|
* @param string $type
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function getClass(string $type): string
|
|
|
|
|
{
|
|
|
|
|
return 'LibreNMS\\Alert\\Transport\\' . ucfirst($type);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 09:25:17 -05:00
|
|
|
protected function isHtmlContent(string $content): bool
|
2021-10-06 07:29:47 -05:00
|
|
|
{
|
|
|
|
|
return $content !== strip_tags($content);
|
|
|
|
|
}
|
2018-07-21 13:34:59 -06:00
|
|
|
}
|