From 090e55ef99aea96c69cdf0fad877a5b62d204da1 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 18 May 2020 14:29:41 +0200 Subject: [PATCH] Use host names stored in the network_addresses table when querying new client suggestions. Signed-off-by: DL6ER --- scripts/pi-hole/php/groups.php | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/scripts/pi-hole/php/groups.php b/scripts/pi-hole/php/groups.php index 2cabf513..241ba58a 100644 --- a/scripts/pi-hole/php/groups.php +++ b/scripts/pi-hole/php/groups.php @@ -172,7 +172,7 @@ if ($_POST['action'] == 'get_groups') { throw new Exception('Error while querying gravity\'s client_by_group table: ' . $db->lastErrorMsg()); } - $stmt = $FTLdb->prepare('SELECT name FROM network_names WHERE network_id = (SELECT network_id FROM network_addresses WHERE ip = :ip);'); + $stmt = $FTLdb->prepare('SELECT name FROM network_addresses WHERE ip = :ip;'); if (!$stmt) { throw new Exception('Error while preparing network table statement: ' . $db->lastErrorMsg()); } @@ -219,13 +219,16 @@ if ($_POST['action'] == 'get_groups') { while ($res = $query->fetchArray(SQLITE3_ASSOC)) { $id = intval($res["id"]); - // Get associated host names - $query_names = $FTLdb->query("SELECT name FROM network_names WHERE network_id = $id ORDER BY lastSeen DESC;"); + // Get possibly associated IP addresses and hostnames for this client + $query_ips = $FTLdb->query("SELECT ip,name FROM network_addresses WHERE network_id = $id ORDER BY lastSeen DESC;"); + $addresses = []; $names = []; - while ($res_names = $query_names->fetchArray(SQLITE3_ASSOC)) { - array_push($names, utf8_encode($res_names["name"])); + while ($res_ips = $query_ips->fetchArray(SQLITE3_ASSOC)) { + array_push($addresses, utf8_encode($res_ips["ip"])); + if($res_ips["name"] !== null) + array_push($names,utf8_encode($res_ips["name"])); } - $query_names->finalize(); + $query_ips->finalize(); // Prepare extra information $extrainfo = ""; @@ -245,14 +248,6 @@ if ($_POST['action'] == 'get_groups') { // Add list of associated host names to info string (if available and if this is not a mock device) if (stripos($res["hwaddr"], "ip-") === FALSE) { - // Get associated IP addresses - $query_ips = $FTLdb->query("SELECT ip FROM network_addresses WHERE network_id = $id ORDER BY lastSeen DESC;"); - $addresses = []; - while ($res_ips = $query_ips->fetchArray(SQLITE3_ASSOC)) { - array_push($addresses, utf8_encode($res_ips["ip"])); - } - $query_ips->finalize(); - if ((count($names) > 0 || strlen($res["macVendor"]) > 0) && count($addresses) > 0) $extrainfo .= "; ";