From 861be945890e31adf6978ddb5a9a02cedf305359 Mon Sep 17 00:00:00 2001 From: Promofaux Date: Mon, 18 Apr 2016 22:46:49 +0100 Subject: [PATCH 01/39] actually limit list of sources to 10. --- data.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data.php b/data.php index 1aca3a42..ebfeeed7 100644 --- a/data.php +++ b/data.php @@ -112,7 +112,7 @@ } } arsort($sources); - array_slice($sources, 0, 10); + $sources = array_slice($sources, 0, 10); return Array( 'top_sources' => $sources ); From 4d6ec1c5a5c01d7a5bbd48f346e382f12ee3ff98 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Mon, 18 Apr 2016 22:07:37 -0400 Subject: [PATCH 02/39] Remove password functionality After the change to 0.0.0.0 there will be no need for a password --- list.php | 151 +++++++++------------------------------------- php/add.php | 9 +-- php/checkPass.php | 10 --- php/functions.php | 5 -- php/sub.php | 9 +-- 5 files changed, 33 insertions(+), 151 deletions(-) delete mode 100644 php/checkPass.php delete mode 100644 php/functions.php diff --git a/list.php b/list.php index 8087f20f..c85523f9 100644 --- a/list.php +++ b/list.php @@ -37,32 +37,6 @@ function getFullName() { Failure! Something went wrong. - - - @@ -72,10 +46,6 @@ require "footer.php"; - + + - + - - - + + + diff --git a/header.html b/header.html index 916e31a4..32fc53af 100644 --- a/header.html +++ b/header.html @@ -2,6 +2,7 @@ + Pi-hole Admin Console @@ -27,8 +28,8 @@ diff --git a/js/Chart.min.js b/js/other/Chart.min.js similarity index 100% rename from js/Chart.min.js rename to js/other/Chart.min.js diff --git a/js/app.min.js b/js/other/app.min.js similarity index 100% rename from js/app.min.js rename to js/other/app.min.js diff --git a/js/dataTables.bootstrap.min.js b/js/other/dataTables.bootstrap.min.js similarity index 100% rename from js/dataTables.bootstrap.min.js rename to js/other/dataTables.bootstrap.min.js diff --git a/js/html5shiv.min.js b/js/other/html5shiv.min.js similarity index 100% rename from js/html5shiv.min.js rename to js/other/html5shiv.min.js diff --git a/js/jquery-ui.min.js b/js/other/jquery-ui.min.js similarity index 100% rename from js/jquery-ui.min.js rename to js/other/jquery-ui.min.js diff --git a/js/jquery.dataTables.min.js b/js/other/jquery.dataTables.min.js similarity index 100% rename from js/jquery.dataTables.min.js rename to js/other/jquery.dataTables.min.js diff --git a/js/jquery.min.js b/js/other/jquery.min.js similarity index 100% rename from js/jquery.min.js rename to js/other/jquery.min.js diff --git a/js/respond.min.js b/js/other/respond.min.js similarity index 100% rename from js/respond.min.js rename to js/other/respond.min.js From d7391dc8606baebec7f37d040a44a0efbd7dbf50 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Sun, 8 May 2016 13:44:18 -0400 Subject: [PATCH 15/39] Move main JS into separate files --- footer.php | 8 +- index.php | 205 ++----------------------------------------- js/pihole/index.js | 189 +++++++++++++++++++++++++++++++++++++++ js/pihole/queries.js | 27 ++++++ queries.php | 30 +------ 5 files changed, 230 insertions(+), 229 deletions(-) create mode 100644 js/pihole/index.js create mode 100644 js/pihole/queries.js diff --git a/footer.php b/footer.php index ff146bd5..8dd6fe96 100644 --- a/footer.php +++ b/footer.php @@ -13,11 +13,11 @@ - - + + - - + + diff --git a/index.php b/index.php index 0ee052b7..01ddf0e8 100644 --- a/index.php +++ b/index.php @@ -1,15 +1,6 @@ - -
- - -

Javascript Is Disabled

Javascript seems to be disabled. This will break some site features.

-

To enable Javascript click here

- -
-
@@ -194,195 +185,17 @@
+ +
+ + +

Javascript Is Disabled

Javascript seems to be disabled. This will break some site features.

+

To enable Javascript click here

+
+ - + diff --git a/js/pihole/index.js b/js/pihole/index.js new file mode 100644 index 00000000..6c251ccb --- /dev/null +++ b/js/pihole/index.js @@ -0,0 +1,189 @@ +// Remove JS warning +var jswarn = document.getElementById("js-warn-exit"); +jswarn.parentNode.removeChild(jswarn); + +$(document).ready(function() { + // Pull in data via AJAX + + updateSummaryData(); + + updateQueriesOverTime(); + + updateQueryTypes(); + + updateTopClientsChart(); + + updateForwardDestinations(); + + updateTopLists(); + + // Create charts + var chartData = { + labels: [], + datasets: [ + { + label: "All Queries", + fillColor: "rgba(220,220,220,0.5)", + strokeColor: "rgba(0, 166, 90,.8)", + pointColor: "rgba(0, 166, 90,.8)" + }, + { + label: "Ad Queries", + fillColor: "rgba(0,192,239,0.5)", + strokeColor: "rgba(0,192,239,1)", + pointColor: "rgba(0,192,239,1)" + } + ] + }; + var isMobile = { + Windows: function() { + return /IEMobile/i.test(navigator.userAgent); + }, + Android: function() { + return /Android/i.test(navigator.userAgent); + }, + BlackBerry: function() { + return /BlackBerry/i.test(navigator.userAgent); + }, + iOS: function() { + return /iPhone|iPad|iPod/i.test(navigator.userAgent); + }, + any: function() { + return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows()); + } + }; + var animate = false; + var ctx = document.getElementById("queryOverTimeChart").getContext("2d"); + timeLineChart = new Chart(ctx).Line(chartData, + { + pointDot : false, + legendTemplate : "
    -legend\"><% for (var i=0; i
  • \"><%if(datasets[i].label){%><%=datasets[i].label%><%}%>
  • <%}%>
