Issues (2963)

includes/html/modal/edit_alert_transport.inc.php (1 issue)

1
<?php
2
/*
3
 * LibreNMS
4
 *
5
 * Copyright (c) 2018 Vivia Nguyen-Tran <[email protected]>
6
 *
7
 * This program is free software: you can redistribute it and/or modify it
8
 * under the terms of the GNU General Public License as published by the
9
 * Free Software Foundation, either version 3 of the License, or (at your
10
 * option) any later version.  Please see LICENSE.txt at the top level of
11
 * the source code distribution for details.
12
 */
13
14
use LibreNMS\Config;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Config. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
16
if (Auth::user()->hasGlobalAdmin()) {
17
    ?>
18
<!--Modal for adding or updating an alert transport -->
19
    <div class="modal fade" id="edit-alert-transport" tabindex="-1" role="dialog"
20
         aria-labelledby="Edit-transport" aria-hidden="true">
21
        <div class="modal-dialog modal-lg">
22
            <div class="modal-content">
23
                <div class="modal-header">
24
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
25
                    <h5 class="modal-title" id="Edit-transport">Alert Transport :: <a target="_blank" href="https://docs.librenms.org/Alerting/">Docs <i class="fa fa-book fa-1x"></i></a> </h5>
26
                </div>
27
                <div class="modal-body">
28
                    <form method="post" role="form" id="transports" class="form-horizontal transports-form">
29
                        <?php echo csrf_field() ?>
30
                        <input type="hidden" name="transport_id" id="transport_id" value="">
31
                        <input type="hidden" name="type" id="type" value="alert-transports">
32
                        <div class='form-group' title="The description of this alert transport.">
33
                            <label for='name' class='col-sm-3 col-md-2 control-label'>Transport name: </label>
34
                            <div class='col-sm-9 col-md-10'>
35
                                <input type='text' id='name' name='name' class='form-control validation' maxlength='200' required>
36
                            </div>
37
                        </div>
38
                        <div class="form-group" title="The type of transport.">
39
                            <label for='transport-choice' class='col-sm-3 col-md-2 control-label'>Transport type: </label>
40
                            <div class="col-sm-3">
41
                                <select name='transport-choice' id='transport-choice' class='form-control'>
42
    <?php
43
44
// Create list of transport
45
    $transport_dir = Config::get('install_dir') . '/LibreNMS/Alert/Transport';
46
    $transports_list = [];
47
    foreach (scandir($transport_dir) as $transport) {
48
        $transport = strstr($transport, '.', true);
49
        if (empty($transport)) {
50
            continue;
51
        }
52
        $transports_list[] = $transport;
53
    }
54
    foreach ($transports_list as $transport) {
55
        echo '<option value="' . strtolower($transport) . '-form">' . $transport . '</option>';
56
    } ?>
57
                                </select>
58
                            </div>
59
                        </div>
60
                        <div class="form-group" title="The transport is default.">
61
                            <label for="default" class="col-sm-3 col-md-2 control-label">Default Alert: </label>
62
                            <div class="col-sm-2">
63
                                <input type="checkbox" name="is_default" id="is_default">
64
                            </div>
65
                        </div>
66
                    </form>
67
    <?php
68
69
    $switches = []; // store names of bootstrap switches
70
    foreach ($transports_list as $transport) {
71
        $class = 'LibreNMS\\Alert\\Transport\\' . $transport;
72
73
        if (! method_exists($class, 'configTemplate')) {
74
            // Skip since support has not been added
75
            continue;
76
        }
77
78
        echo '<form method="post" role="form" id="' . strtolower($transport) . '-form" class="form-horizontal transport">';
79
        echo csrf_field();
80
        echo '<input type="hidden" name="transport-type" id="transport-type" value="' . strtolower($transport) . '">';
81
82
        $tmp = call_user_func($class . '::configTemplate');
83
84
        foreach ($tmp['config'] as $item) {
85
            if ($item['type'] !== 'hidden') {
86
                echo '<div class="form-group" title="' . $item['descr'] . '">';
87
                echo '<label for="' . $item['name'] . '" class="col-sm-3 col-md-2 control-label">' . $item['title'] . ': </label>';
88
                if ($item['type'] == 'text' || $item['type'] == 'password') {
89
                    echo '<div class="col-sm-9 col-md-10">';
90
                    echo '<input type="' . $item['type'] . '" id="' . $item['name'] . '" name="' . $item['name'] . '" class="form-control" ';
91
                    if ($item['required']) {
92
                        echo 'required>';
93
                    } else {
94
                        echo '>';
95
                    }
96
                    echo '</div>';
97
                } elseif ($item['type'] == 'checkbox') {
98
                    echo '<div class="col-sm-2">';
99
                    echo '<input type="checkbox" name="' . $item['name'] . '" id="' . $item['name'] . '">';
100
                    echo '</div>';
101
                    $switches[$item['name']] = $item['default'];
102
                } elseif ($item['type'] == 'select') {
103
                    echo '<div class="col-sm-3">';
104
                    echo '<select name="' . $item['name'] . '" id="' . $item['name'] . '" class="form-control">';
105
                    foreach ($item['options'] as $descr => $opt) {
106
                        echo '<option value="' . $opt . '">' . $descr . '</option>';
107
                    }
108
                    echo '</select>';
109
                    echo '</div>';
110
                } elseif ($item['type'] === 'textarea') {
111
                    echo '<div class="col-sm-9 col-md-10">';
112
                    echo '<textarea name="' . $item['name'] . '" id="' . $item['name'] . '" class="form-control" placeholder="' . $item['descr'] . '">';
113
                    echo '</textarea>';
114
                    echo '</div>';
115
                } elseif ($item['type'] === 'oauth') {
116
                    $class = isset($item['class']) ? $item['class'] : 'btn-success';
117
                    $callback = urlencode(url()->current() . '/?oauthtransport=' . $transport);
118
                    $url = $item['url'] . $callback;
119
120
                    echo '<a class="btn btn-oauth ' . $class . '"';
121
                    echo '" href="' . $url . '" data-base-url="' . $url . '">';
122
                    if (isset($item['icon'])) {
123
                        echo '<img src="' . asset('images/transports/' . $item['icon']) . '"  width="24" height="24"> ';
124
                    }
125
                    echo $item['descr'];
126
                    echo '</a>';
127
                }
128
                echo '</div>';
129
            }
130
        }
131
        echo '<div class="form-group">';
132
        echo '<div class="col-sm-12 text-center">';
133
        echo '<button type="button" class="btn btn-success btn-save" name="save-transport">';
134
        echo 'Save Transport';
135
        echo '</button>';
136
        echo '</div>';
137
        echo '</div>';
138
        echo '</form>';
139
    } ?>
140
                </div>
141
            </div>
142
        </div>
143
    </div>
144
<!-- Modal end for adding or updating an alert tramsport-->
145
146
<!--Modal for deleting an alert transport -->
147
    <div class="modal fade" id="delete-alert-transport" tabindex="-1" role="dialog"
148
         aria-labelledby="Delete" aria-hidden="true">
149
        <div class="modal-dialog modal-sm">
150
            <div class="modal-content">
151
                <div class="modal-header">
152
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
153
                    <h5 class="modal-title" id="Delete">Confirm Transport Delete</h5>
154
                </div>
155
                <div class="modal-body">
156
                    <p>If you would like to remove this alert transport then please click Delete.</p>
157
                </div>
158
                <div class="modal-footer">
159
                    <form role="form" class="remove_transport_form">
160
                        <?php echo csrf_field() ?>
161
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
162
                        <button type="submit" class="btn btn-danger danger" id="remove-alert-transport" data-target="remove-alert-transport">Delete</button>
163
                        <input type="hidden" name="transport_id" id="delete_transport_id" value="">
164
                        <input type="hidden" name="confirm" id="confirm" value="yes">
165
                    </form>
166
                </div>
167
            </div>
168
        </div>
169
    </div>
170
<!--Modal end for deleting an alert transport -->
171
172
    <script>
173
        // Scripts related to editing/updating alert transports
174
175
        // Display different form on selection
176
        $("#transport-choice").on("change", function (){
177
            $(".transport").hide();
178
            $("#" + $(this).val()).show().find("input:text").val("");
179
180
        });
181
182
        $("#edit-alert-transport").on("show.bs.modal", function(e) {
183
            // Get transport id of clicked element
184
            var transport_id = $(e.relatedTarget).data("transport_id");
185
            $("#transport_id").val(transport_id);
186
            if(transport_id > 0) {
187
                $.ajax({
188
                    type: "POST",
189
                    url: "ajax_form.php",
190
                    data: { type: "show-alert-transport", transport_id: transport_id },
191
                    success: function (data) {
192
                        loadTransport(data);
193
                    },
194
                    error: function () {
195
                        toastr.error("Failed to process alert transport");
196
                    }
197
                });
198
199
            } else {
200
            // Resetting to default
201
                $("#name").val("");
202
                $("#transport-choice").val("mail-form");
203
                $(".transport").hide();
204
                $("#" + $("#transport-choice").val()).show().find("input:text").val("");
205
                $("#is_default").bootstrapSwitch('state', false);
206
207
                // Turn on all switches in form
208
                var switches = <?php echo json_encode($switches); ?>;
209
                $.each(switches, function(name, state) {
210
                    $("input[name="+name+"]").bootstrapSwitch('state', state);
211
                });
212
            }
213
        });
214
215
        function loadTransport(transport) {
216
            var form_id = transport.type+"-form";
217
            var transport_form = $("#" + form_id);
218
219
            $("#name").val(transport.name);
220
            $("#transport-choice").val(form_id);
221
            $("#is_default").bootstrapSwitch('state', transport.is_default);
222
            $(".transport").hide();
223
            transport_form.show().find("input:text").val("");
224
225
            // Populate the field values
226
            transport.details.forEach(function(config) {
227
                var $field = transport_form.find("#" + config.name);
228
                if ($field.prop('type') == 'checkbox') {
229
                    $field.bootstrapSwitch('state', config.value);
230
                } else {
231
                    $field.val(config.value);
232
                }
233
            });
234
        }
235
236
        $(".btn-oauth").on("click", function (e) {
237
            this.href = $(this).data('base-url') + '%26id=' + $("#transport_id").val();
238
        });
239
240
        // Save alert transport
241
        $(".btn-save").on("click", function (e) {
242
            e.preventDefault();
243
244
            //Combine form data (general and transport specific)
245
            data = $("form.transports-form").serializeArray();
246
            data = data.concat($("#" + $("#transport-choice").val()).serializeArray());
247
248
            if (data !== null) {
249
                //post data to ajax form
250
                $.ajax({
251
                    type: "POST",
252
                    url: "ajax_form.php",
253
                    data: data,
254
                    dataType: "json",
255
                    success: function (data) {
256
                        if (data.status == 'ok') {
257
                            toastr.success(data.message);
258
                            setTimeout(function (){
259
                                $("#edit-alert-transports").modal("hide");
260
                                window.location.reload();
261
                            }, 500);
262
                        } else {
263
                            toastr.error(data.message);
264
                        }
265
                    },
266
                    error: function () {
267
                        toastr.error("Failed to process alert transport");
268
                    }
269
                });
270
            }
271
        });
272
273
        // Scripts related to deleting an alert transport
274
275
        // Populate transport id value
276
        $("#delete-alert-transport").on("show.bs.modal", function(event) {
277
            transport_id = $(event.relatedTarget).data("transport_id");
278
            $("#delete_transport_id").val(transport_id);
279
        });
280
281
        // Delete the alert transport
282
        $("#remove-alert-transport").on("click", function(event) {
283
            event.preventDefault();
284
            var transport_id = $("#delete_transport_id").val();
285
            $.ajax({
286
                type: "POST",
287
                url: "ajax_form.php",
288
                data: { type: "delete-alert-transport", transport_id: transport_id },
289
                dataType: "json",
290
                success: function(data) {
291
                    if (data.status == 'ok') {
292
                        toastr.success(data.message);
293
                        $("#alert-transport-" + transport_id).remove();
294
                        $("#delete-alert-transport").modal("hide");
295
                    } else {
296
                        toastr.error(data.message);
297
                    }
298
                },
299
                error: function() {
300
                    toastr.error("The alert transport could not be deleted.");
301
                }
302
            });
303
        });
304
305
    </script>
306
307
    <?php
308
}
309