function UFDBGUARD_COMPILE_DB()
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/UFDBGUARD_COMPILE_DB.pid";
    if ($unix->process_exists(@file_get_contents($pidfile))) {
        echo "Process already exists PID: " . @file_get_contents($pidfile) . "\n";
        return;
    }
    @file_put_contents($pidfile, getmypid());
    $ufdbGenTable = $unix->find_program("ufdbGenTable");
    $datas = explode("\n", @file_get_contents("/etc/squid/squidGuard.conf"));
    if (strlen($ufdbGenTable) < 5) {
        echo "ufdbGenTable no such file\n";
        return;
    }
    $md5db = unserialize(@file_get_contents("/etc/artica-postfix/ufdbGenTableMD5"));
    $count = 0;
    while (list($a, $b) = each($datas)) {
        if (preg_match("#domainlist.+?(.+)\\/domains#", $b, $re)) {
            $f["/var/lib/squidguard/{$re[1]}"] = "/var/lib/squidguard/{$re[1]}";
        }
    }
    if (!is_array($datas)) {
        echo "No databases set\n";
        return;
    }
    while (list($directory, $b) = each($f)) {
        $mustrun = false;
        if (preg_match("#.+?\\/([a-zA-Z0-9\\-\\_]+)\$#", $directory, $re)) {
            $category = $re[1];
            $category = substr($category, 0, 15);
            if ($GLOBALS["VERBOSE"]) {
                echo "Checking {$category}\n";
            }
        }
        // ufdbGenTable -n -D -W -t adult -d /var/lib/squidguard/adult/domains -u /var/lib/squidguard/adult/urls
        if (is_file("{$directory}/domains")) {
            $md5 = FileMD5("{$directory}/domains");
            if ($md5 != $md5db["{$directory}/domains"]) {
                $mustrun = true;
                $md5db["{$directory}/domains"] = $md5;
                $dbb[] = "{$directory}/domains";
            } else {
                if ($GLOBALS["VERBOSE"]) {
                    echo "{$md5} is the same, skip {$directory}/domains\n";
                }
            }
            $d = " -d {$directory}/domains";
        } else {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$directory}/domains no such file\n";
            }
        }
        if (is_file("{$directory}/urls")) {
            $md5 = FileMD5("{$directory}/urls");
            if ($md5 != $md5db["{$directory}/urls"]) {
                $mustrun = true;
                $md5db["{$directory}/urls"] = $md5;
                $dbb[] = "{$directory}/urls";
            }
            $u = " -u {$directory}/urls";
        }
        if (!is_file("{$directory}/domains.ufdb")) {
            $mustrun = true;
            $dbb[] = "{$directory}/*";
        }
        if ($mustrun) {
            $dbcount = $dbcount + 1;
            $cmd = "{$ufdbGenTable} -n -D -W -t {$category}{$d}{$u}";
            echo $cmd . "\n";
            shell_exec($cmd);
        }
        $u = null;
        $d = null;
        $md5 = null;
    }
    @file_put_contents("/etc/artica-postfix/ufdbGenTableMD5", serialize($md5db));
    $user = GetSquidUser();
    $chown = $unix->find_program($chown);
    if (is_file($chown)) {
        shell_exec("{$chown} -R {$user} /var/lib/squidguard/*");
        shell_exec("{$chown} -R {$user} /var/log/squid/*");
    }
    if ($dbcount > 0) {
        send_email_events("Maintenance on Web Proxy urls Databases: {$dbcount} database(s)", @implode("\n", $dbb) . "\n", "system");
    }
}
function UFDBGUARD_COMPILE_DB()
{
    $tstart = time();
    $sock = new sockets();
    $EnableRemoteStatisticsAppliance = $sock->GET_INFO("EnableRemoteStatisticsAppliance");
    if (!is_numeric($EnableRemoteStatisticsAppliance)) {
        $EnableRemoteStatisticsAppliance = 0;
    }
    if ($EnableRemoteStatisticsAppliance == 1) {
        return;
    }
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/UFDBGUARD_COMPILE_DB.pid";
    if ($unix->process_exists(@file_get_contents($pidfile))) {
        echo "Process already exists PID: " . @file_get_contents($pidfile) . "\n";
        return;
    }
    @file_put_contents($pidfile, getmypid());
    $ufdbGenTable = $unix->find_program("ufdbGenTable");
    $datas = explode("\n", @file_get_contents("/etc/squid3/ufdbGuard.conf"));
    if (strlen($ufdbGenTable) < 5) {
        echo "ufdbGenTable no such file\n";
        return;
    }
    $md5db = unserialize(@file_get_contents("/etc/artica-postfix/ufdbGenTableMD5"));
    $count = 0;
    while (list($a, $b) = each($datas)) {
        if (preg_match('#domainlist\\s+"(.+)\\/domains#', $b, $re)) {
            $f["/var/lib/squidguard/{$re[1]}"] = "/var/lib/squidguard/{$re[1]}";
        }
    }
    if (!is_array($datas)) {
        echo "No databases set\n";
        return;
    }
    while (list($directory, $b) = each($f)) {
        $mustrun = false;
        if (preg_match("#.+?\\/([a-zA-Z0-9\\-\\_]+)\$#", $directory, $re)) {
            $category = $re[1];
            $category = substr($category, 0, 15);
            if ($GLOBALS["VERBOSE"]) {
                echo "Checking {$category}\n";
            }
        }
        // ufdbGenTable -n -D -W -t adult -d /var/lib/squidguard/adult/domains -u /var/lib/squidguard/adult/urls
        if (is_file("{$directory}/domains")) {
            $md5 = FileMD5("{$directory}/domains");
            if ($md5 != $md5db["{$directory}/domains"]) {
                $mustrun = true;
                $md5db["{$directory}/domains"] = $md5;
                $dbb[] = "{$directory}/domains";
            } else {
                if ($GLOBALS["VERBOSE"]) {
                    echo "{$md5} is the same, skip {$directory}/domains\n";
                }
            }
            $d = " -d {$directory}/domains";
        } else {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$directory}/domains no such file\n";
            }
        }
        if (is_file("{$directory}/urls")) {
            $md5 = FileMD5("{$directory}/urls");
            if ($md5 != $md5db["{$directory}/urls"]) {
                $mustrun = true;
                $md5db["{$directory}/urls"] = $md5;
                $dbb[] = "{$directory}/urls";
            }
            $u = " -u {$directory}/urls";
        }
        if (!is_file("{$directory}/domains.ufdb")) {
            $mustrun = true;
            $dbb[] = "{$directory}/*";
        }
        if ($mustrun) {
            $dbcount = $dbcount + 1;
            $category_compile = $category;
            if (strlen($category_compile) > 15) {
                $category_compile = str_replace("recreation_", "recre_", $category_compile);
                $category_compile = str_replace("automobile_", "auto_", $category_compile);
                $category_compile = str_replace("finance_", "fin_", $category_compile);
                if (strlen($category_compile) > 15) {
                    $category_compile = str_replace("_", "", $category_compile);
                    if (strlen($category_compile) > 15) {
                        $category_compile = substr($category_compile, strlen($category_compile) - 15, 15);
                    }
                }
            }
            $cmd = "{$ufdbGenTable} -n -D -W -t {$category_compile}{$d}{$u}";
            echo $cmd . "\n";
            $t = time();
            shell_exec($cmd);
            $took = $unix->distanceOfTimeInWords($t, time(), true);
            ufdbguard_admin_events("Compiled {$category_compile} in {$directory} took {$took}", @implode("\n", $dbb) . "\n", __FUNCTION__, __FILE__, __LINE__, "ufdb-compile");
            if (function_exists("system_is_overloaded")) {
                if (system_is_overloaded(__FILE__)) {
                    ufdbguard_admin_events("Overloaded system after {$dbcount} compilations, oberting task...", @implode("\n", $dbb) . "\n", __FUNCTION__, __FILE__, __LINE__, "ufdb-compile");
                    return;
                }
            }
        }
        $u = null;
        $d = null;
        $md5 = null;
    }
    @file_put_contents("/etc/artica-postfix/ufdbGenTableMD5", serialize($md5db));
    $user = GetSquidUser();
    $chown = $unix->find_program($chown);
    if (is_file($chown)) {
        shell_exec("{$chown} -R {$user} /var/lib/squidguard/*");
        shell_exec("{$chown} -R {$user} /var/log/squid/*");
    }
    if ($dbcount > 0) {
        $took = $unix->distanceOfTimeInWords($tstart, time(), true);
        ufdbguard_admin_events("Maintenance on Web Proxy urls Databases: {$dbcount} database(s) took {$took}", @implode("\n", $dbb) . "\n", __FUNCTION__, __FILE__, __LINE__, "ufdb-compile");
    }
}