#!/usr/bin/env php

<?php
// Setup standard module env

chdir('/usr/local/atmail/webmail/');
require_once(dirname(dirname(__FILE__)) . "/nfc-bootloader.php");

fwrite(STDOUT, "Purging log-files ...\n");
$config = Zend_Registry::get('config');

// sanitize data

if (preg_match("/[^0-9]+/", $config->global['log_purge_days'])) {
	fwrite(STDERR, "Value for log_purge_days is not valid (must be numeric) skipping purge Log_* tables\n");
} else {
    
	// Don't prune the Log_* tables if the admin has set to keep the data forever
	if ($config->global['log_purge_days'] != 0) {
		$dbAdapter->query("delete from Log_Error where LogDate < DATE_SUB(CURDATE(), INTERVAL {$config->global['log_purge_days']} DAY)");
		$dbAdapter->query("optimize table Log_Error");
		
		$dbAdapter->query("delete from Log_Login  where LogDate < DATE_SUB(CURDATE(), INTERVAL {$config->global['log_purge_days']} DAY)");
		$dbAdapter->query("optimize table Log_Login");
		
		$dbAdapter->query("delete from Log_RecvMail  where LogDate < DATE_SUB(CURDATE(), INTERVAL {$config->global['log_purge_days']} DAY)");
		$dbAdapter->query("optimize table Log_RecvMail");
			
		$dbAdapter->query("delete from Log_SendMail  where LogDate < DATE_SUB(CURDATE(), INTERVAL {$config->global['log_purge_days']} DAY)");
		$dbAdapter->query("optimize table Log_SendMail");
		
		$dbAdapter->query("delete from Log_Virus  where LogDate < DATE_SUB(CURDATE(), INTERVAL {$config->global['log_purge_days']} DAY)");
		$dbAdapter->query("optimize table Log_Virus");
		
		$dbAdapter->query("delete from Log_Spam  where LogDate < DATE_SUB(CURDATE(), INTERVAL {$config->global['log_purge_days']} DAY)");
		$dbAdapter->query("optimize table Log_Spam");

        $dbAdapter->query("delete from Log_Device  where LogDate < DATE_SUB(CURDATE(), INTERVAL {$config->global['log_purge_days']} DAY)");
        $dbAdapter->query("optimize table Log_Device");

    }
}

// Clean the AWL table for auto-white-list!
if (preg_match("/[^0-9]+/", $config->exim['filter_awl_purge_threshold']) || !isset($config->exim['filter_awl_purge_threshold'])) {
	fwrite(STDERR, "Value for filter_awl_purge_threshold is not valid (must be numeric) skipping purge awl table for low volume senders\n");
} else {
	$value = $dbAdapter->fetchAll("select count(*) as total from awl");
	$count = $value[0]['total'];
	$threshold = $config->exim['filter_awl_purge_threshold'];
	
	if($count > $threshold*3) {
		$dbAdapter->query("delete from awl where count >= 3");
	}
	else if($count > $threshold*2) {
		$dbAdapter->query("delete from awl where count = 2");
	}
	else if($count > $threshold) {		
		$dbAdapter->query("delete from awl where count = 1");
	}

	$dbAdapter->query("optimize table awl");
	
}


// Remove the cache data from the PUSH table for Activesync devices
if (preg_match("/[^0-9]+/", $config->global['ActiveSyncCacheLifespan'])) {
    fwrite(STDERR, "Value for ActiveSyncCacheLifespan is not valid (must be numeric) skipping purge of SerialConf table\n");
} else {
	$dbAdapter->query("delete from SerialConf where DateAdded < DATE_SUB(NOW(), INTERVAL {$config->global['ActiveSyncCacheLifespan']} DAY)");
}

fwrite(STDOUT, "Complete\n\n");
