function CleanRotatedFiles()
{
    include_once dirname(__FILE__) . "/ressources/class.mysql.syslog.inc";
    $unix = new unix();
    $sock = new sockets();
    $LogRotateCompress = 1;
    $LogRotatePath = $sock->GET_INFO("LogRotatePath");
    $ApacheLogRotate = $sock->GET_INFO("ApacheLogRotate");
    $cpbin = $unix->find_program("cp");
    $php5 = $unix->LOCATE_PHP5_BIN();
    $tmpdir = $unix->TEMP_DIR();
    if (!is_numeric($ApacheLogRotate)) {
        $ApacheLogRotate = 1;
    }
    if (!is_numeric($LogRotatePath)) {
        $LogRotatePath = "/home/logrotate";
    }
    $DirsToScan["/var/log"] = true;
    $DirsToScan["/var/log/apache2"] = true;
    $DirsToScan["/var/log/lighttpd"] = true;
    $DirsToScan["/var/log/ejabberd"] = true;
    $apache2 = $unix->dirdir("/var/log/apache2");
    while (list($WorkingDir, $ligne) = each($apache2)) {
        $DirsToScan[$WorkingDir] = true;
    }
    $q = new mysql_storelogs();
    while (list($WorkingDir, $ligne) = each($DirsToScan)) {
        $RotateSquid = false;
        if ($WorkingDir == "/var/log/squid") {
            continue;
        }
        $table = $unix->DirFiles($WorkingDir, "(\\.|-)[0-9]+.*?\$");
        $compressed["gz"] = true;
        $compressed["bz"] = true;
        $compressed["bz2"] = true;
        while (list($filename, $ligne) = each($table)) {
            $path = "{$WorkingDir}/{$filename}";
            if ($unix->file_time_min($path) < 1440) {
                continue;
            }
            $filedate = date('Y-m-d H:i:s', filemtime($path));
            $q->events("Injecting {$path} {$filedate}");
            if (!$q->ROTATE_TOMYSQL($path, $filedate)) {
                continue;
            }
        }
    }
}
function BackupToNas($directory)
{
    if (!is_dir($directory)) {
        return;
    }
    $syslog = new mysql_storelogs();
    $sock = new sockets();
    $users = new usersMenus();
    $unix = new unix();
    $myHostname = $unix->hostname_g();
    $DirSuffix = basename($directory);
    $mount = new mount("/var/log/artica-postfix/logrotate.debug");
    $BackupSquidLogsNASIpaddr = $sock->GET_INFO("BackupSquidLogsNASIpaddr");
    $BackupSquidLogsNASFolder = $sock->GET_INFO("BackupSquidLogsNASFolder");
    $BackupSquidLogsNASUser = $sock->GET_INFO("BackupSquidLogsNASUser");
    $BackupSquidLogsNASPassword = $sock->GET_INFO("BackupSquidLogsNASPassword");
    $BackupSquidLogsNASRetry = $sock->GET_INFO("BackupSquidLogsNASRetry");
    if (!is_numeric($BackupSquidLogsNASRetry)) {
        $BackupSquidLogsNASRetry = 0;
    }
    $mount = new mount("/var/log/artica-postfix/logrotate.debug");
    $BackupSquidLogsNASIpaddr = $sock->GET_INFO("BackupSquidLogsNASIpaddr");
    $BackupSquidLogsNASFolder = $sock->GET_INFO("BackupSquidLogsNASFolder");
    $BackupSquidLogsNASUser = $sock->GET_INFO("BackupSquidLogsNASUser");
    $BackupSquidLogsNASPassword = $sock->GET_INFO("BackupSquidLogsNASPassword");
    $BackupSquidLogsNASRetry = $sock->GET_INFO("BackupSquidLogsNASRetry");
    if (!is_numeric($BackupSquidLogsNASRetry)) {
        $BackupSquidLogsNASRetry = 0;
    }
    $mv = $unix->find_program("mv");
    if ($BackupSquidLogsNASIpaddr == null) {
        $this->events("Backup via NAS is disabled, skip", __FUNCTION__, __FILE__, __LINE__, "logrotate");
        return false;
    }
    $mountPoint = "/mnt/BackupSquidLogsUseNas";
    if (!$mount->smb_mount($mountPoint, $BackupSquidLogsNASIpaddr, $BackupSquidLogsNASUser, $BackupSquidLogsNASPassword, $BackupSquidLogsNASFolder)) {
        $syslog->events("Unable to connect to NAS storage system (1): {$BackupSquidLogsNASUser}@{$BackupSquidLogsNASIpaddr}", __FUNCTION__, __FILE__, __LINE__, "logrotate");
        if ($BackupSquidLogsNASRetry == 0) {
            return;
        }
        sleep(3);
        $mount = new mount("/var/log/artica-postfix/logrotate.debug");
        if (!$mount->smb_mount($mountPoint, $BackupSquidLogsNASIpaddr, $BackupSquidLogsNASUser, $BackupSquidLogsNASPassword, $BackupSquidLogsNASFolder)) {
            $syslog->events("Unable to connect to NAS storage system (1): {$BackupSquidLogsNASUser}@{$BackupSquidLogsNASIpaddr}", __FUNCTION__, __FILE__, __LINE__, "logrotate");
            return;
        }
    }
    $syslog->events("Hostname={$myHostname} Suffix = {$DirSuffix} {$BackupSquidLogsNASIpaddr}/{$BackupSquidLogsNASFolder}", __FUNCTION__, __LINE__);
    $BackupMaxDaysDir = "{$mountPoint}/artica-backup-syslog";
    @mkdir("{$BackupMaxDaysDir}", 0755, true);
    if (!is_dir($BackupMaxDaysDir)) {
        $syslog->events("Fatal {$BackupMaxDaysDir} permission denied", __FUNCTION__, __LINE__);
        if ($GLOBALS["VERBOSE"]) {
            echo "Fatal {$BackupMaxDaysDir} permission denied\n";
        }
        squid_admin_mysql(0, "SYSLOG: FATAL {$BackupMaxDaysDir} permission denied", null, __FILE__, __LINE__);
        $mount->umount($mountPoint);
        return false;
    }
    $t = time();
    @file_put_contents("{$BackupMaxDaysDir}/{$t}", time());
    if (!is_file("{$BackupMaxDaysDir}/{$t}")) {
        $syslog->events("Fatal {$BackupMaxDaysDir} permission denied ({$BackupMaxDaysDir}/{$t}) test failed", __FUNCTION__, __LINE__);
        squid_admin_mysql(0, "SYSLOG: FATAL {$BackupMaxDaysDir} permission denied", null, __FILE__, __LINE__);
        $mount->umount($mountPoint);
        return false;
    }
    @unlink("{$BackupMaxDaysDir}/{$t}");
    exec("{$mv} --force {$directory} --target-directory={$BackupMaxDaysDir}/ 2>&1", $results);
    while (list($index, $line) = each($results)) {
        $syslog->events("{$line}", __FUNCTION__, __LINE__);
    }
    analyze_destination_directory($BackupMaxDaysDir . "/proxy");
    $mount->umount($mountPoint);
    return true;
}
function CleanPercent($BackupMaxDaysDir, $TOTAL_AVAILABLE)
{
    $unix = new unix();
    $q = new mysql_storelogs();
    $ARRAY = array();
    $DIRSIZE = $unix->DIRSIZE_BYTES($BackupMaxDaysDir);
    $q->events("Remove files in {$BackupMaxDaysDir} " . FormatBytes($DIRSIZE / 1024) . "/" . FormatBytes($TOTAL_AVAILABLE / 1024), __FUNCTION__, __LINE__);
    $q2 = new mysql();
    $results = $q2->QUERY_SQL("SELECT * FROM backuped_logs ORDER BY zDate LIMIT 0,50", "artica_backup");
    if (!$q2->ok) {
        squid_admin_mysql(0, "MySQL error", $q2->mysql_error, __FILE__, __LINE__);
        return;
    }
    while ($ligne = mysql_fetch_assoc($results)) {
        if (preg_match("#^\\/mnt\\/", $ligne["path"])) {
            continue;
        }
        $filepath = $ligne["path"];
        if (!is_file($filepath)) {
            continue;
        }
        $sizeBytes = @filesize($filepath);
        @unlink($filepath);
        $DIRSIZE = $DIRSIZE - $sizeBytes;
        $q->events("Remove {$filepath} (" . FormatBytes($sizeBytes / 1024) . ") New DIR SIZE=" . FormatBytes($DIRSIZE / 1024) . "/" . FormatBytes($TOTAL_AVAILABLE / 1024), __FUNCTION__, __LINE__);
        if ($DIRSIZE < $TOTAL_AVAILABLE) {
            break;
        }
    }
}
function rotate_client($uuid)
{
    $unix = new unix();
    $workingdir = "/usr/share/artica-postfix/ressources/conf/meta/hosts/uploaded/{$uuid}/syslog";
    $syslog = new mysql_storelogs();
    $files = $unix->DirFiles($workingdir);
    while (list($basepath, $none) = each($files)) {
        $syslog->events("META: Rotate {$basepath}", __FUNCTION__, __LINE__);
        $syslog->ROTATE_ACCESS_TOMYSQL("{$workingdir}/{$basepath}");
    }
}
function CleanMysqlDatabase($PURGE_ALL = false)
{
    $filter = null;
    $users = new usersMenus();
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $timefile = "/etc/artica-postfix/pids/logrotate." . __FUNCTION__ . ".time";
    $pid = @file_get_contents("{$pidfile}");
    if ($unix->process_exists($pid, basename(__FILE__))) {
        system_admin_events("Already executed PID {$pid}", __FUNCTION__, __FILE__, __LINE__, "logrotate");
        die;
    }
    @file_put_contents($pidfile, getmypid());
    $time = $unix->file_time_min($timefile);
    if (!$PURGE_ALL) {
        if (!$GLOBALS["FORCE"]) {
            if ($time < 15) {
                events("No less than 15mn or delete {$timefile} file to force...");
                system_admin_events("No less than 15mn or delete {$timefile} file", __FUNCTION__, __FILE__, __LINE__, "logrotate");
                return;
            }
        }
    }
    @unlink($timefile);
    @file_put_contents($timefile, time());
    $sock = new sockets();
    $MySQLSyslogType = $sock->GET_INFO("MySQLSyslogType");
    $EnableSyslogDB = $sock->GET_INFO("EnableSyslogDB");
    if (!is_numeric($EnableSyslogDB)) {
        $EnableSyslogDB = 0;
    }
    if (!is_numeric($MySQLSyslogType)) {
        $MySQLSyslogType = 1;
    }
    if ($MySQLSyslogType == 0) {
        $MySQLSyslogType = 4;
    }
    $LogRotatePath = $sock->GET_INFO("LogRotatePath");
    if ($LogRotatePath == null) {
        $LogRotatePath = "/home/logrotate";
    }
    $TuningParameters = unserialize(base64_decode($sock->GET_INFO("MySQLSyslogParams")));
    if ($EnableSyslogDB == 1) {
        if ($MySQLSyslogType == 2) {
            events("Is a client of remote MySQL server , aborting");
            if ($GLOBALS["VERBOSE"]) {
                echo "Is a client of remote MySQL server , aborting\n";
            }
            return;
        }
    }
    $LogRotateCompress = 1;
    $LogRotatePath = $sock->GET_INFO("LogRotatePath");
    $SystemLogsPath = $sock->GET_INFO("SystemLogsPath");
    $BackupMaxDays = $sock->GET_INFO("BackupMaxDays");
    $BackupMaxDaysDir = $sock->GET_INFO("BackupMaxDaysDir");
    $BackupSquidLogsUseNas = $sock->GET_INFO("BackupSquidLogsUseNas");
    if ($SystemLogsPath == null) {
        $SystemLogsPath = "/var/log";
    }
    $MySQLSyslogType = $sock->GET_INFO("MySQLSyslogType");
    $EnableSyslogDB = $sock->GET_INFO("EnableSyslogDB");
    if (!is_numeric($EnableSyslogDB)) {
        $EnableSyslogDB = 0;
    }
    if (!is_numeric($MySQLSyslogType)) {
        $MySQLSyslogType = 1;
    }
    if (!is_numeric($BackupSquidLogsUseNas)) {
        $BackupSquidLogsUseNas = 0;
    }
    if ($EnableSyslogDB == 1) {
        if ($MySQLSyslogType != 1) {
            return;
        }
    }
    if (!is_numeric($BackupMaxDays)) {
        $BackupMaxDays = 30;
    }
    if ($LogRotatePath == null) {
        $LogRotatePath = "/home/logrotate";
    }
    if ($BackupMaxDaysDir == null) {
        $BackupMaxDaysDir = "/home/logrotate_backup";
    }
    $mount = new mount("/var/log/artica-postfix/logrotate.debug");
    if ($BackupSquidLogsUseNas == 1) {
        $BackupSquidLogsNASIpaddr = $sock->GET_INFO("BackupSquidLogsNASIpaddr");
        $BackupSquidLogsNASFolder = $sock->GET_INFO("BackupSquidLogsNASFolder");
        $BackupSquidLogsNASUser = $sock->GET_INFO("BackupSquidLogsNASUser");
        $BackupSquidLogsNASPassword = $sock->GET_INFO("BackupSquidLogsNASPassword");
        $BackupSquidLogsNASRetry = $sock->GET_INFO("BackupSquidLogsNASRetry");
        if (!is_numeric($BackupSquidLogsNASRetry)) {
            $BackupSquidLogsNASRetry = 0;
        }
        $mountPoint = "/mnt/BackupSquidLogsUseNas";
        if (!$mount->smb_mount($mountPoint, $BackupSquidLogsNASIpaddr, $BackupSquidLogsNASUser, $BackupSquidLogsNASPassword, $BackupSquidLogsNASFolder)) {
            events("Unable to connect to NAS storage system (1): {$BackupSquidLogsNASUser}@{$BackupSquidLogsNASIpaddr}");
            if ($BackupSquidLogsNASRetry == 0) {
                return;
            }
            sleep(3);
            $mount = new mount("/var/log/artica-postfix/logrotate.debug");
            if (!$mount->smb_mount($mountPoint, $BackupSquidLogsNASIpaddr, $BackupSquidLogsNASUser, $BackupSquidLogsNASPassword, $BackupSquidLogsNASFolder)) {
                events("Unable to connect to NAS storage system (2): {$BackupSquidLogsNASUser}@{$BackupSquidLogsNASIpaddr}");
                return;
            }
        }
        $BackupMaxDaysDir = "{$mountPoint}/artica-backup-syslog/{$users->hostname}";
    }
    @mkdir("{$BackupMaxDaysDir}", 0755, true);
    if (!is_dir($BackupMaxDaysDir)) {
        if ($GLOBALS["VERBOSE"]) {
            echo "Fatal {$BackupMaxDaysDir} permission denied\n";
        }
        events("Fatal {$BackupMaxDaysDir} permission denied");
        squid_admin_notifs("SYSLOG: FATAL {$BackupMaxDaysDir} permission denied", __FUNCTION__, __FILE__, __LINE__);
        system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__, "logrotate");
        if ($BackupSquidLogsUseNas == 1) {
            $mount->umount($mountPoint);
        }
        return false;
    }
    $t = time();
    @file_put_contents("{$BackupMaxDaysDir}/{$t}", time());
    if (!is_file("{$BackupMaxDaysDir}/{$t}")) {
        events("Fatal {$BackupMaxDaysDir} permission denied");
        if ($GLOBALS["VERBOSE"]) {
            echo "Fatal {$BackupMaxDaysDir} permission denied\n";
        }
        squid_admin_notifs("SYSLOG: FATAL {$BackupMaxDaysDir} permission denied", __FUNCTION__, __FILE__, __LINE__);
        system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__, "logrotate");
        if ($BackupSquidLogsUseNas == 1) {
            $mount->umount($mountPoint);
        }
        return false;
    }
    @unlink("{$BackupMaxDaysDir}/{$t}");
    if ($BackupSquidLogsUseNas == 1) {
        if (is_dir("/home/logrotate_backup")) {
            $files = $unix->DirFiles("/home/logrotate_backup");
            events("Scanning the old storage systems.. " . count($files) . " file(s)");
            while (list($basename, $none) = each($files)) {
                $filepath = "/home/logrotate_backup/{$basename}";
                if ($GLOBALS["VERBOSE"]) {
                    echo "Checking \"{$filepath}\"\n";
                }
                $size = @filesize($filepath);
                if ($size < 20) {
                    events("Removing {$filepath}");
                    @unlink($filepath);
                    continue;
                }
                if (!@copy($filepath, "{$BackupMaxDaysDir}/{$basename}")) {
                    events("copy Failed {$filepath} to \"{$BackupMaxDaysDir}/{$basename}\" permission denied...");
                    continue;
                }
                events("Move {$filepath} to {$BackupSquidLogsNASIpaddr} success...");
                @unlink($filepath);
            }
        }
    }
    if ($PURGE_ALL == false) {
        $filter = "WHERE filetime<DATE_SUB(NOW(),INTERVAL {$BackupMaxDays} DAY)";
    }
    if ($EnableSyslogDB == 1) {
        $q = new mysql_storelogs();
        $sql = "SELECT `filename`,`hostname`,`storeid` FROM `files_info` {$filter}";
        if ($GLOBALS["VERBOSE"]) {
            echo "{$sql}\n";
        }
        $results = $q->QUERY_SQL($sql);
        if (!$q->ok) {
            system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__);
            return;
        }
        $Count = mysql_num_rows($results);
        $c = 0;
        while ($ligne = mysql_fetch_assoc($results)) {
            $c++;
            if ($GLOBALS["VERBOSE"]) {
                echo "{$c}/{$Count} ******** {$ligne["filename"]} {$ligne["storeid"]} *********\n";
            }
            if (!$q->ExtractFile("{$BackupMaxDaysDir}/{$ligne["hostname"]}.{$ligne["filename"]}", $ligne["storeid"])) {
                if ($GLOBALS["VERBOSE"]) {
                    echo "{$c}/{$Count} ******** {$ligne["filename"]} ExtractFile() = FALSE !!! *********\n";
                }
                continue;
            }
            if ($GLOBALS["VERBOSE"]) {
                echo "{$c}/{$Count} DelteItem({$ligne["storeid"]}) *********\n";
            }
            $q->DelteItem($ligne["storeid"]);
            if ($GLOBALS["VERBOSE"]) {
                echo "********* EVENTS NOW --->\n";
            }
            $q->events("{$ligne["filename"]} saved into {$BackupMaxDaysDir}");
            if ($GLOBALS["VERBOSE"]) {
                echo "\n\n###### {$c}/{$Count} Continue to Next ##########\n";
            }
        }
        $sql = "SELECT `filename`,`hostname`,`storeid` FROM `accesslogs` {$filter}";
        if ($GLOBALS["VERBOSE"]) {
            echo "{$sql}\n";
        }
        $results = $q->QUERY_SQL($sql);
        if (!$q->ok) {
            system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__);
            return;
        }
        $Count = mysql_num_rows($results);
        $c = 0;
        while ($ligne = mysql_fetch_assoc($results)) {
            $c++;
            if (!$q->ExtractAccessFile("{$BackupMaxDaysDir}/{$ligne["hostname"]}.{$ligne["filename"]}", $ligne["storeid"])) {
                continue;
            }
            $q->DelteAccessItem($ligne["storeid"]);
            $q->events("{$ligne["filename"]} saved into {$BackupMaxDaysDir}");
        }
        if ($BackupSquidLogsUseNas == 1) {
            $mount->umount($mountPoint);
        }
        return;
    }
    $q = new mysql_syslog();
    $sql = "SELECT `filename`,`taskid`,`filesize`,`filetime` FROM `store` {$filter}";
    $results = $q->QUERY_SQL($sql);
    if ($GLOBALS["VERBOSE"]) {
        echo "{$sql} ({$q->mysql_error}) " . mysql_num_rows($results) . " file(s)\n";
    }
    if (!$q->ok) {
        system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__);
        return;
    }
    while ($ligne = mysql_fetch_assoc($results)) {
        if ($GLOBALS["VERBOSE"]) {
            echo "Processing {$ligne["filename"]}\n";
        }
        if (!ExtractFileFromDatabase($ligne["filename"], $BackupMaxDaysDir)) {
            events("Unable to extract {$ligne["filename"]} to {$BackupMaxDaysDir}");
            squid_admin_notifs("SYSLOG: Unable to extract {$ligne["filename"]} to {$BackupMaxDaysDir}", __FUNCTION__, __FILE__, __LINE__);
            if ($BackupSquidLogsUseNas == 1) {
                $mount->umount($mountPoint);
            }
            return false;
        } else {
            events("Success extracting {$ligne["filename"]} to {$BackupMaxDaysDir}");
        }
    }
    if ($BackupSquidLogsUseNas == 1) {
        $mount->umount($mountPoint);
    }
}