", + animation : animate + } + ); + + ctx = document.getElementById("queryTypeChart").getContext("2d"); + queryTypeChart = new Chart(ctx).Doughnut([], + { + legendTemplate : "
    -legend\"><% for (var i=0; i
  • \"><%if(segments[i].label){%><%=segments[i].label%><%}%>
  • <%}%>
", + animation : animate + } + ); + + ctx = document.getElementById("forwardDestinationChart").getContext("2d"); + forwardDestinationChart = new Chart(ctx).Doughnut([], + { + legendTemplate : "
    -legend\"><% for (var i=0; i
  • \"><%if(segments[i].label){%><%=segments[i].label%><%}%>
  • <%}%>
", + animation : animate + } + ); +}); + +// Functions to oupdate data in page + +function updateSummaryData(runOnce) { + $.getJSON("api.php?summary", function LoadSummaryData(data) { + //$("h3.statistic").addClass("glow"); + if ($("h3#ads_blocked_today").text() != data.ads_blocked_today) { + $("h3#ads_blocked_today").addClass("glow"); + } + if ($("h3#dns_queries_today").text() != data.dns_queries_today) { + $("h3#dns_queries_today").addClass("glow"); + } + if ($("h3#ads_percentage_today").text() != data.ads_percentage_today) { + $("h3#ads_percentage_today").addClass("glow"); + } + + window.setTimeout(function(){ + $("h3#ads_blocked_today").text(data.ads_blocked_today); + $("h3#dns_queries_today").text(data.dns_queries_today); + $("h3#domains_being_blocked").text(data.domains_being_blocked); + $("h3#ads_percentage_today").text(data.ads_percentage_today + "%"); + $("h3.statistic.glow").removeClass("glow") + }, 500); + }).done(function() { + if (runOnce !== true) { + setTimeout(updateSummaryData, 10000); + } + }).fail(function() { + if (runOnce !== true) { + setTimeout(updateSummaryData, (1000 * 60 * 5)); + } + });; +} + +function updateQueriesOverTime() { + $.getJSON("api.php?overTimeData", function(data) { + for (hour in data.ads_over_time) { + timeLineChart.addData([data.domains_over_time[hour], data.ads_over_time[hour]], hour + ":00"); + } + $('#queries-over-time .overlay').remove(); + //$('#queries-over-time').append(timeLineChart.generateLegend()); + }); +} + +function updateTopClientsChart() { + $.getJSON("api.php?summaryRaw&getQuerySources", function(data) { + var clienttable = $('#client-frequency').find('tbody:last'); + for (domain in data.top_sources) { + clienttable.append(' ' + domain + + ' ' + data.top_sources[domain] + '
'); + } + + $('#client-frequency .overlay').remove(); + }); +} + +function updateQueryTypes() { + $.getJSON("api.php?getQueryTypes", function(data) { + var colors = []; + $.each($.AdminLTE.options.colors, function(key, value) { colors.push(value); }); + $.each(data, function(key , value) { + queryTypeChart.addData({ + value: value, + color: colors.shift(), + label: key.substr(6,key.length - 7) + }); + }); + $('#query-types .overlay').remove(); + //$('#query-types').append(queryTypeChart.generateLegend()); + }); +} + +function updateForwardDestinations() { + $.getJSON("api.php?getForwardDestinations", function(data) { + var colors = []; + $.each($.AdminLTE.options.colors, function(key, value) { colors.push(value); }); + $.each(data, function(key , value) { + forwardDestinationChart.addData({ + value: value, + color: colors.shift(), + label: key + }); + }); + $('#forward-destinations .overlay').remove(); + //$('#forward-destinations').append(forwardDestinationChart.generateLegend()); + }); +} + +function updateTopLists() { + $.getJSON("api.php?summaryRaw&topItems", function(data) { + var domaintable = $('#domain-frequency').find('tbody:last'); + var adtable = $('#ad-frequency').find('tbody:last'); + + for (domain in data.top_queries) { + domaintable.append(' ' + domain + + ' ' + data.top_queries[domain] + '
'); + } + for (domain in data.top_ads) { + adtable.append(' ' + domain + + ' ' + data.top_ads[domain] + '
'); + } + + $('#domain-frequency .overlay').remove(); + $('#ad-frequency .overlay').remove(); + }); +} \ No newline at end of file diff --git a/js/pihole/queries.js b/js/pihole/queries.js new file mode 100644 index 00000000..d13008dc --- /dev/null +++ b/js/pihole/queries.js @@ -0,0 +1,27 @@ +$(document).ready(function() { + tableApi = $('#all-queries').DataTable( { + "rowCallback": function( row, data, index ){ + if (data[4] == "Pi-holed") { + $(row).css('color','red') + } + else{ + $(row).css('color','green') + } + + }, + "ajax": "api.php?getAllQueries", + "autoWidth" : false, + "order" : [[0, "desc"]], + "columns": [ + { "width" : "20%", "type": "date" }, + { "width" : "10%" }, + { "width" : "40%" }, + { "width" : "15%" }, + { "width" : "15%" } + ] + }) +} ); + +function refreshData() { + tableApi.ajax.url("api.php?getAllQueries").load(); +} \ No newline at end of file diff --git a/queries.php b/queries.php index 83891daa..af951eb5 100644 --- a/queries.php +++ b/queries.php @@ -49,32 +49,4 @@ require "footer.php"; ?> - + From 48109666fe32af88fa4c81dbdd86e5873e617875 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Sun, 8 May 2016 13:59:59 -0400 Subject: [PATCH 16/39] Allow inline CSS --- header.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/header.html b/header.html index 32fc53af..ebdc54ea 100644 --- a/header.html +++ b/header.html @@ -2,7 +2,7 @@ - + Pi-hole Admin Console From 46df1bddc505cd555a094058092d65abd67368b9 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Sun, 8 May 2016 14:39:12 -0400 Subject: [PATCH 17/39] Loosen up CSP for Chart.js Chart.js requires 'unsafe-eval', and the latest version is too much of a hassle to upgrade to currently (believe me, I tried...) --- header.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/header.html b/header.html index ebdc54ea..734a4838 100644 --- a/header.html +++ b/header.html @@ -2,7 +2,7 @@ - + Pi-hole Admin Console From d38486fbd615d40017fae17eeedc3818b173c7e6 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Tue, 10 May 2016 16:47:22 -0400 Subject: [PATCH 18/39] Possible fix for incorrect token It appears that sometimes the first time you open the page, the token is not synced correctly or something, causing the CSRF check to fail. Starting the session before any HTML is outputted seems to be a recommended first step to troubleshooting PHP session errors. I have tried to test this fix, but right now I don't know exactly what triggers the error. --- list.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/list.php b/list.php index 1e4c350a..74a4926e 100644 --- a/list.php +++ b/list.php @@ -1,4 +1,5 @@ Date: Tue, 10 May 2016 20:21:30 -0400 Subject: [PATCH 19/39] Change Blacklist icon to fa-ban --- header.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/header.html b/header.html index a304b7c7..515c2236 100644 --- a/header.html +++ b/header.html @@ -138,7 +138,7 @@
  • - Blacklist + Blacklist
  • From 8a33af6d6be9073287188faf1056cd9f02531608 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Tue, 10 May 2016 20:30:28 -0400 Subject: [PATCH 20/39] Possible fix for FireFox and IE Both failed CORS as both browsers, unlike Chrome, do not send `Origin` headers for same-origin requests. Now the scripts check if the `Host` header equals the IP of the Pi-hole. IE may require more fixing, as it's IE. :P --- php/add.php | 2 +- php/sub.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/php/add.php b/php/add.php index 8c116c2e..879830ec 100644 --- a/php/add.php +++ b/php/add.php @@ -3,7 +3,7 @@ if(!isset($_POST['domain'], $_POST['list'], $_POST['token'])) die("Missing POST variables"); // Check CORS -if($_SERVER['HTTP_ORIGIN'] != "http://pi.hole" && $_SERVER['HTTP_ORIGIN'] != "http://${_SERVER['SERVER_ADDR']}") +if($_SERVER['HTTP_ORIGIN'] != "http://pi.hole" && $_SERVER['HTTP_ORIGIN'] != "http://${_SERVER['SERVER_ADDR']}" || $_SERVER['HTTP_HOST'] != $_SERVER['SERVER_ADDR']) die("Failed CORS"); header("Access-Control-Allow-Origin: ${_SERVER['HTTP_ORIGIN']}"); diff --git a/php/sub.php b/php/sub.php index d2730054..408370c6 100644 --- a/php/sub.php +++ b/php/sub.php @@ -3,7 +3,7 @@ if(!isset($_POST['domain'], $_POST['list'], $_POST['token'])) die("Missing POST variables"); // Check CORS -if($_SERVER['HTTP_ORIGIN'] != "http://pi.hole" && $_SERVER['HTTP_ORIGIN'] != "http://${_SERVER['SERVER_ADDR']}") +if($_SERVER['HTTP_ORIGIN'] != "http://pi.hole" && $_SERVER['HTTP_ORIGIN'] != "http://${_SERVER['SERVER_ADDR']}" || $_SERVER['HTTP_HOST'] != $_SERVER['SERVER_ADDR']) die("Failed CORS"); header("Access-Control-Allow-Origin: ${_SERVER['HTTP_ORIGIN']}"); From 0e44f7b99220fd5c989429d357df409c67c1d12a Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Tue, 10 May 2016 20:41:59 -0400 Subject: [PATCH 21/39] Complete the fix This fixes the fix. I still needed to account for `pi.hole` as a Host value. --- php/add.php | 3 ++- php/sub.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/php/add.php b/php/add.php index 879830ec..4adc8c0e 100644 --- a/php/add.php +++ b/php/add.php @@ -3,7 +3,8 @@ if(!isset($_POST['domain'], $_POST['list'], $_POST['token'])) die("Missing POST variables"); // Check CORS -if($_SERVER['HTTP_ORIGIN'] != "http://pi.hole" && $_SERVER['HTTP_ORIGIN'] != "http://${_SERVER['SERVER_ADDR']}" || $_SERVER['HTTP_HOST'] != $_SERVER['SERVER_ADDR']) +if($_SERVER['HTTP_ORIGIN'] != "http://pi.hole" && $_SERVER['HTTP_ORIGIN'] != "http://${_SERVER['SERVER_ADDR']}" && + $_SERVER['HTTP_HOST'] != $_SERVER['SERVER_ADDR'] && $_SERVER['HTTP_HOST'] != "pi.hole") die("Failed CORS"); header("Access-Control-Allow-Origin: ${_SERVER['HTTP_ORIGIN']}"); diff --git a/php/sub.php b/php/sub.php index 408370c6..5382a5e1 100644 --- a/php/sub.php +++ b/php/sub.php @@ -3,7 +3,8 @@ if(!isset($_POST['domain'], $_POST['list'], $_POST['token'])) die("Missing POST variables"); // Check CORS -if($_SERVER['HTTP_ORIGIN'] != "http://pi.hole" && $_SERVER['HTTP_ORIGIN'] != "http://${_SERVER['SERVER_ADDR']}" || $_SERVER['HTTP_HOST'] != $_SERVER['SERVER_ADDR']) +if($_SERVER['HTTP_ORIGIN'] != "http://pi.hole" && $_SERVER['HTTP_ORIGIN'] != "http://${_SERVER['SERVER_ADDR']}" && + $_SERVER['HTTP_HOST'] != $_SERVER['SERVER_ADDR'] && $_SERVER['HTTP_HOST'] != "pi.hole") die("Failed CORS"); header("Access-Control-Allow-Origin: ${_SERVER['HTTP_ORIGIN']}"); From 7067473d8954b4b55e1a1cb56c802f75758857c7 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Wed, 11 May 2016 16:31:07 -0400 Subject: [PATCH 22/39] Return correct CORS header for Host If only Host was correct (FireFox and IE only set Host for same-origin requests) then it would still use the empty Origin header for the CORS response, leading to `Access-Control-Allow-Origin: ` --- php/add.php | 9 +++++---- php/sub.php | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/php/add.php b/php/add.php index 4adc8c0e..e5b3c966 100644 --- a/php/add.php +++ b/php/add.php @@ -3,12 +3,13 @@ if(!isset($_POST['domain'], $_POST['list'], $_POST['token'])) die("Missing POST variables"); // Check CORS -if($_SERVER['HTTP_ORIGIN'] != "http://pi.hole" && $_SERVER['HTTP_ORIGIN'] != "http://${_SERVER['SERVER_ADDR']}" && - $_SERVER['HTTP_HOST'] != $_SERVER['SERVER_ADDR'] && $_SERVER['HTTP_HOST'] != "pi.hole") +if($_SERVER['HTTP_ORIGIN'] == "http://pi.hole" || $_SERVER['HTTP_ORIGIN'] == "http://${_SERVER['SERVER_ADDR']}") + header("Access-Control-Allow-Origin: ${_SERVER['HTTP_ORIGIN']}"); +else if($_SERVER['HTTP_HOST'] == $_SERVER['SERVER_ADDR'] || $_SERVER['HTTP_HOST'] == "pi.hole") + header("Access-Control-Allow-Origin: ${_SERVER['HTTP_HOST']}"); +else die("Failed CORS"); -header("Access-Control-Allow-Origin: ${_SERVER['HTTP_ORIGIN']}"); - session_start(); // Check CSRF token diff --git a/php/sub.php b/php/sub.php index 5382a5e1..cee19723 100644 --- a/php/sub.php +++ b/php/sub.php @@ -3,12 +3,13 @@ if(!isset($_POST['domain'], $_POST['list'], $_POST['token'])) die("Missing POST variables"); // Check CORS -if($_SERVER['HTTP_ORIGIN'] != "http://pi.hole" && $_SERVER['HTTP_ORIGIN'] != "http://${_SERVER['SERVER_ADDR']}" && - $_SERVER['HTTP_HOST'] != $_SERVER['SERVER_ADDR'] && $_SERVER['HTTP_HOST'] != "pi.hole") +if($_SERVER['HTTP_ORIGIN'] == "http://pi.hole" || $_SERVER['HTTP_ORIGIN'] == "http://${_SERVER['SERVER_ADDR']}") + header("Access-Control-Allow-Origin: ${_SERVER['HTTP_ORIGIN']}"); +else if($_SERVER['HTTP_HOST'] == $_SERVER['SERVER_ADDR'] || $_SERVER['HTTP_HOST'] == "pi.hole") + header("Access-Control-Allow-Origin: ${_SERVER['HTTP_HOST']}"); +else die("Failed CORS"); -header("Access-Control-Allow-Origin: ${_SERVER['HTTP_ORIGIN']}"); - session_start(); // Check CSRF token From 66d354d2105d405defee7ddc3e0b757e68596781 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Wed, 11 May 2016 16:35:32 -0400 Subject: [PATCH 23/39] Turn off caching for jQuery AJAX calls IE would cache the list contents, so that if you updated the list it would not show up as changed. --- list.php | 1 + 1 file changed, 1 insertion(+) diff --git a/list.php b/list.php index 74a4926e..b0c27255 100644 --- a/list.php +++ b/list.php @@ -53,6 +53,7 @@ require "footer.php"; + + diff --git a/header.html b/header.html index 189a7c81..5de7a5d7 100644 --- a/header.html +++ b/header.html @@ -50,8 +50,8 @@ - +