<commit_before><?php
    $log = array();
    $setupVars = parse_ini_file("/etc/pihole/setupVars.conf");

    $hosts = file_exists("/etc/hosts") ? file("/etc/hosts") : array();

    // Check if pihole.log exists and is readable
    $logListName = checkfile("/var/log/pihole.log");
    $log = new \SplFileObject($logListName);

    // Check if preEventHorizon exists and is readable
    $gravityListName = checkfile("/etc/pihole/list.preEventHorizon");
    $gravity = new \SplFileObject($gravityListName);

    // whitelist.txt is optional and might not be there
    $whiteListFile = checkfile("/etc/pihole/whitelist.txt");
    $whitelist = new \SplFileObject($whiteListFile);

    // blacklist.txt is optional and might not be there
    $blackListFile = checkfile("/etc/pihole/blacklist.txt");
    $blacklist = new \SplFileObject($blackListFile);

    if(isset($setupVars["API_PRIVACY_MODE"]))
    {
        $privacyMode = $setupVars["API_PRIVACY_MODE"];
    }
    else
    {
        $privacyMode = false;
    }

    /*******   Public Members ********/
    function getSummaryData() {
        $domains_being_blocked = gravityCount();

        $dns_queries_today = countDnsQueries();

        $ads_blocked_today = countBlockedQueries();

        $ads_percentage_today = $dns_queries_today > 0 ? ($ads_blocked_today / $dns_queries_today * 100) : 0;

        return array(
            'domains_being_blocked' => $domains_being_blocked,
            'dns_queries_today' => $dns_queries_today,
            'ads_blocked_today' => $ads_blocked_today,
            'ads_percentage_today' => $ads_percentage_today,
        );
    }

    function getOverTimeData() {
        global $log;

        // Get log lines
        $dns_queries = getDnsQueries($log);

        // Get list of ad domains
        $gravity_domains = getGravity();

        // Bin log entries separated into Domains and Ads in 1 hour intervals
        list($domains_over_time, $ads_over_time) = overTime($dns_queries, $gravity_domains);

        // Align arrays
        alignTimeArrays($ads_over_time, $domains_over_time);

        // Provide a minimal valid array if there have are no blocked
        // queries at all. Otherwise the output of the API is inconsistent.
        if(count($ads_over_time) == 0)
        {
            $ads_over_time = [1 => 0];
        }

        return Array(
            'domains_over_time' => $domains_over_time,
            'ads_over_time' => $ads_over_time,
        );
    }

    function getOverTimeData10mins() {
        global $log;

        // Get log lines
        $dns_queries = getDnsQueries($log);

        // Get list of ad domains
        $gravity_domains = getGravity();

        // Bin log entries separated into Domains and Ads in 10 minute intervals
        list($domains_over_time, $ads_over_time) = overTime10mins($dns_queries, $gravity_domains);

        // Align arrays (in case there have been hours without ad queries)
        alignTimeArrays($ads_over_time, $domains_over_time);

        // Provide a minimal valid array if there have are no blocked
        // queries at all. Otherwise the output of the API is inconsistent.
        if(count($ads_over_time) == 0)
        {
            $ads_over_time = [1 => 0];
        }

        return Array(
            'domains_over_time' => $domains_over_time,
            'ads_over_time' => $ads_over_time,
        );
    }

    function getTopItems($argument) {
        global $log,$setupVars,$privacyMode;

        // Process log file
        $dns_domains = getDnsQueryDomains($log);
        // Get list of ad domains
        $gravity_domains = getGravity();

        // Exclude domains the user doesn't want to see
        if(isset($setupVars["API_EXCLUDE_DOMAINS"]))
        {
            excludeFromList($dns_domains, "API_EXCLUDE_DOMAINS");
        }

        // Sort array in descending order
        arsort($dns_domains);

        // Prepare arrays and counters for Top Items
        $topDomains = []; $domaincounter = 0;
        $topAds = []; $adcounter = 0;

        // Default number of Top Items to show is 10
        $qty = 10;

        // If argument is numeric, the user may want to
        // see a different number of entries
        if(is_numeric($argument))
        {
            $qty = intval($argument);
        }

        // Process sorted domain names
        foreach ($dns_domains as $key => $value) {
            if(isset($gravity_domains[$key]) && $adcounter < $qty)
            {
                // New entry for Top Ads
                $topAds[$key] = $value;
                $adcounter++;
            }<commit_msg>Exclude whitelisted domains from showing up in the Top Ads List<commit_after><?php
    $log = array();
    $setupVars = parse_ini_file("/etc/pihole/setupVars.conf");

    $hosts = file_exists("/etc/hosts") ? file("/etc/hosts") : array();

    // Check if pihole.log exists and is readable
    $logListName = checkfile("/var/log/pihole.log");
    $log = new \SplFileObject($logListName);

    // Check if preEventHorizon exists and is readable
    $gravityListName = checkfile("/etc/pihole/list.preEventHorizon");
    $gravity = new \SplFileObject($gravityListName);

    // whitelist.txt is optional and might not be there
    $whiteListFile = checkfile("/etc/pihole/whitelist.txt");
    $whitelist = new \SplFileObject($whiteListFile);

    // blacklist.txt is optional and might not be there
    $blackListFile = checkfile("/etc/pihole/blacklist.txt");
    $blacklist = new \SplFileObject($blackListFile);

    if(isset($setupVars["API_PRIVACY_MODE"]))
    {
        $privacyMode = $setupVars["API_PRIVACY_MODE"];
    }
    else
    {
        $privacyMode = false;
    }

    /*******   Public Members ********/
    function getSummaryData() {
        $domains_being_blocked = gravityCount();

        $dns_queries_today = countDnsQueries();

        $ads_blocked_today = countBlockedQueries();

        $ads_percentage_today = $dns_queries_today > 0 ? ($ads_blocked_today / $dns_queries_today * 100) : 0;

        return array(
            'domains_being_blocked' => $domains_being_blocked,
            'dns_queries_today' => $dns_queries_today,
            'ads_blocked_today' => $ads_blocked_today,
            'ads_percentage_today' => $ads_percentage_today,
        );
    }

    function getOverTimeData() {
        global $log;

        // Get log lines
        $dns_queries = getDnsQueries($log);

        // Get list of ad domains
        $gravity_domains = getGravity();

        // Bin log entries separated into Domains and Ads in 1 hour intervals
        list($domains_over_time, $ads_over_time) = overTime($dns_queries, $gravity_domains);

        // Align arrays
        alignTimeArrays($ads_over_time, $domains_over_time);

        // Provide a minimal valid array if there have are no blocked
        // queries at all. Otherwise the output of the API is inconsistent.
        if(count($ads_over_time) == 0)
        {
            $ads_over_time = [1 => 0];
        }

        return Array(
            'domains_over_time' => $domains_over_time,
            'ads_over_time' => $ads_over_time,
        );
    }

    function getOverTimeData10mins() {
        global $log;

        // Get log lines
        $dns_queries = getDnsQueries($log);

        // Get list of ad domains
        $gravity_domains = getGravity();

        // Bin log entries separated into Domains and Ads in 10 minute intervals
        list($domains_over_time, $ads_over_time) = overTime10mins($dns_queries, $gravity_domains);

        // Align arrays (in case there have been hours without ad queries)
        alignTimeArrays($ads_over_time, $domains_over_time);

        // Provide a minimal valid array if there have are no blocked
        // queries at all. Otherwise the output of the API is inconsistent.
        if(count($ads_over_time) == 0)
        {
            $ads_over_time = [1 => 0];
        }

        return Array(
            'domains_over_time' => $domains_over_time,
            'ads_over_time' => $ads_over_time,
        );
    }

    // Test if variable exists and is positive
    function ispositive(&$arg)
    {
        if(isset($arg))
        {
            if($arg > 0)
            {
                return true;
            }
            return false;
        }
        return false;
    }

    function getTopItems($argument) {
        global $log,$setupVars,$privacyMode;

        // Process log file
        $dns_domains = getDnsQueryDomains($log);
        // Get list of ad domains
        $gravity_domains = getGravity();

        // Exclude domains the user doesn't want to see
        if(isset($setupVars["API_EXCLUDE_DOMAINS"]))
        {
            excludeFromList($dns_domains, "API_EXCLUDE_DOMAINS");
        }

        // Sort array in descending order
        arsort($dns_domains);

        // Prepare arrays and counters for Top Items
        $topDomains = []; $domaincounter = 0;
        $topAds = []; $adcounter = 0;

        // Default number of Top Items to show is 10
        $qty = 10;

        // If argument is numeric, the user may want to
        // see a different number of entries
        if(is_numeric($argument))
        {
            $qty = intval($argument);
        }

        // Process sorted domain names
        foreach ($dns_domains as $key => $value) {
            if(ispositive($gravity_domains[$key]) && $adcounter < $qty)
            {
                // New entry for Top Ads
                $topAds[$key] = $value;
                $adcounter++;
            }
{'fraction_non_alphanumeric': 0.1109943241538785, 'fraction_numerical': 0.005150304813958377, 'mean_word_length': 2.1865371734762222, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4200346', 'n_tokens_mistral': 3021, 'n_tokens_neox': 2804, 'n_words': 696}