Issues (2963)

includes/html/modal/new_service.inc.php (3 issues)

1
<?php
2
/*
3
 * LibreNMS
4
 *
5
 * Copyright (c) 2016 Aaron Daniels <[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
if (Auth::user()->hasGlobalAdmin()) {
15
    // Build the types list.
16
    $dir = \LibreNMS\Config::get('nagios_plugins');
17
    if (file_exists($dir) && is_dir($dir)) {
0 ignored issues
show
It seems like $dir can also be of type null; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

17
    if (file_exists(/** @scrutinizer ignore-type */ $dir) && is_dir($dir)) {
Loading history...
It seems like $dir can also be of type null; however, parameter $filename of is_dir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

17
    if (file_exists($dir) && is_dir(/** @scrutinizer ignore-type */ $dir)) {
Loading history...
18
        $files = scandir($dir);
0 ignored issues
show
It seems like $dir can also be of type null; however, parameter $directory of scandir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

18
        $files = scandir(/** @scrutinizer ignore-type */ $dir);
Loading history...
19
        $dir .= DIRECTORY_SEPARATOR;
20
        foreach ($files as $file) {
21
            if (is_executable($dir . $file) && is_file($dir . $file) && strstr($file, 'check_')) {
22
                [,$check_name] = explode('_', $file, 2);
23
                $stype .= "<option value='$check_name'>$check_name</option>";
24
            }
25
        }
26
    } ?>
27
28
<div class="modal fade bs-example-modal-sm" id="create-service" tabindex="-1" role="dialog" aria-labelledby="Create" aria-hidden="true">
29
    <div class="modal-dialog">
30
        <div class="modal-content">
31
            <div class="modal-header">
32
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
33
                <h2 class="modal-title" id="Create">Add / Edit Service</h2>
34
            </div>
35
            <div class='alert alert-info'>Service will modified for the specified Device.</div>
36
            <div class='well well-lg'>
37
                <div class="modal-body">
38
                    <form method="post" role="form" id="service" class="form-horizontal service-form">
39
                        <?php echo csrf_field() ?>
40
                        <input type="hidden" name="service_id" id="service_id" value="">
41
                        <input type="hidden" name="service_template_id" id="service_template_id" value="">
42
                        <input type="hidden" name="device_id" id="device_id" value="<?php echo $device['device_id']?>">
43
                        <input type="hidden" name="type" id="type" value="create-service">
44
                        <div class="form-group">
45
                            <div class="col-sm-12">
46
                                <span id="ajax_response">&nbsp;</span>
47
                            </div>
48
                        </div>
49
                        <div class="form-group row">
50
                            <label for='name' class='col-sm-3 control-label'>Name: </label>
51
                            <div class="col-sm-9">
52
                                <input type='text' id='name' name='name' class='form-control input-sm' placeholder=''/>
53
                            </div>
54
                            <div class='col-sm-9'>
55
                            </div>
56
                        </div>
57
                        <div class="form-group row">
58
                            <label for='stype' class='col-sm-3 control-label'>Check Type: </label>
59
                            <div class="col-sm-9">
60
                                <select id='stype' name='stype' placeholder='type' class='form-control has-feedback'>
61
                                    <?php echo $stype?>
62
                                </select>
63
                            </div>
64
                            <div class='col-sm-9'>
65
                            </div>
66
                        </div>
67
                        <div class='form-group row'>
68
                            <label for='desc' class='col-sm-3 control-label'>Description: </label>
69
                            <div class='col-sm-9'>
70
                                <textarea id='desc' name='desc' class='form-control' rows='5'></textarea>
71
                            </div>
72
                            <div class='col-sm-9'>
73
                            </div>
74
                        </div>
75
                        <div class="form-group row">
76
                            <label for='ip' class='col-sm-3 control-label'>Remote Host: </label>
77
                            <div class="col-sm-9">
78
                                <input type='text' id='ip' name='ip' class='form-control has-feedback' placeholder='IP Address or Hostname'/>
79
                            </div>
80
                            <div class='col-sm-9'>
81
                            </div>
82
                        </div>
83
                        <div class="form-group row">
84
                            <label for='param' class='col-sm-3 control-label'>Parameters: </label>
85
                            <div class="col-sm-9">
86
                                <input type='text' id='param' name='param' class='form-control has-feedback' placeholder=''/>
87
                            </div>
88
                            <div class='col-sm-9'>
89
                            </div>
90
                        </div>
91
                        <div class="form-group row">
92
                            <div class="col-sm-12 alert alert-info">
93
                                <label class='control-label text-left input-sm'>Parameters may be required and will be different depending on the service check.</label>
94
                            </div>
95
                        </div>
96
                        <div class="form-group row">
97
                            <label for='ignore' class='col-sm-3 control-label'>Ignore alert tag: </label>
98
                            <div class="col-sm-9">
99
                                <input type="hidden" name="ignore" id='ignore' value="0">
100
                                <input type='checkbox' id='ignore_box' name='ignore_box' onclick="$('#ignore').attr('value', $('#ignore_box').prop('checked')?1:0);">
101
                            </div>
102
                        </div>
103
                        <div class="form-group row">
104
                            <label for='disabled' class='col-sm-3 control-label'>Disable polling and alerting: </label>
105
                            <div class="col-sm-9">
106
                                <input type='hidden' id='disabled' name='disabled' value="0">
107
                                <input type='checkbox' id='disabled_box' name='disabled_box' onclick="$('#disabled').attr('value', $('#disabled_box').prop('checked')?1:0);">
108
                            </div>
109
                        </div>
110
                        <hr>
111
                        <div class="form-group row">
112
                            <center><button class="btn btn-default btn-sm" type="submit" name="service-submit" id="service-submit" value="save">Save Service</button></center>
113
                        </div>
114
                        <div class="clearfix"></div>
115
                    </form>
116
                </div>
117
            </div>
118
        </div>
119
    </div>
120
</div>
121
122
<script>
123
124
// on-hide
125
$('#create-service').on('hide.bs.modal', function (event) {
126
    $('#stype').val('');
127
    $("#stype").prop("disabled", false);
128
    $('#ip').val('');
129
    $('#desc').val('');
130
    $('#param').val('');
131
    $('#ignore').val('');
132
    $('#ignore_box').val('');
133
    $('#disabled').val('');
134
    $('#disabled_box').val('');
135
    $('#service_template_id').val('');
136
    $('#name').val('');
137
    $('#service_template_name').val('');
138
});
139
140
// on-load
141
$('#create-service').on('show.bs.modal', function (e) {
142
    var button = $(e.relatedTarget);
143
    var service_id = button.data('service_id');
144
    var modal = $(this)
145
    $('#service_id').val(service_id);
146
    $.ajax({
147
        type: "POST",
148
        url: "ajax_form.php",
149
        data: { type: "parse-service", service_id: service_id },
150
        dataType: "json",
151
        success: function(output) {
152
            $('#stype').val(output['stype']);
153
            $("#stype").prop("disabled", true);
154
            $('#ip').val(output['ip']);
155
            $('#desc').val(output['desc']);
156
            $('#param').val(output['param']);
157
            $('#ignore').val(output['ignore']);
158
            $('#disabled').val(output['disabled']);
159
            $('#ignore_box').val(output['ignore']);
160
            $('#disabled_box').val(output['disabled']);
161
            if ($('#ignore').attr('value') == 1) {
162
                $('#ignore_box').prop("checked", true);
163
            }
164
            if ($('#disabled').attr('value') == 1) {
165
                $('#disabled_box').prop("checked", true);
166
            }
167
            $('#service_template_id').val(output['service_template_id']);
168
            $('#name').val(output['name']);
169
        }
170
    });
171
172
});
173
174
// on-submit
175
$('#service-submit').on("click", function(e) {
176
    e.preventDefault();
177
    $.ajax({
178
        type: "POST",
179
        url: "ajax_form.php",
180
        data: $('form.service-form').serialize(),
181
        success: function(result){
182
            if (result.status == 0) {
183
                // Yay.
184
                $("#create-service").modal('hide');
185
                $('#message').html('<div class="alert alert-info">' + result.message + '</div>');
186
                setTimeout(function() {
187
                    location.reload(1);
188
                }, 1500);
189
            }
190
            else {
191
                // Nay.
192
                $("#ajax_response").html('<div class="alert alert-danger">'+result.message+'</div>');
193
            }
194
        },
195
        error: function(){
196
            $("#ajax_response").html('<div class="alert alert-info">An error occurred creating this service.</div>');
197
        }
198
    });
199
});
200
201
</script>
202
    <?php
203
}
204