<?php
!defined('P_W') && exit('Forbidden');
$basename="$admin_file?adminjob=modeset";
if (!$action) {
	if ($fp = opendir(R_P.'mode')) {
		if ($db_mode) {
			$defalutchecked = '';
		} else {
			$defalutchecked = 'checked="checked"';
		}
		$uninstall = $infodb = array();
		while (($modedir = readdir($fp))) {
			if (strpos($modedir,'.')===false) {
				$modename = $modedir;
				if (array_key_exists($modename,$db_modes)) {
					continue;
				}
				$modeuse = $db_mode == $modename ? 1 : 0;
				if (function_exists('file_get_contents')) {
					$filedata = @file_get_contents(R_P."mode/$modedir/info.xml");
				} else {
					$filedata = readover(R_P."mode/$modedir/info.xml");
				}
				if (preg_match('/\<modename\>(.+?)\<\/modename\>\s+\<descrip\>(.+?)\<\/descrip\>/is',$filedata,$infodb)) {
					$infodb[1] && $modename = Char_cv(str_replace(array("\n"),'',$infodb[1]));
					$modedescrip = Char_cv(str_replace(array("\n"),'',$infodb[2]));
				}
				$uninstall[] = array($modename,$modedescrip,$modedir);
			}
		}
		closedir($fp);
	}

} elseif ($action == 'install') {
	require_once(R_P.'require/sql_deal.php');
	InitGP('mode',null,'1');
	if ($mode && !array_key_exists($mode,$db_modes)) {
		!file_exists(R_P.'mode/'.$mode) && adminmsg('mode_no_directory');
		!file_exists(R_P.'mode/'.$mode.'/info.xml') && adminmsg('mode_no_info');
		if (function_exists('file_get_contents')) {
			$filedata = @file_get_contents(R_P."mode/$mode/info.xml");
		} else {
			$filedata = readover(R_P."mode/$mode/info.xml");
		}
		$sqlarray = file_exists(R_P."mode/$mode/sql.txt") ? FileArray($hackdir) : array();
		!empty($sqlarray) && SQLCreate($sqlarray);

		$params = xml2array($filedata);
		if (!$db_modes || !is_array($db_modes)) {
			$db_modes = array();
		}
		$m_name = char_cv(pwConvert($params['modename'],'gbk',$db_charset));
		$db_modes[$mode] = array('m_name' => $m_name,'ifopen'=>1);
		$db_modes = serialize($db_modes);
		$rt = $db->get_one("SELECT db_name FROM pw_config WHERE db_name='db_modes'");
		if (!empty($rt)) {
			$db->update("UPDATE pw_config SET db_value=".pwEscape($db_modes,false)." WHERE db_name='db_modes'");
		} else {
			$db->update("INSERT INTO pw_config SET db_name='db_modes',db_value=".pwEscape($db_modes,false));
		}
		if ($params['pages']['item']) {
			$items = $params['pages']['item'];
			$pages = array();
			foreach ($items as $value) {
				!$value['scr'] && $value['scr'] = 'public';
				$pages[$value['scr']] = array('name'=>$value['name'],'template'=>$value['template']);
			}
			if ($pages) {
				$db_modepages[$mode] = $pages;
			} else {
				$db_modepages[$mode] = '';
			}
			$db_modepages = serialize($db_modepages);

			$rt = $db->get_one("SELECT db_name FROM pw_config WHERE db_name='db_modepages'");
			if (!empty($rt)) {
				$db->update("UPDATE pw_config SET db_value=".pwEscape($db_modepages,false)." WHERE db_name='db_modepages'");
			} else {
				$db->update("INSERT INTO pw_config SET db_name='db_modepages',db_value=".pwEscape($db_modepages,false));
			}
		}
		if ((int) $params['ifpwcache']) {
			$params['ifpwcache'] = (int) $params['ifpwcache'];
			$rt = $db->get_one("SELECT db_name FROM pw_config WHERE db_name='db_ifpwcache'");
			if (!empty($rt)) {
				$db->update("UPDATE pw_config SET db_value=db_value|".$params['ifpwcache']." WHERE db_name='db_ifpwcache'");
			} else {
				$db->update("INSERT INTO pw_config SET db_name='db_ifpwcache',db_value=".pwEscape($params['ifpwcache']));
			}
		}
		if (file_exists(R_P."mode/$mode/pageconfig.txt")) {
			$str = readover(R_P."mode/$mode/pageconfig.txt");
			$importInfo = base64_decode($str);
			$importInfo = unserialize($importInfo);
			$importInfo = pwArrayConvert($importInfo,$db_charset,'gbk',false);
			if (is_array($importInfo) && $importInfo['modename']==$mode) {
				unset($importInfo['modename']);
				foreach ($importInfo as $key=>$value) {
					if (!file_exists(D_P.'data/bbscache/mode_'.$key.'_'.$mode.'_config.php')) {
						writeover(D_P.'data/bbscache/mode_'.$key.'_'.$mode.'_config.php',"<?php\r\n\$".$key."_config = ".pw_var_export($value)."\r\n?>");
					}
				}
			}
		}
		$fp = opendir(D_P.'data/tplcache/');
		while ($filename = readdir($fp)) {
			if($filename=='..' || $filename=='.' || strpos($filename,'.htm')===false) continue;
			@unlink(Pcv(D_P.'data/tplcache/'.$filename));
		}
		closedir($fp);
	}
	updatecache_c();
	adminmsg('operate_success');
} elseif ($action == 'fourmtypecache') {
	InitGP('m',null,'1');
	!array_key_exists($m,$db_modes) && adminmsg('mode_have_noopen');
	$fp = opendir(D_P.'data/bbscache/');
	while ($filename = readdir($fp)) {
		if($filename=='..' || $filename=='.') continue;
		if (strpos($filename,'mode_'.$m) !==false) {
			P_unlink(Pcv(D_P.'data/bbscache/'.$filename));
		}
	}
	closedir($fp);
	$fp = opendir(D_P.'data/tplcache/');
	while ($filename = readdir($fp)) {
		if($filename=='..' || $filename=='.' || strpos($filename,'.htm')===false) continue;
		if (strpos($filename,$m.'_') === 0) {
			P_unlink(Pcv(D_P.'data/tplcache/'.$filename));
		}
	}
	closedir($fp);
	$fp = opendir($attachdir.'/mini/');
	while ($filename = readdir($fp)) {
		if($filename=='..' || $filename=='.') continue;
		P_unlink(Pcv($attachdir.'/mini/'.$filename));
	}
	closedir($fp);
	adminmsg('operate_success');
} elseif ($action == 'uninstall') {
	InitGP('m',null,'1');
	!array_key_exists($m,$db_modes) && adminmsg('mode_have_noopen');
	$fp = opendir(D_P.'data/bbscache/');
	while ($filename = readdir($fp)) {
		if($filename=='..' || $filename=='.') continue;
		if (strpos($filename,'mode_'.$m) !==false) {
			P_unlink(Pcv(D_P.'data/bbscache/'.$filename));
		}
	}
	closedir($fp);
	$fp = opendir(D_P.'data/tplcache/');
	while ($filename = readdir($fp)) {
		if($filename=='..' || $filename=='.' || strpos($filename,'.htm')===false) continue;
		if (strpos($filename,$m.'_') === 0) {
			P_unlink(Pcv(D_P.'data/tplcache/'.$filename));
		}
	}
	closedir($fp);
	unset($db_modes[$m]);
	$db_modes = serialize($db_modes);
	$db->update("UPDATE pw_config SET db_value=".pwEscape($db_modes,false)." WHERE db_name='db_modes'");
	unset($db_modepages[$m]);
	$db_modepages = serialize($db_modepages);
	$db->update("UPDATE pw_config SET db_value=".pwEscape($db_modepages,false)." WHERE db_name='db_modepages'");
	if ($m == $db_mode) {
		$db->update("UPDATE pw_config SET db_value='' WHERE db_name='db_mode'");
	}
	updatecache_c();
	adminmsg('operate_success');
} elseif ($action == 'set') {
	InitGP(array('defaultmode','ifopen','domain','title'),'P',1);

	$temp = array();
	foreach ($domain as $key=>$value) {
		if ($value && array_key_exists($key,$db_modes)) {
			if (preg_match('/^[a-z0-9\.]+?$/i',$value)) {
				$temp[$key] = strtolower($value);
			} else {
				adminmsg('mode_domain_error');
			}
		}
	}
	if ($db_modedomain != $temp) {
		$temp = serialize($temp);
		$rt = $db->get_one("SELECT db_name FROM pw_config WHERE db_name='db_modedomain'");
		if (!empty($rt)) {
			$db->update("UPDATE pw_config SET db_value=".pwEscape($temp,false)."WHERE db_name='db_modedomain'");
		} else {
			$db->update("INSERT INTO pw_config SET db_name='db_modedomain',db_value=".pwEscape($temp,false));
		}
	}

	if (!$defaultmode || array_key_exists($defaultmode,$db_modes)) {
		$rt = $db->get_one("SELECT db_name FROM pw_config WHERE db_name='db_mode'");
		if (!empty($rt)) {
			$db->update("UPDATE pw_config SET db_value=".pwEscape($defaultmode,false)."WHERE db_name='db_mode'");
		} else {
			$db->update("INSERT INTO pw_config SET db_name='db_mode',db_value=".pwEscape($defaultmode,false));
		}
		if ($defaultmode && !in_array($defaultmode,$ifopen)) {
			$ifopen[] = $defaultmode;
		}
	}

	foreach ($db_modes as $key=>$value) {
		if (in_array($key,$ifopen)) {
			$db_modes[$key]['ifopen'] = 1;
		} else {
			$db_modes[$key]['ifopen'] = 0;
		}
		$db_modes[$key]['title'] = $title[$key] ? $title[$key] : $db_modes[$key]['m_name'];
	}
	$db_modes = serialize($db_modes);
	$rt = $db->get_one("SELECT db_name FROM pw_config WHERE db_name='db_modes'");
	if (!empty($rt)) {
		$db->update("UPDATE pw_config SET db_value=".pwEscape($db_modes,false)." WHERE db_name='db_modes'");
	} else {
		$db->update("INSERT INTO pw_config SET db_name='db_modes',db_value=".pwEscape($db_modes,false));
	}
	updatecache_c();
	adminmsg('operate_success');
}
include PrintEot('modeset');exit;

