2015-04-04 11:42:30 +01:00
< ? php
2023-10-06 07:34:14 -05:00
use LibreNMS\Util\Mac ;
2015-04-04 11:42:30 +01:00
$param = [];
2022-08-30 12:55:37 -05:00
$sql = ' FROM `ipv4_mac` AS M, `ports` AS P, `devices` AS D ' ;
$where = '' ;
2015-07-07 23:26:03 +01:00
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:26:03 +01:00
}
$sql .= " WHERE M.port_id = P.port_id AND P.device_id = D.device_id $where " ;
2015-04-04 11:42:30 +01:00
2018-03-25 22:50:09 +02:00
if ( is_numeric ( $vars [ 'device_id' ])) {
2015-04-04 11:42:30 +01:00
$sql .= ' AND P.device_id = ?' ;
2018-03-25 22:50:09 +02:00
$param [] = $vars [ 'device_id' ];
2015-04-04 11:42:30 +01:00
}
2022-08-30 12:55:37 -05:00
if ( isset ( $vars [ 'port_id' ]) && is_numeric ( $vars [ 'port_id' ])) {
2015-12-24 10:58:12 -06:00
$sql .= ' AND P.port_id = ?' ;
2018-03-25 22:50:09 +02:00
$param [] = $vars [ 'port_id' ];
2015-12-24 10:58:12 -06:00
}
2018-03-25 22:50:09 +02:00
if ( isset ( $vars [ 'searchPhrase' ]) && ! empty ( $vars [ 'searchPhrase' ])) {
2021-03-28 17:25:30 -05:00
$ip_search = '%' . trim ( $vars [ 'searchPhrase' ]) . '%' ;
$mac_search = '%' . str_replace ([ ':' , ' ' , '-' , '.' , '0x' ], '' , $vars [ 'searchPhrase' ]) . '%' ;
2015-12-24 12:09:01 -06:00
2018-03-25 22:50:09 +02:00
if ( isset ( $vars [ 'searchby' ]) && $vars [ 'searchby' ] == 'ip' ) {
2015-12-24 12:09:01 -06:00
$sql .= ' AND `ipv4_address` LIKE ?' ;
$param [] = $ip_search ;
2018-03-25 22:50:09 +02:00
} elseif ( isset ( $vars [ 'searchby' ]) && $vars [ 'searchby' ] == 'mac' ) {
2015-12-24 12:09:01 -06:00
$sql .= ' AND `mac_address` LIKE ?' ;
$param [] = $mac_search ;
2016-08-18 20:28:22 -05:00
} else {
2015-12-24 12:09:01 -06:00
$sql .= ' AND (`ipv4_address` LIKE ? OR `mac_address` LIKE ?)' ;
$param [] = $ip_search ;
$param [] = $mac_search ;
}
}
2015-04-04 11:42:30 +01:00
$count_sql = " SELECT COUNT(`M`.`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 )) {
2023-03-13 22:32:22 +01: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 *,`P`.`ifDescr` AS `interface` $sql " ;
foreach ( dbFetchRows ( $sql , $param ) as $entry ) {
2017-04-04 08:08:23 +01:00
$entry = cleanPort ( $entry );
2022-09-05 23:06:42 +02:00
if ( empty ( $ignore )) {
2015-04-04 11:42:30 +01:00
if ( $entry [ 'ifInErrors' ] > 0 || $entry [ 'ifOutErrors' ] > 0 ) {
2017-06-29 15:03:56 -05:00
$error_img = generate_port_link ( $entry , " <i class='fa fa-flag fa-lg' style='color:red' aria-hidden='true'></i> " , 'port_errors' );
2016-08-18 20:28:22 -05:00
} else {
2015-04-04 11:42:30 +01:00
$error_img = '' ;
}
$arp_host = dbFetchRow ( 'SELECT * FROM ipv4_addresses AS A, ports AS I, devices AS D WHERE A.ipv4_address = ? AND I.port_id = A.port_id AND D.device_id = I.device_id' , [ $entry [ 'ipv4_address' ]]);
if ( $arp_host ) {
$arp_name = generate_device_link ( $arp_host );
2016-08-18 20:28:22 -05:00
} else {
2015-04-04 11:42:30 +01:00
unset ( $arp_name );
}
2015-07-13 20:10:26 +02:00
2015-04-04 11:42:30 +01:00
if ( $arp_host ) {
2021-05-13 17:14:27 +02:00
$arp_host = cleanPort ( $arp_host );
2015-04-04 11:42:30 +01:00
$arp_if = generate_port_link ( $arp_host );
2016-08-18 20:28:22 -05:00
} else {
2015-04-04 11:42:30 +01:00
unset ( $arp_if );
}
2015-07-13 20:10:26 +02:00
2015-04-04 11:42:30 +01:00
if ( $arp_host [ 'device_id' ] == $entry [ 'device_id' ]) {
$arp_name = 'Localhost' ;
}
if ( $arp_host [ 'port_id' ] == $entry [ 'port_id' ]) {
$arp_if = 'Local port' ;
2015-07-13 20:10:26 +02:00
}
2023-10-06 07:34:14 -05:00
$mac = Mac :: parse ( $entry [ 'mac_address' ]);
2015-04-04 11:42:30 +01:00
$response [] = [
2024-01-05 05:39:12 +01:00
'mac_address' => $mac -> readable (),
'mac_oui' => $mac -> vendor (),
'ipv4_address' => $entry [ 'ipv4_address' ],
'hostname' => generate_device_link ( $entry ),
'interface' => generate_port_link ( $entry , makeshortif ( $entry [ 'label' ])) . ' ' . $error_img ,
'remote_device' => $arp_name ,
2015-04-04 11:42:30 +01:00
'remote_interface' => $arp_if ,
];
2015-07-13 20:10:26 +02:00
} //end if
2015-04-04 11:42:30 +01:00
unset ( $ignore );
2015-07-13 20:10:26 +02:00
} //end foreach
2015-04-04 11:42:30 +01:00
$output = [
2024-01-05 05:39:12 +01:00
'current' => $current ,
2015-04-04 11:42:30 +01:00
'rowCount' => $rowCount ,
2024-01-05 05:39:12 +01:00
'rows' => $response ,
'total' => $total ,
2015-07-13 20:10:26 +02:00
];
2021-03-04 07:55:41 -06:00
echo json_encode ( $output , JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );