Files

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

116 lines
3.9 KiB
PHP
Raw Permalink Normal View History

2018-07-21 13:34:59 -06:00
<?php
/**
* alert-transports.inc.php
*
* LibreNMS alert-transports.inc.php for processor
*
* 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
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2021-02-09 00:29:04 +01:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-07-21 13:34:59 -06:00
*
2021-02-09 00:29:04 +01:00
* @link https://www.librenms.org
2021-09-10 20:09:53 +02:00
*
2018-07-21 13:34:59 -06:00
* @copyright 2018 Vivia Nguyen-Tran
* @author Vivia Nguyen-Tran <[email protected]>
*/
header('Content-type: application/json');
if (! Auth::user()->hasGlobalAdmin()) {
2018-07-21 13:34:59 -06:00
exit(json_encode([
'status' => 'error',
'message' => 'You need to be admin',
]));
}
$status = 'ok';
$message = '';
2022-02-13 11:00:56 +01:00
$transport_id = strip_tags($vars['transport_id']);
$name = strip_tags($vars['name']);
$is_default = (int) (isset($vars['is_default']) && $vars['is_default'] == 'on');
2022-02-13 11:00:56 +01:00
$transport_type = strip_tags($vars['transport-type']);
2018-07-21 13:34:59 -06:00
if (empty($name)) {
$status = 'error';
$message = 'No transport name provided';
} elseif (empty($transport_type)) {
$status = 'error';
$message = 'Missing transport information';
} else {
$details = [
'transport_name' => $name,
'is_default' => $is_default,
];
if (is_numeric($transport_id) && $transport_id > 0) {
// Update the fields -- json config field will be updated later
dbUpdate($details, 'alert_transports', 'transport_id=?', [$transport_id]);
} else {
// Insert the new alert transport
$newEntry = true;
$transport_id = dbInsert($details, 'alert_transports');
}
if ($transport_id) {
$class = 'LibreNMS\\Alert\\Transport\\' . ucfirst($transport_type);
if (! method_exists($class, 'configTemplate')) {
exit(json_encode([
'status' => 'error',
'message' => 'This transport type is not yet supported',
]));
}
2020-09-21 15:40:17 +02:00
2018-07-21 13:34:59 -06:00
// Build config values
$result = call_user_func_array($class . '::configTemplate', []);
2021-10-06 07:29:47 -05:00
$validator = Validator::make($vars, $result['validation']);
2018-07-21 13:34:59 -06:00
if ($validator->fails()) {
$errors = $validator->errors();
foreach ($errors->all() as $error) {
$message .= "$error<br>";
}
$status = 'error';
} else {
2019-06-26 09:47:03 -05:00
$transport_config = (array) json_decode(dbFetchCell('SELECT transport_config FROM alert_transports WHERE transport_id=?', [$transport_id]), true);
2018-07-21 13:34:59 -06:00
foreach ($result['config'] as $tmp_config) {
if (isset($tmp_config['name']) && $tmp_config['type'] !== 'hidden') {
$transport_config[$tmp_config['name']] = $vars[$tmp_config['name']];
}
2018-07-21 13:34:59 -06:00
}
//Update the json config field
2019-06-26 09:47:03 -05:00
$detail = [
'transport_type' => $transport_type,
'transport_config' => json_encode($transport_config),
];
$where = 'transport_id=?';
2018-07-21 13:34:59 -06:00
2019-06-26 09:47:03 -05:00
dbUpdate($detail, 'alert_transports', $where, [$transport_id]);
2018-07-21 13:34:59 -06:00
2019-06-26 09:47:03 -05:00
$status = 'ok';
$message = 'Updated alert transports';
2018-07-21 13:34:59 -06:00
}
if ($status == 'error' && $newEntry) {
//If error, we will have to delete the new entry in alert_transports tbl
$where = '`transport_id`=?';
dbDelete('alert_transports', $where, [$transport_id]);
}
} else {
$status = 'error';
$message = 'Failed to update transport';
}
}
exit(json_encode([
'status' => $status,
'message' => $message,
]));