From 3769069a0ad11d67a3188336d393d0cc926423ad Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 17 Feb 2018 19:51:36 +0100 Subject: [PATCH] Obtain host names for IP addresses in database. This can also handle cases where we have mixed host names and IP addresses in the result (Pi-hole v3.3+ stores IP addresses in the database, for consistency) Signed-off-by: DL6ER --- api_db.php | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/api_db.php b/api_db.php index 21f1106d..3e3fdf03 100644 --- a/api_db.php +++ b/api_db.php @@ -74,10 +74,32 @@ if (isset($_GET['getAllQueries']) && $auth) $stmt->bindValue(":from", intval($from), SQLITE3_INTEGER); $stmt->bindValue(":until", intval($until), SQLITE3_INTEGER); $results = $stmt->execute(); + $clients = array(); if(!is_bool($results)) while ($row = $results->fetchArray()) { - $allQueries[] = [$row[0],$row[1] == 1 ? "IPv4" : "IPv6",$row[2],$row[3],$row[4]]; + $c = $row[3]; + if(array_key_exists($row[3], $clients)) + { + // Entry already exists + $c = $clients[$row[3]]; + } + else + { + if(filter_var($row[3], FILTER_VALIDATE_IP)) + { + // Get host name of client and convert to lower case + $c = strtolower(gethostbyaddr($row[3])); + } + else + { + // This is already a host name + $c = strtolower($row[3]); + } + // Buffer result + $clients[$row[3]] = $c; + } + $allQueries[] = [$row[0],$row[1] == 1 ? "IPv4" : "IPv6",$row[2],$c,$row[4]]; } } $result = array('data' => $allQueries); @@ -110,8 +132,16 @@ if (isset($_GET['topClients']) && $auth) if(!is_bool($results)) while ($row = $results->fetchArray()) { - // Convert client to lower case - $c = strtolower($row[0]); + if(filter_var($row[0], FILTER_VALIDATE_IP)) + { + // Get host name of client and convert to lower case + $c = strtolower(gethostbyaddr($row[0])); + } + else + { + // This is already a host name + $c = strtolower($row[0]); + } if(array_key_exists($c, $clients)) { // Entry already exists, add to it (might appear multiple times due to mixed capitalization in the database)