2015-04-04 11:42:30 +01:00
|
|
|
<?php
|
|
|
|
|
2017-08-08 14:14:58 -05:00
|
|
|
use LibreNMS\Util\IP;
|
2018-09-11 07:51:35 -05:00
|
|
|
use LibreNMS\Authentication\LegacyAuth;
|
2017-08-08 14:14:58 -05:00
|
|
|
|
2015-04-04 11:42:30 +01:00
|
|
|
$param = array();
|
|
|
|
|
2018-09-11 07:51:35 -05:00
|
|
|
if (!LegacyAuth::user()->hasGlobalRead()) {
|
2015-07-13 20:10:26 +02:00
|
|
|
$perms_sql .= ' LEFT JOIN `devices_perms` AS `DP` ON `D`.`device_id` = `DP`.`device_id`';
|
|
|
|
$where .= ' AND `DP`.`user_id`=?';
|
2018-09-11 07:51:35 -05:00
|
|
|
$param[] = array(LegacyAuth::id());
|
2015-07-07 23:59:13 +01:00
|
|
|
}
|
|
|
|
|
2018-03-25 22:50:09 +02:00
|
|
|
list($address,$prefix) = explode('/', $vars['address']);
|
|
|
|
if ($vars['search_type'] == 'ipv4') {
|
2015-07-13 20:10:26 +02:00
|
|
|
$sql = ' FROM `ipv4_addresses` AS A, `ports` AS I, `ipv4_networks` AS N, `devices` AS D';
|
2015-07-07 23:59:13 +01:00
|
|
|
$sql .= $perms_sql;
|
|
|
|
$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)) {
|
|
|
|
$sql .= " AND ipv4_address LIKE '%".$address."%'";
|
|
|
|
}
|
2015-07-13 20:10:26 +02:00
|
|
|
|
2015-04-21 22:39:28 +01:00
|
|
|
if (!empty($prefix)) {
|
2015-07-13 20:10:26 +02:00
|
|
|
$sql .= " AND ipv4_prefixlen='?'";
|
2015-04-21 22:39:28 +01:00
|
|
|
$param[] = array($prefix);
|
|
|
|
}
|
2018-03-25 22:50:09 +02:00
|
|
|
} elseif ($vars['search_type'] == 'ipv6') {
|
2015-07-13 20:10:26 +02:00
|
|
|
$sql = ' FROM `ipv6_addresses` AS A, `ports` AS I, `ipv6_networks` AS N, `devices` AS D';
|
2015-07-07 23:59:13 +01:00
|
|
|
$sql .= $perms_sql;
|
|
|
|
$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)) {
|
|
|
|
$sql .= " AND (ipv6_address LIKE '%".$address."%' OR ipv6_compressed LIKE '%".$address."%')";
|
|
|
|
}
|
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-13 20:10:26 +02:00
|
|
|
$sql = ' FROM `ports` AS I, `devices` AS D';
|
|
|
|
$sql .= $perms_sql;
|
2018-03-25 22:50:09 +02:00
|
|
|
$sql .= " WHERE I.device_id = D.device_id AND `ifPhysAddress` LIKE '%".str_replace(array(':', ' ', '-', '.', '0x'), '', mres($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-07-13 20:10:26 +02:00
|
|
|
$sql .= ' AND I.device_id = ?';
|
2018-03-25 22:50:09 +02:00
|
|
|
$param[] = array($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']) {
|
2015-07-13 20:10:26 +02:00
|
|
|
$sql .= " AND I.ifDescr LIKE '?'";
|
2018-03-25 22:50:09 +02:00
|
|
|
$param[] = array($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-07-13 20:10:26 +02:00
|
|
|
$count_sql = "SELECT COUNT(`port_id`) $sql";
|
|
|
|
}
|
|
|
|
|
|
|
|
$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)) {
|
2015-07-13 20:10:26 +02:00
|
|
|
$limit_low = (($current * $rowCount) - ($rowCount));
|
2015-04-04 11:42:30 +01:00
|
|
|
$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']);
|
2015-07-13 20:10:26 +02:00
|
|
|
$type = humanmedia($interface['ifType']);
|
2015-04-04 11:42:30 +01:00
|
|
|
|
2018-03-25 22:50:09 +02:00
|
|
|
if ($vars['search_type'] == 'ipv6') {
|
2017-08-08 14:14:58 -05:00
|
|
|
$address = (string)IP::parse($interface['ipv6_network'], true);
|
2018-03-25 22:50:09 +02:00
|
|
|
} 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 {
|
2017-08-10 09:07:39 -05:00
|
|
|
$address = (string)IP::parse($interface['ipv4_network'], true);
|
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-07-13 20:10:26 +02:00
|
|
|
$error_img = '';
|
|
|
|
}
|
|
|
|
|
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);
|
2015-07-13 20:10:26 +02:00
|
|
|
$response[] = array(
|
|
|
|
'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-07-13 20:10:26 +02:00
|
|
|
);
|
2015-04-04 11:42:30 +01:00
|
|
|
}
|
2015-07-13 20:10:26 +02:00
|
|
|
}//end foreach
|
2015-04-04 11:42:30 +01:00
|
|
|
|
2015-07-13 20:10:26 +02:00
|
|
|
$output = array(
|
|
|
|
'current' => $current,
|
|
|
|
'rowCount' => $rowCount,
|
|
|
|
'rows' => $response,
|
|
|
|
'total' => $total,
|
|
|
|
);
|
2015-04-04 11:42:30 +01:00
|
|
|
echo _json_encode($output);
|