<?php
!defined("MONKEYMAO") && die("Accessed Defined!");
	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("/^\-?[1-9]\d*$/", $val) || strlen($val) > 12) ? '\''.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;
	}
	function cache($cachename = ''){
		global $db,$tablepre;
		$cachetable = array(
			'groups' => array('usergroups','admingroups'),		
		);
	
		foreach($cachetable as $script => $cachenames) {
			if(!$cachename || ($cachename && in_array($cachename, $cachenames))) {
				writecache($script, $cachenames);
			}
		}
		if(!$cachename || $cachename == 'settings'){
			$set = array();
			$query = $db -> query("SELECT * FROM {$tablepre}settings");
			$data = '';
			while($sett = $db -> fetch_array($query)){
				$sett['value'] = (!preg_match("/^\-?[1-9]\d*$/", $sett['value']) || strlen($sett['value']) > 12) ? '\''.addcslashes($sett['value'], '\'\\').'\'' : $sett['value'];
				$data .= '$'.$sett['variable'].' = '.$sett['value'].";\n";
			}
			writecache('settings', '', $data);
		}
		if(!$cachename || $cachename == 'usergroups'){
		
			$query = $db -> query("SELECT * FROM {$tablepre}usergroups");
	
			while($group = $db -> fetch_array($query)){
				
				writecache($group['groupid'], '', getcachevars($group), 'usergroup_');
			}			
		}
		if(!$cachename || $cachename == 'admingroups'){		
			$query = $db -> query("SELECT * FROM {$tablepre}admingroups");		
			while($group = $db -> fetch_array($query)){				
				writecache($group['adminid'], '', getcachevars($group), 'admingroups_');
			}			
		}
	}

	function writecache($script, $cachenames, $cachedata = '', $prefix = 'cache_') {
		if(is_array($cachenames) && !$cachedata) {
			foreach($cachenames as $name) {
				$cachedata .= getfromdb($name);
			}
		}

		$dir = MODEPATH.'./cache/';
		if(!is_dir($dir)) {
			@mkdir($dir, 0777);
		}
		if(@$fp = fopen("$dir$prefix$script.php", 'w')) {
			fwrite($fp, "<?php\n//Monkey Mao! cache file, DO NOT modify me!\n".
				"//Created on ".date("M j, Y, G:i")."\n\n$cachedata?>");
			fclose($fp);
		} else {
			mexit('Can not write to cache files, please check directory ./cache/ .');
		}
	}

	function getfromdb($name){
		global $db, $time, $tablepre;
		$cols = '*';
		$conditions = '';
		switch($name){
			case 'usergroups':
				$table = 'usergroups';
				$cols = 'groupid, grouptitle';

			break;
			case 'admingroups':
				$table = 'admingroups';
				$cols = 'adminid, grouptitle';
			break;
		}
		$query = $db -> query("SELECT $cols FROM {$tablepre}$table $conditions");
		$data=array();
		switch($name){
			case 'usergroups':
				while($group = $db -> fetch_array($query)){
					$data[$group['groupid']] = $group;
				}
				

			break;
			case 'admingroups':
				while($group = $db -> fetch_array($query)){
					$data[$group['adminid']] = $group;
				}
			break;
		}
		return "\n\$cache_{$name} = unserialize('".addcslashes(serialize($data),  '\'\\')."');\n";
	}
	function getcachevars($data, $type = 'VAR') {
		$evaluate = '';
		foreach($data as $key => $val) {
			if(is_array($val)) {
				$evaluate .= "\$$key = ".arrayeval($val).";\n";
			} else {
				$val = addcslashes($val, '\'\\');
				$evaluate .= $type == 'VAR' ? "\$$key = '$val';\n" : "define('".strtoupper($key)."', '$val');\n";
			}
		}
		return $evaluate;
	}
?>