Files
librenms-librenms/LibreNMS/Alert/Transport.php
T
0b8b97bb68 Push Notifications (Mobile and PC) (#13277)
* Update manifest and add service worker
cleanup icons a bit

* Push notifications WIP

* navigate working

* cleanup

* acknowledge wired up

* Set VAPID keys on composer install

* Component to control notification permissions.

* Allow all user option to validate

* Enable on browser load if transport exists.

* Check for transport before showing user permissions
translations

* Documentation

* style fixes

* access via the attribute model

* fix alerting test

* update schema

* cleanup subscription on disable

* non-configurable db and table for webpush subscriptions (respect system connection)

* revert AlertTransport change
hopefully phpstan can figure it out

* phpstan fixes

* Support custom details display

* Match transport names to brand's preferred display

* less duplicate id errors

* Tests are done in Laravel code now so
remove legacy function usage... could be better, but ok

* Style fixes

* Style fixes 2

* Fix alert test

* Doc updates requires HTTPS and GMP

* unregister subscription when permission is set to denied

* cleanup after user deletion

* delete the right thing

* fix whitespace

* update install docs to include php-gmp

* suggest ext-gmp

* update javascript

* Update functions.php

Co-authored-by: Jellyfrog <[email protected]>
2021-10-06 07:29:47 -05:00

146 lines
3.9 KiB
PHP

<?php
namespace LibreNMS\Alert;
use App\Models\AlertTransport;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Str;
use LibreNMS\Config;
use LibreNMS\Enum\AlertState;
use LibreNMS\Interfaces\Alert\Transport as TransportInterface;
abstract class Transport implements TransportInterface
{
protected $config;
/**
* @var string
*/
protected $name;
public static function make(string $type): TransportInterface
{
$class = self::getClass($type);
return new $class();
}
/**
* Transport constructor.
*
* @param null $transport
*/
public function __construct($transport = null)
{
if (! empty($transport)) {
if ($transport instanceof AlertTransport) {
$this->config = $transport->transport_config;
} else {
try {
$model = \App\Models\AlertTransport::findOrFail($transport); /** @var AlertTransport $model */
$this->config = $model->transport_config;
} catch (ModelNotFoundException $e) {
$this->config = [];
}
}
}
}
/**
* @return string The display name of this transport
*/
public function name(): string
{
if ($this->name !== null) {
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
*
* @param string $input
* @return array
*/
protected function parseUserOptions($input)
{
$options = [];
foreach (explode(PHP_EOL, $input) as $option) {
if (Str::contains($option, '=')) {
[$k,$v] = explode('=', $option, 2);
$options[$k] = trim($v);
}
}
return $options;
}
/**
* Get the hex color string for a particular state
*
* @param int $state State code from alert
* @return string Hex color, default to #337AB7 blue if state unrecognised
*/
public static function getColorForState($state)
{
$colors = [
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';
}
/**
* 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($content): bool
{
return $content !== strip_tags($content);
}
}