From da12f6f16d8dd6a30a1cb6bb32f53b4c0e99c084 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 19 Dec 2016 10:44:04 +0100 Subject: [PATCH 01/21] Change the "none" to "nothing" --- data.php | 2 +- php/savesettings.php | 2 +- queries.php | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/data.php b/data.php index 2a145110..7060dc4e 100644 --- a/data.php +++ b/data.php @@ -163,7 +163,7 @@ $showBlocked = true; $showPermitted = false; } - elseif($setupVars["API_QUERY_LOG_SHOW"] === "none") + elseif($setupVars["API_QUERY_LOG_SHOW"] === "nothing") { $showBlocked = false; $showPermitted = false; diff --git a/php/savesettings.php b/php/savesettings.php index 27fa763c..49b856c8 100644 --- a/php/savesettings.php +++ b/php/savesettings.php @@ -205,7 +205,7 @@ function validDomain($domain_name) } else { - exec("sudo pihole -a setquerylog none"); + exec("sudo pihole -a setquerylog nothing"); $success .= "No entries will be shown in Query Log"; } diff --git a/queries.php b/queries.php index 153b5cbf..8907367f 100644 --- a/queries.php +++ b/queries.php @@ -23,6 +23,10 @@ if(isset($setupVars["API_QUERY_LOG_SHOW"])) { $showing = "(showing blocked queries only)"; } + elseif($setupVars["API_QUERY_LOG_SHOW"] === "nothing") + { + $showing = "(showing no queries at all)"; + } } ?> From 77c1e8ff946f53a643a15af1b53f01e99e7a6b47 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 19 Dec 2016 12:44:24 +0100 Subject: [PATCH 02/21] Immediately apply changed temperature unit --- header.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/header.php b/header.php index ca688c30..f13a47c7 100644 --- a/header.php +++ b/header.php @@ -17,6 +17,11 @@ { $temperatureunit = "C"; } + // Override temperature unit setting if it is changed via Settings page + if(isset($_POST["tempunit"])) + { + $temperatureunit = $_POST["tempunit"]; + } // Get load $loaddata = sys_getloadavg(); @@ -69,7 +74,7 @@ $boxedlayout = true; } - // Override layout setting if layout is changed via Settings page3 + // Override layout setting if layout is changed via Settings page if(isset($_POST["field"])) { if($_POST["field"] === "webUI" && isset($_POST["boxedlayout"])) From 79af8826277a61a0d329128b23a2f7914d4ccb16 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 19 Dec 2016 13:06:49 +0100 Subject: [PATCH 03/21] Show "N/A" if we cannot get the temperature on the current machine (like in a virtual box) --- header.php | 62 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/header.php b/header.php index f13a47c7..d23dcdbf 100644 --- a/header.php +++ b/header.php @@ -4,23 +4,30 @@ check_cors(); - $cmd = "echo $((`cat /sys/class/thermal/thermal_zone0/temp | cut -c1-2`))"; - $output = shell_exec($cmd); - $celsius = str_replace(array("\r\n","\r","\n"),"", $output); - $fahrenheit = round(str_replace(["\r\n","\r","\n"],"", $output*9./5)+32); - - if(isset($setupVars['TEMPERATUREUNIT'])) + if(file_exists("/sys/class/thermal/thermal_zone0/temp")) { - $temperatureunit = $setupVars['TEMPERATUREUNIT']; + $cmd = "echo $((`cat /sys/class/thermal/thermal_zone0/temp | cut -c1-2`))"; + $output = rtrim(shell_exec($cmd)); + $celsius = intVal($output); + $fahrenheit = round(($celsius*9./5)+32.0); + + if(isset($setupVars['TEMPERATUREUNIT'])) + { + $temperatureunit = $setupVars['TEMPERATUREUNIT']; + } + else + { + $temperatureunit = "C"; + } + // Override temperature unit setting if it is changed via Settings page + if(isset($_POST["tempunit"])) + { + $temperatureunit = $_POST["tempunit"]; + } } else { - $temperatureunit = "C"; - } - // Override temperature unit setting if it is changed via Settings page - if(isset($_POST["tempunit"])) - { - $temperatureunit = $_POST["tempunit"]; + $celsius = -273.16; } // Get load @@ -229,20 +236,31 @@ } // CPU Temp - echo ' 45 || $celsius < -273.15) { + echo "#FF0000"; } else { - echo '#3366FF'; + echo "#3366FF"; + } + echo "\"> Temp: "; + if($celsius >= -273.15) + { + if($temperatureunit != "F") + { + echo $celsius . "°C"; + } + else + { + echo $fahrenheit . "°F"; + } } - echo '"> Temp: '; - if($temperatureunit != "F") - echo $celsius . '°C'; else - echo $fahrenheit . '°F'; - echo ''; + { + echo "N/A"; + } + echo ""; ?>
Date: Mon, 19 Dec 2016 13:09:44 +0100 Subject: [PATCH 04/21] Increase resolution of displayed temperature --- header.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/header.php b/header.php index d23dcdbf..473ab23e 100644 --- a/header.php +++ b/header.php @@ -6,10 +6,10 @@ if(file_exists("/sys/class/thermal/thermal_zone0/temp")) { - $cmd = "echo $((`cat /sys/class/thermal/thermal_zone0/temp | cut -c1-2`))"; + $cmd = "echo $((`cat /sys/class/thermal/thermal_zone0/temp`))"; $output = rtrim(shell_exec($cmd)); - $celsius = intVal($output); - $fahrenheit = round(($celsius*9./5)+32.0); + $celsius = intVal($output)*1e-3; + $fahrenheit = ($celsius*9./5)+32.0; if(isset($setupVars['TEMPERATUREUNIT'])) { @@ -249,11 +249,11 @@ { if($temperatureunit != "F") { - echo $celsius . "°C"; + echo round($celsius,1) . "°C"; } else { - echo $fahrenheit . "°F"; + echo round($fahrenheit,1) . "°F"; } } else From d18269e8355af77001ac9bdbd3a94caa0cf3fa01 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 19 Dec 2016 13:15:59 +0100 Subject: [PATCH 05/21] Hide CPU temp if it is not available --- header.php | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/header.php b/header.php index 473ab23e..dc9827f0 100644 --- a/header.php +++ b/header.php @@ -236,17 +236,16 @@ } // CPU Temp - echo " 45 || $celsius < -273.15) { - echo "#FF0000"; - } - else - { - echo "#3366FF"; - } - echo "\"> Temp: "; - if($celsius >= -273.15) - { + if ($celsius >= -273.15) { + echo " 45) { + echo "#FF0000"; + } + else + { + echo "#3366FF"; + } + echo "\"> Temp: "; if($temperatureunit != "F") { echo round($celsius,1) . "°C"; @@ -255,12 +254,8 @@ { echo round($fahrenheit,1) . "°F"; } + echo ""; } - else - { - echo "N/A"; - } - echo ""; ?>
Date: Mon, 19 Dec 2016 13:25:20 +0100 Subject: [PATCH 06/21] Something different as well --- settings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/settings.php b/settings.php index ed2e7b0b..2c58bc49 100644 --- a/settings.php +++ b/settings.php @@ -140,7 +140,7 @@ disabled> - +
Router
From da1c38ea5a79b76bec8ac77e1096a68ac76b7887 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 19 Dec 2016 13:44:12 +0100 Subject: [PATCH 07/21] Pass "none" to backend if we want no secondary DNS server --- php/savesettings.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/php/savesettings.php b/php/savesettings.php index 27fa763c..edf67902 100644 --- a/php/savesettings.php +++ b/php/savesettings.php @@ -65,7 +65,14 @@ function validDomain($domain_name) // Get secondary DNS server IP address if($secondaryDNS === "Custom") { - $secondaryIP = $_POST["DNS2IP"]; + if(strlen($_POST["DNS2IP"]) > 0) + { + $secondaryIP = $_POST["DNS2IP"]; + } + else + { + $secondaryIP = "none"; + } } else { From 4ddadd7043eba4bfc5a1ce66771582afceaa2f10 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 19 Dec 2016 13:46:38 +0100 Subject: [PATCH 08/21] Skip validation of secondary IP if set to "none" --- php/savesettings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/savesettings.php b/php/savesettings.php index edf67902..911b82ee 100644 --- a/php/savesettings.php +++ b/php/savesettings.php @@ -80,7 +80,7 @@ function validDomain($domain_name) } // Validate secondary IP - if (!validIP($secondaryIP) && strlen($secondaryIP) > 0) + if (!validIP($secondaryIP) && $secondaryIP != "none" && strlen($secondaryIP) > 0) { $error .= "Secondary IP (".$secondaryIP.") is invalid!
"; } From fc43a05e885564bdb1b6969985d742bd43078be8 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 19 Dec 2016 14:09:58 +0100 Subject: [PATCH 09/21] Try to get temperature value from different places (OS dependent) --- header.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/header.php b/header.php index dc9827f0..c4aea030 100644 --- a/header.php +++ b/header.php @@ -4,10 +4,23 @@ check_cors(); + // Try to get temperature value from different places (OS dependent) if(file_exists("/sys/class/thermal/thermal_zone0/temp")) { - $cmd = "echo $((`cat /sys/class/thermal/thermal_zone0/temp`))"; - $output = rtrim(shell_exec($cmd)); + $output = rtrim(file_get_contents("/sys/class/thermal/thermal_zone0/temp")); + } + elseif (file_exists("/sys/class/hwmon/hwmon0/temp1_input")) + { + $output = rtrim(file_get_contents("/sys/class/hwmon/hwmon0/temp1_input")); + } + else + { + $output = ""; + } + + // Test if we succeeded in getting the temperature + if(is_numeric($output)) + { $celsius = intVal($output)*1e-3; $fahrenheit = ($celsius*9./5)+32.0; From 7ab611eb44e10ea6eba9643b1c70468cb69d195b Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 19 Dec 2016 22:57:23 +0100 Subject: [PATCH 10/21] Added comment about absolute zero --- header.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/header.php b/header.php index c4aea030..56f970dd 100644 --- a/header.php +++ b/header.php @@ -40,6 +40,8 @@ } else { + // Nothing can be colder than -273.15 degree Celsius (= 0 Kelvin) + // This is the minimum temperature possible (AKA absolute zero) $celsius = -273.16; } From f5eaf6d1407c831e9a2dfd53aac35a039de60063 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 20 Dec 2016 12:17:59 +0100 Subject: [PATCH 11/21] Add Kelvin --- header.php | 11 ++++++++--- php/savesettings.php | 7 +++++-- settings.php | 1 + 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/header.php b/header.php index 56f970dd..8e99b752 100644 --- a/header.php +++ b/header.php @@ -22,6 +22,7 @@ if(is_numeric($output)) { $celsius = intVal($output)*1e-3; + $kelvin = $celsius + 273.15; $fahrenheit = ($celsius*9./5)+32.0; if(isset($setupVars['TEMPERATUREUNIT'])) @@ -261,13 +262,17 @@ echo "#3366FF"; } echo "\"> Temp: "; - if($temperatureunit != "F") + if($temperatureunit === "F") { - echo round($celsius,1) . "°C"; + echo round($fahrenheit,1) . "°F"; + } + elseif($temperatureunit === "K") + { + echo round($kelvin,1) . "K"; } else { - echo round($fahrenheit,1) . "°F"; + echo round($celsius,1) . "°C"; } echo ""; } diff --git a/php/savesettings.php b/php/savesettings.php index ced3b7a3..4dd357c5 100644 --- a/php/savesettings.php +++ b/php/savesettings.php @@ -222,12 +222,14 @@ function validDomain($domain_name) if($_POST["tempunit"] == "F") { exec('sudo pihole -a -f'); - $success .= "The webUI settings have been updated"; + } + elseif($_POST["tempunit"] == "K") + { + exec('sudo pihole -a -k'); } else { exec('sudo pihole -a -c'); - $success .= "The webUI settings have been updated"; } if(isset($_POST["boxedlayout"])) { @@ -237,6 +239,7 @@ function validDomain($domain_name) { exec('sudo pihole -a layout traditional'); } + $success .= "The webUI settings have been updated"; break; case "reboot": diff --git a/settings.php b/settings.php index 2c58bc49..87bfc815 100644 --- a/settings.php +++ b/settings.php @@ -452,6 +452,7 @@

CPU Temperature Unit

+
From ff7d1ed8fbe89471451fdc94c5f76dd851c9cf00 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 20 Dec 2016 12:21:30 +0100 Subject: [PATCH 12/21] Small bugfix --- header.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/header.php b/header.php index 8e99b752..27d4aff0 100644 --- a/header.php +++ b/header.php @@ -58,7 +58,7 @@ if(count($data) > 0) { foreach ($data as $line) { - list($key, $val) = explode(":", $line); + @list($key, $val) = explode(":", $line); // remove " kB" fron the end of the string and make an integer $meminfo[$key] = intVal(substr(trim($val),0, -3)); } From 0196bf5c5c74834e7f0cb0914814159c21c17b7b Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 20 Dec 2016 13:22:02 +0100 Subject: [PATCH 13/21] Show percentage of blocked queries in graph tooltip --- js/pihole/index.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/js/pihole/index.js b/js/pihole/index.js index 5d7dba0e..7bdc39f1 100644 --- a/js/pihole/index.js +++ b/js/pihole/index.js @@ -282,6 +282,23 @@ $(document).ready(function() { var from = padNumber(h)+":"+padNumber(m)+":00"; var to = padNumber(h)+":"+padNumber(m+9)+":59"; return "Queries from "+from+" to "+to; + }, + label(tooltipItems, data) { + if(tooltipItems.datasetIndex === 1) + { + var percentage = 0.0; + var total = parseInt(data.datasets[0].data[tooltipItems.index]); + var blocked = parseInt(data.datasets[1].data[tooltipItems.index]); + if(total > 0) + { + percentage = 100.0*blocked/total; + } + return data.datasets[tooltipItems.datasetIndex].label + ": " + tooltipItems.yLabel + " (" + percentage.toFixed(1) + "%)"; + } + else + { + return data.datasets[tooltipItems.datasetIndex].label + ": " + tooltipItems.yLabel; + } } } }, From b3907ea57003b784e7eb0222439a58c117535ced Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 20 Dec 2016 17:53:06 +0100 Subject: [PATCH 14/21] Test if line should be evaluated --- header.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/header.php b/header.php index 27d4aff0..166b9c97 100644 --- a/header.php +++ b/header.php @@ -58,9 +58,12 @@ if(count($data) > 0) { foreach ($data as $line) { - @list($key, $val) = explode(":", $line); - // remove " kB" fron the end of the string and make an integer - $meminfo[$key] = intVal(substr(trim($val),0, -3)); + $expl = explode(":", $line); + if(count($expl) == 2) + { + // remove " kB" fron the end of the string and make an integer + $meminfo[$expl[0]] = intVal(substr(trim($expl[1]),0, -3)); + } } $memory_used = $meminfo["MemTotal"]-$meminfo["MemFree"]-$meminfo["Buffers"]-$meminfo["Cached"]; $memory_total = $meminfo["MemTotal"]; From a54dfdefc913bb821d8fa4ec9d2242295f6aa034 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 20 Dec 2016 17:54:10 +0100 Subject: [PATCH 15/21] trim string --- header.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/header.php b/header.php index 166b9c97..39146967 100644 --- a/header.php +++ b/header.php @@ -58,11 +58,11 @@ if(count($data) > 0) { foreach ($data as $line) { - $expl = explode(":", $line); + $expl = explode(":", trim($line)); if(count($expl) == 2) { - // remove " kB" fron the end of the string and make an integer - $meminfo[$expl[0]] = intVal(substr(trim($expl[1]),0, -3)); + // remove " kB" from the end of the string and make it an integer + $meminfo[$expl[0]] = intVal(substr($expl[1],0, -3)); } } $memory_used = $meminfo["MemTotal"]-$meminfo["MemFree"]-$meminfo["Buffers"]-$meminfo["Cached"]; From 4b7f20cfaecbebcc2296aad58975f63a1e0cc6cf Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Tue, 20 Dec 2016 13:11:15 -0500 Subject: [PATCH 16/21] Take into account the whitelist and blacklist --- data.php | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/data.php b/data.php index 7060dc4e..5be0a459 100644 --- a/data.php +++ b/data.php @@ -5,6 +5,8 @@ $hosts = file_exists("/etc/hosts") ? file("/etc/hosts") : array(); $log = new \SplFileObject('/var/log/pihole.log'); $gravity = new \SplFileObject('/etc/pihole/list.preEventHorizon'); + $whitelist = new \SplFileObject('/etc/pihole/whitelist.txt'); + $blacklist = new \SplFileObject('/etc/pihole/blacklist.txt'); /******* Public Members ********/ function getSummaryData() { @@ -183,10 +185,15 @@ } function getAllQueries($orderBy) { - global $log,$gravity,$showBlocked,$showPermitted; + global $log,$gravity,$showBlocked,$showPermitted,$whitelist,$blacklist; $allQueries = array("data" => array()); $dns_queries = getDnsQueriesAll($log); - $gravity_domains = getGravityDomains($gravity); + $gravity_domains = getDomains($gravity, true); + $whitelist_domains = getDomains($whitelist, false); + $blacklist_domains = getDomains($blacklist, true); + + $gravity_domains = array_merge($gravity_domains, $whitelist_domains, $blacklist_domains); + foreach ($dns_queries as $query) { $time = date_create(substr($query, 0, 16)); @@ -198,7 +205,9 @@ if (substr($tmp, 0, 5) == "query") { - $status = isset($gravity_domains[$domain]) ? "Pi-holed" : "OK"; + $status = isset($gravity_domains[$domain]) ? + $gravity_domains[$domain] === true ? "Pi-holed" : "OK" + : "OK"; if(($status === "Pi-holed" && $showBlocked) || ($status === "OK" && $showPermitted)) { $type = substr($exploded[count($exploded)-4], 6, -1); @@ -265,13 +274,13 @@ return $lines; } - function getGravityDomains($gravity){ - $gravity->rewind(); + function getDomains($file, $default_value){ + $file->rewind(); $lines=[]; - foreach ($gravity as $line) { + foreach ($file as $line) { // Strip newline (and possibly carriage return) from end of string // using rtrim() - $lines[rtrim($line)] = true; + $lines[rtrim($line)] = $default_value; } return $lines; From f9a601c5b0490d2ab8b52bdd13af5787596e15e3 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Tue, 20 Dec 2016 13:38:42 -0500 Subject: [PATCH 17/21] Avoid array_merge Simply unset and set when applying whitelist and blacklist --- data.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/data.php b/data.php index 5be0a459..713ba25a 100644 --- a/data.php +++ b/data.php @@ -188,11 +188,21 @@ global $log,$gravity,$showBlocked,$showPermitted,$whitelist,$blacklist; $allQueries = array("data" => array()); $dns_queries = getDnsQueriesAll($log); - $gravity_domains = getDomains($gravity, true); - $whitelist_domains = getDomains($whitelist, false); - $blacklist_domains = getDomains($blacklist, true); + $gravity_domains = getDomains($gravity); + $whitelist_domains = getDomains($whitelist); + $blacklist_domains = getDomains($blacklist); - $gravity_domains = array_merge($gravity_domains, $whitelist_domains, $blacklist_domains); + // Remove whitelisted domains + foreach($gravity_domains as $domain) { + if(isset($whitelist_domains[$domain])) { + unset($gravity_domains[$domain]); + } + } + + // Add blacklisted domains if they aren't already there + foreach($blacklist_domains as $domain) { + $gravity_domains[$domain] = null; + } foreach ($dns_queries as $query) { @@ -274,13 +284,13 @@ return $lines; } - function getDomains($file, $default_value){ + function getDomains($file){ $file->rewind(); $lines=[]; foreach ($file as $line) { // Strip newline (and possibly carriage return) from end of string // using rtrim() - $lines[rtrim($line)] = $default_value; + $lines[rtrim($line)] = null; } return $lines; From b298f1ab06231a771b844758f8ae286b137aa815 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 20 Dec 2016 21:19:49 +0100 Subject: [PATCH 18/21] Loop over $whiteList, simplified code slightly --- data.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/data.php b/data.php index 713ba25a..8c7bf2df 100644 --- a/data.php +++ b/data.php @@ -188,20 +188,22 @@ global $log,$gravity,$showBlocked,$showPermitted,$whitelist,$blacklist; $allQueries = array("data" => array()); $dns_queries = getDnsQueriesAll($log); - $gravity_domains = getDomains($gravity); + + $gravity_domains = getDomains($gravity); $whitelist_domains = getDomains($whitelist); $blacklist_domains = getDomains($blacklist); - // Remove whitelisted domains - foreach($gravity_domains as $domain) { - if(isset($whitelist_domains[$domain])) { + // Remove whitelisted domains if they exist in + // $gravity_domains + foreach($whitelist_domains as $domain) { + if(isset($gravity_domains[$domain])) { unset($gravity_domains[$domain]); } } - // Add blacklisted domains if they aren't already there + // Add blacklisted domains (do not need to check if they are already there) foreach($blacklist_domains as $domain) { - $gravity_domains[$domain] = null; + $gravity_domains[$domain] = true; } @@ -215,9 +217,7 @@ if (substr($tmp, 0, 5) == "query") { - $status = isset($gravity_domains[$domain]) ? - $gravity_domains[$domain] === true ? "Pi-holed" : "OK" - : "OK"; + $status = isset($gravity_domains[$domain]) ? "Pi-holed" : "OK"; if(($status === "Pi-holed" && $showBlocked) || ($status === "OK" && $showPermitted)) { $type = substr($exploded[count($exploded)-4], 6, -1); @@ -290,7 +290,7 @@ foreach ($file as $line) { // Strip newline (and possibly carriage return) from end of string // using rtrim() - $lines[rtrim($line)] = null; + $lines[rtrim($line)] = true; } return $lines; From be5a6cb5df53dbbf2ec7f47d669bcd6adc38d5c6 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 20 Dec 2016 21:31:49 +0100 Subject: [PATCH 19/21] Use only one array, no additional loops --- api.php | 2 +- data.php | 57 ++++++++++++++++++++++++++++++++++---------------------- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/api.php b/api.php index 3c3ad7d1..150dc9e2 100644 --- a/api.php +++ b/api.php @@ -77,7 +77,7 @@ } if (isset($_GET['getGravityDomains'])) { - $data = array_merge($data, getGravityDomains($gravity)); + $data = array_merge($data, getGravity()); } function filterArray(&$inArray) { diff --git a/data.php b/data.php index 8c7bf2df..c12dec25 100644 --- a/data.php +++ b/data.php @@ -185,27 +185,12 @@ } function getAllQueries($orderBy) { - global $log,$gravity,$showBlocked,$showPermitted,$whitelist,$blacklist; + global $log,$showBlocked,$showPermitted; $allQueries = array("data" => array()); $dns_queries = getDnsQueriesAll($log); - $gravity_domains = getDomains($gravity); - $whitelist_domains = getDomains($whitelist); - $blacklist_domains = getDomains($blacklist); - - // Remove whitelisted domains if they exist in - // $gravity_domains - foreach($whitelist_domains as $domain) { - if(isset($gravity_domains[$domain])) { - unset($gravity_domains[$domain]); - } - } - - // Add blacklisted domains (do not need to check if they are already there) - foreach($blacklist_domains as $domain) { - $gravity_domains[$domain] = true; - } - + // Create empty array for gravity + $gravity_domains = getGravity(); foreach ($dns_queries as $query) { $time = date_create(substr($query, 0, 16)); @@ -284,18 +269,46 @@ return $lines; } - function getDomains($file){ + function getDomains($file, &$array, $action){ $file->rewind(); $lines=[]; foreach ($file as $line) { - // Strip newline (and possibly carriage return) from end of string - // using rtrim() - $lines[rtrim($line)] = true; + // Strip newline (and possibly carriage return) from end of key + $key = rtrim($line); + // if $action = true -> we want that domain to be ADDED to the list + // doesn't harm to do this if it has already been set before + // (e.g. once in gravity list, once in blacklist) + if($action && strlen($key) > 0) + { + $lines[$key] = true; + } + elseif(isset($lines[$key])) + { + // $action is not true (we want to remove) *and* key is set + unset($lines[$key]); + } + // else: Remove, but not set -> don't have to don anything } return $lines; } + function getGravity() { + global $gravity,$whitelist,$blacklist; + $domains = []; + + // ADD (true) preEventHorizon domains + getDomains($gravity, $domains, true); + + // ADD (true) blacklist domains + getDomains($blacklist, $domains, true); + + // REMOVE (false) whitelist domains + getDomains($whitelist, $domains, false); + + return $domains; + } + function getBlockedQueries(\SplFileObject $log) { $log->rewind(); $lines = []; From d89fb7a0e0961cc5e21e5538eefe6cdff55c69be Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 20 Dec 2016 21:38:51 +0100 Subject: [PATCH 20/21] Have to modify the correct array for this to work... --- data.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/data.php b/data.php index c12dec25..dd5c5c5b 100644 --- a/data.php +++ b/data.php @@ -271,7 +271,6 @@ function getDomains($file, &$array, $action){ $file->rewind(); - $lines=[]; foreach ($file as $line) { // Strip newline (and possibly carriage return) from end of key $key = rtrim($line); @@ -280,17 +279,15 @@ // (e.g. once in gravity list, once in blacklist) if($action && strlen($key) > 0) { - $lines[$key] = true; + $array[$key] = true; } - elseif(isset($lines[$key])) + elseif(isset($array[$key])) { // $action is not true (we want to remove) *and* key is set - unset($lines[$key]); + unset($array[$key]); } // else: Remove, but not set -> don't have to don anything } - - return $lines; } function getGravity() { From b01d8293db978fa72220f10c4f0bb7f9eced1fd0 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 20 Dec 2016 21:40:19 +0100 Subject: [PATCH 21/21] Improved comments --- data.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data.php b/data.php index dd5c5c5b..43ba51e6 100644 --- a/data.php +++ b/data.php @@ -279,14 +279,14 @@ // (e.g. once in gravity list, once in blacklist) if($action && strlen($key) > 0) { + // $action is true (we want to add) *and* key is not empty $array[$key] = true; } - elseif(isset($array[$key])) + elseif(!$action && isset($array[$key])) { - // $action is not true (we want to remove) *and* key is set + // $action is false (we want to remove) *and* key is set unset($array[$key]); } - // else: Remove, but not set -> don't have to don anything } }