<?php
if(!defined('IN_TM')) exit('Access Denied');

function cache_all(){
	deltreedir(CACHE_PATH); 
	cache_setting();
	cache_seccode();
	cache_seccodedata();
	cache_template(); 
	cache_business();      
	return true;
}
function cache_setting(){    
	cache_write('setting','TM',setting_get('setup'));    
	return true;
}
function cache_business(){    
	cache_write('business','business',setting_get('business'));    
	return true;
}
function cache_seccode(){    
	cache_write('seccode','seccode',setting_get('seccode'));    
	return true;
}
function cache_seccodedata(){      
	cache_write('seccodedata','seccodedata',setting_get('seccodedata'));    
	return true;
}
function cache_template(){      
	deltreedir(TM_ROOT.TPL_CACHE_PATH); 
	return true;
}
//获取目录
function sreaddir($dir, $extarr=array()) {	
	$dirs = array();	
	if($dh = opendir($dir)) {		
	while (($file = readdir($dh)) !== false) {			
		if(!empty($extarr) && is_array($extarr)) {				
			if(in_array(strtolower(fileext($file)), $extarr)) {					
				$dirs[] = $file;				
			}			
		} else if($file != '.' && $file != '..') {				
			$dirs[] = $file;			
		}		
	}		
	closedir($dh);	
	}	
	return $dirs;
}
//写入
function cache_write($name, $var, $values) {	$cachefile = CACHE_PATH.'/data_'.$name.'.php';	$cachetext = "<?php\r\n".		"if(!defined('IN_TM')) exit('Access Denied');\r\n".		'$'.$var.'='.arrayeval($values).		"\r\n?>";	if(!swritefile($cachefile, $cachetext)) {		exit("File: $cachefile write error.");	}}
//写入文件
function swritefile($filename, $writetext, $openmod='w') {	if(@$fp = fopen($filename, $openmod)) {		flock($fp, 2);		fwrite($fp, $writetext);		fclose($fp);		return true;	} else {		runlog('error', "File: $filename write error.");		return false;	}}
//递归清空目录
function deltreedir($dir) {	$files = sreaddir($dir);	foreach ($files as $file) {		if(is_dir("$dir/$file")) {			deltreedir("$dir/$file");		} else {			@unlink("$dir/$file");		}	}}
//数组转换成字串
function arrayeval($array, $level = 0) {	$space = '';	for($i = 0; $i <= $level; $i++) {		$space .= "\t";	}	$evaluate = "Array\n$space(\n";	$comma = $space;	foreach($array as $key => $val) {		$key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;		$val = !is_array($val) && (!preg_match("/^\-?\d+$/", $val) || strlen($val) > 12 || substr($val, 0, 1)=='0') ? '\''.addcslashes($val, '\'\\').'\'' : $val;		if(is_array($val)) {			$evaluate .= "$comma$key => ".arrayeval($val, $level + 1);		} else {			$evaluate .= "$comma$key => $val";		}		$comma = ",\n$space";	}	$evaluate .= "\n$space)";	return $evaluate;}
?>