Issues (2963)

includes/html/table/ports.inc.php (3 issues)

1
<?php
2
/**
3
 * ports.inc.php
4
 *
5
 * Exports the ports table to json
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  2016 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
$where = "`D`.`hostname` != '' ";
26
$param = [];
27
$sql = 'FROM `ports`';
28
29
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

29
if (! Auth::user()->/** @scrutinizer ignore-call */ hasGlobalRead()) {
Loading history...
30
    $port_ids = Permissions::portsForUser()->toArray() ?: [0];
0 ignored issues
show
The method portsForUser() 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

30
    $port_ids = Permissions::/** @scrutinizer ignore-call */ portsForUser()->toArray() ?: [0];
Loading history...
31
    $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

31
    $device_ids = Permissions::/** @scrutinizer ignore-call */ devicesForUser()->toArray() ?: [0];
Loading history...
32
    $where .= ' AND (`ports`.`port_id` IN ' . dbGenPlaceholders(count($port_ids));
33
    $where .= ' OR `D`.`device_id` IN ' . dbGenPlaceholders(count($device_ids));
34
    $where .= ')';
35
    $param = array_merge($param, $port_ids, $device_ids);
36
}
37
38
$sql .= ' LEFT JOIN `devices` AS `D` ON `ports`.`device_id` = `D`.`device_id`';
39
40
if (! empty($vars['hostname'])) {
41
    $where .= ' AND (D.hostname LIKE ? OR D.sysName LIKE ?)';
42
    $param += array_fill(count($param), 2, '%' . $vars['hostname'] . '%');
43
}
44
45
if (! empty($vars['location'])) {
46
    $where .= ' AND `D`.`location_id` = ?';
47
    $param[] = $vars['location'];
48
}
49
50
$sql .= " WHERE $where ";
51
52
if (! empty($vars['errors'])) {
53
    $sql .= ' AND (`ports`.`ifInErrors_delta` > 0 OR `ports`.`ifOutErrors_delta` > 0)';
54
}
55
56
if (! empty($vars['device_id'])) {
57
    $sql .= ' AND `ports`.`device_id`=?';
58
    $param[] = $vars['device_id'];
59
}
60
61
if (! empty($vars['state'])) {
62
    switch ($vars['state']) {
63
        case 'down':
64
            $sql .= ' AND `ports`.`ifAdminStatus` = ? AND `ports`.`ifOperStatus` = ?';
65
            $param[] = 'up';
66
            $param[] = 'down';
67
            break;
68
        case 'up':
69
            $sql .= ' AND `ports`.`ifAdminStatus` = ? AND `ports`.`ifOperStatus` = ?';
70
            $param[] = 'up';
71
            $param[] = 'up';
72
            break;
73
        case 'admindown':
74
            $sql .= ' AND `ports`.`ifAdminStatus` = ? AND `D`.`ignore` = 0';
75
            $param[] = 'down';
76
            break;
77
    }
78
}
79
80
if (! empty($vars['ifSpeed'])) {
81
    $sql .= ' AND `ports`.`ifSpeed`=?';
82
    $param[] = $vars['ifSpeed'];
83
}
84
85
if (! empty($vars['ifType'])) {
86
    $sql .= ' AND `ports`.`ifType`=?';
87
    $param[] = $vars['ifType'];
88
}
89
90
if (! empty($vars['port_descr_type'])) {
91
    $sql .= ' AND `ports`.`port_descr_type`=?';
92
    $param[] = $vars['port_descr_type'];
93
}
94
95
if (! empty($vars['ifAlias'])) {
96
    $sql .= ' AND `ports`.`ifAlias` LIKE ?';
97
    $param[] = '%' . $vars['ifAlias'] . '%';
98
}
99
100
$sql .= ' AND `ports`.`disabled`=?';
101
$param[] = (int) (isset($vars['disabled']) && $vars['disabled']);
102
103
$sql .= ' AND `ports`.`ignore`=?';
104
$param[] = (int) (isset($vars['ignore']) && $vars['ignore']);
105
106
$sql .= ' AND `ports`.`deleted`=?';
107
$param[] = (int) (isset($vars['deleted']) && $vars['deleted']);
108
109
$count_sql = "SELECT COUNT(`ports`.`port_id`) $sql";
110
$total = (int) dbFetchCell($count_sql, $param);
111
112
if (isset($sort) && ! empty($sort)) {
113
    [$sort_column, $sort_order] = explode(' ', trim($sort));
114
    if ($sort_column == 'device') {
115
        $sql .= " ORDER BY `D`.`hostname` $sort_order";
116
    } elseif ($sort_column == 'port') {
117
        $sql .= " ORDER BY `ifDescr` $sort_order";
118
    } elseif ($sort_column == 'ifLastChange') {
119
        $sql .= " ORDER BY `secondsIfLastChange` $sort_order";
120
    } else {
121
        $sql .= " ORDER BY `$sort_column` $sort_order";
122
    }
123
}
124
125
if (isset($current)) {
126
    $limit_low = (($current * $rowCount) - ($rowCount));
127
    $limit_high = $rowCount;
128
}
129
130
if ($rowCount != -1) {
131
    $sql .= " LIMIT $limit_low,$limit_high";
132
}
133
134
$query = 'SELECT DISTINCT(`ports`.`port_id`),`ports`.*';
135
// calculate ifLastChange as seconds ago
136
$query .= ',`D`.`uptime` - `ports`.`ifLastChange` / 100 as secondsIfLastChange ';
137
$query .= $sql;
138
139
foreach (dbFetchRows($query, $param) as $port) {
140
    $device = device_by_id_cache($port['device_id']);
141
    $port = cleanPort($port, $device);
142
143
    switch ($port['ifOperStatus']) {
144
        case 'up':
145
            $status = 'label-success';
146
            break;
147
        case 'down':
148
            switch ($port['ifAdminStatus']) {
149
                case 'up':
150
                    $status = 'label-danger';
151
                    break;
152
                case 'down':
153
                    $status = 'label-warning';
154
                    break;
155
            }
156
            break;
157
    }
158
159
    // FIXME what actions should we have?
160
    $actions = '<div class="container-fluid"><div class="row">';
161
162
    if ($vars['deleted'] !== 'yes') {
163
        $actions .= '<div class="col-xs-1"><a href="';
164
        $actions .= \LibreNMS\Util\Url::deviceUrl((int) $device['device_id'], ['tab' => 'alerts']);
165
        $actions .= '" title="View alerts"><i class="fa fa-exclamation-circle fa-lg icon-theme" aria-hidden="true"></i></a></div>';
166
167
        if (Auth::user()->hasGlobalAdmin()) {
168
            $actions .= '<div class="col-xs-1"><a href="';
169
            $actions .= \LibreNMS\Util\Url::deviceUrl((int) $device['device_id'], ['tab' => 'edit', 'section' => 'ports']);
170
            $actions .= '" title="Edit ports"><i class="fa fa-pencil fa-lg icon-theme" aria-hidden="true"></i></a></div>';
171
        }
172
    }
173
174
    if ($vars['deleted'] === 'yes') {
175
        if (port_permitted($port['port_id'], $device['device_id'])) {
176
            $actions .= '<div class="col-xs-1"><a href="ports/deleted=yes/purge=' . $port['port_id'] . '" title="Delete port"><i class="fa fa-times fa-lg icon-theme"></i></a></div>';
177
        }
178
    }
179
180
    $actions .= '</div></div>';
181
182
    $response[] = [
183
        'status' => $status,
184
        'device' => generate_device_link($device),
185
        'port' => generate_port_link($port),
186
        'ifLastChange' => ceil($port['secondsIfLastChange']),
187
        'ifConnectorPresent' => ($port['ifConnectorPresent'] == 'true') ? 'yes' : 'no',
188
        'ifSpeed' => $port['ifSpeed'],
189
        'ifMtu' => $port['ifMtu'],
190
        'ifInOctets_rate' => $port['ifInOctets_rate'] * 8,
191
        'ifOutOctets_rate' => $port['ifOutOctets_rate'] * 8,
192
        'ifInUcastPkts_rate' => $port['ifInUcastPkts_rate'],
193
        'ifOutUcastPkts_rate' => $port['ifOutUcastPkts_rate'],
194
        'ifInErrors' => $port['poll_period'] ? \LibreNMS\Util\Number::formatSi($port['ifInErrors_delta'] / $port['poll_period'], 2, 3, 'EPS') : '',
195
        'ifOutErrors' => $port['poll_period'] ? \LibreNMS\Util\Number::formatSi($port['ifOutErrors_delta'] / $port['poll_period'], 2, 3, 'EPS') : '',
196
        'ifType' => \LibreNMS\Util\Rewrite::normalizeIfType($port['ifType']),
197
        'ifAlias' => $port['ifAlias'],
198
        'actions' => $actions,
199
    ];
200
}
201
202
$output = [
203
    'current' => $current,
204
    'rowCount' => $rowCount,
205
    'rows' => $response,
206
    'total' => $total,
207
];
208
209
echo json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
210