function ufdbguard_schedule()
{
    $sock = new sockets();
    $unix = new unix();
    $UfdbGuardSchedule = unserialize(base64_decode($sock->GET_INFO("UfdbGuardSchedule")));
    $cronfile = "/etc/cron.d/artica-ufdb-dbs";
    if (!is_numeric($UfdbGuardSchedule["EnableSchedule"])) {
        $UfdbGuardSchedule["EnableSchedule"] = 0;
    }
    if ($UfdbGuardSchedule["EnableSchedule"] == 0) {
        @unlink($cronfile);
        echo "Starting......: ufdbGuard recompile all databases is not scheduled\n";
        return;
    }
    $f[] = "MAILTO=\"\"";
    $f[] = "{$UfdbGuardSchedule["H"]} {$UfdbGuardSchedule["M"]} * * * root " . $unix->LOCATE_PHP5_BIN() . " " . __FILE__ . " --ufdbguard-recompile-dbs >/dev/null 2>&1";
    @file_put_contents($cronfile, @implode("\n", $f));
    echo "Starting......: ufdbGuard recompile all databases each day at {$UfdbGuardSchedule["H"]}:{$UfdbGuardSchedule["M"]}\n";
    events_ufdb_tail("ufdbGuard recompile all databases each day at {$UfdbGuardSchedule["H"]}:{$UfdbGuardSchedule["M"]}", __LINE__);
}
Example #2
0
function UFDBGUARD_COMPILE_SINGLE_DB($path)
{
    $timeStart = time();
    $OriginalDirename = dirname($path);
    $unix = new unix();
    $path = str_replace(".ufdb", "", $path);
    $pidpath = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . md5($path) . ".pid";
    $pid = @file_get_contents($pidpath);
    if ($unix->process_exists($pid)) {
        events_ufdb_tail("Check \"{$path}\"... Already process PID \"{$pid}\" running task has been aborted");
        return;
    }
    $category = null;
    $ufdbGenTable = $unix->find_program("ufdbGenTable");
    if (!is_file($ufdbGenTable)) {
        writelogs("ufdbGenTable no such binary", __FUNCTION__, __FILE__, __LINE__);
        return;
    }
    events_ufdb_tail("Check \"{$path}\"...", __LINE__);
    if (preg_match("#\\/var\\/lib\\/squidguard\\/(.+?)\\/(.+?)/(.+?)\$#", $path, $re)) {
        $category = $re[2];
        $domain_path = "/var/lib/squidguard/{$re[1]}/{$re[2]}/domains";
    }
    if ($category == null) {
        if (preg_match("#\\/var\\/lib\\/squidguard\\/(.+?)\\/domains#", $path, $re)) {
            $category = $re[1];
            $domain_path = "/var/lib/squidguard/{$re[1]}/domains";
        }
    }
    if (preg_match("#web-filter-plus\\/BL\\/(.+?)\\/domains#", $path, $re)) {
        $category = $re[1];
        $domain_path = "/var/lib/squidguard/web-filter-plus/BL/{$category}/domains";
    }
    if (preg_match("#blacklist-artica\\/(.+?)\\/(.+?)\\/domains#", $path, $re)) {
        events_ufdb_tail("find double category \"{$re[1]}-{$re[2]}\"...", __LINE__);
        $category = "{$re[1]}-{$re[2]}";
        $domain_path = "/var/lib/squidguard/blacklist-artica/{$re[1]}/{$re[2]}/domains";
    }
    if (preg_match("#blacklist-artica\\/sex\\/(.+?)\\/domains#", $path, $re)) {
        $category = $re[1];
        $domain_path = "/var/lib/squidguard/blacklist-artica/sex/{$category}/domains";
    }
    if ($category == null) {
        events_ufdb_tail("exec.squidguard.php:: \"{$path}\" cannot understand...");
    }
    events_ufdb_tail("exec.squidguard.php:: Found category \"{$category}\"", __LINE__);
    if (!is_file($path)) {
        events_ufdb_tail("exec.squidguard.php:{$category}: \"{$path}\" no such file, build it", __LINE__);
        @file_put_contents($domain_path, " ");
    }
    $category_compile = substr($category, 0, 15);
    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);
            }
        }
    }
    events_ufdb_tail("exec.squidguard.php:: category \"{$category}\" retranslated to \"{$category_compile}\"", __LINE__);
    if (is_file("{$domain_path}.ufdb")) {
        events_ufdb_tail("exec.squidguard.php:: removing \"{$domain_path}.ufdb\" ...");
        @unlink("{$domain_path}.ufdb");
    }
    if (!is_file($domain_path)) {
        events_ufdb_tail("exec.squidguard.php:: {$domain_path} no such file, create an empty one", __LINE__);
        @mkdir(dirname($domain_path), 0755, true);
        @file_put_contents($domain_path, "#");
    }
    $urlcmd = null;
    $d = " -d {$domain_path}";
    if (is_file("{$OriginalDirename}/urls")) {
        $urlssize = @filesize("{$OriginalDirename}/urls");
        events_ufdb_tail("exec.squidguard.php:: {$OriginalDirename}/urls {$urlssize} bytes...", __LINE__);
        if ($urlssize > 50) {
            $urlcmd = " -u {$OriginalDirename}/urls";
        }
    }
    $NICE = EXEC_NICE();
    $cmd = "{$NICE}{$ufdbGenTable} -n -D -W -t {$category_compile}{$d}{$urlcmd} 2>&1";
    events_ufdb_tail("exec.squidguard.php:{$category}:{$cmd}");
    $time = time();
    exec($cmd, $results);
    exec($cmd, $results);
    while (list($a, $b) = each($results)) {
        if (strpos($b, "is not added because it was already matched")) {
            continue;
        }
        if (strpos($b, "has optimised subdomains")) {
            continue;
        }
        events_ufdb_tail("exec.squidguard.php:{$category}:{$b}");
    }
    $tookrecompile = $unix->distanceOfTimeInWords($time, time());
    events_ufdb_tail("exec.squidguard.php:{$category_compile}: execution {$tookrecompile}", __LINE__);
    events_ufdb_tail("exec.squidguard.php:{$category}:done..");
    $user = GetSquidUser();
    $chown = $unix->find_program("chown");
    if (is_file($chown)) {
        events_ufdb_tail("exec.squidguard.php:{$category}:{$chown} -R {$user} {$OriginalDirename}");
        shell_exec("{$chown} -R {$user} {$OriginalDirename}/*");
        shell_exec("{$chown} -R {$user} /var/log/squid/*");
    }
    $sock = new sockets();
    $took = $unix->distanceOfTimeInWords($timeStart, time());
    $sock->TOP_NOTIFY("{$OriginalDirename} webfiltering database ({$category}) was recompiled took {$took} hard compilation took: {$tookrecompile}", "info");
}