Files

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

97 lines
3.8 KiB
PHP
Raw Permalink Normal View History

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.
*
* 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/>. */
2014-11-30 17:49:52 +00:00
/**
* Alert Templates
2021-09-10 20:09:53 +02:00
*
2014-11-30 17:49:52 +00:00
* @author f0o <f0o@devilcode.org>
* @copyright 2014 f0o, LibreNMS
* @license GPL
*/
$status = 'error';
if (! Auth::user()->hasGlobalAdmin()) {
header('Content-Type: application/json');
$response = ['status' => $status, 'message' => 'You need to be admin'];
2018-09-11 07:51:35 -05:00
exit(json_encode($response));
2014-11-30 17:49:52 +00:00
}
2022-04-14 11:29:29 -05:00
try {
Blade::render($vars['template']);
Blade::render($vars['title']);
Blade::render($vars['title_rec']);
2022-04-14 11:29:29 -05:00
$template_id = 0;
$template_newid = 0;
$create = true;
$name = strip_tags($vars['name']);
if (! empty($name)) {
if ($vars['template'] && is_numeric($vars['template_id'])) {
// Update template
$create = false;
$template_id = $vars['template_id'];
if (! dbUpdate(['template' => $vars['template'], 'name' => $name, 'title' => $vars['title'], 'title_rec' => $vars['title_rec']], 'alert_templates', 'id = ?', [$template_id]) >= 0) {
$status = 'ok';
} else {
2022-04-14 11:29:29 -05:00
$message = 'Failed to update the template';
}
} elseif ($vars['template']) {
// Create template
if ($name != 'Default Alert Template') {
$template_newid = dbInsert(['template' => $vars['template'], 'name' => $name, 'title' => $vars['title'], 'title_rec' => $vars['title_rec']], 'alert_templates');
if ($template_newid != false) {
$template_id = $template_newid;
$status = 'ok';
} else {
$message = 'Could not create alert template';
}
} else {
$message = 'This template name is reserved!';
}
2016-08-18 20:28:22 -05:00
} else {
2022-04-14 11:29:29 -05:00
$message = 'We could not work out what you wanted to do!';
2014-11-30 17:49:52 +00:00
}
2022-04-14 11:29:29 -05:00
if ($status == 'ok') {
$alertRulesOk = true;
dbDelete('alert_template_map', 'alert_templates_id = ?', [$template_id]);
$rules = explode(',', $vars['rules']);
if ($rules !== false) {
foreach ($rules as $rule_id) {
if (! dbInsert(['alert_rule_id' => $rule_id, 'alert_templates_id' => $template_id], 'alert_template_map')) {
$alertRulesOk = false;
}
2018-10-25 22:31:12 +02:00
}
}
2022-04-14 11:29:29 -05:00
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.';
}
2018-10-25 22:31:12 +02:00
}
2022-04-14 11:29:29 -05:00
} else {
$message = "You haven't given name to your template";
2018-10-25 22:31:12 +02:00
}
2022-04-14 11:29:29 -05:00
} catch (Exception $e) {
$message = 'Template failed to be parsed, please check the syntax. ';
$message .= $e->getMessage();
2014-11-30 17:49:52 +00:00
}
2018-10-25 22:31:12 +02:00
$response = ['status' => $status, 'message' => $message, 'newid' => $template_newid];
2021-03-04 07:55:41 -06:00
echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);