diff --git a/api_db.php b/api_db.php index 3e3fdf03..aee13616 100644 --- a/api_db.php +++ b/api_db.php @@ -16,6 +16,37 @@ check_cors(); ini_set("max_execution_time","600"); $data = array(); +$clients = array(); +function resolveHostname($clientip, $printIP) +{ + global $clients; + $ipaddr = strtolower($clientip); + if(array_key_exists($clientip, $clients)) + { + // Entry already exists + $clientname = $clients[$ipaddr]; + if($printIP) + return $clientname."|".$clientip; + return $clientname; + } + + else if(filter_var($clientip, FILTER_VALIDATE_IP)) + { + // Get host name of client and convert to lower case + $clientname = strtolower(gethostbyaddr($ipaddr)); + } + else + { + // This is already a host name + $clientname = $ipaddr; + } + // Buffer result + $clients[$ipaddr] = $clientname; + + if($printIP) + return $clientname."|".$clientip; + return $clientname; +} // Get posible non-standard location of FTL's database $FTLsettings = parse_ini_file("/etc/pihole/pihole-FTL.conf"); @@ -74,31 +105,10 @@ 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()) { - $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; - } + $c = resolveHostname($row[3],false); $allQueries[] = [$row[0],$row[1] == 1 ? "IPv4" : "IPv6",$row[2],$c,$row[4]]; } } @@ -127,40 +137,33 @@ if (isset($_GET['topClients']) && $auth) $stmt->bindValue(":until", intval($_GET['until']), SQLITE3_INTEGER); $results = $stmt->execute(); - $clients = array(); + $clientnums = array(); if(!is_bool($results)) while ($row = $results->fetchArray()) { - 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)) + + $c = resolveHostname($row[0],false); + + if(array_key_exists($c, $clientnums)) { // Entry already exists, add to it (might appear multiple times due to mixed capitalization in the database) - $clients[$c] += intval($row[1]); + $clientnums[$c] += intval($row[1]); } else { // Entry does not yet exist - $clients[$c] = intval($row[1]); + $clientnums[$c] = intval($row[1]); } } // Sort by number of hits - arsort($clients); + arsort($clientnums); // Extract only the first ten entries - $clients = array_slice($clients, 0, 10); + $clientnums = array_slice($clientnums, 0, 10); - $result = array('top_sources' => $clients); + $result = array('top_sources' => $clientnums); $data = array_merge($data, $result); }