function xml2array($content){
	$temp_array = array();
	if (preg_match('/<modename>(.*?)<\/modename>/i',$content,$match)) {
		$temp_array['modename'] = $match['1'];
	}
	if (preg_match('/<ifpwcache>(\d+)<\/ifpwcache>/i',$content,$match)) {
		$temp_array['ifpwcache'] = $match['1'];
	}
	$match_1 = $match_2 = array();
	if (preg_match('/<pages>([^\x00]*?)<\/pages>/i',$content,$match_1)) {
		if(preg_match_all('/<item>[^\x00]*?<name>(.*?)<\/name>[^\x00]*?<template>(.*?)<\/template>[^\x00]*?<scr>(.*?)<\/scr>[^\x00]*?<\/item>/i',$match_1[1],$match_2)) {
			foreach ($match_2[1] as $key => $value) {
				$temp_array['pages']['item'][$key]['name']		= $value;
				$temp_array['pages']['item'][$key]['template']	= $match_2[2][$key];
				$temp_array['pages']['item'][$key]['scr']		= $match_2[3][$key];
			}
		}
	}
	return $temp_array;
}

function pwArrayConvert($array,$to_encoding,$from_encoding,$ifmb=true){
	if (is_array($array)) {
		foreach ($array as $key=>$value) {
			$array[$key] = pwArrayConvert($value,$to_encoding,$from_encoding,$ifmb);
		}
	} else {
		$array = pwConvert($array,$to_encoding,$from_encoding,$ifmb);
	}
	return $array;
}

?>