<?php
!defined('P_W') && exit('Forbidden');
require_once(R_P.'admin/cache.php');

InitGP('m',null,1);
!array_key_exists($m,$db_modes) && adminmsg('mode_have_noopen');
define('M_P',R_P."mode/$m/");
$basename="$admin_file?adminjob=modepage&m=$m";
if (!file_exists(D_P.'data/bbscache/mode_stamp.php')) {
	updateStampCache();
}
if (!file_exists(D_P.'data/bbscache/mode_block.php')) {
	updateBlockCache();
}
include_once(D_P.'data/bbscache/mode_stamp.php');
include_once(D_P.'data/bbscache/mode_block.php');
$db_mode = $m;
$db_modepages = $db_modepages[$m];
if (!$action) {

} elseif ($action == 'setpage') {
	InitGP(array('template','step'));
	$thispage = $db_modepages[$template];
	!$thispage && adminmsg('mode_page_noconfig');
	$scr = $template;
	!file_exists(M_P."template/".$thispage['template'].".htm") && adminmsg('mode_template_not_exist');
	if (!file_exists(D_P.'data/bbscache/mode_'.$scr.'_'.$m.'_config.php') || pwFilemtime(M_P."template/".$thispage['template'].".htm")>pwFilemtime(D_P.'data/bbscache/mode_'.$scr.'_'.$m.'_config.php')) {
		require_once(R_P.'require/parsetpl.php');
		$file_str = readover(M_P."template/".$thispage['template'].".htm");
		tplParsePW($file_str,$scr);
	}
	include(Pcv(D_P.'data/bbscache/mode_'.$scr.'_'.$m.'_config.php'));
	$thisconfig = ${$scr."_config"};
	!$thisconfig && adminmsg('mode_page_no_need_config');
	if (!$step) {
		$mode_stamp_block = array();
		foreach ($mode_block as $key=>$value) {
			if (($scr == 'index' && ($value['fid'] =='cateid'||$value['fid'] =='fid'))||($scr == 'cate' && $value['fid'] =='fid')) {
				continue;
			}
			$mode_stamp_block[$value['sid']][] = $value;
		}
		$pageconfig = array();
		foreach ($thisconfig as $key=>$value) {
			$value['sid']	= $mode_stamp[$value['stamp']]['sid'];
			$value['bid']	= $value['bid'] ? $value['bid'] : $mode_stamp[$value['stamp']]['init'];
			$value['stamp']	= $mode_stamp[$value['stamp']]['name'];
			$value['block'] = $mode_stamp_block[$value['sid']];
			$pageconfig[$key] = $value;
		}
	} elseif ($step == 2) {
		InitGP(array('blocks','names'));
		include(Pcv(D_P.'data/bbscache/mode_push_config.php'));
		$temp_push = $PUSH;
		if (is_array($blocks)) {
			foreach ($blocks as $key=>$value) {
				$block = $mode_block[$value];
				$thisconfig[$key]['bid'] 		= $value;
				$thisconfig[$key]['function'] 	= $block['function'];
				$thisconfig[$key]['type'] 		= $block['type'];
				$thisconfig[$key]['fid'] 		= $block['fid'];
				$thisconfig[$key]['special'] 	= $block['special'];
				$thisconfig[$key]['ifextra'] 	= $block['ifextra'];
				$thisconfig[$key]['cachetime'] 	= $block['cachetime'];
			}
		}
		if (is_array($names)) {
			foreach ($names as $key=>$value) {
				$thisconfig[$key]['name']	= $value;
				if ($thisconfig[$key]['stamp'] =='push' && array_key_exists($key,$temp_push) && $temp_push[$key]['mode'] == $m) {
					$temp_push[$key]['name'] = $value;
				}
			}
		}
		writeover(D_P.'data/bbscache/mode_'.$scr.'_'.$m.'_config.php',"<?php\r\n\$".$scr."_config = ".pw_var_export($thisconfig)."\r\n?>");
		if ($PUSH && $temp_push != $PUSH) {
			writeover(D_P.'data/bbscache/mode_push_config.php',"<?php\r\n\$PUSH = ".pw_var_export($temp_push)."\r\n?>");
		}
		adminmsg('operate_success',$basename."&action=setpage&template=".$template);
	}
} elseif ($action == 'setelement') {
	InitGP(array('id','step','scr','template'));
	include(Pcv(D_P.'data/bbscache/mode_'.$scr.'_'.$m.'_config.php'));
	$thisconfig = ${$scr."_config"};
	!$thisconfig && adminmsg('mode_page_no_need_config');
	!$thisconfig[$id]['param'] && adminmsg('mode_page_no_need_config');
	if ($step == 2) {
		InitGP(array('custom'));
		$result = array();
		if (is_array($custom)) {
			foreach ($custom as $key => $value) {
				$full = 1;
				foreach ($value as $val) {
					if (!$val) {
						$full = 0;
						break 1;
					}
				}
				if ($full) {
					$result[] = $value;
				}
			}
		}
		$thisconfig[$id]['custom'] = $result;
		writeover(D_P.'data/bbscache/mode_'.$scr.'_'.$m.'_config.php',"<?php\r\n\$".$scr."_config = ".pw_var_export($thisconfig)."\r\n?>");
		if ($scr=='index') {
			require_once(R_P.'require/elementcache.php');
			$cachefile = ElementCache::getFileName($scr,$m);
			if ($cachefile) {
				@include(Pcv($cachefile));
				unset($elementsinfo[$id]);
				writeover($cachefile,"<?php\r\n\$elementsinfo = ".pw_var_export($elementsinfo).";\r\n?>");
			}
		}

		adminmsg('operate_success',$basename."&action=setpage&template=".$template);
	} else {
		$params = $thisconfig[$id]['param'];
		if ($thisconfig[$id]['custom']) {
			$custom = $thisconfig[$id]['custom'];
		} else {
			$custom = array();
		}
		$tem_params = array();
		foreach ($params as $key => $value) {
			if (strpos($value,',')) {
				$tem_param = trim(substr($value,0,strpos($value,',')));
			} else {
				$tem_param = trim($value);
			}
			if ($tem_params[$tem_param]) {
				$params[$key] = $tem_param."_".$tem_params[$tem_param];
			} else {
				$params[$key] = $tem_param;
			}
			$tem_params[$tem_param]++;
		}
		$numlimit = $thisconfig[$id]['num'] ? $thisconfig[$id]['num']:10;
	}
} elseif ($action == 'updatelement') {
	InitGP(array('id','scr','template'));
	include(Pcv(D_P.'data/bbscache/mode_'.$scr.'_'.$m.'_config.php'));
	$thisconfig = ${$scr."_config"};
	!$thisconfig && adminmsg('mode_page_no_need_config');
	require_once(R_P.'require/elementcache.php');
	$cachefile = ElementCache::getFileName($scr,$m);
	if ($cachefile) {
		@include(Pcv($cachefile));
		unset($elementsinfo[$id]);
		writeover($cachefile,"<?php\r\n\$elementsinfo = ".pw_var_export($elementsinfo).";\r\n?>");
	}
	adminmsg('operate_success',$basename."&action=setpage&template=".$template);
} elseif ($action == 'export') {
	$scrs = array();
	foreach ($db_modepages as $value) {
		if (!in_array($value['scr'])) {
			$scrs[] = $value['scr'];
		}
	}
	$exportconfig = array();
	$exportconfig['modename'] = $m;
	foreach ($scrs as $value) {
		if (file_exists(D_P.'data/bbscache/mode_'.$value.'_'.$m.'_config.php')) {
			include_once(D_P.'data/bbscache/mode_'.$value.'_'.$m.'_config.php');
			$exportconfig[$value] =${$value."_config"};
		}
	}
	$filename = 'pageconfig.txt';
	$exportconfig = pwArrayConvert($exportconfig,'gbk',$db_charset,false);
	$exportInfo = serialize($exportconfig);
	$exportInfo = base64_encode($exportInfo);
	$filesize = strlen($exportInfo);
	ob_end_clean();
	header('Pragma: no-cache');
	header('Content-Encoding: none');
	header('Content-Disposition: attachment; filename='.$filename);
	header('Content-type: txt');
	header('Content-Length: '.$filesize);
	echo $exportInfo;
	exit();
} elseif ($action == 'import') {
	if(empty($_FILES)) adminmsg('mode_nofile');
	foreach ($_FILES as $key=>$value) {
		if($key!=='modeconfig') continue;
		if(is_array($value)){
			$filename = $value['name'];
			$tmpfile = $value['tmp_name'];
			$filesize = $value['size'];
		}else{
			$filename = ${$key.'_name'};
			$tmpfile = $$key;
			$filesize = ${$key.'_size'};
		}
	}
	$ext = end(explode('.',$filename));
	if(strtolower($ext)!=='txt') adminmsg('mode_fileexterror');
	require_once(R_P.'require/postfunc.php');
	$newname = $timestamp.'.txt';
	postupload($tmpfile,D_P.'data/'.$newname);
	$str = readover(D_P.'data/'.$newname);
	$importInfo = base64_decode($str);
	$importInfo = unserialize($importInfo);
	$importInfo = pwArrayConvert($importInfo,$db_charset,'gbk',false);
	if (is_array($importInfo) && $importInfo['modename']==$m) {
		unset($importInfo['modename']);
		$sql = "REPLACE INTO pw_mpageconfig(mode,scr,config) VALUES";
		$sql_array = array();
		foreach ($importInfo as $key=>$value) {
			writeover(D_P.'data/bbscache/mode_'.$key.'_'.$m.'_config.php',"<?php\r\n\$".$key."_config = ".pw_var_export($value)."\r\n?>");
			$sql_array[] = array($m,$key,serialize($value));
		}
		if ($sql_array) {
			$db->update("REPLACE INTO pw_mpageconfig(mode,scr,config) VALUES".pwSqlMulti($sql_array,false));
		}
	} else {
		P_unlink(D_P.'data/'.$newname);
		adminmsg('mode_import_error');
	}
	unlink(D_P.'data/'.$newname);
	adminmsg('operate_success');
} elseif ($action == 'default') {
	if (file_exists(R_P."mode/$m/pageconfig.txt")) {
		$str = readover(R_P."mode/$m/pageconfig.txt");
		$importInfo = base64_decode($str);
		$importInfo = unserialize($importInfo);
		$importInfo = pwArrayConvert($importInfo,$db_charset,'gbk',false);
		if (is_array($importInfo) && $importInfo['modename']==$m) {
			unset($importInfo['modename']);
			$sql_array = array();
			foreach ($importInfo as $key=>$value) {
				writeover(D_P.'data/bbscache/mode_'.$key.'_'.$m.'_config.php',"<?php\r\n\$".$key."_config = ".pw_var_export($value)."\r\n?>");
				$sql_array[] = array($m,$key,serialize($value));
			}
			if ($sql_array) {
				$db->update("REPLACE INTO pw_mpageconfig(mode,scr,config) VALUES".pwSqlMulti($sql_array,false));
			}
		}
	} else {
		adminmsg('mode_default_config_nexist');
	}
	adminmsg('operate_success');
}
include PrintEot('modepage');exit;


function randomStr($num){
	$word= 'abcdefghijklmnopqrstuvwxyz0123456789';
	$len = strlen($word);
	$len = $len-2;
	$str = '';
	for ($x=0;$x<$num;$x++){
		$i = rand(0,$len);
		$theword = substr($word,$i,1);
		$str .= $theword;
	}
	return $str;
}

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;
}
?>