<?php
!defined('P_W') && exit('Forbidden');
require_once(R_P.'admin/cache.php');
!$db_mode && adminmsg('mode_have_noopen');
$basename = $admin_file."?adminjob=modepage";
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');
define('M_P',R_P."mode/$db_mode/");
$db_modepages =  unserialize($db_modepages);
if (!$action) {

} elseif ($action == 'setpage') {
	InitGP(array('template','step'));
	$thispage = $db_modepages[$template];
	!$thispage && adminmsg('mode_page_noconfig');
	$scr = $thispage['scr'];
	!file_exists(M_P."template/".$thispage['template'].".htm") && adminmsg('mode_template_not_exist');
	if (!file_exists(D_P.'data/bbscache/mode_'.$scr.'_'.$db_mode.'_config.php') || filemtime(M_P."template/".$thispage['template'].".htm")>filemtime(D_P.'data/bbscache/mode_'.$scr.'_'.$db_mode.'_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.'_'.$db_mode.'_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_'.$db_mode.'_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]['name'] = $value;
				}
			}
		}
		writeover(D_P.'data/bbscache/mode_'.$scr.'_'.$db_mode.'_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_'.$db_mode.'_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.'_'.$db_mode.'_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.'_'.$db_mode.'_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);
			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,',')));
				//$params[$key] = trim(substr($value,0,strpos($value,',')));
			} else {
				$tem_param = trim($value);
				//$params[$key] = 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.'_'.$db_mode.'_config.php'));
	$thisconfig = ${$scr."_config"};
	!$thisconfig && adminmsg('mode_page_no_need_config');
	require_once(R_P.'require/elementcache.php');
	$cachefile = ElementCache::getFileName($scr);
	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'] = $db_mode;
	foreach ($scrs as $value) {
		if (file_exists(D_P.'data/bbscache/mode_'.$value.'_'.$db_mode.'_config.php')) {
			include_once(D_P.'data/bbscache/mode_'.$value.'_'.$db_mode.'_config.php');
			$exportconfig[$value] = ${$value."_config"};
		}
	}
	$filename = 'pageconfig.txt';
	$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);
	if (is_array($importInfo) && $importInfo['modename']==$db_mode) {
		unset($importInfo['modename']);
		foreach ($importInfo as $key=>$value) {
			writeover(D_P.'data/bbscache/mode_'.$key.'_'.$db_mode.'_config.php',"<?php\r\n\$".$key."_config = ".pw_var_export($value)."\r\n?>");
		}
	} 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/$db_mode/pageconfig.txt")) {
		$str = readover(R_P."mode/$db_mode/pageconfig.txt");
		$importInfo = base64_decode($str);
		$importInfo = unserialize($importInfo);
		if (is_array($importInfo) && $importInfo['modename']==$db_mode) {
			unset($importInfo['modename']);
			foreach ($importInfo as $key=>$value) {
				writeover(D_P.'data/bbscache/mode_'.$key.'_'.$db_mode.'_config.php',"<?php\r\n\$".$key."_config = ".pw_var_export($value)."\r\n?>");
			}
		}
	} 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;
}
?>