mirror of
https://github.com/pi-hole/web.git
synced 2024-12-06 19:36:21 +01:00
Calling FTL API directly from PHP
- Using constants for default values - Remove unnecessary variable - Replace `piholeStatusAPI()` with a call to FTL API - Transfer `api.php?status` code from `api.php` to `api_FTL.php` - Change `piholeStatus()` to use FTL info (no need to use `pihole`). Signed-off-by: RD WebDesign <github@rdwebdesign.com.br>
This commit is contained in:
@@ -13,14 +13,10 @@ require_once("scripts/pi-hole/php/database.php");
|
||||
require_once("scripts/pi-hole/php/auth.php");
|
||||
check_cors();
|
||||
|
||||
$FTL_IP = "127.0.0.1";
|
||||
|
||||
$data = array();
|
||||
|
||||
// Common API functions
|
||||
if (isset($_GET['status'])) {
|
||||
$data = array_merge($data, array("status" => piholeStatusAPI()));
|
||||
} elseif (isset($_GET['enable']) && $auth) {
|
||||
if (isset($_GET['enable']) && $auth) {
|
||||
if(isset($_GET["auth"]))
|
||||
{
|
||||
if($_GET["auth"] !== $pwhash)
|
||||
|
||||
+303
-377
@@ -6,429 +6,355 @@
|
||||
* This file is copyright under the latest version of the EUPL.
|
||||
* Please see LICENSE file for your rights under this license */
|
||||
|
||||
|
||||
if(!isset($api))
|
||||
{
|
||||
die("Direct call to api_FTL.php is not allowed!");
|
||||
if (!isset($api)) {
|
||||
die("Direct call to api_FTL.php is not allowed!");
|
||||
}
|
||||
|
||||
// $FTL_IP is defined in api.php
|
||||
$socket = connectFTL($FTL_IP);
|
||||
|
||||
if(!is_resource($socket))
|
||||
{
|
||||
$data = array_merge($data, array("FTLnotrunning" => true));
|
||||
if (isset($_GET['type'])) {
|
||||
$data["type"] = "FTL";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isset($_GET['type'])) {
|
||||
$data["type"] = "FTL";
|
||||
}
|
||||
|
||||
if (isset($_GET['version'])) {
|
||||
$data["version"] = 3;
|
||||
}
|
||||
if (isset($_GET['version'])) {
|
||||
$data["version"] = 3;
|
||||
}
|
||||
|
||||
if (isset($_GET['summary']) || isset($_GET['summaryRaw']) || !count($_GET)) {
|
||||
require_once("scripts/pi-hole/php/gravity.php");
|
||||
sendRequestFTL("stats");
|
||||
$return = getResponseFTL();
|
||||
if (isset($_GET['status'])) {
|
||||
$return = callFTLAPI("stats");
|
||||
|
||||
$stats = [];
|
||||
foreach($return as $line) {
|
||||
$tmp = explode(" ",$line);
|
||||
if (in_array("status enabled", $return)) {
|
||||
$data = array_merge($data, array("status" => "enabled"));
|
||||
} else {
|
||||
$data = array_merge($data, array("status" => "disabled"));
|
||||
}
|
||||
}
|
||||
|
||||
if($tmp[0] === "domains_being_blocked" && !is_numeric($tmp[1])) {
|
||||
// Expect string response
|
||||
$stats[$tmp[0]] = $tmp[1];
|
||||
} elseif ($tmp[0] === "status") {
|
||||
// Expect string response
|
||||
$stats[$tmp[0]] = piholeStatusAPI();
|
||||
} elseif (isset($_GET['summary'])) {
|
||||
// "summary" expects a formmated string response
|
||||
if($tmp[0] !== "ads_percentage_today") {
|
||||
$stats[$tmp[0]] = number_format($tmp[1]);
|
||||
} else {
|
||||
$stats[$tmp[0]] = number_format($tmp[1], 1, '.', '');
|
||||
}
|
||||
} else {
|
||||
// Expect float response
|
||||
$stats[$tmp[0]] = floatval($tmp[1]);
|
||||
}
|
||||
if (isset($_GET['summary']) || isset($_GET['summaryRaw']) || !count($_GET)) {
|
||||
require_once("scripts/pi-hole/php/gravity.php");
|
||||
|
||||
}
|
||||
$stats['gravity_last_updated'] = gravity_last_update(true);
|
||||
$data = array_merge($data,$stats);
|
||||
}
|
||||
$return = callFTLAPI("stats");
|
||||
|
||||
if (isset($_GET["getMaxlogage"]) && $auth) {
|
||||
sendRequestFTL("maxlogage");
|
||||
// Convert seconds to hours and rounds to one decimal place.
|
||||
$ret = round(intval(getResponseFTL()[0]) / 3600, 1);
|
||||
// Return 24h if value is 0, empty, null or non numeric.
|
||||
$ret = $ret ?: 24;
|
||||
$stats = [];
|
||||
foreach ($return as $line) {
|
||||
$tmp = explode(" ",$line);
|
||||
|
||||
$data = array_merge($data, array("maxlogage" => $ret));
|
||||
}
|
||||
if ($tmp[0] === "domains_being_blocked" && !is_numeric($tmp[1]) || $tmp[0] === "status") {
|
||||
// Expect string response
|
||||
$stats[$tmp[0]] = $tmp[1];
|
||||
} elseif (isset($_GET['summary'])) {
|
||||
// "summary" expects a formmated string response
|
||||
if ($tmp[0] !== "ads_percentage_today") {
|
||||
$stats[$tmp[0]] = number_format($tmp[1]);
|
||||
} else {
|
||||
$stats[$tmp[0]] = number_format($tmp[1], 1, '.', '');
|
||||
}
|
||||
} else {
|
||||
// Expect float response
|
||||
$stats[$tmp[0]] = floatval($tmp[1]);
|
||||
}
|
||||
|
||||
if (isset($_GET['overTimeData10mins']))
|
||||
{
|
||||
sendRequestFTL("overTime");
|
||||
$return = getResponseFTL();
|
||||
}
|
||||
$stats['gravity_last_updated'] = gravity_last_update(true);
|
||||
$data = array_merge($data,$stats);
|
||||
}
|
||||
|
||||
$domains_over_time = array();
|
||||
$ads_over_time = array();
|
||||
foreach($return as $line)
|
||||
{
|
||||
$tmp = explode(" ",$line);
|
||||
$domains_over_time[intval($tmp[0])] = intval($tmp[1]);
|
||||
$ads_over_time[intval($tmp[0])] = intval($tmp[2]);
|
||||
}
|
||||
$result = array('domains_over_time' => $domains_over_time,
|
||||
'ads_over_time' => $ads_over_time);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
if (isset($_GET["getMaxlogage"]) && $auth) {
|
||||
$return = callFTLAPI("stats");
|
||||
|
||||
if (isset($_GET['topItems']) && $auth)
|
||||
{
|
||||
if($_GET['topItems'] === "audit")
|
||||
{
|
||||
sendRequestFTL("top-domains for audit");
|
||||
}
|
||||
else if(is_numeric($_GET['topItems']))
|
||||
{
|
||||
sendRequestFTL("top-domains (".$_GET['topItems'].")");
|
||||
}
|
||||
else
|
||||
{
|
||||
sendRequestFTL("top-domains");
|
||||
}
|
||||
// Convert seconds to hours and rounds to one decimal place.
|
||||
$ret = round(intval($return[0]) / 3600, 1);
|
||||
// Return 24h if value is 0, empty, null or non numeric.
|
||||
$ret = $ret ?: 24;
|
||||
|
||||
$return = getResponseFTL();
|
||||
$top_queries = array();
|
||||
foreach($return as $line)
|
||||
{
|
||||
$tmp = explode(" ",$line);
|
||||
if(count($tmp) == 2) $tmp[2]="";
|
||||
$domain = utf8_encode($tmp[2]);
|
||||
$top_queries[$domain] = intval($tmp[1]);
|
||||
}
|
||||
$data = array_merge($data, array("maxlogage" => $ret));
|
||||
}
|
||||
|
||||
if($_GET['topItems'] === "audit")
|
||||
{
|
||||
sendRequestFTL("top-ads for audit");
|
||||
}
|
||||
else if(is_numeric($_GET['topItems']))
|
||||
{
|
||||
sendRequestFTL("top-ads (".$_GET['topItems'].")");
|
||||
}
|
||||
else
|
||||
{
|
||||
sendRequestFTL("top-ads");
|
||||
}
|
||||
if (isset($_GET['overTimeData10mins'])) {
|
||||
$return = callFTLAPI("overTime");
|
||||
|
||||
$return = getResponseFTL();
|
||||
$top_ads = array();
|
||||
foreach($return as $line)
|
||||
{
|
||||
$tmp = explode(" ",$line);
|
||||
$domain = utf8_encode($tmp[2]);
|
||||
if(count($tmp) > 3)
|
||||
$top_ads[$domain." (".$tmp[3].")"] = intval($tmp[1]);
|
||||
else
|
||||
$top_ads[$domain] = intval($tmp[1]);
|
||||
}
|
||||
$domains_over_time = array();
|
||||
$ads_over_time = array();
|
||||
foreach ($return as $line) {
|
||||
$tmp = explode(" ",$line);
|
||||
$domains_over_time[intval($tmp[0])] = intval($tmp[1]);
|
||||
$ads_over_time[intval($tmp[0])] = intval($tmp[2]);
|
||||
}
|
||||
|
||||
$result = array('top_queries' => $top_queries,
|
||||
'top_ads' => $top_ads);
|
||||
$result = array(
|
||||
'domains_over_time' => $domains_over_time,
|
||||
'ads_over_time' => $ads_over_time
|
||||
);
|
||||
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
|
||||
if ((isset($_GET['topClients']) || isset($_GET['getQuerySources'])) && $auth)
|
||||
{
|
||||
if (isset($_GET['topItems']) && $auth) {
|
||||
if ($_GET['topItems'] === "audit") {
|
||||
$return = callFTLAPI("top-domains for audit");
|
||||
} elseif (is_numeric($_GET['topItems'])) {
|
||||
$return = callFTLAPI("top-domains (".$_GET['topItems'].")");
|
||||
} else {
|
||||
$return = callFTLAPI("top-domains");
|
||||
}
|
||||
|
||||
if(isset($_GET['topClients']))
|
||||
{
|
||||
$number = $_GET['topClients'];
|
||||
}
|
||||
elseif(isset($_GET['getQuerySources']))
|
||||
{
|
||||
$number = $_GET['getQuerySources'];
|
||||
}
|
||||
$top_queries = array();
|
||||
foreach ($return as $line) {
|
||||
$tmp = explode(" ",$line);
|
||||
if (count($tmp) == 2) {
|
||||
$tmp[2]="";
|
||||
}
|
||||
$domain = utf8_encode($tmp[2]);
|
||||
$top_queries[$domain] = intval($tmp[1]);
|
||||
}
|
||||
|
||||
if(is_numeric($number))
|
||||
{
|
||||
sendRequestFTL("top-clients (".$number.")");
|
||||
}
|
||||
else
|
||||
{
|
||||
sendRequestFTL("top-clients");
|
||||
}
|
||||
if ($_GET['topItems'] === "audit") {
|
||||
$return = callFTLAPI("top-ads for audit");
|
||||
} elseif (is_numeric($_GET['topItems'])) {
|
||||
$return = callFTLAPI("top-ads (".$_GET['topItems'].")");
|
||||
} else {
|
||||
$return = callFTLAPI("top-ads");
|
||||
}
|
||||
|
||||
$return = getResponseFTL();
|
||||
$top_clients = array();
|
||||
foreach($return as $line)
|
||||
{
|
||||
$tmp = explode(" ",$line);
|
||||
$clientip = utf8_encode($tmp[2]);
|
||||
if(count($tmp) > 3 && strlen($tmp[3]) > 0)
|
||||
{
|
||||
$clientname = utf8_encode($tmp[3]);
|
||||
$top_clients[$clientname."|".$clientip] = intval($tmp[1]);
|
||||
}
|
||||
else
|
||||
$top_clients[$clientip] = intval($tmp[1]);
|
||||
}
|
||||
$top_ads = array();
|
||||
foreach ($return as $line) {
|
||||
$tmp = explode(" ",$line);
|
||||
$domain = utf8_encode($tmp[2]);
|
||||
if (count($tmp) > 3) {
|
||||
$top_ads[$domain." (".$tmp[3].")"] = intval($tmp[1]);
|
||||
} else {
|
||||
$top_ads[$domain] = intval($tmp[1]);
|
||||
}
|
||||
}
|
||||
|
||||
$result = array('top_sources' => $top_clients);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
$result = array(
|
||||
'top_queries' => $top_queries,
|
||||
'top_ads' => $top_ads
|
||||
);
|
||||
|
||||
if (isset($_GET['topClientsBlocked']) && $auth)
|
||||
{
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
|
||||
if(isset($_GET['topClientsBlocked']))
|
||||
{
|
||||
$number = $_GET['topClientsBlocked'];
|
||||
}
|
||||
if ((isset($_GET['topClients']) || isset($_GET['getQuerySources'])) && $auth) {
|
||||
|
||||
if(is_numeric($number))
|
||||
{
|
||||
sendRequestFTL("top-clients blocked (".$number.")");
|
||||
}
|
||||
else
|
||||
{
|
||||
sendRequestFTL("top-clients blocked");
|
||||
}
|
||||
if (isset($_GET['topClients'])) {
|
||||
$number = $_GET['topClients'];
|
||||
} elseif (isset($_GET['getQuerySources'])) {
|
||||
$number = $_GET['getQuerySources'];
|
||||
}
|
||||
|
||||
$return = getResponseFTL();
|
||||
$top_clients = array();
|
||||
foreach($return as $line)
|
||||
{
|
||||
$tmp = explode(" ",$line);
|
||||
$clientip = utf8_encode($tmp[2]);
|
||||
if(count($tmp) > 3 && strlen($tmp[3]) > 0)
|
||||
{
|
||||
$clientname = utf8_encode($tmp[3]);
|
||||
$top_clients[$clientname."|".$clientip] = intval($tmp[1]);
|
||||
}
|
||||
else
|
||||
$top_clients[$clientip] = intval($tmp[1]);
|
||||
}
|
||||
if (is_numeric($number)) {
|
||||
$return = callFTLAPI("top-clients (".$number.")");
|
||||
} else {
|
||||
$return = callFTLAPI("top-clients");
|
||||
}
|
||||
|
||||
$result = array('top_sources_blocked' => $top_clients);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
$top_clients = array();
|
||||
foreach ($return as $line) {
|
||||
$tmp = explode(" ",$line);
|
||||
$clientip = utf8_encode($tmp[2]);
|
||||
if (count($tmp) > 3 && strlen($tmp[3]) > 0) {
|
||||
$clientname = utf8_encode($tmp[3]);
|
||||
$top_clients[$clientname."|".$clientip] = intval($tmp[1]);
|
||||
} else {
|
||||
$top_clients[$clientip] = intval($tmp[1]);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($_GET['getForwardDestinations']) && $auth)
|
||||
{
|
||||
if($_GET['getForwardDestinations'] === "unsorted")
|
||||
{
|
||||
sendRequestFTL("forward-dest unsorted");
|
||||
}
|
||||
else
|
||||
{
|
||||
sendRequestFTL("forward-dest");
|
||||
}
|
||||
$return = getResponseFTL();
|
||||
$forward_dest = array();
|
||||
foreach($return as $line)
|
||||
{
|
||||
$tmp = explode(" ",$line);
|
||||
$forwardip = utf8_encode($tmp[2]);
|
||||
if(count($tmp) > 3 && strlen($tmp[3]) > 0)
|
||||
{
|
||||
$forwardname = utf8_encode($tmp[3]);
|
||||
$forward_dest[$forwardname."|".$forwardip] = floatval($tmp[1]);
|
||||
}
|
||||
else
|
||||
$forward_dest[$forwardip] = floatval($tmp[1]);
|
||||
}
|
||||
$result = array('top_sources' => $top_clients);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
|
||||
$result = array('forward_destinations' => $forward_dest);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
if (isset($_GET['topClientsBlocked']) && $auth) {
|
||||
|
||||
if (isset($_GET['getQueryTypes']) && $auth)
|
||||
{
|
||||
sendRequestFTL("querytypes");
|
||||
$return = getResponseFTL();
|
||||
$querytypes = array();
|
||||
foreach($return as $ret)
|
||||
{
|
||||
$tmp = explode(": ",$ret);
|
||||
// Reply cannot contain non-ASCII characters
|
||||
$querytypes[$tmp[0]] = floatval($tmp[1]);
|
||||
}
|
||||
if (isset($_GET['topClientsBlocked'])) {
|
||||
$number = $_GET['topClientsBlocked'];
|
||||
}
|
||||
|
||||
$result = array('querytypes' => $querytypes);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
if (is_numeric($number)) {
|
||||
$return = callFTLAPI("top-clients blocked (".$number.")");
|
||||
} else {
|
||||
$return = callFTLAPI("top-clients blocked");
|
||||
}
|
||||
|
||||
if (isset($_GET['getCacheInfo']) && $auth)
|
||||
{
|
||||
sendRequestFTL("cacheinfo");
|
||||
$return = getResponseFTL();
|
||||
$cacheinfo = array();
|
||||
foreach($return as $ret)
|
||||
{
|
||||
$tmp = explode(": ",$ret);
|
||||
// Reply cannot contain non-ASCII characters
|
||||
$cacheinfo[$tmp[0]] = floatval($tmp[1]);
|
||||
}
|
||||
$top_clients = array();
|
||||
foreach ($return as $line) {
|
||||
$tmp = explode(" ",$line);
|
||||
$clientip = utf8_encode($tmp[2]);
|
||||
if (count($tmp) > 3 && strlen($tmp[3]) > 0) {
|
||||
$clientname = utf8_encode($tmp[3]);
|
||||
$top_clients[$clientname."|".$clientip] = intval($tmp[1]);
|
||||
} else {
|
||||
$top_clients[$clientip] = intval($tmp[1]);
|
||||
}
|
||||
}
|
||||
|
||||
$result = array('cacheinfo' => $cacheinfo);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
$result = array('top_sources_blocked' => $top_clients);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
|
||||
if (isset($_GET['getAllQueries']) && $auth)
|
||||
{
|
||||
if(isset($_GET['from']) && isset($_GET['until']))
|
||||
{
|
||||
// Get limited time interval
|
||||
sendRequestFTL("getallqueries-time ".$_GET['from']." ".$_GET['until']);
|
||||
}
|
||||
else if(isset($_GET['domain']))
|
||||
{
|
||||
// Get specific domain only
|
||||
sendRequestFTL("getallqueries-domain ".$_GET['domain']);
|
||||
}
|
||||
else if(isset($_GET['client']) && (isset($_GET['type']) && $_GET['type'] === "blocked"))
|
||||
{
|
||||
// Get specific client only
|
||||
sendRequestFTL("getallqueries-client-blocked ".$_GET['client']);
|
||||
}
|
||||
else if(isset($_GET['client']))
|
||||
{
|
||||
// Get specific client only
|
||||
sendRequestFTL("getallqueries-client ".$_GET['client']);
|
||||
}
|
||||
else if(isset($_GET['querytype']))
|
||||
{
|
||||
// Get specific query type only
|
||||
sendRequestFTL("getallqueries-qtype ".$_GET['querytype']);
|
||||
}
|
||||
else if(isset($_GET['forwarddest']))
|
||||
{
|
||||
// Get specific forward destination only
|
||||
sendRequestFTL("getallqueries-forward ".$_GET['forwarddest']);
|
||||
}
|
||||
else if(is_numeric($_GET['getAllQueries']))
|
||||
{
|
||||
sendRequestFTL("getallqueries (".$_GET['getAllQueries'].")");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Get all queries
|
||||
sendRequestFTL("getallqueries");
|
||||
}
|
||||
$return = getResponseFTL();
|
||||
$allQueries = array();
|
||||
foreach($return as $line)
|
||||
{
|
||||
$tmp = str_getcsv($line," ");
|
||||
// UTF-8 encode domain
|
||||
$tmp[2] = utf8_encode(str_replace("~"," ",$tmp[2]));
|
||||
// UTF-8 encode client host name
|
||||
$tmp[3] = utf8_encode($tmp[3]);
|
||||
array_push($allQueries,$tmp);
|
||||
}
|
||||
if (isset($_GET['getForwardDestinations']) && $auth) {
|
||||
if ($_GET['getForwardDestinations'] === "unsorted") {
|
||||
$return = callFTLAPI("forward-dest unsorted");
|
||||
} else {
|
||||
$return = callFTLAPI("forward-dest");
|
||||
}
|
||||
|
||||
$result = array('data' => $allQueries);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
$forward_dest = array();
|
||||
foreach ($return as $line) {
|
||||
$tmp = explode(" ",$line);
|
||||
$forwardip = utf8_encode($tmp[2]);
|
||||
if (count($tmp) > 3 && strlen($tmp[3]) > 0) {
|
||||
$forwardname = utf8_encode($tmp[3]);
|
||||
$forward_dest[$forwardname."|".$forwardip] = floatval($tmp[1]);
|
||||
} else {
|
||||
$forward_dest[$forwardip] = floatval($tmp[1]);
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($_GET["recentBlocked"]) && $auth)
|
||||
{
|
||||
sendRequestFTL("recentBlocked");
|
||||
die(utf8_encode(getResponseFTL()[0]));
|
||||
unset($data);
|
||||
}
|
||||
$result = array('forward_destinations' => $forward_dest);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
|
||||
if (isset($_GET['getForwardDestinationNames']) && $auth)
|
||||
{
|
||||
sendRequestFTL("forward-names");
|
||||
$return = getResponseFTL();
|
||||
$forward_dest = array();
|
||||
foreach($return as $line)
|
||||
{
|
||||
$tmp = explode(" ",$line);
|
||||
$forwardip = utf8_encode($tmp[2]);
|
||||
if(count($tmp) > 3)
|
||||
{
|
||||
$forwardname = utf8_encode($tmp[3]);
|
||||
$forward_dest[$forwardname."|".$forwardip] = floatval($tmp[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
$forward_dest[$forwardip] = floatval($tmp[1]);
|
||||
}
|
||||
}
|
||||
if (isset($_GET['getQueryTypes']) && $auth) {
|
||||
$return = callFTLAPI("querytypes");
|
||||
|
||||
$result = array('forward_destinations' => $forward_dest);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
$querytypes = array();
|
||||
foreach ($return as $ret) {
|
||||
$tmp = explode(": ",$ret);
|
||||
// Reply cannot contain non-ASCII characters
|
||||
$querytypes[$tmp[0]] = floatval($tmp[1]);
|
||||
}
|
||||
|
||||
if (isset($_GET['overTimeDataQueryTypes']) && $auth)
|
||||
{
|
||||
sendRequestFTL("QueryTypesoverTime");
|
||||
$return = getResponseFTL();
|
||||
$over_time = array();
|
||||
$result = array('querytypes' => $querytypes);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
|
||||
foreach($return as $line)
|
||||
{
|
||||
$tmp = explode(" ",$line);
|
||||
for ($i=0; $i < count($tmp)-1; $i++) {
|
||||
$over_time[intval($tmp[0])][$i] = floatval($tmp[$i+1]);
|
||||
}
|
||||
}
|
||||
$result = array('over_time' => $over_time);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
if (isset($_GET['getCacheInfo']) && $auth) {
|
||||
$return = callFTLAPI("cacheinfo");
|
||||
$cacheinfo = array();
|
||||
foreach ($return as $ret) {
|
||||
$tmp = explode(": ",$ret);
|
||||
// Reply cannot contain non-ASCII characters
|
||||
$cacheinfo[$tmp[0]] = floatval($tmp[1]);
|
||||
}
|
||||
|
||||
if (isset($_GET['getClientNames']) && $auth)
|
||||
{
|
||||
sendRequestFTL("client-names");
|
||||
$return = getResponseFTL();
|
||||
$client_names = array();
|
||||
foreach($return as $line)
|
||||
{
|
||||
$tmp = explode(" ", $line);
|
||||
$client_names[] = array(
|
||||
"name" => utf8_encode($tmp[0]),
|
||||
"ip" => utf8_encode($tmp[1])
|
||||
);
|
||||
}
|
||||
$result = array('cacheinfo' => $cacheinfo);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
|
||||
$result = array('clients' => $client_names);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
if (isset($_GET['getAllQueries']) && $auth) {
|
||||
if (isset($_GET['from']) && isset($_GET['until'])) {
|
||||
// Get limited time interval
|
||||
$return = callFTLAPI("getallqueries-time ".$_GET['from']." ".$_GET['until']);
|
||||
} elseif (isset($_GET['domain'])) {
|
||||
// Get specific domain only
|
||||
$return = callFTLAPI("getallqueries-domain ".$_GET['domain']);
|
||||
} elseif (isset($_GET['client']) && (isset($_GET['type']) && $_GET['type'] === "blocked")) {
|
||||
// Get specific client only
|
||||
$return = callFTLAPI("getallqueries-client-blocked ".$_GET['client']);
|
||||
} elseif (isset($_GET['client'])) {
|
||||
// Get specific client only
|
||||
$return = callFTLAPI("getallqueries-client ".$_GET['client']);
|
||||
} elseif (isset($_GET['querytype'])) {
|
||||
// Get specific query type only
|
||||
$return = callFTLAPI("getallqueries-qtype ".$_GET['querytype']);
|
||||
} elseif (isset($_GET['forwarddest'])) {
|
||||
// Get specific forward destination only
|
||||
$return = callFTLAPI("getallqueries-forward ".$_GET['forwarddest']);
|
||||
} elseif (is_numeric($_GET['getAllQueries'])) {
|
||||
$return = callFTLAPI("getallqueries (".$_GET['getAllQueries'].")");
|
||||
} else {
|
||||
// Get all queries
|
||||
$return = callFTLAPI("getallqueries");
|
||||
}
|
||||
|
||||
if (isset($_GET['overTimeDataClients']) && $auth)
|
||||
{
|
||||
sendRequestFTL("ClientsoverTime");
|
||||
$return = getResponseFTL();
|
||||
$over_time = array();
|
||||
$allQueries = array();
|
||||
foreach ($return as $line) {
|
||||
$tmp = str_getcsv($line," ");
|
||||
// UTF-8 encode domain
|
||||
$tmp[2] = utf8_encode(str_replace("~"," ",$tmp[2]));
|
||||
// UTF-8 encode client host name
|
||||
$tmp[3] = utf8_encode($tmp[3]);
|
||||
array_push($allQueries,$tmp);
|
||||
}
|
||||
|
||||
foreach($return as $line)
|
||||
{
|
||||
$tmp = explode(" ",$line);
|
||||
for ($i=0; $i < count($tmp)-1; $i++) {
|
||||
$over_time[intval($tmp[0])][$i] = floatval($tmp[$i+1]);
|
||||
}
|
||||
}
|
||||
$result = array('over_time' => $over_time);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
$result = array('data' => $allQueries);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
|
||||
if (isset($_GET['delete_lease']) && $auth)
|
||||
{
|
||||
sendRequestFTL("delete-lease ".$_GET['delete_lease']);
|
||||
$return = getResponseFTL();
|
||||
$data["delete_lease"] = $return[0];
|
||||
}
|
||||
if (isset($_GET["recentBlocked"]) && $auth) {
|
||||
die(utf8_encode(callFTLAPI("recentBlocked")[0]));
|
||||
unset($data);
|
||||
}
|
||||
|
||||
disconnectFTL();
|
||||
if (isset($_GET['getForwardDestinationNames']) && $auth) {
|
||||
$return = callFTLAPI("forward-names");
|
||||
|
||||
$forward_dest = array();
|
||||
foreach ($return as $line) {
|
||||
$tmp = explode(" ",$line);
|
||||
$forwardip = utf8_encode($tmp[2]);
|
||||
if (count($tmp) > 3) {
|
||||
$forwardname = utf8_encode($tmp[3]);
|
||||
$forward_dest[$forwardname."|".$forwardip] = floatval($tmp[1]);
|
||||
} else {
|
||||
$forward_dest[$forwardip] = floatval($tmp[1]);
|
||||
}
|
||||
}
|
||||
|
||||
$result = array('forward_destinations' => $forward_dest);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
|
||||
if (isset($_GET['overTimeDataQueryTypes']) && $auth) {
|
||||
$return = callFTLAPI("QueryTypesoverTime");
|
||||
$over_time = array();
|
||||
|
||||
foreach ($return as $line) {
|
||||
$tmp = explode(" ",$line);
|
||||
for ($i=0; $i < count($tmp)-1; $i++) {
|
||||
$over_time[intval($tmp[0])][$i] = floatval($tmp[$i+1]);
|
||||
}
|
||||
}
|
||||
$result = array('over_time' => $over_time);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
|
||||
if (isset($_GET['getClientNames']) && $auth) {
|
||||
$return = callFTLAPI("client-names");
|
||||
$client_names = array();
|
||||
foreach ($return as $line) {
|
||||
$tmp = explode(" ", $line);
|
||||
$client_names[] = array(
|
||||
"name" => utf8_encode($tmp[0]),
|
||||
"ip" => utf8_encode($tmp[1])
|
||||
);
|
||||
}
|
||||
|
||||
$result = array('clients' => $client_names);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
|
||||
if (isset($_GET['overTimeDataClients']) && $auth) {
|
||||
$return = callFTLAPI("ClientsoverTime");
|
||||
$over_time = array();
|
||||
|
||||
foreach ($return as $line) {
|
||||
$tmp = explode(" ",$line);
|
||||
for ($i=0; $i < count($tmp)-1; $i++) {
|
||||
$over_time[intval($tmp[0])][$i] = floatval($tmp[$i+1]);
|
||||
}
|
||||
}
|
||||
$result = array('over_time' => $over_time);
|
||||
$data = array_merge($data, $result);
|
||||
}
|
||||
|
||||
if (isset($_GET['delete_lease']) && $auth) {
|
||||
$return = callFTLAPI("delete-lease ".$_GET['delete_lease']);
|
||||
$data["delete_lease"] = $return[0];
|
||||
}
|
||||
|
||||
if (isset($_GET['dns-port']) && $auth) {
|
||||
$return = callFTLAPI("dns-port");
|
||||
$data["dns-port"] = $return[0];
|
||||
}
|
||||
?>
|
||||
|
||||
+20
-10
@@ -583,7 +583,7 @@ function updateForwardDestinationsPie() {
|
||||
}
|
||||
|
||||
function updateTopClientsChart() {
|
||||
$.getJSON("api.php?summaryRaw&getQuerySources&topClientsBlocked", function (data) {
|
||||
$.getJSON("api.php?getQuerySources&topClientsBlocked", function (data) {
|
||||
if ("FTLnotrunning" in data) {
|
||||
return;
|
||||
}
|
||||
@@ -618,12 +618,14 @@ function updateTopClientsChart() {
|
||||
'">' +
|
||||
clientname +
|
||||
"</a>";
|
||||
percentage = (data.top_sources[client] / data.dns_queries_today) * 100;
|
||||
percentage = (data.top_sources[client] / summary.dns_queries_today) * 100;
|
||||
clienttable.append(
|
||||
"<tr> " +
|
||||
utils.addTD(url) +
|
||||
utils.addTD(data.top_sources[client]) +
|
||||
utils.addTD(utils.colorBar(percentage, data.dns_queries_today, "progress-bar-blue")) +
|
||||
utils.addTD(
|
||||
utils.colorBar(percentage, summary.dns_queries_today, "progress-bar-blue")
|
||||
) +
|
||||
"</tr> "
|
||||
);
|
||||
}
|
||||
@@ -658,12 +660,14 @@ function updateTopClientsChart() {
|
||||
'">' +
|
||||
clientname +
|
||||
"</a>";
|
||||
percentage = (data.top_sources_blocked[client] / data.ads_blocked_today) * 100;
|
||||
percentage = (data.top_sources_blocked[client] / summary.ads_blocked_today) * 100;
|
||||
clientblockedtable.append(
|
||||
"<tr> " +
|
||||
utils.addTD(url) +
|
||||
utils.addTD(data.top_sources_blocked[client]) +
|
||||
utils.addTD(utils.colorBar(percentage, data.ads_blocked_today, "progress-bar-blue")) +
|
||||
utils.addTD(
|
||||
utils.colorBar(percentage, summary.ads_blocked_today, "progress-bar-blue")
|
||||
) +
|
||||
"</tr> "
|
||||
);
|
||||
}
|
||||
@@ -687,7 +691,7 @@ function updateTopClientsChart() {
|
||||
}
|
||||
|
||||
function updateTopLists() {
|
||||
$.getJSON("api.php?summaryRaw&topItems", function (data) {
|
||||
$.getJSON("api.php?topItems", function (data) {
|
||||
if ("FTLnotrunning" in data) {
|
||||
return;
|
||||
}
|
||||
@@ -709,12 +713,14 @@ function updateTopLists() {
|
||||
domain = utils.escapeHtml(domain);
|
||||
urlText = domain === "" ? "." : domain;
|
||||
url = '<a href="queries.php?domain=' + domain + '">' + urlText + "</a>";
|
||||
percentage = (data.top_queries[domain] / data.dns_queries_today) * 100;
|
||||
percentage = (data.top_queries[domain] / summary.dns_queries_today) * 100;
|
||||
domaintable.append(
|
||||
"<tr> " +
|
||||
utils.addTD(url) +
|
||||
utils.addTD(data.top_queries[domain]) +
|
||||
utils.addTD(utils.colorBar(percentage, data.dns_queries_today, "queries-permitted")) +
|
||||
utils.addTD(
|
||||
utils.colorBar(percentage, summary.dns_queries_today, "queries-permitted")
|
||||
) +
|
||||
"</tr> "
|
||||
);
|
||||
}
|
||||
@@ -736,12 +742,12 @@ function updateTopLists() {
|
||||
domain = utils.escapeHtml(domain);
|
||||
urlText = domain === "" ? "." : domain;
|
||||
url = '<a href="queries.php?domain=' + domain + '">' + urlText + "</a>";
|
||||
percentage = (data.top_ads[domain] / data.ads_blocked_today) * 100;
|
||||
percentage = (data.top_ads[domain] / summary.ads_blocked_today) * 100;
|
||||
adtable.append(
|
||||
"<tr> " +
|
||||
utils.addTD(url) +
|
||||
utils.addTD(data.top_ads[domain]) +
|
||||
utils.addTD(utils.colorBar(percentage, data.ads_blocked_today, "queries-blocked")) +
|
||||
utils.addTD(utils.colorBar(percentage, summary.ads_blocked_today, "queries-blocked")) +
|
||||
"</tr> "
|
||||
);
|
||||
}
|
||||
@@ -759,6 +765,7 @@ function updateTopLists() {
|
||||
});
|
||||
}
|
||||
|
||||
var summary;
|
||||
var FTLoffline = false;
|
||||
function updateSummaryData(runOnce) {
|
||||
var setTimer = function (timeInSeconds) {
|
||||
@@ -792,6 +799,9 @@ function updateSummaryData(runOnce) {
|
||||
updateTopLists();
|
||||
}
|
||||
|
||||
// use the same data to update all lists
|
||||
summary = data;
|
||||
|
||||
var formatter = new Intl.NumberFormat();
|
||||
//Element name might have a different name to the property of the API so we split it at |
|
||||
[
|
||||
|
||||
+70
-65
@@ -6,88 +6,93 @@
|
||||
* This file is copyright under the latest version of the EUPL.
|
||||
* Please see LICENSE file for your rights under this license. */
|
||||
|
||||
$piholeFTLConfFile = "/etc/pihole/pihole-FTL.conf";
|
||||
const DEFAULT_FTLCONFFILE = "/etc/pihole/pihole-FTL.conf";
|
||||
const DEFAULT_FTL_IP = "127.0.0.1";
|
||||
const DEFAULT_FTL_PORT = 4711;
|
||||
|
||||
function piholeFTLConfig($force=false)
|
||||
{
|
||||
static $piholeFTLConfig;
|
||||
global $piholeFTLConfFile;
|
||||
function piholeFTLConfig($piholeFTLConfFile = DEFAULT_FTLCONFFILE, $force = false) {
|
||||
static $piholeFTLConfig;
|
||||
|
||||
if(isset($piholeFTLConfig) && !$force)
|
||||
{
|
||||
return $piholeFTLConfig;
|
||||
}
|
||||
if (isset($piholeFTLConfig) && !$force) {
|
||||
return $piholeFTLConfig;
|
||||
}
|
||||
|
||||
if(is_readable($piholeFTLConfFile))
|
||||
{
|
||||
$piholeFTLConfig = parse_ini_file($piholeFTLConfFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
$piholeFTLConfig = array();
|
||||
}
|
||||
if (is_readable($piholeFTLConfFile)) {
|
||||
$piholeFTLConfig = parse_ini_file($piholeFTLConfFile);
|
||||
} else {
|
||||
$piholeFTLConfig = array();
|
||||
}
|
||||
|
||||
return $piholeFTLConfig;
|
||||
return $piholeFTLConfig;
|
||||
}
|
||||
|
||||
function connectFTL($address, $port=4711)
|
||||
{
|
||||
if($address == "127.0.0.1")
|
||||
{
|
||||
$config = piholeFTLConfig();
|
||||
// Read port
|
||||
$portfileName = isset($config['PORTFILE']) ? $config['PORTFILE'] : '';
|
||||
if ($portfileName != '')
|
||||
{
|
||||
$portfileContents = file_get_contents($portfileName);
|
||||
if(is_numeric($portfileContents))
|
||||
$port = intval($portfileContents);
|
||||
}
|
||||
}
|
||||
function connectFTL($address, $port) {
|
||||
if ($address == DEFAULT_FTL_IP) {
|
||||
$config = piholeFTLConfig();
|
||||
// Read port
|
||||
$portfileName = isset($config['PORTFILE']) ? $config['PORTFILE'] : '';
|
||||
if ($portfileName != '') {
|
||||
$portfileContents = file_get_contents($portfileName);
|
||||
if (is_numeric($portfileContents)) {
|
||||
$port = intval($portfileContents);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Open Internet socket connection
|
||||
$socket = @fsockopen($address, $port, $errno, $errstr, 1.0);
|
||||
// Open Internet socket connection
|
||||
$socket = @fsockopen($address, $port, $errno, $errstr, 1.0);
|
||||
|
||||
return $socket;
|
||||
return $socket;
|
||||
}
|
||||
|
||||
function sendRequestFTL($requestin)
|
||||
{
|
||||
global $socket;
|
||||
|
||||
$request = ">".$requestin;
|
||||
fwrite($socket, $request) or die('{"error":"Could not send data to server"}');
|
||||
function sendRequestFTL($requestin, $socket) {
|
||||
$request = ">".$requestin;
|
||||
fwrite($socket, $request) or die('{"error":"Could not send data to server"}');
|
||||
}
|
||||
|
||||
function getResponseFTL()
|
||||
{
|
||||
global $socket;
|
||||
function getResponseFTL($socket) {
|
||||
$response = [];
|
||||
|
||||
$response = [];
|
||||
$errCount = 0;
|
||||
while (true) {
|
||||
$out = fgets($socket);
|
||||
if ($out == "") {
|
||||
$errCount++;
|
||||
}
|
||||
|
||||
$errCount = 0;
|
||||
while(true)
|
||||
{
|
||||
$out = fgets($socket);
|
||||
if ($out == "") $errCount++;
|
||||
if ($errCount > 100) {
|
||||
// Tried 100 times, but never got proper reply, fail to prevent busy loop
|
||||
die('{"error":"Tried 100 times to connect to FTL server, but never got proper reply. Please check Port and logs!"}');
|
||||
}
|
||||
if(strrpos($out,"---EOM---") !== false)
|
||||
break;
|
||||
if ($errCount > 100) {
|
||||
// Tried 100 times, but never got proper reply, fail to prevent busy loop
|
||||
die('{"error":"Tried 100 times to connect to FTL server, but never got proper reply. Please check Port and logs!"}');
|
||||
}
|
||||
|
||||
$out = rtrim($out);
|
||||
if(strlen($out) > 0)
|
||||
$response[] = $out;
|
||||
}
|
||||
if (strrpos($out,"---EOM---") !== false) {
|
||||
break;
|
||||
}
|
||||
|
||||
return $response;
|
||||
$out = rtrim($out);
|
||||
if (strlen($out) > 0) {
|
||||
$response[] = $out;
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
function disconnectFTL()
|
||||
{
|
||||
global $socket;
|
||||
fclose($socket);
|
||||
function disconnectFTL($socket) {
|
||||
fclose($socket);
|
||||
}
|
||||
|
||||
function callFTLAPI($request, $FTL_IP = DEFAULT_FTL_IP, $port = DEFAULT_FTL_PORT) {
|
||||
$socket = connectFTL($FTL_IP, $port);
|
||||
|
||||
if (!is_resource($socket)) {
|
||||
$data = array("FTLnotrunning" => true);
|
||||
} else {
|
||||
sendRequestFTL($request, $socket);
|
||||
$data = getResponseFTL($socket);
|
||||
}
|
||||
disconnectFTL($socket);
|
||||
|
||||
return $data;
|
||||
}
|
||||
?>
|
||||
|
||||
@@ -484,30 +484,33 @@ function getQueryTypeStr($querytype)
|
||||
}
|
||||
|
||||
// Returns an integer representing pihole blocking status
|
||||
function piholeStatus()
|
||||
{
|
||||
// Receive the return of "pihole status web"
|
||||
$pistatus = pihole_execute('status web');
|
||||
return isset($pistatus[0]) ? intval($pistatus[0]) : -2;
|
||||
}
|
||||
function piholeStatus() {
|
||||
// Retrieve DNS Port calling FTL API directly
|
||||
$port = callFTLAPI("dns-port");
|
||||
|
||||
// Returns pihole status, using only valid API responses (enabled/disabled)
|
||||
function piholeStatusAPI()
|
||||
{
|
||||
// Receive the return of "pihole status web"
|
||||
$pistatus = piholeStatus();
|
||||
// Retrieve FTL status
|
||||
$FTLstats = callFTLAPI("stats");
|
||||
|
||||
switch ($pistatus) {
|
||||
case -2: // Unkown
|
||||
case -1: // DNS service not running"
|
||||
case 0: // Offline
|
||||
$response = "disabled";
|
||||
break;
|
||||
|
||||
default:
|
||||
// DNS service running on port $returncode
|
||||
$response = "enabled";
|
||||
if (array_key_exists("FTLnotrunning", $port) || array_key_exists("FTLnotrunning", $FTLstats)){
|
||||
// FTL is not running
|
||||
$ret = -1;
|
||||
} elseif (in_array("status enabled", $FTLstats)) {
|
||||
// FTL is enabled
|
||||
if (intval($port[0]) <= 0) {
|
||||
// Port=0; FTL is not listening
|
||||
$ret = -1;
|
||||
} else {
|
||||
// FTL is running on this port
|
||||
$ret = intval($port[0]);
|
||||
}
|
||||
} elseif (in_array("status disabled", $FTLstats)) {
|
||||
// FTL is disabled
|
||||
$ret = 0;
|
||||
} else {
|
||||
// Unknown (unexpected) response
|
||||
$ret = -2;
|
||||
}
|
||||
return $response;
|
||||
|
||||
return $ret;
|
||||
}
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user