From ee0913a7a2fc5f53cc49830562045ea39b172380 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Sat, 3 Dec 2016 13:40:04 -0500 Subject: [PATCH] Move enable/disable to API Also check CORS for all API calls. With the same import, we can enable CSRF token checking on the API. --- api.php | 31 +++++++++++++++++++++---------- header.php | 20 +++++--------------- js/pihole/footer.js | 25 +++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 25 deletions(-) diff --git a/api.php b/api.php index 9a48d158..cb6cbcb2 100644 --- a/api.php +++ b/api.php @@ -1,18 +1,21 @@ "enabled" + )); + } + + if (isset($_GET['disable']) && $auth) { + exec('sudo pihole disable'); + $data = array_merge($data, Array( + "status" => "disabled" + )); + } + function filterArray(&$a) { $sanArray = array(); foreach ($a as $k=>$v) { diff --git a/header.php b/header.php index 8c67db6c..a262a809 100644 --- a/header.php +++ b/header.php @@ -4,16 +4,6 @@ check_cors(); - if (isset($_GET['enable']) && $auth) { - exec('sudo pihole enable'); - $refer = $_SERVER['HTTP_REFERER']; - header("location:$refer"); - } elseif (isset($_GET['disable']) && $auth) { - exec('sudo pihole disable'); - $refer = $_SERVER['HTTP_REFERER']; - header("location:$refer"); - } - // Web based change of temperature unit if (isset($_GET['tempunit'])) { @@ -193,11 +183,11 @@ Active'; + echo ' Active'; } elseif ($pistatus == "0") { - echo ' Offline'; + echo ' Offline'; } else { - echo ' Starting'; + echo ' Starting'; } // CPU Temp @@ -285,9 +275,9 @@ Disable'; + echo '
  • Disable
  • '; } else { - echo '
  • Enable
  • '; + echo '
  • Enable
  • '; } ?> diff --git a/js/pihole/footer.js b/js/pihole/footer.js index 2f6a8758..9c4471e1 100644 --- a/js/pihole/footer.js +++ b/js/pihole/footer.js @@ -8,6 +8,31 @@ $("body").on("click", function(event) { } }); +// Enable/Disable +$("#flip-status").on("click", () => { + const btnStatus = $("#flip-status"); + const status = $("#status"); + + switch(btnStatus.text().trim()) { + case "Enable": + $.getJSON("api.php?enable", (data) => { + if(data.status == "enabled") { + btnStatus.html(' Disable'); + status.html(' Active'); + } + }); + break; + case "Disable": + $.getJSON("api.php?disable", (data) => { + if(data.status == "disabled") { + btnStatus.html(' Enable'); + status.html(' Offline'); + } + }); + break; + } +}); + var piholeVersion = $("#piholeVersion").html(); var webVersion = $("#webVersion").html();