From bc2fe6aeb17e8b86f819f5d99752d58f75b90db3 Mon Sep 17 00:00:00 2001 From: Neil Lathwood Date: Fri, 17 Aug 2018 22:38:00 +0100 Subject: [PATCH] Tidy up some transport code from previous pr (#8927) --- LibreNMS/Alert/AlertUtil.php | 33 +++++++++++-------- LibreNMS/Alert/Transport.php | 5 ++- LibreNMS/Alert/Transport/Api.php | 6 ++-- LibreNMS/Interfaces/Alert/Transport.php | 5 +++ .../forms/delete-alert-transport.inc.php | 6 ++-- .../forms/show-alert-transport.inc.php | 4 +-- .../forms/show-transport-group.inc.php | 4 +-- html/includes/list/transport_groups.inc.php | 2 +- html/includes/list/transports.inc.php | 8 ++--- includes/alerts.inc.php | 4 ++- 10 files changed, 47 insertions(+), 30 deletions(-) diff --git a/LibreNMS/Alert/AlertUtil.php b/LibreNMS/Alert/AlertUtil.php index ede85dac45..aec41b03de 100644 --- a/LibreNMS/Alert/AlertUtil.php +++ b/LibreNMS/Alert/AlertUtil.php @@ -3,35 +3,42 @@ namespace LibreNMS\Alert; class AlertUtil { - // Return rule id from alert id + /** + * + * Get the rule_id for a specific alert + * + * @param $alert_id + * @return mixed|null + */ private static function getRuleId($alert_id) { $query = "SELECT `rule_id` FROM `alerts` WHERE `id`=?"; return dbFetchCell($query, [$alert_id]); } - // Return all alert transports mapped to a rule (includies transport groups) - // @retyurn array [$transport_id $transport_type] + /** + * + * Get the transport for a given alert_id + * + * @param $alert_id + * @return array + */ public static function getAlertTransports($alert_id) { - // Query for list of transport ids $query = "SELECT b.transport_id, b.transport_type FROM alert_transport_map AS a LEFT JOIN alert_transports AS b ON b.transport_id=a.transport_or_group_id WHERE a.target_type='single' AND a.rule_id=? UNION DISTINCT SELECT d.transport_id, d.transport_type FROM alert_transport_map AS a LEFT JOIN alert_transport_groups AS b ON a.transport_or_group_id=b.transport_group_id LEFT JOIN transport_group_transport AS c ON b.transport_group_id=c.transport_group_id LEFT JOIN alert_transports AS d ON c.transport_id=d.transport_id WHERE a.target_type='group' AND a.rule_id=?"; $rule_id = self::getRuleId($alert_id); return dbFetchRows($query, [$rule_id, $rule_id]); } - // Return transports configured as default - // @return array [$transport_id $transport_type] + /** + * + * Returns the default transports + * + * @return array + */ public static function getDefaultAlertTransports() { $query = "SELECT transport_id, transport_type FROM alert_transports WHERE is_default=true"; return dbFetchRows($query); } - - // Return list of transport types with a default configured - public static function getDefaultTransportList() - { - $query = "SELECT DISTINCT transport_type FROM alert_transports WHERE is_default=true "; - return dbFetchColumn($query); - } } diff --git a/LibreNMS/Alert/Transport.php b/LibreNMS/Alert/Transport.php index 9f2c548037..35ec8dea75 100644 --- a/LibreNMS/Alert/Transport.php +++ b/LibreNMS/Alert/Transport.php @@ -8,7 +8,10 @@ abstract class Transport implements TransportInterface { protected $config; - // Sets config field to an associative array of transport config values + /** + * Transport constructor. + * @param null $transport_id + */ public function __construct($transport_id = null) { if (!empty($transport_id)) { diff --git a/LibreNMS/Alert/Transport/Api.php b/LibreNMS/Alert/Transport/Api.php index 6f01971868..1cbd0e701d 100644 --- a/LibreNMS/Alert/Transport/Api.php +++ b/LibreNMS/Alert/Transport/Api.php @@ -1,5 +1,5 @@ +/* Copyright (C) 2014 Daniel Preussker * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or @@ -11,11 +11,11 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program. If not, see . */ + * along with this program. If not, see . */ /** * API Transport - * @author f0o + * @author f0o * @copyright 2014 f0o, LibreNMS * @license GPL * @package LibreNMS diff --git a/LibreNMS/Interfaces/Alert/Transport.php b/LibreNMS/Interfaces/Alert/Transport.php index 4eb1661023..65ce9bf7b0 100644 --- a/LibreNMS/Interfaces/Alert/Transport.php +++ b/LibreNMS/Interfaces/Alert/Transport.php @@ -35,4 +35,9 @@ interface Transport * @return mixed Returns if the call was successful */ public function deliverAlert($alert_data, $opts); + + /** + * @return array + */ + public static function configTemplate(); } diff --git a/html/includes/forms/delete-alert-transport.inc.php b/html/includes/forms/delete-alert-transport.inc.php index 89859f0fda..49b8d28237 100644 --- a/html/includes/forms/delete-alert-transport.inc.php +++ b/html/includes/forms/delete-alert-transport.inc.php @@ -17,7 +17,7 @@ header('Content-type: application/json'); if (!Auth::user()->hasGlobalAdmin()) { die(json_encode([ 'status' => 'error', - 'message' => 'ERROR: You need to be admin.' + 'message' => 'You need to be admin.' ])); } @@ -26,7 +26,7 @@ $message = ''; if (!is_numeric($vars['transport_id'])) { $status = 'error'; - $message = 'ERROR: No transport selected'; + $message = 'No transport selected'; } else { if (dbDelete('alert_transports', '`transport_id` = ?', [$vars['transport_id']])) { dbDelete('alert_transport_map', '`target_type` = "single" AND `transport_or_group_id` = ?', [$vars['transport_id']]); @@ -34,7 +34,7 @@ if (!is_numeric($vars['transport_id'])) { $message = 'Alert transport has been deleted'; } else { - $message = 'ERROR: Alert transport has not been deleted'; + $message = 'Alert transport has not been deleted'; } } diff --git a/html/includes/forms/show-alert-transport.inc.php b/html/includes/forms/show-alert-transport.inc.php index fdfa38ade1..4585f92538 100644 --- a/html/includes/forms/show-alert-transport.inc.php +++ b/html/includes/forms/show-alert-transport.inc.php @@ -17,7 +17,7 @@ header('Content-type: application/json'); if (!Auth::user()->hasGlobalAdmin()) { die(json_encode([ 'status' => 'error', - 'message' => 'ERROR: You need to be admin' + 'message' => 'You need to be admin' ])); } @@ -51,6 +51,6 @@ if (is_array($transport)) { } else { die(json_encode([ 'status' => 'error', - 'message' => 'ERROR: No alert transport found' + 'message' => 'No alert transport found' ])); } diff --git a/html/includes/forms/show-transport-group.inc.php b/html/includes/forms/show-transport-group.inc.php index becf2daf85..935746baa5 100644 --- a/html/includes/forms/show-transport-group.inc.php +++ b/html/includes/forms/show-transport-group.inc.php @@ -17,7 +17,7 @@ header('Content-type: application/json'); if (!Auth::user()->hasGlobalAdmin()) { die(json_encode([ 'status' => 'error', - 'message' => 'ERROR: You need to be admin' + 'message' => 'You need to be admin' ])); } @@ -45,6 +45,6 @@ if (is_array($members)) { } else { die(json_encode([ 'status' => 'error', - 'message' => 'ERROR: No transport group found' + 'message' => 'No transport group found' ])); } diff --git a/html/includes/list/transport_groups.inc.php b/html/includes/list/transport_groups.inc.php index 227e2adbfc..7f9745373b 100644 --- a/html/includes/list/transport_groups.inc.php +++ b/html/includes/list/transport_groups.inc.php @@ -36,7 +36,7 @@ $params = []; if (!empty($_REQUEST['search'])) { $query .= ' WHERE `transport_group_name` LIKE ?'; - $params[] = '%' . mres($_REQUEST['search']) . '%'; + $params[] = '%' . $vars['search'] . '%'; } $total = dbFetchCell("SELECT COUNT(*) FROM `alert_transport_groups` $query", $params); diff --git a/html/includes/list/transports.inc.php b/html/includes/list/transports.inc.php index 7489ba0125..e9c3911abe 100644 --- a/html/includes/list/transports.inc.php +++ b/html/includes/list/transports.inc.php @@ -32,17 +32,17 @@ if (!Auth::user()->hasGlobalRead()) { $query = ''; $params = []; -if (!empty($_REQUEST['search'])) { +if (!empty($vars['search'])) { $query .= ' WHERE `transport_name` LIKE ?'; - $params[] = '%' . mres($_REQUEST['search']) . '%'; + $params[] = '%' . $vars['search'] . '%'; } $total = dbFetchCell("SELECT COUNT(*) FROM `alert_transports` $query", $params); $more = false; if (!empty($_REQUEST['limit'])) { - $limit = (int) $_REQUEST['limit']; - $page = isset($_REQUEST['page']) ? (int) $_REQUEST['page'] : 1; + $limit = (int) $vars['limit']; + $page = isset($vars['page']) ? (int) $vars['page'] : 1; $offset = ($page - 1) * $limit; $query .= " LIMIT $offset, $limit"; diff --git a/includes/alerts.inc.php b/includes/alerts.inc.php index 70842a9b6e..dfea835d27 100644 --- a/includes/alerts.inc.php +++ b/includes/alerts.inc.php @@ -820,7 +820,9 @@ function ExtTransports($obj) if (!$transport_maps) { $transport_maps = AlertUtil::getDefaultAlertTransports(); - $legacy_transports = AlertUtil::getDefaultTransportList(); + $legacy_transports = array_unique(array_map(function ($transports) { + return $transports['transport_type']; + }, $transport_maps)); foreach ($config['alert']['transports'] as $transport => $opts) { if (in_array($transport, $legacy_transports)) { // If it is a default transport type, then the alert has already been sent out, so skip