Exemple #1
0
function caches_generate()
{
    $unix = new unix();
    $php5 = $unix->LOCATE_PHP5_BIN();
    $pidffile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $pid = $unix->get_pid_from_file($pidffile);
    if ($unix->process_exists($pid)) {
        events_squid_caches("Starting......: " . date("H:i:s") . " [SMP] Aready running pid {$pid}", __FUNCTION__, __LINE__);
        return;
    }
    $squidbin = $unix->LOCATE_SQUID_BIN();
    events_squid_caches("Starting......: " . date("H:i:s") . " [SMP] Using binary `{$squidbin}`", __FUNCTION__, __LINE__);
    if (!is_file($squidbin)) {
        events_squid_caches("Starting......: " . date("H:i:s") . " [SMP] unable to stat squid...", __FUNCTION__, __LINE__);
        return;
    }
    @file_put_contents($pidffile, getmypid());
    $uuid = $unix->GetUniqueID();
    events_squid_caches("Starting......: " . date("H:i:s") . " [SMP] uuid={$uuid}", __FUNCTION__, __LINE__);
    $q = new mysql();
    $results = $q->QUERY_SQL("SELECT * FROM squid_caches32 WHERE enabled=1 AND uuid='{$uuid}' AND ToDelete=0 AND Building=0", "artica_backup");
    if (mysql_num_rows($results) == 0) {
        events_squid_caches("Starting......: " . date("H:i:s") . " [SMP] No cache to build..", __FUNCTION__, __LINE__);
        caches_delete();
        return;
    }
    $stopstart = false;
    $conffile = "/tmp/squid-" . time() . ".conf";
    $f[] = "cache_effective_user squid";
    $f[] = "pid_filename\t/var/run/squid-temp.pid";
    $f[] = "http_port 65478";
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $cache_directory = $ligne["cache_directory"];
        $cacheid = $ligne["cacheid"];
        $cache_size = $ligne["size"] * 1000;
        $cache_dir_level1 = $ligne["cache_dir_level1"];
        $cache_dir_level2 = $ligne["cache_dir_level2"];
        $cache_maxsize = $ligne["cache_maxsize"];
        $cache_type = $ligne["cache_type"];
        $f[] = "cache_dir\t{$cache_type} {$cache_directory} {$cache_size} {$cache_dir_level1} {$cache_dir_level2}";
        if (!is_dir($cache_directory)) {
            $stopstart = true;
            events_squid_caches("Starting......: " . date("H:i:s") . " [SMP] creating {$cache_directory}", __FUNCTION__, __LINE__);
            @mkdir($cache_directory, 0755, true);
            @chown($cache_directory, "squid");
            @chgrp($cache_directory, "squid");
        }
        events_squid_caches("Starting......: " . date("H:i:s") . " [SMP] Stamp cache {$cacheid} to be build", __FUNCTION__, __LINE__);
        $cacheid_array[$cacheid] = true;
        $q->QUERY_SQL("UPDATE squid_caches32 SET Building=1 WHERE cacheid='{$cacheid}'", "artica_backup");
    }
    $su = $unix->find_program("su");
    echo "Starting......: " . date("H:i:s") . " [SMP] writing config {$conffile}\n";
    @file_put_contents($conffile, @implode("\n", $f));
    $cmd = "{$su} -c \"{$squidbin} -f {$conffile} -z\" squid 2>&1";
    events_squid_caches("Starting......: " . date("H:i:s") . " [SMP] Launch {$cmd}", __FUNCTION__, __LINE__);
    exec("{$cmd}", $results);
    while (list($a, $b) = each($results)) {
        events_squid_caches("Starting......: " . date("H:i:s") . " [SMP] {$b}", __FUNCTION__, __LINE__);
    }
    while (list($cacheid, $val) = each($cacheid_array)) {
        events_squid_caches("Starting......: " . date("H:i:s") . " [SMP] Stamp cache {$cacheid} to be builded", __FUNCTION__, __LINE__);
        $q->QUERY_SQL("UPDATE squid_caches32 SET Building=2 WHERE cacheid='{$cacheid}'", "artica_backup");
    }
    events_squid_caches("Starting......: " . date("H:i:s") . " [SMP] reconfiguring the proxy cache", __FUNCTION__, __LINE__);
    $results = array();
    exec("{$php5} /usr/share/artica-postfix/exec.squid.php --build --force --nocaches 2>&1", $results);
    while (list($a, $b) = each($results)) {
        events_squid_caches("Starting......: " . date("H:i:s") . " [SMP] {$b}", __FUNCTION__, __LINE__);
    }
    events_squid_caches("Starting......: " . date("H:i:s") . " [SMP] restarting the proxy cache", __FUNCTION__, __LINE__);
    $results = array();
    exec("{$php5} /usr/share/artica-postfix/exec.squid.watchdog.php --restart --force --script=" . basename(__FILE__) . " 2>&1", $results);
    while (list($a, $b) = each($results)) {
        events_squid_caches("Starting......: " . date("H:i:s") . " [SMP] {$b}", __FUNCTION__, __LINE__);
    }
    caches_delete();
}
    exit;
}
if (isset($_POST["pattern-id"])) {
    rule_save();
    exit;
}
if (isset($_GET["caches"])) {
    caches_section();
    exit;
}
if (isset($_GET["search-caches"])) {
    caches_search();
    exit;
}
if (isset($_POST["caches-delete"])) {
    caches_delete();
    exit;
}
if (isset($_GET["cache-js"])) {
    caches_js();
    exit;
}
if (isset($_GET["cache-id"])) {
    caches_popup();
    exit;
}
if (isset($_POST["cache-id"])) {
    caches_save();
    exit;
}
if (isset($_GET["events"])) {