1 | <?php |
||
2 | /** |
||
3 | * alert-transports.inc.php |
||
4 | * |
||
5 | * LibreNMS alert-transports.inc.php for processor |
||
6 | * |
||
7 | * This program is free software: you can redistribute it and/or modify |
||
8 | * it under the terms of the GNU General Public License as published by |
||
9 | * the Free Software Foundation, either version 3 of the License, or |
||
10 | * (at your option) any later version. |
||
11 | * |
||
12 | * This program is distributed in the hope that it will be useful, |
||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
||
15 | * GNU General Public License for more details. |
||
16 | * |
||
17 | * You should have received a copy of the GNU General Public License |
||
18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
19 | * |
||
20 | * @link https://www.librenms.org |
||
21 | * |
||
22 | * @copyright 2018 Vivia Nguyen-Tran |
||
23 | * @author Vivia Nguyen-Tran <[email protected]> |
||
24 | */ |
||
25 | |||
26 | use Illuminate\Container\Container; |
||
27 | use Illuminate\Filesystem\Filesystem; |
||
28 | use Illuminate\Translation\FileLoader; |
||
29 | use Illuminate\Translation\Translator; |
||
30 | use Illuminate\Validation\Factory; |
||
31 | |||
32 | header('Content-type: application/json'); |
||
33 | |||
34 | if (! Auth::user()->hasGlobalAdmin()) { |
||
35 | exit(json_encode([ |
||
36 | 'status' => 'error', |
||
37 | 'message' => 'You need to be admin', |
||
38 | ])); |
||
39 | } |
||
40 | |||
41 | $status = 'ok'; |
||
42 | $message = ''; |
||
43 | |||
44 | $transport_id = $vars['transport_id']; |
||
45 | $name = $vars['name']; |
||
46 | $is_default = (int) (isset($vars['is_default']) && $vars['is_default'] == 'on'); |
||
47 | $transport_type = $vars['transport-type']; |
||
48 | |||
49 | if (empty($name)) { |
||
50 | $status = 'error'; |
||
51 | $message = 'No transport name provided'; |
||
52 | } elseif (empty($transport_type)) { |
||
53 | $status = 'error'; |
||
54 | $message = 'Missing transport information'; |
||
55 | } else { |
||
56 | $details = [ |
||
57 | 'transport_name' => $name, |
||
58 | 'is_default' => $is_default, |
||
59 | ]; |
||
60 | |||
61 | if (is_numeric($transport_id) && $transport_id > 0) { |
||
62 | // Update the fields -- json config field will be updated later |
||
63 | dbUpdate($details, 'alert_transports', 'transport_id=?', [$transport_id]); |
||
64 | } else { |
||
65 | // Insert the new alert transport |
||
66 | $newEntry = true; |
||
67 | $transport_id = dbInsert($details, 'alert_transports'); |
||
68 | } |
||
69 | |||
70 | if ($transport_id) { |
||
71 | $class = 'LibreNMS\\Alert\\Transport\\' . ucfirst($transport_type); |
||
72 | |||
73 | if (! method_exists($class, 'configTemplate')) { |
||
74 | exit(json_encode([ |
||
75 | 'status' => 'error', |
||
76 | 'message' => 'This transport type is not yet supported', |
||
77 | ])); |
||
78 | } |
||
79 | |||
80 | // Build config values |
||
81 | $result = call_user_func_array($class . '::configTemplate', []); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
82 | $loader = new FileLoader(new Filesystem, "$install_dir/resources/lang"); |
||
83 | $translator = new Translator($loader, 'en'); |
||
84 | $validation = new Factory($translator, new Container); |
||
85 | $validator = $validation->make($vars, $result['validation']); |
||
86 | if ($validator->fails()) { |
||
87 | $errors = $validator->errors(); |
||
88 | foreach ($errors->all() as $error) { |
||
89 | $message .= "$error<br>"; |
||
90 | } |
||
91 | $status = 'error'; |
||
92 | } else { |
||
93 | $transport_config = (array) json_decode(dbFetchCell('SELECT transport_config FROM alert_transports WHERE transport_id=?', [$transport_id]), true); |
||
94 | foreach ($result['config'] as $tmp_config) { |
||
95 | if (isset($tmp_config['name']) && $tmp_config['type'] !== 'hidden') { |
||
96 | $transport_config[$tmp_config['name']] = $vars[$tmp_config['name']]; |
||
97 | } |
||
98 | } |
||
99 | //Update the json config field |
||
100 | $detail = [ |
||
101 | 'transport_type' => $transport_type, |
||
102 | 'transport_config' => json_encode($transport_config), |
||
103 | ]; |
||
104 | $where = 'transport_id=?'; |
||
105 | |||
106 | dbUpdate($detail, 'alert_transports', $where, [$transport_id]); |
||
107 | |||
108 | $status = 'ok'; |
||
109 | $message = 'Updated alert transports'; |
||
110 | } |
||
111 | if ($status == 'error' && $newEntry) { |
||
112 | //If error, we will have to delete the new entry in alert_transports tbl |
||
113 | $where = '`transport_id`=?'; |
||
114 | dbDelete('alert_transports', $where, [$transport_id]); |
||
115 | } |
||
116 | } else { |
||
117 | $status = 'error'; |
||
118 | $message = 'Failed to update transport'; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | exit(json_encode([ |
||
123 | 'status' => $status, |
||
124 | 'message' => $message, |
||
125 | ])); |
||
126 |