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 = [];