2015-04-04 11:42:30 +01:00
|
|
|
<?php
|
|
|
|
|
2017-08-08 14:14:58 -05:00
|
|
|
use LibreNMS\Util\IP;
|
|
|
|
|
2015-04-04 11:42:30 +01:00
|
|
|
$param = [];
|
|
|
|
|
2019-08-05 14:16:05 -05:00
|
|
|
if (! Auth::user()->hasGlobalRead()) {
|
2019-12-30 12:11:26 +01:00
|
|
|
$device_ids = Permissions::devicesForUser()->toArray() ?: [0];
|
|
|
|
$where .= ' AND `D`.`device_id` IN ' . dbGenPlaceholders(count($device_ids));
|
|
|
|
$param = array_merge($param, $device_ids);
|
2015-07-07 23:59:13 +01:00
|
|
|
}
|
|
|
|
|
2018-03-25 22:50:09 +02:00
|
|
|
[$address,$prefix] = explode('/', $vars['address']);
|
|
|
|
if ($vars['search_type'] == 'ipv4') {
|
2015-07-07 23:59:13 +01:00
|
|
|
$sql = ' FROM `ipv4_addresses` AS A, `ports` AS I, `ipv4_networks` AS N, `devices` AS D';
|
|
|
|
$sql .= " WHERE I.port_id = A.port_id AND I.device_id = D.device_id AND N.ipv4_network_id = A.ipv4_network_id $where ";
|
2015-04-21 22:39:28 +01:00
|
|
|
if (! empty($address)) {
|
2020-07-10 16:17:09 +02:00
|
|
|
$sql .= ' AND ipv4_address LIKE ?';
|
|
|
|
$param[] = "%$address%";
|
2015-04-21 22:39:28 +01:00
|
|
|
}
|
2015-07-13 20:10:26 +02:00
|
|
|
|
2015-04-21 22:39:28 +01:00
|
|
|
if (! empty($prefix)) {
|
|
|
|
$sql .= " AND ipv4_prefixlen='?'";
|
|
|
|
$param[] = [$prefix];
|
|
|
|
}
|
2018-03-25 22:50:09 +02:00
|
|
|
} elseif ($vars['search_type'] == 'ipv6') {
|
2015-07-07 23:59:13 +01:00
|
|
|
$sql = ' FROM `ipv6_addresses` AS A, `ports` AS I, `ipv6_networks` AS N, `devices` AS D';
|
|
|
|
$sql .= " WHERE I.port_id = A.port_id AND I.device_id = D.device_id AND N.ipv6_network_id = A.ipv6_network_id $where ";
|
2015-04-21 22:39:28 +01:00
|
|
|
if (! empty($address)) {
|
2020-07-10 16:17:09 +02:00
|
|
|
$sql .= ' AND (ipv6_address LIKE ? OR ipv6_compressed LIKE ?)';
|
|
|
|
$param[] = "%$address%";
|
|
|
|
$param[] = "%$address%";
|
2015-04-21 22:39:28 +01:00
|
|
|
}
|
2015-07-13 20:10:26 +02:00
|
|
|
|
2015-04-21 22:39:28 +01:00
|
|
|
if (! empty($prefix)) {
|
|
|
|
$sql .= " AND ipv6_prefixlen = '$prefix'";
|
|
|
|
}
|
2018-03-25 22:50:09 +02:00
|
|
|
} elseif ($vars['search_type'] == 'mac') {
|
2015-07-07 23:59:13 +01:00
|
|
|
$sql = ' FROM `ports` AS I, `devices` AS D';
|
2022-07-09 11:51:07 +02:00
|
|
|
$sql .= " WHERE I.device_id = D.device_id AND `ifPhysAddress` LIKE '%" . trim(str_replace([':', ' ', '-', '.', '0x'], '', $vars['address'])) . "%' $where ";
|
2015-07-13 20:10:26 +02:00
|
|
|
}//end if
|
2018-03-25 22:50:09 +02:00
|
|
|
if (is_numeric($vars['device_id'])) {
|
2015-04-04 11:42:30 +01:00
|
|
|
$sql .= ' AND I.device_id = ?';
|
2019-04-28 10:04:56 +02:00
|
|
|
$param[] = $vars['device_id'];
|
2015-04-04 11:42:30 +01:00
|
|
|
}
|
2015-07-13 20:10:26 +02:00
|
|
|
|
2018-03-25 22:50:09 +02:00
|
|
|
if ($vars['interface']) {
|
2019-04-28 10:04:56 +02:00
|
|
|
$sql .= ' AND I.ifDescr LIKE ?';
|
|
|
|
$param[] = $vars['interface'];
|
2015-04-04 11:42:30 +01:00
|
|
|
}
|
|
|
|
|
2018-03-25 22:50:09 +02:00
|
|
|
if ($vars['search_type'] == 'ipv4') {
|
2015-04-04 11:42:30 +01:00
|
|
|
$count_sql = "SELECT COUNT(`ipv4_address_id`) $sql";
|
2018-03-25 22:50:09 +02:00
|
|
|
} elseif ($vars['search_type'] == 'ipv6') {
|
2015-04-04 11:42:30 +01:00
|
|
|
$count_sql = "SELECT COUNT(`ipv6_address_id`) $sql";
|
2018-03-25 22:50:09 +02:00
|
|
|
} elseif ($vars['search_type'] == 'mac') {
|
2015-04-04 11:42:30 +01:00
|
|
|
$count_sql = "SELECT COUNT(`port_id`) $sql";
|
2015-07-13 20:10:26 +02:00
|
|
|
}
|
|
|
|
|
2015-04-04 11:42:30 +01:00
|
|
|
$total = dbFetchCell($count_sql, $param);
|
2015-04-12 11:47:21 +01:00
|
|
|
if (empty($total)) {
|
|
|
|
$total = 0;
|
|
|
|
}
|
2015-04-04 11:42:30 +01:00
|
|
|
|
|
|
|
if (! isset($sort) || empty($sort)) {
|
|
|
|
$sort = '`hostname` ASC';
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= " ORDER BY $sort";
|
|
|
|
|
|
|
|
if (isset($current)) {
|
|
|
|
$limit_low = (($current * $rowCount) - ($rowCount));
|
|
|
|
$limit_high = $rowCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($rowCount != -1) {
|
|
|
|
$sql .= " LIMIT $limit_low,$limit_high";
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = "SELECT *,`I`.`ifDescr` AS `interface` $sql";
|
|
|
|
|
|
|
|
foreach (dbFetchRows($sql, $param) as $interface) {
|
2021-03-28 17:25:30 -05:00
|
|
|
$speed = \LibreNMS\Util\Number::formatSi($interface['ifSpeed'], 2, 3, 'bps');
|
|
|
|
$type = \LibreNMS\Util\Rewrite::normalizeIfType($interface['ifType']);
|
2015-04-04 11:42:30 +01:00
|
|
|
|
2018-03-25 22:50:09 +02:00
|
|
|
if ($vars['search_type'] == 'ipv6') {
|
2019-04-29 18:52:32 +02:00
|
|
|
$address = (string) IP::parse($interface['ipv6_address'], true) . '/' . $interface['ipv6_prefixlen'];
|
2018-03-25 22:50:09 +02:00
|
|
|
} elseif ($vars['search_type'] == 'mac') {
|
2021-03-28 17:25:30 -05:00
|
|
|
$address = \LibreNMS\Util\Rewrite::readableMac($interface['ifPhysAddress']);
|
2021-05-10 21:56:48 +02:00
|
|
|
$mac_oui = \LibreNMS\Util\Rewrite::readableOUI($interface['ifPhysAddress']);
|
2016-08-18 20:28:22 -05:00
|
|
|
} else {
|
2019-04-29 18:52:32 +02:00
|
|
|
$address = (string) IP::parse($interface['ipv4_address'], true) . '/' . $interface['ipv4_prefixlen'];
|
2015-04-21 22:39:28 +01:00
|
|
|
}
|
2015-04-04 11:42:30 +01:00
|
|
|
|
2015-04-21 22:39:28 +01:00
|
|
|
if ($interface['in_errors'] > 0 || $interface['out_errors'] > 0) {
|
2018-07-13 17:08:00 -05:00
|
|
|
$error_img = generate_port_link($interface, "<i class='fa fa-flag fa-lg' style='color:red' aria-hidden='true'></i>", 'errors');
|
2016-08-18 20:28:22 -05:00
|
|
|
} else {
|
2015-04-21 22:39:28 +01:00
|
|
|
$error_img = '';
|
2015-07-13 20:10:26 +02:00
|
|
|
}
|
|
|
|
|
2015-04-21 22:39:28 +01:00
|
|
|
if (port_permitted($interface['port_id'])) {
|
2017-04-04 08:08:23 +01:00
|
|
|
$interface = cleanPort($interface, $interface);
|
2021-05-10 21:56:48 +02:00
|
|
|
$row = [
|
2015-04-21 22:39:28 +01:00
|
|
|
'hostname' => generate_device_link($interface),
|
|
|
|
'interface' => generate_port_link($interface) . ' ' . $error_img,
|
|
|
|
'address' => $address,
|
2017-04-04 08:08:23 +01:00
|
|
|
'description' => $interface['ifAlias'],
|
2015-04-21 22:39:28 +01:00
|
|
|
];
|
2021-05-10 21:56:48 +02:00
|
|
|
if ($vars['search_type'] == 'mac') {
|
|
|
|
$row['mac_oui'] = $mac_oui;
|
|
|
|
}
|
|
|
|
$response[] = $row;
|
2015-04-04 11:42:30 +01:00
|
|
|
}
|
|
|
|
}//end foreach
|
|
|
|
|
|
|
|
$output = [
|
|
|
|
'current' => $current,
|
|
|
|
'rowCount' => $rowCount,
|
|
|
|
'rows' => $response,
|
|
|
|
'total' => $total,
|
|
|
|
];
|
2021-03-04 07:55:41 -06:00
|
|
|
echo json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|