Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

146 lines
4.1 KiB
PHP
Raw Permalink Normal View History

2018-07-21 13:34:59 -06:00
<?php
namespace LibreNMS\Alert;
2021-10-06 07:29:47 -05:00
use App\Models\AlertTransport;
use App\View\SimpleTemplate;
2020-04-17 17:37:56 -05:00
use Illuminate\Support\Str;
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
{
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
/**
* 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;
}
public function __construct(?AlertTransport $transport = null)
2018-07-21 13:34:59 -06:00
{
$this->config = $transport ? $transport->transport_config : [];
2018-07-21 13:34:59 -06:00
}
2021-10-06 07:29:47 -05:00
/**
* @return string The display name of this transport
*/
public function name(): string
{
if ($this->name !== '') {
2021-10-06 07:29:47 -05:00
return $this->name;
}
$path = explode('\\', get_called_class());
return array_pop($path);
}
/**
* 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
* @param array $replacements for SimpleTemplate if desired
* @return array
*/
protected function parseUserOptions(string $input, array $replacements = []): array
{
$options = [];
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, '=')) {
[$k, $v] = explode('=', $option, 2);
$options[$k] = empty($replacements) ? trim($v) : SimpleTemplate::parse(trim($v), $replacements);
}
}
2020-09-21 14:54:51 +02:00
return $options;
}
/**
* 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
* @return string Hex color, default to #337AB7 blue if state unrecognised
*/
public static function getColorForState($state)
{
$colors = [
2020-05-24 04:14:36 +02:00
AlertState::CLEAR => Config::get('alert_colour.ok'),
AlertState::ACTIVE => Config::get('alert_colour.bad'),
AlertState::ACKNOWLEDGED => Config::get('alert_colour.acknowledged'),
AlertState::WORSE => Config::get('alert_colour.worse'),
AlertState::BETTER => Config::get('alert_colour.better'),
];
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>&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;</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);
}
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
}