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 7060dc4e..43ba51e6 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,12 @@ } function getAllQueries($orderBy) { - global $log,$gravity,$showBlocked,$showPermitted; + global $log,$showBlocked,$showPermitted; $allQueries = array("data" => array()); $dns_queries = getDnsQueriesAll($log); - $gravity_domains = getGravityDomains($gravity); + + // Create empty array for gravity + $gravity_domains = getGravity(); foreach ($dns_queries as $query) { $time = date_create(substr($query, 0, 16)); @@ -265,16 +269,41 @@ return $lines; } - function getGravityDomains($gravity){ - $gravity->rewind(); - $lines=[]; - foreach ($gravity as $line) { - // Strip newline (and possibly carriage return) from end of string - // using rtrim() - $lines[rtrim($line)] = true; + function getDomains($file, &$array, $action){ + $file->rewind(); + foreach ($file as $line) { + // 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) + { + // $action is true (we want to add) *and* key is not empty + $array[$key] = true; + } + elseif(!$action && isset($array[$key])) + { + // $action is false (we want to remove) *and* key is set + unset($array[$key]); + } } + } - 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) {