Issues (2963)

html/ajax_search.php (2 issues)

Labels
Severity
1
<?php
2
3
use LibreNMS\Util\Debug;
4
5
$init_modules = ['web', 'auth'];
6
require realpath(__DIR__ . '/..') . '/includes/init.php';
7
8
if (! Auth::check()) {
9
    exit('Unauthorized');
10
}
11
12
Debug::set($_REQUEST['debug']);
13
14
$device = [];
15
$ports = [];
16
$bgp = [];
17
$limit = (int) \LibreNMS\Config::get('webui.global_search_result_limit');
18
19
if (isset($_REQUEST['search'])) {
20
    $search = $_REQUEST['search'];
21
    header('Content-type: application/json');
22
    if (strlen($search) > 0) {
23
        $found = 0;
24
25
        if (! Auth::user()->hasGlobalRead()) {
0 ignored issues
show
The method hasGlobalRead() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

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

25
        if (! Auth::user()->/** @scrutinizer ignore-call */ hasGlobalRead()) {
Loading history...
26
            $device_ids = Permissions::devicesForUser()->toArray() ?: [0];
0 ignored issues
show
The method devicesForUser() does not exist on App\Facades\Permissions. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

26
            $device_ids = Permissions::/** @scrutinizer ignore-call */ devicesForUser()->toArray() ?: [0];
Loading history...
27
            $perms_sql = '`D`.`device_id` IN ' . dbGenPlaceholders(count($device_ids)) . ' AND ';
28
        } else {
29
            $device_ids = [];
30
            $perms_sql = '';
31
        }
32
33
        if ($_REQUEST['type'] == 'group') {
34
            foreach (dbFetchRows('SELECT id,name FROM device_groups WHERE name LIKE ?', ["%$search%"]) as $group) {
35
                if ($_REQUEST['map']) {
36
                    $results[] = [
37
                        'name'     => 'g:' . $group['name'],
38
                        'group_id' => $group['id'],
39
                    ];
40
                } else {
41
                    $results[] = ['name' => $group['name']];
42
                }
43
            }
44
45
            exit(json_encode($results));
46
        } elseif ($_REQUEST['type'] == 'alert-rules') {
47
            foreach (dbFetchRows('SELECT name FROM alert_rules WHERE name LIKE ?', ["%$search%"]) as $rules) {
48
                $results[] = ['name' => $rules['name']];
49
            }
50
51
            exit(json_encode($results));
52
        } elseif ($_REQUEST['type'] == 'device') {
53
            // Device search
54
55
            $query = 'SELECT *, `D`.`device_id` AS `device_id` FROM `devices` as `D`
56
                      LEFT JOIN `locations` AS `L` ON `L`.`id` = `D`.`location_id`';
57
58
            // user depending limitation
59
            if (! Auth::user()->hasGlobalRead()) {
60
                $query_args_list = $device_ids;
61
                $query_filter = $perms_sql;
62
            } else {
63
                $query_args_list = [];
64
                $query_filter = '';
65
            }
66
67
            // search filter
68
            $query_filter .= '(`D`.`hostname` LIKE ?
69
                              OR `L`.`location` LIKE ?
70
                              OR `D`.`sysName` LIKE ?
71
                              OR `D`.`purpose` LIKE ?
72
                              OR `D`.`serial` LIKE ?
73
                              OR `D`.`notes` LIKE ?';
74
            $query_args_list = array_merge($query_args_list, ["%$search%", "%$search%", "%$search%",
75
                "%$search%", "%$search%", "%$search%", ]);
76
77
            if (\LibreNMS\Util\IPv4::isValid($search, false)) {
78
                $query .= ' LEFT JOIN `ports` AS `P` ON `P`.`device_id` = `D`.`device_id`
79
                                LEFT JOIN `ipv4_addresses` AS `V4` ON `V4`.`port_id` = `P`.`port_id`';
80
                $query_filter .= ' OR `V4`.`ipv4_address` LIKE ?
81
                                       OR `D`.`overwrite_ip` LIKE ?
82
                                       OR `D`.`ip` = ? ';
83
                $query_args_list = array_merge($query_args_list, ["%$search%", "%$search%", inet_pton($search)]);
84
            } elseif (\LibreNMS\Util\IPv6::isValid($search, false)) {
85
                $query .= ' LEFT JOIN `ports` AS `P` ON `P`.`device_id` = `D`.`device_id`
86
                                LEFT JOIN `ipv6_addresses` AS `V6` ON `V6`.`port_id` = `P`.`port_id`';
87
                $query_filter .= ' OR `V6`.`ipv6_address` LIKE ?
88
                                       OR `D`.`overwrite_ip` LIKE ?
89
                                       OR `D`.`ip` = ? ';
90
                $query_args_list = array_merge($query_args_list, ["%$search%", "%$search%", inet_pton($search)]);
91
            } elseif (ctype_xdigit($mac_search = str_replace([':', '-', '.'], '', $search))) {
92
                $query .= ' LEFT JOIN `ports` as `M` on `M`.`device_id` = `D`.`device_id`';
93
                $query_filter .= ' OR `M`.`ifPhysAddress` LIKE ? ';
94
                $query_args_list[] = "%$mac_search%";
95
            }
96
97
            $query_filter .= ')';
98
99
            // result limitation
100
            $query_args_list[] = $limit;
101
            $results = dbFetchRows($query .
102
                                   ' WHERE ' . $query_filter .
103
                                   ' GROUP BY `D`.`hostname`
104
                                     ORDER BY `D`.`hostname` LIMIT ?', $query_args_list);
105
106
            if (count($results)) {
107
                $found = 1;
108
                $devices = count($results);
109
110
                foreach ($results as $result) {
111
                    $name = $result['hostname'];
112
                    if ($_REQUEST['map'] != 1 && $result['sysName'] != $name && ! empty($result['sysName'])) {
113
                        $name .= ' (' . $result['sysName'] . ') ';
114
                    }
115
                    if ($result['disabled'] == 1) {
116
                        $highlight_colour = '#808080';
117
                    } elseif ($result['ignored'] == 1 && $result['disabled'] == 0) {
118
                        $highlight_colour = '#000000';
119
                    } elseif ($result['status'] == 0 && $result['ignore'] == 0 && $result['disabled'] == 0) {
120
                        $highlight_colour = '#ff0000';
121
                    } elseif ($result['status'] == 1 && $result['ignore'] == 0 && $result['disabled'] == 0) {
122
                        $highlight_colour = '#008000';
123
                    }
124
125
                    $num_ports = dbFetchCell('SELECT COUNT(*) FROM `ports` AS `I`, `devices` AS `D` WHERE ' . $perms_sql . ' `I`.`device_id` = `D`.`device_id` AND `I`.`ignore` = 0 AND `I`.`deleted` = 0 AND `D`.`device_id` = ?', array_merge($device_ids, [$result['device_id']]));
126
127
                    $device[] = [
128
                        'name'            => $name,
129
                        'device_id'       => $result['device_id'],
130
                        'url'             => \LibreNMS\Util\Url::deviceUrl((int) $result['device_id']),
131
                        'colours'         => $highlight_colour,
132
                        'device_ports'    => $num_ports,
133
                        'device_image'    => getIcon($result),
134
                        'device_hardware' => $result['hardware'],
135
                        'device_os' => \LibreNMS\Config::getOsSetting($result['os'], 'text'),
136
                        'version'         => $result['version'],
137
                        'location'        => $result['location'],
138
                    ];
139
                }//end foreach
140
            }//end if
141
142
            $json = json_encode($device);
143
            exit($json);
144
        } elseif ($_REQUEST['type'] == 'ports') {
145
            // Search ports
146
            if (Auth::user()->hasGlobalRead()) {
147
                $results = dbFetchRows(
148
                    'SELECT `ports`.*,`devices`.* FROM `ports` LEFT JOIN `devices` ON  `ports`.`device_id` =  `devices`.`device_id` WHERE `ifAlias` LIKE ? OR `ifDescr` LIKE ? OR `ifName` LIKE ? ORDER BY ifDescr LIMIT ?',
149
                    ["%$search%", "%$search%", "%$search%", $limit]
150
                );
151
            } else {
152
                $results = dbFetchRows(
153
                    "SELECT DISTINCT(`I`.`port_id`), `I`.*, `D`.`hostname` FROM `ports` AS `I`, `devices` AS `D` WHERE $perms_sql `D`.`device_id` = `I`.`device_id` AND (`ifAlias` LIKE ? OR `ifDescr` LIKE ? OR `ifName` LIKE ?) ORDER BY ifDescr LIMIT ?",
154
                    array_merge($device_ids, ["%$search%", "%$search%", "%$search%", $limit])
155
                );
156
            }
157
158
            if (count($results)) {
159
                $found = 1;
160
161
                foreach ($results as $result) {
162
                    $name = $result['ifDescr'] == $result['ifAlias'] ? $result['ifName'] : $result['ifDescr'];
163
                    $description = \LibreNMS\Util\Clean::html($result['ifAlias'], []);
164
165
                    if ($result['deleted'] == 0 && ($result['ignore'] == 0 || $result['ignore'] == 0) && ($result['ifInErrors_delta'] > 0 || $result['ifOutErrors_delta'] > 0)) {
166
                        // Errored ports
167
                        $port_colour = '#ffa500';
168
                    } elseif ($result['deleted'] == 0 && ($result['ignore'] == 1 || $result['ignore'] == 1)) {
169
                        // Ignored ports
170
                        $port_colour = '#000000';
171
                    } elseif ($result['deleted'] == 0 && $result['ifAdminStatus'] == 'down' && $result['ignore'] == 0 && $result['ignore'] == 0) {
172
                        // Shutdown ports
173
                        $port_colour = '#808080';
174
                    } elseif ($result['deleted'] == 0 && $result['ifOperStatus'] == 'down' && $result['ifAdminStatus'] == 'up' && $result['ignore'] == 0 && $result['ignore'] == 0) {
175
                        // Down ports
176
                        $port_colour = '#ff0000';
177
                    } elseif ($result['deleted'] == 0 && $result['ifOperStatus'] == 'up' && $result['ignore'] == 0 && $result['ignore'] == 0) {
178
                        // Up ports
179
                        $port_colour = '#008000';
180
                    }//end if
181
182
                    $ports[] = [
183
                        'count'       => count($results),
184
                        'url'         => generate_port_url($result),
185
                        'name'        => $name,
186
                        'description' => $description,
187
                        'colours'     => $port_colour,
188
                        'hostname'    => format_hostname($result),
189
                        'port_id'     => $result['port_id'],
190
                    ];
191
                }//end foreach
192
            }//end if
193
194
            $json = json_encode($ports);
195
            exit($json);
196
        } elseif ($_REQUEST['type'] == 'bgp') {
197
            // Search bgp peers
198
            $results = dbFetchRows(
199
                "SELECT `bgpPeers`.*,`D`.* FROM `bgpPeers`, `devices` AS `D` WHERE $perms_sql `bgpPeers`.`device_id`=`D`.`device_id` AND  (`astext` LIKE ? OR `bgpPeerIdentifier` LIKE ? OR `bgpPeerRemoteAs` LIKE ?) ORDER BY `astext` LIMIT ?",
200
                array_merge($device_ids, ["%$search%", "%$search%", "%$search%", $limit])
201
            );
202
203
            if (count($results)) {
204
                $found = 1;
205
206
                foreach ($results as $result) {
207
                    $name = $result['bgpPeerIdentifier'];
208
                    $description = $result['astext'];
209
                    $remoteas = $result['bgpPeerRemoteAs'];
210
                    $localas = $result['bgpLocalAs'];
211
212
                    if ($result['bgpPeerAdminStatus'] == 'start' && $result['bgpPeerState'] != 'established') {
213
                        // Session active but errored
214
                        $port_colour = '#ffa500';
215
                    } elseif ($result['bgpPeerAdminStatus'] != 'start') {
216
                        // Session inactive
217
                        $port_colour = '#000000';
218
                    } elseif ($result['bgpPeerAdminStatus'] == 'start' && $result['bgpPeerState'] == 'established') {
219
                        // Session Up
220
                        $port_colour = '#008000';
221
                    }
222
223
                    if ($result['bgpPeerRemoteAs'] == $result['bgpLocalAs']) {
224
                        $bgp_image = 'fa fa-square fa-lg icon-theme';
225
                    } else {
226
                        $bgp_image = 'fa fa-external-link-square fa-lg icon-theme';
227
                    }
228
229
                    $bgp[] = [
230
                        'count'       => count($results),
231
                        'url'         => \LibreNMS\Util\Url::generate(['page' => 'device', 'device' => $result['device_id'], 'tab' => 'routing', 'proto' => 'bgp'], []),
232
                        'name'        => $name,
233
                        'description' => $description,
234
                        'localas'     => $localas,
235
                        'bgp_image'   => $bgp_image,
236
                        'remoteas'    => $remoteas,
237
                        'colours'     => $port_colour,
238
                        'hostname'    => format_hostname($result),
239
                    ];
240
                }//end foreach
241
            }//end if
242
243
            $json = json_encode($bgp);
244
            exit($json);
245
        } elseif ($_REQUEST['type'] == 'applications') {
246
            // Device search
247
            $results = dbFetchRows(
248
                "SELECT * FROM `applications` INNER JOIN `devices` AS `D` ON `D`.`device_id` = `applications`.`device_id` WHERE $perms_sql (`app_type` LIKE ? OR `hostname` LIKE ?) ORDER BY hostname LIMIT ?",
249
                array_merge($device_ids, ["%$search%", "%$search%", $limit])
250
            );
251
252
            if (count($results)) {
253
                $found = 1;
254
                $devices = count($results);
255
256
                foreach ($results as $result) {
257
                    $name = $result['app_type'];
258
                    if ($result['disabled'] == 1) {
259
                        $highlight_colour = '#808080';
260
                    } elseif ($result['ignored'] == 1 && $result['disabled'] == 0) {
261
                        $highlight_colour = '#000000';
262
                    } elseif ($result['status'] == 0 && $result['ignore'] == 0 && $result['disabled'] == 0) {
263
                        $highlight_colour = '#ff0000';
264
                    } elseif ($result['status'] == 1 && $result['ignore'] == 0 && $result['disabled'] == 0) {
265
                        $highlight_colour = '#008000';
266
                    }
267
268
                    $device[] = [
269
                        'name'            => $name,
270
                        'hostname'        => format_hostname($result),
271
                        'app_id'          => $result['app_id'],
272
                        'device_id'       => $result['device_id'],
273
                        'colours'         => $highlight_colour,
274
                        'device_image'    => getIcon($result),
275
                        'device_hardware' => $result['hardware'],
276
                        'device_os' => \LibreNMS\Config::getOsSetting($result['os'], 'text'),
277
                        'version'         => $result['version'],
278
                        'location'        => $result['location'],
279
                    ];
280
                }//end foreach
281
            }//end if
282
283
            $json = json_encode($device);
284
            exit($json);
285
        } elseif ($_REQUEST['type'] == 'munin') {
286
            // Device search
287
            $results = dbFetchRows(
288
                "SELECT * FROM `munin_plugins` INNER JOIN `devices` AS `D` ON `D`.`device_id` = `munin_plugins`.`device_id` WHERE $perms_sql (`mplug_type` LIKE ? OR `mplug_title` LIKE ? OR `hostname` LIKE ?) ORDER BY hostname LIMIT ?",
289
                array_merge($device_ids, ["%$search%", "%$search%", "%$search%", $limit])
290
            );
291
292
            if (count($results)) {
293
                $found = 1;
294
                $devices = count($results);
295
296
                foreach ($results as $result) {
297
                    $name = $result['mplug_title'];
298
                    if ($result['disabled'] == 1) {
299
                        $highlight_colour = '#808080';
300
                    } elseif ($result['ignored'] == 1 && $result['disabled'] == 0) {
301
                        $highlight_colour = '#000000';
302
                    } elseif ($result['status'] == 0 && $result['ignore'] == 0 && $result['disabled'] == 0) {
303
                        $highlight_colour = '#ff0000';
304
                    } elseif ($result['status'] == 1 && $result['ignore'] == 0 && $result['disabled'] == 0) {
305
                        $highlight_colour = '#008000';
306
                    }
307
308
                    $device[] = [
309
                        'name'            => $name,
310
                        'hostname'        => format_hostname($result),
311
                        'device_id'       => $result['device_id'],
312
                        'colours'         => $highlight_colour,
313
                        'device_image'    => getIcon($result),
314
                        'device_hardware' => $result['hardware'],
315
                        'device_os' => \LibreNMS\Config::getOsSetting($result['os'], 'text'),
316
                        'version'         => $result['version'],
317
                        'location'        => $result['location'],
318
                        'plugin'          => $result['mplug_type'],
319
                    ];
320
                }//end foreach
321
            }//end if
322
323
            $json = json_encode($device);
324
            exit($json);
325
        } elseif ($_REQUEST['type'] == 'iftype') {
326
            // Device search
327
            $results = dbFetchRows(
328
                "SELECT `ports`.ifType FROM `ports` WHERE $perms_sql `ifType` LIKE ? GROUP BY ifType ORDER BY ifType LIMIT ?",
329
                array_merge($device_ids, ["%$search%", $limit])
330
            );
331
332
            if (count($results)) {
333
                $found = 1;
334
                $devices = count($results);
335
336
                foreach ($results as $result) {
337
                    $device[] = [
338
                        'filter'            => $result['ifType'],
339
                    ];
340
                }//end foreach
341
            }//end if
342
343
            $json = json_encode($device);
344
            exit($json);
345
        } elseif ($_REQUEST['type'] == 'bill') {
346
            // Device search
347
            if (Auth::user()->hasGlobalRead()) {
348
                $results = dbFetchRows(
349
                    'SELECT `bills`.bill_id, `bills`.bill_name FROM `bills` WHERE `bill_name` LIKE ? OR `bill_notes` LIKE ? LIMIT ?',
350
                    ["%$search%", "%$search%", $limit]
351
                );
352
            } else {
353
                $results = dbFetchRows(
354
                    'SELECT `bills`.bill_id, `bills`.bill_name FROM `bills` INNER JOIN `bill_perms` ON `bills`.bill_id = `bill_perms`.bill_id WHERE `bill_perms`.user_id = ? AND (`bill_name` LIKE ? OR `bill_notes` LIKE ?) LIMIT ?',
355
                    [Auth::id(), "%$search%", "%$search%", $limit]
356
                );
357
            }
358
            $json = json_encode($results);
359
            exit($json);
360
        }//end if
361
    }//end if
362
}//end if
363