Issues (2963)

includes/html/list/devices.inc.php (2 issues)

1
<?php
2
/**
3
 * devices.inc.php
4
 *
5
 * List devices
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 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
$query = '';
26
$where = [];
27
$params = [];
28
29
if (! Auth::user()->hasGlobalRead()) {
30
    $device_ids = Permissions::devicesForUser()->toArray() ?: [0];
31
    $where[] = ' `devices`.`device_id` IN ' . dbGenPlaceholders(count($device_ids));
32
    $params = array_merge($params, $device_ids);
33
}
34
35
if (! empty($_REQUEST['search'])) {
36
    $where[] = '(`hostname` LIKE ? OR `sysName` LIKE ?)';
37
    $search = '%' . $_REQUEST['search'] . '%';
38
    $params[] = $search;
39
    $params[] = $search;
40
}
41
42
if (! empty($where)) {
43
    $query .= ' WHERE ';
44
    $query .= implode(' AND ', $where);
45
}
46
47
$total = dbFetchCell("SELECT COUNT(*) FROM `devices` $query", $params);
48
$more = false;
49
50
if (! empty($_REQUEST['limit'])) {
51
    $limit = (int) $_REQUEST['limit'];
52
    $page = isset($_REQUEST['page']) ? (int) $_REQUEST['page'] : 1;
53
    $offset = ($page - 1) * $limit;
54
55
    $query .= " LIMIT $offset, $limit";
56
} else {
57
    $offset = 0;
58
}
59
60
$sql = "SELECT `device_id`, `hostname`, `sysName` FROM `devices` $query";
61
$devices = array_map(function ($device) {
62
    return [
63
        'id' => $device['device_id'],
64
        'text' => format_hostname($device),
65
    ];
66
}, dbFetchRows($sql, $params));
67
68
$more = ($offset + count($devices)) < $total;
69
70
array_multisort(array_column($devices, 'text'), SORT_ASC, $devices);
0 ignored issues
show
array_column($devices, 'text') cannot be passed to array_multisort() as the parameter $array expects a reference. ( Ignorable by Annotation )

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

70
array_multisort(/** @scrutinizer ignore-type */ array_column($devices, 'text'), SORT_ASC, $devices);
Loading history...
SORT_ASC cannot be passed to array_multisort() as the parameter $rest expects a reference. ( Ignorable by Annotation )

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

70
array_multisort(array_column($devices, 'text'), /** @scrutinizer ignore-type */ SORT_ASC, $devices);
Loading history...
71
72
return [$devices, $more];
73