diff --git a/api.php b/api.php index eaed3c03..d75b55ff 100644 --- a/api.php +++ b/api.php @@ -7,8 +7,61 @@ * Please see LICENSE file for your rights under this license */ $api = true; +header('Content-type: application/json'); require("scripts/pi-hole/php/FTL.php"); +require("scripts/pi-hole/php/password.php"); +require("scripts/pi-hole/php/auth.php"); +check_cors(); + +$data = array(); + +// Common API functions +if (isset($_GET['enable']) && $auth) +{ + if(isset($_GET["auth"])) + { + if($_GET["auth"] !== $pwhash) + die("Not authorized!"); + } + else + { + // Skip token validation if explicit auth string is given + check_csrf($_GET['token']); + } + exec('sudo pihole enable'); + $data = array_merge($data, array("status" => "enabled")); + unlink("../custom_disable_timer"); +} +elseif (isset($_GET['disable']) && $auth) +{ + if(isset($_GET["auth"])) + { + if($_GET["auth"] !== $pwhash) + die("Not authorized!"); + } + else + { + // Skip token validation if explicit auth string is given + check_csrf($_GET['token']); + } + $disable = intval($_GET['disable']); + // intval returns the integer value on success, or 0 on failure + if($disable > 0) + { + $timestamp = time(); + exec("sudo pihole disable ".$disable."s"); + file_put_contents("../custom_disable_timer",($timestamp+$disable)*1000); + } + else + { + exec('sudo pihole disable'); + unlink("../custom_disable_timer"); + } + $data = array_merge($data, array("status" => "disabled")); +} + +// Other API functions if(testFTL() && !isset($_GET["PHP"])) { require("api_FTL.php"); @@ -17,4 +70,13 @@ else { require("api_PHP.php"); } + +if(isset($_GET["jsonForceObject"])) +{ + echo json_encode($data, JSON_FORCE_OBJECT); +} +else +{ + echo json_encode($data); +} ?> diff --git a/api_FTL.php b/api_FTL.php index e48983a2..23419a45 100644 --- a/api_FTL.php +++ b/api_FTL.php @@ -12,14 +12,7 @@ if(!isset($api)) die("Direct call to api_FTL.php is not allowed!"); } -require "scripts/pi-hole/php/password.php"; -require "scripts/pi-hole/php/auth.php"; - -check_cors(); $socket = connectFTL("127.0.0.1"); -header('Content-type: application/json'); - -$data = []; if (isset($_GET['type'])) { $data["type"] = "FTL"; @@ -229,48 +222,6 @@ if (isset($_GET['getAllQueries']) && $auth) $data = array_merge($data, $result); } - if (isset($_GET['enable']) && $auth) { - if(isset($_GET["auth"])) - { - if($_GET["auth"] !== $pwhash) - die("Not authorized!"); - } - else - { - // Skip token validation if explicit auth string is given - check_csrf($_GET['token']); - } - exec('sudo pihole enable'); - $data = array_merge($data, array("status" => "enabled")); - unlink("../custom_disable_timer"); - } - elseif (isset($_GET['disable']) && $auth) { - if(isset($_GET["auth"])) - { - if($_GET["auth"] !== $pwhash) - die("Not authorized!"); - } - else - { - // Skip token validation if explicit auth string is given - check_csrf($_GET['token']); - } - $disable = intval($_GET['disable']); - // intval returns the integer value on success, or 0 on failure - if($disable > 0) - { - $timestamp = time(); - exec("sudo pihole disable ".$disable."s"); - file_put_contents("../custom_disable_timer",($timestamp+$disable)*1000); - } - else - { - exec('sudo pihole disable'); - unlink("../custom_disable_timer"); - } - $data = array_merge($data, array("status" => "disabled")); - } - if(isset($_GET["recentBlocked"])) { sendRequestFTL("recentBlocked"); @@ -278,14 +229,5 @@ if(isset($_GET["recentBlocked"])) unset($data); } -if(isset($_GET["jsonForceObject"])) -{ - echo json_encode($data, JSON_FORCE_OBJECT); -} -else -{ - echo json_encode($data); -} - disconnectFTL(); ?> diff --git a/api_PHP.php b/api_PHP.php index c41024e6..dc7a1d2b 100644 --- a/api_PHP.php +++ b/api_PHP.php @@ -12,15 +12,7 @@ die("Direct call to api_PHP.php is not allowed!"); } - require "scripts/pi-hole/php/password.php"; - require "scripts/pi-hole/php/auth.php"; - - check_cors(); - include('scripts/pi-hole/php/data.php'); - header('Content-type: application/json'); - - $data = array(); // Non-Auth @@ -81,48 +73,6 @@ $data = array_merge($data, getAllQueries($_GET['getAllQueries'])); } - if (isset($_GET['enable']) && $auth) { - if(isset($_GET["auth"])) - { - if($_GET["auth"] !== $pwhash) - die("Not authorized!"); - } - else - { - // Skip token validation if explicit auth string is given - check_csrf($_GET['token']); - } - exec('sudo pihole enable'); - $data = array_merge($data, array("status" => "enabled")); - unlink("../custom_disable_timer"); - } - elseif (isset($_GET['disable']) && $auth) { - if(isset($_GET["auth"])) - { - if($_GET["auth"] !== $pwhash) - die("Not authorized!"); - } - else - { - // Skip token validation if explicit auth string is given - check_csrf($_GET['token']); - } - $disable = intval($_GET['disable']); - // intval returns the integer value on success, or 0 on failure - if($disable > 0) - { - $timestamp = time(); - exec("sudo pihole disable ".$disable."s"); - file_put_contents("../custom_disable_timer",($timestamp+$disable)*1000); - } - else - { - exec('sudo pihole disable'); - unlink("../custom_disable_timer"); - } - $data = array_merge($data, array("status" => "disabled")); - } - if (isset($_GET['getGravityDomains'])) { $data = array_merge($data, getGravity()); } @@ -140,13 +90,4 @@ } $data = filterArray($data); - - if(isset($_GET["jsonForceObject"])) - { - echo json_encode($data, JSON_FORCE_OBJECT); - } - else - { - echo json_encode($data); - } ?>