1 | <?php |
||
2 | /* Copyright (C) 2014 Daniel Preussker <[email protected]> |
||
3 | * This program is free software: you can redistribute it and/or modify |
||
4 | * it under the terms of the GNU General Public License as published by |
||
5 | * the Free Software Foundation, either version 3 of the License, or |
||
6 | * (at your option) any later version. |
||
7 | * |
||
8 | * This program is distributed in the hope that it will be useful, |
||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
||
11 | * GNU General Public License for more details. |
||
12 | * |
||
13 | * You should have received a copy of the GNU General Public License |
||
14 | * along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
||
15 | |||
16 | /** |
||
17 | * Alert Templates |
||
18 | * |
||
19 | * @author f0o <[email protected]> |
||
20 | * @copyright 2014 f0o, LibreNMS |
||
21 | * @license GPL |
||
22 | */ |
||
23 | $status = 'error'; |
||
24 | |||
25 | if (! Auth::user()->hasGlobalAdmin()) { |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
26 | header('Content-Type: application/json'); |
||
27 | $response = ['status' => $status, 'message' => 'You need to be admin']; |
||
28 | exit(json_encode($response)); |
||
29 | } |
||
30 | |||
31 | $template_id = 0; |
||
32 | $template_newid = 0; |
||
33 | $create = true; |
||
34 | |||
35 | $name = $vars['name']; |
||
36 | if (isset($vars['template']) && empty(view(['template' => $vars['template']], [])->__toString())) { |
||
37 | $message = 'Template failed to be parsed, please check the syntax'; |
||
38 | } elseif (! empty($name)) { |
||
39 | if ($vars['template'] && is_numeric($vars['template_id'])) { |
||
40 | // Update template |
||
41 | $create = false; |
||
42 | $template_id = $vars['template_id']; |
||
43 | if (! dbUpdate(['template' => $vars['template'], 'name' => $name, 'title' => $vars['title'], 'title_rec' => $vars['title_rec']], 'alert_templates', 'id = ?', [$template_id]) >= 0) { |
||
44 | $status = 'ok'; |
||
45 | } else { |
||
46 | $message = 'Failed to update the template'; |
||
47 | } |
||
48 | } elseif ($vars['template']) { |
||
49 | // Create template |
||
50 | if ($name != 'Default Alert Template') { |
||
51 | $template_newid = dbInsert(['template' => $vars['template'], 'name' => $name, 'title' => $vars['title'], 'title_rec' => $vars['title_rec']], 'alert_templates'); |
||
52 | if ($template_newid != false) { |
||
0 ignored issues
–
show
|
|||
53 | $template_id = $template_newid; |
||
54 | $status = 'ok'; |
||
55 | } else { |
||
56 | $message = 'Could not create alert template'; |
||
57 | } |
||
58 | } else { |
||
59 | $message = 'This template name is reserved!'; |
||
60 | } |
||
61 | } else { |
||
62 | $message = 'We could not work out what you wanted to do!'; |
||
63 | } |
||
64 | if ($status == 'ok') { |
||
65 | $alertRulesOk = true; |
||
66 | dbDelete('alert_template_map', 'alert_templates_id = ?', [$template_id]); |
||
67 | $rules = explode(',', $vars['rules']); |
||
68 | if ($rules !== false) { |
||
69 | foreach ($rules as $rule_id) { |
||
70 | if (! dbInsert(['alert_rule_id' => $rule_id, 'alert_templates_id' => $template_id], 'alert_template_map')) { |
||
71 | $alertRulesOk = false; |
||
72 | } |
||
73 | } |
||
74 | } |
||
75 | if ($alertRulesOk) { |
||
76 | $status = 'ok'; |
||
77 | $message = 'Alert template has been ' . ($create ? 'created' : 'updated') . ' and attached rules have been updated.'; |
||
78 | } else { |
||
79 | $status = 'warning'; |
||
80 | $message = 'Alert template has been ' . ($create ? 'created' : 'updated') . ' but some attached rules have not been updated.'; |
||
81 | } |
||
82 | } |
||
83 | } else { |
||
84 | $message = "You haven't given name to your template"; |
||
85 | } |
||
86 | |||
87 | $response = ['status' => $status, 'message' => $message, 'newid' => $template_newid]; |
||
88 | |||
89 | echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
||
90 |