Files

118 lines
3.8 KiB
PHP
Raw Permalink Normal View History

<?php
use LibreNMS\Util\IP;
2020-09-21 15:40:17 +02:00
$param = [];
2020-09-21 15:40:17 +02:00
if (! Auth::user()->hasGlobalRead()) {
2019-12-30 12:11:26 +01:00
$device_ids = Permissions::devicesForUser()->toArray() ?: [0];
2020-09-21 15:59:34 +02:00
$where .= ' AND `D`.`device_id` IN ' . dbGenPlaceholders(count($device_ids));
2019-12-30 12:11:26 +01:00
$param = array_merge($param, $device_ids);
}
2020-09-21 15:40:17 +02:00
[$address,$prefix] = explode('/', $vars['address']);
if ($vars['search_type'] == 'ipv4') {
2020-09-21 15:40:17 +02: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 ";
2020-09-21 15:40:17 +02:00
if (! empty($address)) {
2020-09-21 15:59:34 +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
2020-09-21 15:40:17 +02:00
if (! empty($prefix)) {
$sql .= " AND ipv4_prefixlen='?'";
$param[] = [$prefix];
2015-04-21 22:39:28 +01:00
}
} elseif ($vars['search_type'] == 'ipv6') {
2020-09-21 15:40:17 +02: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 ";
2020-09-21 15:40:17 +02:00
if (! empty($address)) {
2020-09-21 15:59:34 +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
2020-09-21 15:40:17 +02:00
if (! empty($prefix)) {
2015-04-21 22:39:28 +01:00
$sql .= " AND ipv6_prefixlen = '$prefix'";
}
} elseif ($vars['search_type'] == 'mac') {
2020-09-21 15:40:17 +02:00
$sql = ' FROM `ports` AS I, `devices` AS D';
$sql .= " WHERE I.device_id = D.device_id AND `ifPhysAddress` LIKE '%" . str_replace([':', ' ', '-', '.', '0x'], '', mres($vars['address'])) . "%' $where ";
2015-07-13 20:10:26 +02:00
}//end if
if (is_numeric($vars['device_id'])) {
2020-09-21 15:40:17 +02:00
$sql .= ' AND I.device_id = ?';
$param[] = $vars['device_id'];
}
2015-07-13 20:10:26 +02:00
if ($vars['interface']) {
2020-09-21 15:59:34 +02:00
$sql .= ' AND I.ifDescr LIKE ?';
$param[] = $vars['interface'];
}
if ($vars['search_type'] == 'ipv4') {
$count_sql = "SELECT COUNT(`ipv4_address_id`) $sql";
} elseif ($vars['search_type'] == 'ipv6') {
$count_sql = "SELECT COUNT(`ipv6_address_id`) $sql";
} elseif ($vars['search_type'] == 'mac') {
2015-07-13 20:10:26 +02:00
$count_sql = "SELECT COUNT(`port_id`) $sql";
}
$total = dbFetchCell($count_sql, $param);
if (empty($total)) {
$total = 0;
}
2020-09-21 15:40:17 +02:00
if (! isset($sort) || empty($sort)) {
$sort = '`hostname` ASC';
}
$sql .= " ORDER BY $sort";
if (isset($current)) {
2020-09-21 15:40:17 +02:00
$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) {
2015-04-21 22:39:28 +01:00
$speed = humanspeed($interface['ifSpeed']);
2020-09-21 15:40:17 +02:00
$type = humanmedia($interface['ifType']);
if ($vars['search_type'] == 'ipv6') {
2020-09-21 15:40:17 +02:00
$address = (string) IP::parse($interface['ipv6_address'], true) . '/' . $interface['ipv6_prefixlen'];
} elseif ($vars['search_type'] == 'mac') {
2015-04-21 22:39:28 +01:00
$address = formatMac($interface['ifPhysAddress']);
2016-08-18 20:28:22 -05:00
} else {
2020-09-21 15:40:17 +02:00
$address = (string) IP::parse($interface['ipv4_address'], true) . '/' . $interface['ipv4_prefixlen'];
2015-04-21 22:39:28 +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-07-13 20:10:26 +02:00
$error_img = '';
}
2015-04-21 22:39:28 +01:00
if (port_permitted($interface['port_id'])) {
2020-09-21 15:40:17 +02:00
$interface = cleanPort($interface, $interface);
$response[] = [
2015-07-13 20:10:26 +02:00
'hostname' => generate_device_link($interface),
2020-09-21 15:40:17 +02:00
'interface' => generate_port_link($interface) . ' ' . $error_img,
2015-07-13 20:10:26 +02:00
'address' => $address,
'description' => $interface['ifAlias'],
2020-09-21 15:40:17 +02:00
];
}
2015-07-13 20:10:26 +02:00
}//end foreach
2020-09-21 15:40:17 +02:00
$output = [
2015-07-13 20:10:26 +02:00
'current' => $current,
'rowCount' => $rowCount,
'rows' => $response,
'total' => $total,
2020-09-21 15:40:17 +02:00
];
echo _json_encode($output);