}
if ($GLOBALS["VERBOSE"]) {
    ini_set('display_errors', 1);
    ini_set('html_errors', 0);
    ini_set('display_errors', 1);
    ini_set('error_reporting', E_ALL);
}
if (posix_getuid() != 0) {
    die("Cannot be used in web server mode\n\n");
}
$unix = new unix();
if ($unix->process_number_me($argv) > 0) {
    die("Already executed\n\n");
}
if ($argv[1] == "--test-nas") {
    tests_nas(true);
    die;
}
if ($argv[1] == "--quotas") {
    CleanQuotas(true);
    die;
}
if ($argv[1] == "--remove-all") {
    removeall(true);
    die;
}
if ($argv[1] == "--numeric-members") {
    remove_numeric_members(true);
    die;
}
if ($argv[1] == "--backup") {
function exec_resources()
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $pid = $unix->get_pid_from_file($pidfile);
    if ($unix->process_exists($pid)) {
        $TIMEF = $unix->PROCCESS_TIME_MIN($pid);
        cyrus_admin_mysql(1, "An Artica task already running PID {$pid} since {$TIMEF}Mn", "Aborted", __FILE__, __LINE__);
        return;
    }
    @file_put_contents($pidfile, getmypid());
    LoadConfig();
    if ($GLOBALS["CyrusBackupNas"]["WEBDAV_ENABLE"] == 1) {
        exec_webdav();
    }
    if ($GLOBALS["CyrusBackupNas"]["NAS_ENABLE"] == 0) {
        return;
    }
    $TimeStart = time();
    if (!tests_nas()) {
        cyrus_admin_mysql(0, "Unable to backup cyrus-mailboxes", null, __FILE__, __LINE__);
        return;
    }
    $hostname = $unix->hostname_g();
    $GLOBALS["DIRBYTES"] = date("YmdH");
    $GLOBALS["MOUNTED_PATH__BACKUPDIR"] = "{$GLOBALS["MOUNT_POINT"]}/{$hostname}";
    $GLOBALS["MOUNTED_PATH_FINAL"] = "{$GLOBALS["MOUNT_POINT"]}/{$hostname}/{$GLOBALS["DIRBYTES"]}";
    if (!is_dir($GLOBALS["MOUNTED_PATH_FINAL"])) {
        @mkdir($GLOBALS["MOUNTED_PATH_FINAL"], 0755, true);
        if (!is_dir($GLOBALS["MOUNTED_PATH_FINAL"])) {
            cyrus_admin_mysql(0, "Unable to backup: Permission denied on NAS", null, __FILE__, __LINE__);
            return;
        }
    }
    backup_ldap();
    backup_cyrus();
    $report[] = "Started at : " . date("Y-m-d H:i:s", $TimeStart);
    $report[] = "End at : " . date("Y-m-d H:i:s");
    $report[] = "Duration: " . $unix->distanceOfTimeInWords($TimeStart, time());
    @file_put_contents("{$GLOBALS["MOUNTED_PATH_FINAL"]}/report.txt", @implode("\r\n", $report));
    remove_containers();
    killNas();
}
function analyze_all($aspid = false)
{
    $unix = new unix();
    $GLOBALS["NICE"] = $unix->EXEC_NICE();
    $sock = new sockets();
    $q = new mysql_squid_builder();
    $pidTime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    if ($GLOBALS["VERBOSE"]) {
        echo "pidTime={$pidTime}\n";
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "pidfile={$pidfile}\n";
    }
    if (!$aspid) {
        $pid = @file_get_contents($pidfile);
        if ($pid < 100) {
            $pid = null;
        }
        if ($unix->process_exists($pid, basename(__FILE__))) {
            $timepid = $unix->PROCCESS_TIME_MIN($pid);
            events("Already executed pid {$pid} since {$timepid}Mn");
            return;
        }
        @file_put_contents($pidfile, time());
    }
    if (!$GLOBALS["NOTIME"]) {
        $TimeExec = $unix->file_time_min($pidTime);
        if ($TimeExec < 30) {
            return;
        }
    }
    @unlink($pidTime);
    @file_put_contents($pidTime, time());
    $GLOBALS["nohup"] = $unix->find_program("nohup");
    $sock = new sockets();
    $unix = new unix();
    $SquidOldLogsNAS = unserialize(base64_decode($sock->GET_INFO("SquidOldLogsNAS")));
    $mountPoint = "/mnt/SquidImportLogs";
    $NasFolder = $SquidOldLogsNAS["folder"];
    $NasFolder = str_replace('\\', '/', $NasFolder);
    $NasFolder = str_replace('//', '/', $NasFolder);
    if (strpos($NasFolder, "/") > 0) {
        $f = explode("/", $NasFolder);
        unset($f[0]);
        $NasFolder = @implode("/", $f);
    }
    events("Ressources: {$mountPoint}/{$NasFolder}");
    $q = new mysql_squid_builder();
    if (!$q->TABLE_EXISTS("accesslogs_import")) {
        events("accesslogs_import no such table");
        return;
    }
    if ($q->COUNT_ROWS("accesslogs_import") == 0) {
        if ($GLOBALS["VERBOSE"]) {
            echo "accesslogs_import no row\n";
        }
        return;
    }
    $results = $q->QUERY_SQL("SELECT * FROM accesslogs_import WHERE status<3 ORDER BY zDate");
    if (mysql_num_rows($results) == 0) {
        killNas();
        if ($GLOBALS["VERBOSE"]) {
            echo "accesslogs_import all done\n";
        }
        return;
    }
    if (!tests_nas()) {
        events("NAS is unavailable");
        return;
    }
    while ($ligne = mysql_fetch_assoc($results)) {
        $filename = $ligne["filename"];
        $targetFile = "{$mountPoint}/{$NasFolder}/{$filename}";
        events("Scanning {$targetFile}");
        if ($GLOBALS["VERBOSE"]) {
            echo "Scanning {$targetFile}\n";
        }
        $targetFile = str_replace("//", "/", $targetFile);
        analyze_single_file($filename);
        if (system_is_overloaded()) {
            killNas();
            return;
        }
    }
    $prefixcmd = $GLOBALS["nohup"] . " {$GLOBALS["NICE"]}" . $unix->LOCATE_PHP5_BIN() . " ";
    shell_exec("{$prefixcmd} " . dirname(__FILE__) . "/exec.squid.stats.hours.php --nocheck >/dev/null 2>&1 &");
    scan(true);
    killNas();
}