Example #1
0
function Clean_tmp_path($aspid = false)
{
    $unix = new unix();
    $PidTime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    // /etc/artica-postfix/pids/exec.clean.logs.php.Clean_tmp_path.time
    if ($GLOBALS["VERBOSE"]) {
        echo "PidTime: {$PidTime}\n";
    }
    if (!$GLOBALS["VERBOSE"]) {
        $timed = $unix->file_time_min($PidTime);
        if ($timed < 60) {
            return;
        }
    }
    if ($aspid) {
        $pidpath = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
        $pid = @file_get_contents($pidpath);
        if ($unix->process_exists($pid)) {
            $unix->events(basename(__FILE__) . ":: " . __FUNCTION__ . " Already process {$pid} running.. Aborting");
            return;
        }
        @file_put_contents($pidpath, getmypid());
    }
    $sock = new sockets();
    $EnableRemoteSyslogStatsAppliance = $sock->GET_INFO("EnableRemoteSyslogStatsAppliance");
    $DisableArticaProxyStatistics = $sock->GET_INFO("DisableArticaProxyStatistics");
    $EnableRemoteStatisticsAppliance = $sock->GET_INFO("EnableRemoteStatisticsAppliance");
    if (!is_numeric($EnableRemoteStatisticsAppliance)) {
        $EnableRemoteStatisticsAppliance = 0;
    }
    if ($DisableArticaProxyStatistics == 1) {
        $EnableRemoteStatisticsAppliance = 1;
    }
    if ($EnableRemoteSyslogStatsAppliance == 1) {
        $EnableRemoteSyslogStatsAppliance = 1;
    }
    if ($EnableRemoteSyslogStatsAppliance == 1) {
        clean_squid_users_size(true);
    }
    $LogRotatePath = $sock->GET_INFO("LogRotatePath");
    if ($LogRotatePath == null) {
        $LogRotatePath = "/home/logrotate";
    }
    @unlink($PidTime);
    @file_put_contents($PidTime, time());
    $syslog_sql = new mysql_storelogs();
    $LogsDirectoryStorage = $sock->GET_INFO("LogsDirectoryStorage");
    if (trim($LogsDirectoryStorage) == null) {
        $LogsDirectoryStorage = "/home/logs-backup";
    }
    if (is_dir($LogsDirectoryStorage)) {
        if ($handle = opendir($LogsDirectoryStorage)) {
            while (false !== ($file = readdir($handle))) {
                if ($file == ".") {
                    continue;
                }
                if ($file == "..") {
                    continue;
                }
                $path = "{$LogsDirectoryStorage}/{$file}";
                if (is_dir($path)) {
                    continue;
                }
                if ($GLOBALS["VERBOSE"]) {
                    echo "{$path} -> INJECT\n";
                }
                if (!$syslog_sql->ROTATE_ACCESS_TOMYSQL($path)) {
                    continue;
                }
                @unlink($path);
            }
        }
    }
    if (is_dir("/etc/artica-postfix/pids")) {
        if ($handle = opendir("/etc/artica-postfix/pids")) {
            while (false !== ($file = readdir($handle))) {
                if ($file == ".") {
                    continue;
                }
                if ($file == "..") {
                    continue;
                }
                $path = "/etc/artica-postfix/pids/{$file}";
                if (is_dir($path)) {
                    continue;
                }
                if ($unix->file_time_min($path) > 72000) {
                    @unlink($path);
                }
            }
        }
    }
    logs_urgency();
    Clean_attachments();
    Clean_apache_logs();
    $tmpdir = $unix->TEMP_DIR();
    $mysql_storelogs = new mysql_storelogs();
    $echo = $unix->find_program("echo");
    if (is_file("/var/log/apache2/unix-varrunnginx-authenticator.sock/nginx.access.log")) {
        shell_exec("{$echo} \" \" > /var/log/apache2/unix-varrunnginx-authenticator.sock/nginx.access.log");
    }
    if (is_file("/var/log/apache2/unix-varrunnginx-authenticator.sock/nginx.error.log")) {
        shell_exec("{$echo} \" \" > /var/log/apache2/unix-varrunnginx-authenticator.sock/nginx.error.log");
    }
    if (!is_dir("/home/logrotate/work")) {
        @mkdir("/home/logrotate/work", 0755, true);
    }
    if (!is_dir("/var/log/artica-postfix/postqueue")) {
        @mkdir("/var/log/artica-postfix/postqueue", 0755, true);
    }
    $q = new mysql_storelogs();
    if ($handle = opendir("/home/logrotate/work")) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                $path = "/home/logrotate/work/{$file}";
                if (preg_match("#^php\\.log-#", $file)) {
                    @unlink($path);
                    continue;
                }
                if (preg_match("#^daemon\\.log#", $file)) {
                    @unlink($path);
                    continue;
                }
                if (preg_match("#^debug-#", $file)) {
                    @unlink($path);
                    continue;
                }
                if (preg_match("#^kern\\.log#", $file)) {
                    @unlink($path);
                    continue;
                }
                if (preg_match("#^messages-[0-9]+\\.gz#", $file)) {
                    @unlink($path);
                    continue;
                }
                if (preg_match("#^access\\.#", $file)) {
                    continue;
                }
                if (preg_match("#^cache\\.#", $file)) {
                    continue;
                }
                if ($unix->maillog_to_backupdir($path)) {
                    continue;
                }
                $timef = $unix->file_time_min($path);
                if ($timef > 5760) {
                    $GLOBALS["DELETED_FILES"] = $GLOBALS["DELETED_FILES"] + 1;
                    $q->events("Removing {$path}, exceed 5760mn", __FUNCTION__, __LINE__);
                    @unlink($path);
                }
            }
        }
    }
    if ($LogRotatePath != "/home/logrotate") {
        if ($handle = opendir("{$LogRotatePath}/work")) {
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                    $path = "{$LogRotatePath}/work/{$file}";
                    if (preg_match("#^php\\.log-#", $file)) {
                        @unlink($path);
                        continue;
                    }
                    if (preg_match("#^daemon\\.log#", $file)) {
                        @unlink($path);
                        continue;
                    }
                    if (preg_match("#^debug-#", $file)) {
                        @unlink($path);
                        continue;
                    }
                    if (preg_match("#^kern\\.log#", $file)) {
                        @unlink($path);
                        continue;
                    }
                    if (preg_match("#^messages-[0-9]+\\.gz#", $file)) {
                        @unlink($path);
                        continue;
                    }
                    if (preg_match("#^access\\.#", $file)) {
                        continue;
                    }
                    if (preg_match("#^cache\\.#", $file)) {
                        continue;
                    }
                    if ($unix->maillog_to_backupdir($path)) {
                        continue;
                    }
                    $timef = $unix->file_time_min($path);
                    if ($timef > 5760) {
                        $GLOBALS["DELETED_FILES"] = $GLOBALS["DELETED_FILES"] + 1;
                        $q->events("Removing {$path}, exceed 5760mn", __FUNCTION__, __LINE__);
                        @unlink($path);
                    }
                }
            }
        }
    }
    if (is_dir("/home/logs-backup/olds")) {
        if ($handle = opendir("/home/logs-backup/olds")) {
            while (false !== ($file = readdir($handle))) {
                if ($file == ".") {
                    continue;
                }
                if ($file == "..") {
                    continue;
                }
                $path = "/home/logs-backup/olds/{$file}";
                if (preg_match("#^php\\.log-#", $file)) {
                    @unlink($path);
                    continue;
                }
                if (preg_match("#^daemon\\.log#", $file)) {
                    @unlink($path);
                    continue;
                }
                if (preg_match("#^debug-#", $file)) {
                    @unlink($path);
                    continue;
                }
                if (preg_match("#^kern\\.log#", $file)) {
                    @unlink($path);
                    continue;
                }
                if ($unix->maillog_to_backupdir($path)) {
                    continue;
                }
                if ($GLOBALS["VERBOSE"]) {
                    echo "{$file} {$timef}mn\n";
                }
                $timef = $unix->file_time_min($path);
                if ($timef > 1880) {
                    $GLOBALS["DELETED_FILES"] = $GLOBALS["DELETED_FILES"] + 1;
                    $q->events("Removing {$path}, exceed 1880mn", __FUNCTION__, __LINE__);
                    @unlink($path);
                }
            }
        }
    }
    if ($handle = opendir("/root")) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                $path = "/root/{$file}";
                if (is_dir($path)) {
                    continue;
                }
                if (is_numeric($file)) {
                    @unlink($path);
                    continue;
                }
                if (preg_match("#_[0-9]+_tmp#", $file)) {
                    @unlink($path);
                    continue;
                }
            }
        }
    }
    if (is_dir("/opt/artica/ldap-backup")) {
        if ($handle = opendir("/opt/artica/ldap-backup")) {
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                    $path = "/opt/artica/ldap-backup/{$file}";
                    if (preg_match("#[0-9]+-0-9]+-0-9]+-0-9]+\\.tar\\.gz\$#", $file)) {
                        $time = $unix->file_time_min($path);
                        if ($time > 7200) {
                            @unlink($path);
                        }
                        continue;
                    }
                    if (is_numeric($file)) {
                        @unlink($path);
                        continue;
                    }
                }
            }
        }
    }
    if ($handle = opendir($tmpdir)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                $path = "{$tmpdir}/{$file}";
                if (is_dir($path)) {
                    if (preg_match("#^category_#", $file)) {
                        $time = $unix->dir_time_min($path);
                        if ($time > 120) {
                            @rmdir($path);
                            continue;
                        }
                    }
                    if (preg_match("#^[0-9]+\\.[0-9]+.[0-9]+\$#", $file)) {
                        $time = $unix->dir_time_min($path);
                        if ($time > 120) {
                            @rmdir($path);
                            continue;
                        }
                    }
                    continue;
                }
                if ($GLOBALS["VERBOSE"]) {
                    echo "{$path} ?\n";
                }
                if (is_dir($path)) {
                    if ($GLOBALS["VERBOSE"]) {
                        echo "{$path} is a directory\n";
                    }
                    continue;
                }
                if (preg_match("#\\.gif\$#", $file)) {
                    $time = $unix->file_time_min($path);
                    if ($time > 1) {
                        $size = @filesize($path) / 1024;
                        $GLOBALS["DELETED_SIZE"] = $GLOBALS["DELETED_SIZE"] + $size;
                        $GLOBALS["DELETED_FILES"] = $GLOBALS["DELETED_FILES"] + 1;
                        if ($GLOBALS["VERBOSE"]) {
                            echo "{$path} - > DELETE\n";
                        }
                        @unlink($path);
                        continue;
                    }
                }
                if (preg_match("#process1-(.+?)\\.tmp\$#", $file)) {
                    $time = $unix->file_time_min($path);
                    if ($time > 1) {
                        $size = @filesize($path) / 1024;
                        $GLOBALS["DELETED_SIZE"] = $GLOBALS["DELETED_SIZE"] + $size;
                        $GLOBALS["DELETED_FILES"] = $GLOBALS["DELETED_FILES"] + 1;
                        if ($GLOBALS["VERBOSE"]) {
                            echo "{$path} - > DELETE\n";
                        }
                        @unlink($path);
                        continue;
                    }
                    continue;
                }
                if (preg_match("#^artica-.+?\\.tmp#", $file)) {
                    $time = $unix->file_time_min($path);
                    if ($GLOBALS["VERBOSE"]) {
                        echo "{$path} - > {$time}Mn\n";
                    }
                    if ($time > 10) {
                        $size = @filesize($path) / 1024;
                        $GLOBALS["DELETED_SIZE"] = $GLOBALS["DELETED_SIZE"] + $size;
                        $GLOBALS["DELETED_FILES"] = $GLOBALS["DELETED_FILES"] + 1;
                        if ($GLOBALS["VERBOSE"]) {
                            echo "{$path} - > DELETE\n";
                        }
                        @unlink($path);
                        continue;
                    }
                } else {
                    if ($GLOBALS["VERBOSE"]) {
                        echo "{$file} -> NO MATCH ^artica-.+?\\.tmp \n";
                    }
                }
                if (preg_match("#^artica-php#", $file)) {
                    $time = $unix->file_time_min($path);
                    if ($GLOBALS["VERBOSE"]) {
                        echo "{$path} - > {$time}Mn\n";
                    }
                    if ($time > 10) {
                        $size = @filesize($path) / 1024;
                        $GLOBALS["DELETED_SIZE"] = $GLOBALS["DELETED_SIZE"] + $size;
                        $GLOBALS["DELETED_FILES"] = $GLOBALS["DELETED_FILES"] + 1;
                        if ($GLOBALS["VERBOSE"]) {
                            echo "{$path} - > DELETE\n";
                        }
                        @unlink($path);
                        continue;
                    }
                } else {
                    if ($GLOBALS["VERBOSE"]) {
                        echo "{$file} -> NO MATCH ^artica-php \n";
                    }
                }
            }
        }
    } else {
        if ($GLOBALS["VERBOSE"]) {
            echo "{$tmpdir} failed...\n";
        }
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "{$tmpdir} done..\n";
    }
    CleanAllindDir("/etc-artica-postfix/artica-postfix/cron.1", 2880);
    CleanAllindDir("/tmp", 2880);
    CleanAllindDir("/var/lib/c_icap/temporary", 2880);
    CleanAllindDir("/var/log/clamav", 2880);
    CleanAllindDir("/etc/artica-postfix/artica-postfix/cron.2", 2880);
    CleanAllindDir("/etc/artica-postfix/artica-postfix/croned.1", 2880);
    CleanAllindDir("/etc/artica-postfix/artica-postfix/croned.2", 2880);
    CleanAllindDir("/etc/artica-postfix/artica-postfix/pids", 2880);
    CleanAllindDir("/etc/artica-postfix/artica-postfix/pids.3", 2880);
    CleanAllindDir("/etc/artica-postfix/artica-postfix/loadavg.queue", 750);
    CleanAllindDir("/var/log/artica-postfix/sys_mem", 750);
    CleanAllindDir("/var/log/artica-postfix/sys_loadavg", 750);
    CleanAllindDir("/var/log/artica-postfix/events", 240);
    CleanAllindDir("/var/log/artica-postfix/postqueue", 240);
    CleanAllindDir("/var/log/artica-postfix/system_failover_events", 240);
    CleanAllindDir("/var/log/artica-postfix/squid-brut", 2880);
    CleanAllindDir("/var/log/artica-postfix/ufdbguard_admin_events", 240);
    CleanAllindDir("/usr/share/artica-postfix/ressources/logs", 180);
    CleanAllindDir("/usr/share/artica-postfix/ressources/logs/web", 180);
    CleanAllindDir("/usr/share/artica-postfix/ressources/conf/upload", 30);
    CleanAllindDir("/usr/share/artica-postfix/ressources/support", 30);
    ZarafaLocks();
    clean_rotate_events();
    CleanCacheStores();
    CleanRotatedFiles();
    CleanLogsDatabases();
    sessions_clean();
    clean_artica_workfiles("/var/log/artica-postfix/Postfix-sql-error");
    clean_artica_workfiles("/usr/share/artica-postfix/ressources/conf/upload", 60);
    clean_artica_workfiles("/usr/share/artica-postfix/ressources/logs/web", 240);
    clean_artica_workfiles("/usr/share/artica-postfix/ressources/logs/web/cache", 240);
    clean_artica_workfiles("/usr/share/artica-postfix/ressources/logs/web/queue/sessions", 240);
    clean_artica_workfiles("/usr/share/artica-postfix/ressources/logs/categorize-tables", 240);
    clean_artica_workfiles("/usr/share/artica-postfix/ressources/logs/jGrowl", 240);
    clean_artica_workfiles("/usr/share/artica-postfix/ressources/support", 240);
    clean_artica_workfiles("/usr/share/artica-postfix/ressources/logs", 240);
    clean_squid_users_size(true);
    artica_logs();
}
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}");
    }
}