2014-11-30 17:49:52 +00:00
|
|
|
<?php
|
|
|
|
/* Copyright (C) 2014 Daniel Preussker <f0o@devilcode.org>
|
|
|
|
* 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.
|
2015-07-13 20:10:26 +02:00
|
|
|
*
|
2014-11-30 17:49:52 +00:00
|
|
|
* 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.
|
2015-07-13 20:10:26 +02:00
|
|
|
*
|
2014-11-30 17:49:52 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alert Templates
|
|
|
|
* @author f0o <f0o@devilcode.org>
|
|
|
|
* @copyright 2014 f0o, LibreNMS
|
|
|
|
* @license GPL
|
|
|
|
*/
|
2018-07-14 22:15:43 +01:00
|
|
|
$status = 'error';
|
2018-04-07 15:55:28 -05:00
|
|
|
|
2020-09-21 15:40:17 +02:00
|
|
|
if (! Auth::user()->hasGlobalAdmin()) {
|
2018-07-14 22:15:43 +01:00
|
|
|
header('Content-Type: application/json');
|
2020-09-21 15:40:17 +02:00
|
|
|
$response = ['status' => $status, 'message' => 'You need to be admin'];
|
|
|
|
exit(json_encode($response));
|
2014-11-30 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-07 15:22:08 +03:00
|
|
|
$template_id = 0;
|
2018-10-25 22:31:12 +02:00
|
|
|
$template_newid = 0;
|
|
|
|
$create = true;
|
2018-04-07 15:22:08 +03:00
|
|
|
|
2018-07-14 22:15:43 +01:00
|
|
|
$name = mres($vars['name']);
|
|
|
|
if (isset($vars['template']) && empty(view(['template' => $vars['template']], [])->__toString())) {
|
|
|
|
$message = 'Template failed to be parsed, please check the syntax';
|
2020-09-21 15:40:17 +02:00
|
|
|
} elseif (! empty($name)) {
|
2018-10-25 22:31:12 +02:00
|
|
|
if ($vars['template'] && is_numeric($vars['template_id'])) {
|
|
|
|
// Update template
|
|
|
|
$create = false;
|
|
|
|
$template_id = $vars['template_id'];
|
2020-09-21 15:40:17 +02:00
|
|
|
if (! dbUpdate(['template' => $vars['template'], 'name' => $name, 'title' => $vars['title'], 'title_rec' => $vars['title_rec']], "alert_templates", "id = ?", [$template_id]) >= 0) {
|
2018-04-07 15:22:08 +03:00
|
|
|
$status = 'ok';
|
2016-08-18 20:28:22 -05:00
|
|
|
} else {
|
2018-04-07 15:22:08 +03:00
|
|
|
$message = "Failed to update the template";
|
2014-11-30 17:49:52 +00:00
|
|
|
}
|
2018-07-14 22:15:43 +01:00
|
|
|
} elseif ($vars['template']) {
|
2018-10-25 22:31:12 +02:00
|
|
|
// Create template
|
2017-08-12 22:21:13 +03:00
|
|
|
if ($name != 'Default Alert Template') {
|
2020-09-21 15:40:17 +02:00
|
|
|
$template_newid = dbInsert(['template' => $vars['template'], 'name' => $name, 'title' => $vars['title'], 'title_rec' => $vars['title_rec']], "alert_templates");
|
2018-10-25 22:31:12 +02:00
|
|
|
if ($template_newid != false) {
|
|
|
|
$template_id = $template_newid;
|
2018-04-07 15:22:08 +03:00
|
|
|
$status = 'ok';
|
2017-08-12 22:21:13 +03:00
|
|
|
} else {
|
2018-04-07 15:22:08 +03:00
|
|
|
$message = "Could not create alert template";
|
2017-08-12 22:21:13 +03:00
|
|
|
}
|
2016-08-18 20:28:22 -05:00
|
|
|
} else {
|
2018-04-07 15:22:08 +03:00
|
|
|
$message = "This template name is reserved!";
|
2014-11-30 17:49:52 +00:00
|
|
|
}
|
2016-08-18 20:28:22 -05:00
|
|
|
} else {
|
2018-04-07 15:22:08 +03:00
|
|
|
$message = "We could not work out what you wanted to do!";
|
2014-11-30 17:49:52 +00:00
|
|
|
}
|
2018-10-25 22:31:12 +02:00
|
|
|
if ($status == 'ok') {
|
|
|
|
$alertRulesOk = true;
|
2020-09-21 15:40:17 +02:00
|
|
|
dbDelete('alert_template_map', 'alert_templates_id = ?', [$template_id]);
|
2018-10-25 22:31:12 +02:00
|
|
|
$rules = explode(',', $vars['rules']);
|
|
|
|
if ($rules !== false) {
|
|
|
|
foreach ($rules as $rule_id) {
|
2020-09-21 15:40:17 +02:00
|
|
|
if (! dbInsert(['alert_rule_id' => $rule_id, 'alert_templates_id' => $template_id], 'alert_template_map')) {
|
2018-10-25 22:31:12 +02:00
|
|
|
$alertRulesOk = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($alertRulesOk) {
|
|
|
|
$status = 'ok';
|
|
|
|
$message = 'Alert template has been ' . ($create ? 'created' : 'updated') . ' and attached rules have been updated.';
|
|
|
|
} else {
|
|
|
|
$status = 'warning';
|
|
|
|
$message = 'Alert template has been ' . ($create ? 'created' : 'updated') . ' but some attached rules have not been updated.';
|
|
|
|
}
|
|
|
|
}
|
2016-08-18 20:28:22 -05:00
|
|
|
} else {
|
2018-10-25 22:31:12 +02:00
|
|
|
$message = "You haven't given name to your template";
|
2014-11-30 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 15:40:17 +02:00
|
|
|
$response = ['status' => $status, 'message' => $message, 'newid' => $template_newid];
|
2018-04-07 15:22:08 +03:00
|
|
|
|
|
|
|
echo _json_encode($response);
|