<?
class purgatory{
	function purge_cache() {

		require_once('models/systemModel.php');
		$mysystemModel=new systemModel;

		$systemsettings_data = $mysystemModel->getdata_systemsettings();

		$cachepath = $systemsettings_data['system_cache_path'];
		$purgeoption = $systemsettings_data['system_cache_purge'];
		$ageinmonths = $systemsettings_data['system_cache_purgeage'];
		$ageinseconds = 2592000 * $ageinmonths;
		$ageboundary = time() - $ageinseconds;

		if (($purgeoption=="yes")&&($ageinmonths>0)) {

			// get list of files..
			$cachefiles=array();
			$cachepath = rtrim($cachepath, "/")."/";
			if ($handle = opendir($cachepath)) {
				while (($file = readdir($handle)) !== false) {
					if (($file != ".") && ($file != "..")) {
						if (is_dir($cachepath.$file."/")) {
							$cachefiles[] = $file;
						}
					}
				}
				closedir($handle);
			}

			// check file modified time..
			foreach ($cachefiles as $cachefile) {
				$filedate = filemtime($cachepath.$cachefile."/".$cachefile.".html");
				if ($filedate < $ageboundary) {
					// unlink($cachefile);
					$this->purge_dir($cachepath.$cachefile."/");
					// what about page_contents table?
				}
			}

		}

	}

	function purge_audit() {

		require_once('models/systemModel.php');
		$mysystemModel=new systemModel;

		$systemsettings_data = $mysystemModel->getdata_systemsettings();

		$purgeoption = $systemsettings_data['system_audit_purge'];
		$ageinmonths = $systemsettings_data['system_audit_purgeage'];
		$ageinseconds = 2592000 * $ageinmonths;
		$ageboundary = time() - $ageinseconds;

		if (($purgeoption=="yes")&&($ageinmonths>0)) {

			$opresult = $mysystemModel->deletedata_audittrail($ageboundary);

		}
	}

	// recursive function to delete directory and subdirs..
	function purge_dir($dirpath) {

		$files = glob( $dirpath . '*', GLOB_MARK ); 
		foreach( $files as $file ){ 
			if( substr( $file, -1 ) == '/' ) 
				purge_dir( $file ); 
			else 
				unlink( $file ); 
		} 
		if (is_dir($dirpath)) rmdir( $dirpath ); 
    
	} 
}
?>