<?php
define('THIS_SCRIPT', 'template');
require 'global.php';
require INC_PATH.'filehandle.misc.php';
require INC_PATH.'FileHandler.class.php';

function simpleMapPath($path)
{
	global $_SERVER;
	if (empty($path)) {
		return false;
	}
	$path = preg_replace('#(?:\\+)|(?:/+)#i', '/', $path);
	if ($path{0} === '/') {
		$documentroot = realpath(str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']));
		return $documentroot.$path;
	} elseif(substr($path, 0, 3) === '../') {
		return $path;
	} else {
		return ROOT_PATH.$path;
	}
}

checklogin();
$action = strtolower($action);
if ('edit' == $action) {

		$query=$db->query("Select styleid,name,ename,directory,stype,tpl_type,copyright,content,parentid,charset From {$dv}styles Where styleid='$tplid'");
		$tplinfo = $db->fetch_array($query);
		if (false !== $tplinfo) {
			$tplinfo['content'] = unserialize(base64_decode($tplinfo['content']));
		}

		if (false !== $tplinfo AND 1 == $tplinfo['stype']) {
			$charset = $tplinfo['charset'];
			$langpath = dirname(LANG_PATH).'/'.$tplinfo['ename'].'/';
		}

		$myquery=$db->query("Select name,directory,stype,tpl_type from {$dv}styles Where styleid='{$tplinfo[parentid]}'");
		$mytpl = $db->fetch_array($myquery);
		if( $tplinfo['stype'] != 2 ){
			if (1 == intval($tplinfo['tpl_type']) AND 0 == intval($tplinfo['stype'])) {
				$filename = ROOT_PATH.$tplinfo['directory'].$tplinfo['ename'].'.'.$tpl;
			} else {
				$filename = ROOT_PATH.$tplinfo['directory'].$tplinfo['ename'].'/'.$tpl;
			}
			$content = '';
			$filesize = filesize($filename);
			if ($filesize > 0 AND $fp = fopen($filename, 'rb')) {
				$content = fread($fp, $filesize);
				fclose($fp);
				$content = str_replace("\\'", "'", htmlspecialchars($content));
			}
		}
		$tpl = explode('.',$tpl);
} elseif('edit' === $action){
} elseif('save' === $action){
} elseif('edittplpage' === $action){
} elseif('save_base_info' === $action){
} else {
	$_TPL = false;
	if (empty($forum_sid) AND empty($forum_lang) AND empty($forum_cid)) {
		$forum_sid = 1;
	}
	if ($query = $db->query("SELECT styleid,name,ename,directory,stype,tpl_type,charset,(CASE WHEN styleid='{$forum_sid}' OR styleid='{$forum_lang}' OR styleid='{$forum_cid}' THEN content ELSE '' END) AS content,parentid FROM {$dv}styles ORDER BY styleid DESC")) {
		if ($db->num_rows($query) <= 0) {
			require_once 'inc/template.cache.php';
			if (empty($_TPL)) {
				header("content-Type: text/html; charset=".$charset);
				$lang = load_lang($lang, THIS_SCRIPT);
				show_msg($lang['page_title'], $lang['error.str0']);
				footer();
				exit;
			}
			foreach ($_TPL AS $key => $cacheinfo) {
				$stmt = "";
				$comma = '';
				//当多种编码的字符同时存在于数据库中的时候，需要对content字段进行base64_encode编码再存放于数据库中
				foreach ($cacheinfo AS $styleid => $dbstruct) {
					$db->query("INSERT INTO {$dv}styles(styleid, name, ename, content, directory, copyright, stype, tpl_type, parentid, charset) VALUES({$styleid}, '".$db->escape_string($dbstruct['name'])."', '".$db->escape_string($dbstruct['ename'])."', '".$db->escape_string(base64_encode(serialize($dbstruct['content'])))."', '".$db->escape_string($dbstruct['directory'])."', '".$db->escape_string($dbstruct['copyright'])."', '{$dbstruct['stype']}', '{$dbstruct['tpl_type']}', {$dbstruct['parentid']}, '{$dbstruct['charset']}')");
				}
			}
		} else {
			while($currtplinfo = $db->fetch_array($query)){
				if ($currtplinfo['stype'] == 0) {//风格模板
					$tplkey = 'templates';
				} elseif ($currtplinfo['stype'] == 1) {//语言包
					$tplkey = 'language';
				} elseif ($currtplinfo['stype'] == 2) {//风格样式
					$tplkey = 'stylecss';
				} else {
					continue;
				}
				$_TPL["{$tplkey}"]["{$currtplinfo['styleid']}"] = array('styleid' => $currtplinfo['styleid'], 'name' => $currtplinfo['name'], 'ename' => $currtplinfo['ename'], 'content' => unserialize(base64_decode($currtplinfo['content'])), 'directory' => $currtplinfo['directory'], 'copyright' => $currtplinfo['copyright'], 'stype' => $currtplinfo['stype'], 'tpl_type' => $currtplinfo['tpl_type'], 'parentid' => $currtplinfo['parentid'], 'charset' => $currtplinfo['charset']);
			}
		}
	}
	if (empty($_TPL)) {
		header("content-Type: text/html; charset=".$charset);
		$lang = load_lang($lang, THIS_SCRIPT);
		show_msg($lang['page_title'], $lang['error.str0']);
		footer();
		exit;
	}
	$tpllist = false;
	if( $forum_sid == '' && $forum_cid == '' && $forum_lang == '' ){
		$tpllist =& $_TPL['templates']['1'];
	}elseif( $forum_sid != '' ){
		$tpllist =& $_TPL['templates'][$forum_sid];
	}elseif( $forum_lang != '' ){
		$tpllist =& $_TPL['language'][$forum_lang];
	}elseif( $forum_cid != '' ){
		$tpllist =& $_TPL['stylecss'][$forum_cid];
	}
	$tplname = $tpllist['ename'];
	$tplinfo =& $tpllist;
}
$charset = strtolower(trim($tpllist['charset']));
switch ($charset) {
	case 'big5':
		$langpath = dirname(LANG_PATH).'/zh-tw/';
		break;
	case 'gbk':
	case 'gb2312':
	default:
		$langpath = dirname(LANG_PATH).'/zh-cn/';
		break;
}

header("content-Type: text/html; charset=".$charset);
$lang = load_lang($lang, THIS_SCRIPT, $langpath);
head();
if ( strpos($sysinfo['flag'],'19,') === false ){
	show_msg($lang['readme_tit'],$lang['error.str1'],0,'','','','');
	exit;
}
readme($lang['page_title'],$lang['page_readme'],$lang['nav']);

if('edit' === $action){
	require_once printout('template');
} elseif ('save' === $action) {
	$query=$db->query("Select styleid,name,ename,directory,stype,tpl_type,copyright,content,parentid From {$dv}styles Where styleid='$tplid'");
	$tplinfo = $db->fetch_array($query);
	if ($tplinfo === false) {
		show_msg($lang['page_title'], $lang['tpl.error1']);
		footer();
		exit;
	}
	if (!empty($tplinfo['content'])) {
		$tplinfo['content'] = unserialize(base64_decode($tplinfo['content']));
	}
	if ((3 == $stype OR 4 == $stype) AND !isset($tplinfo['content']['.css'])) {
		$tplinfo['content']['.css'] = '';
	}
	if ((3 == $stype OR 4 == $stype) AND empty($tplinfo['content']['.php'])) {
		for ($i = 0 ; $i < 43 ; $i++) {
			$tplinfo['content']['.php'][$i] = '';
		}
	}
	list($tplkey,) = explode('.', $tplname);
	switch($stype){
		case 1:	//	编辑语言包
			$suc = 0;
			$actionmsg = $lang['tpl.error13'];
			if ($fp = fopen(ROOT_PATH.$tplinfo['directory'].$tplinfo['ename'].'/'.$tplname, 'wb')) {
				@flock($fp, 2);
				@fwrite($fp, stripslashes(str_replace("\x0d\x0a", "\x0a", $content)));
				@fclose($fp);
				if (!empty($tplkey) AND isset($_POST['tpl_description'])) {
					$tplinfo['content'][$tplkey] = ($magic_quotes_gpc ? stripslashes($_POST['tpl_description']) : $_POST['tpl_description']);
					$db->query("UPDATE {$dv}styles SET content='".$db->escape_string(base64_encode(serialize($tplinfo['content'])))."' WHERE styleid='{$tplinfo['styleid']}'");
				}
				$suc = 1;
				$actionmsg = $lang['tpl.str7'];
			}
			show_msg($lang['page_title'], $actionmsg, $suc);
			footer();
			exit;
			break;
		case 2:	//	编辑模板
			$suc = 0;
			$actionmsg = $lang['tpl.error13'];
			if (1 == intval($tplinfo['tpl_type'])) {
				$_filename = ROOT_PATH.$tplinfo['directory'].$tplinfo['ename'].'.'.$tplname;
			} else {
				$_filename = ROOT_PATH.$tplinfo['directory'].$tplinfo['ename'].'/'.$tplname;
			}
			if ($fp = fopen($_filename, 'wb')) {
				@flock($fp, 2);
				@fwrite($fp, stripslashes(str_replace("\x0d\x0a", "\x0a", $content)));
				@fclose($fp);
				if (!empty($tplkey) AND isset($_POST['tpl_description'])) {
					$tplinfo['content'][$tplkey] = ($magic_quotes_gpc ? stripslashes($_POST['tpl_description']) : $_POST['tpl_description']);
					$db->query("UPDATE {$dv}styles SET content='".$db->escape_string(base64_encode(serialize($tplinfo['content'])))."' WHERE styleid='{$tplinfo['styleid']}'");
				}
				$suc = 1;
				$actionmsg = $lang['tpl.str7'];
			}
			show_msg($lang['page_title'], $actionmsg, $suc);
			footer();
			exit;
			break;
		case 3:	//	编辑样式(存储于 CSS 文件中)
			$content_w = $content;
			$tplinfo['content']['.css'] = ($magic_quotes_gpc ? stripslashes($content) : $content);
			$tplinfo['content']['.css'] = stripslashes(str_replace("\x0d\x0a", "\x0a", $tplinfo['content']['.css']));
			$db->query("Update {$dv}styles Set content = '".base64_encode(serialize($tplinfo['content']))."',parentid='{$parentid}' Where styleid='$tplid'");
			$csarr = Array(0=>'{$width}',1=>'{$bodybgcolor}',2=>'{$bodybgimg}',3=>'{$fontfamily}',4=>'{$fontsize}',5=>'{$fontcolor}',6=>'{$linkcolor}',7=>'{$linkhovercolor}',8=>'{$bordercolor1}',9=>'{$bordercolor2}',10=>'{$borderbg1}',11=>'{$titlebgimg}',12=>'{$titlefontcolor}',13=>'{$titlelink}',14=>'{$titlehover}',15=>'{$tablebody1}',16=>'{$tablebody2}',17=>'{$borderbg2}',18=>'{$alertcolor1}',19=>'{$alertcolor2}',20=>'{$tplurl}',21=>'{$img1}',22=>'{$img2}',23=>'{$img3}',24=>'{$img4}',25=>'{$img5}',26=>'{$img6}',27=>'{$img7}',28=>'{$img8}',29=>'{$img9}',30=>'{$img10}',31=>'{$img11}',32=>'{$img12}',33=>'{$img13}',34=>'{$img14}',35=>'{$img15}',36=>'{$img16}',37=>'{$color1}',38=>'{$color2}',39=>'{$color3}',40=>'{$color4}',41=>'{$color5}',42=>'{$color6}');
			if (is_array($tplinfo['content']['.php'])) {
				foreach ($tplinfo['content']['.php'] AS $i => $val) {
					switch($i){
						case 1:
							if( $val != '' ){
								$content_w = str_replace($csarr[$i], 'background-color:'.$val.';', $content_w);
							}else{
								$content_w = str_replace($csarr[$i], '', $content_w);
							}
							break;
						case 2:
							if( $val != '' ){
								$content_w = str_replace($csarr[$i], 'background-image :url("'.$val.'");', $content_w);
							}else{
								$content_w = str_replace($csarr[$i], '', $content_w);
							}
							break;
						case 11:
							if( $val != '' ){
								$content_w = str_replace($csarr[$i], 'background-image :url("'.$val.'");', $content_w);
							}else{
								$content_w = str_replace($csarr[$i], '', $content_w);
							}
							break;
						default:
							$content_w = str_replace($csarr[$i], $val, $content_w);
							break;
					}
				}
			}
			if ($fp = fopen(ROOT_PATH.$tplinfo['directory'].$tplinfo['ename'].'.css', 'wb')) {
				@flock($fp, 2);
				@fwrite($fp, stripslashes(str_replace("\x0d\x0a", "\x0a", $content_w)));
				@fclose($fp);
				@chmod(ROOT_PATH.$tplinfo['directory'].$tplinfo['ename'].'.css', 0777);
				require_once INC_PATH.'BoardCache.class.php';
				$mycache =& new BoardCache($db);
				$mycache->createstylecss();
				show_msg($lang['page_title'], $lang['tpl.str0'], 1);
				footer();
				exit;
			} else {
				show_msg($lang['page_title'], $lang['tpl.error1']);
				footer();
				exit;
			}
			break;
		case 4:	//	编辑样式(存储于 PHP 文件中)
			$content_s =& $tplinfo['content']['.css'];
			$cs =& $tplinfo['content']['.php'];
			$csarr = Array(0=>'{$width}',1=>'{$bodybgcolor}',2=>'{$bodybgimg}',3=>'{$fontfamily}',4=>'{$fontsize}',5=>'{$fontcolor}',6=>'{$linkcolor}',7=>'{$linkhovercolor}',8=>'{$bordercolor1}',9=>'{$bordercolor2}',10=>'{$borderbg1}',11=>'{$titlebgimg}',12=>'{$titlefontcolor}',13=>'{$titlelink}',14=>'{$titlehover}',15=>'{$tablebody1}',16=>'{$tablebody2}',17=>'{$borderbg2}',18=>'{$alertcolor1}',19=>'{$alertcolor2}',20=>'{$tplurl}',21=>'{$img1}',22=>'{$img2}',23=>'{$img3}',24=>'{$img4}',25=>'{$img5}',26=>'{$img6}',27=>'{$img7}',28=>'{$img8}',29=>'{$img9}',30=>'{$img10}',31=>'{$img11}',32=>'{$img12}',33=>'{$img13}',34=>'{$img14}',35=>'{$img15}',36=>'{$img16}',37=>'{$color1}',38=>'{$color2}',39=>'{$color3}',40=>'{$color4}',41=>'{$color5}',42=>'{$color6}');
			if (is_array($csssetting)) {
				foreach( $csssetting AS $i => $val ){
					switch($i){
						case 1:
							if( $val != '' ){
								$tplinfo['content']['.css'] = str_replace($csarr[$i],'background-color:'.$val.';',$tplinfo['content']['.css']);
							}else{
								$tplinfo['content']['.css'] = str_replace($csarr[$i],'',$tplinfo['content']['.css']);
							}
							break;
						case 2:
							if( $val != '' ){
								$tplinfo['content']['.css'] = str_replace($csarr[$i],'background-image :url("'.$val.'");',$tplinfo['content']['.css']);
							}else{
								$tplinfo['content']['.css'] = str_replace($csarr[$i],'',$tplinfo['content']['.css']);
							}
							break;
						case 11:
							if( $val != '' ){
								$tplinfo['content']['.css'] = str_replace($csarr[$i],'background-image :url("'.$val.'");',$tplinfo['content']['.css']);
							}else{
								$tplinfo['content']['.css'] = str_replace($csarr[$i],'',$tplinfo['content']['.css']);
							}
							break;
						default:
							$tplinfo['content']['.css'] = str_replace($csarr[$i],$val,$tplinfo['content']['.css']);
							break;
					}
				}
			}
			$tplinfo['content']['.php'] =& $csssetting;
			if ($fp = fopen(ROOT_PATH.$tplinfo['directory'].$tplinfo['ename'].'.css', 'wb')) {
				@flock($fp, 2);
				@fwrite($fp, stripslashes(str_replace("\x0d\x0a", "\x0a", $tplinfo['content']['.css'])));
				@fclose($fp);
				@chmod(ROOT_PATH.$tplinfo['directory'].$tplinfo['ename'].'.css', 0777);
				$db->query("Update {$dv}styles Set content = '".$db->escape_string(base64_encode(serialize($tplinfo['content'])))."' Where styleid='$tplid'");
				require_once INC_PATH.'BoardCache.class.php';
				$mycache =& new BoardCache($db);
				$mycache->createstylecss();
				show_msg($lang['page_title'], $lang['tpl.str0'], 1);
				footer();
				exit;
			} else {
				show_msg($lang['page_title'], $lang['tpl.error1']);
				footer();
				exit;
			}
			break;
	}
} elseif ('edittplpage' === $action) {//新建、删除、编辑模板页面
	$tplinfo = false;
	if ($styleid > 0) {
		if ($query = $db->query("SELECT styleid,ename,name,directory,stype,tpl_type,content,parentid FROM {$dv}styles WHERE styleid='{$styleid}'")) {
			if ($tplinfo = $db->fetch_array($query)) {
				$db->free_result($query);
			}
		}
	}
	if (false !== $tplinfo) {
		$content = unserialize(base64_decode($tplinfo['content']));
		if (empty($content)) {
			$content = array('tpllist' => array());
		}
		if (0 == $tplinfo['stype']) {
			$filepostfix = '.tpl.php';
		} elseif (1 == intval($tplinfo['stype'])) {
			$filepostfix = '.lang.php';
		}
		if (('addnew' === $sub_action)) {
			if (1 == intval($tplinfo['tpl_type']) AND 0 == intval($tplinfo['stype'])) {
				$destfile = simpleMapPath($tplinfo['directory'].'/'.$tplinfo['ename'].'.'.$tplpage_ename.$filepostfix);
			} else {
				$destfile = simpleMapPath($tplinfo['directory'].'/'.$tplinfo['ename'].'/'.$tplpage_ename.$filepostfix);
			}
			if (isset($content[$tplpage_ename]) OR is_file($destfile)) {
				show_msg($lang['page_title'], $lang['tpl.error7']);
				footer();
				exit;
			}
			if (!touch($destfile) OR !chmod($destfile, 0777)) {
				if (is_file($destfile)) {
					@unlink($destfile);
				}
				show_msg($lang['page_title'], $lang['tpl.error8']);
				footer();
				exit;
			}
			$content[$tplpage_ename] = $tplpage_name;
			$actionmsg = $lang['tpl.str4'];
		} elseif ('edit' === $sub_action) {
			$oldfile = simpleMapPath($tplinfo['directory'].'/'.$tplinfo['ename'].'/'.$tplpage_old_ename.$filepostfix);
			$newfile = simpleMapPath($tplinfo['directory'].'/'.$tplinfo['ename'].'/'.$tplpage_new_ename.$filepostfix);
			if (!is_file($oldfile) OR !rename($oldfile, $newfile)) {
				show_msg($lang['page_title'], $lang['tpl.error11']);
				footer();
				exit;
			}
			unset($content[$tplpage_old_ename]);
			$content[$tplpage_new_ename] = $tplpage_new_name;
			$actionmsg = $lang['tpl.str5'];
		} elseif ('delete' === $sub_action) {
			$destfile = simpleMapPath($tplinfo['directory'].'/'.$tplinfo['ename'].'/'.$tplpage_ename.$filepostfix);
			if (is_file($destfile) AND unlink($destfile)) {
				unset($content[$tplpage_ename]);
			} else {
				show_msg($lang['page_title'], $lang['tpl.error10']);
				footer();
				exit;
			}
			unset($content[$tplpage_ename]);
			$actionmsg = $lang['tpl.str6'];
		} else {
			show_msg($lang['page_title'], $lang['tpl.error9']);
			footer();
			exit;
		}
		$db->query("UPDATE {$dv}styles SET content='".$db->escape_string(base64_encode(serialize($content)))."' WHERE styleid='{$styleid}'");
		show_msg($lang['page_title'], $actionmsg, 1);
		footer();
		exit;
	}
} elseif ('save_base_info' === $action) {
	$suc = 0;
	$tplinfo = false;
	if ($_POST['styleid'] > 0) {
		if ($query = $db->query("SELECT styleid,name,ename,directory,stype,tpl_type,content,parentid FROM {$dv}styles WHERE styleid='{$_POST['styleid']}'")) {
			if ($tplinfo = $db->fetch_array($query)) {
				$db->free_result($query);
			}
		}
	}
	if ($_POST['sub_action'] === 'modify') {//修改模板
		if ($tplinfo !== false) {
			$destfile = simpleMapPath($tplinfo['directory'].$_POST['edit_ename']);
			$rawfile =  simpleMapPath($tplinfo['directory'].$tplinfo['ename']);
			$deletecssfile = '';
			if (intval($tplinfo['stype']) == 2) {
				$deletecssfile = simpleMapPath(RELATED_CACHE_PATH.'style/'.$tplinfo['ename'].'.php');
				$destfile .= '.css';
				$rawfile .= '.css';
			}
			$cancontinue = true;
			if ($rawfile !== $destfile) {
				if (!FileHandler::rename($rawfile, $destfile)) {
					$cancontinue = false;
					$actionmsg = $lang['tpl.error17'];
				}
			}
			if (true === $cancontinue) {
				require_once INC_PATH.'BoardCache.class.php';
				$mycache =& new BoardCache($db);
				$db->query("UPDATE {$dv}styles SET name='{$_POST['edit_name']}', ename='{$_POST['edit_ename']}' WHERE styleid='{$tplinfo['styleid']}'");
				$db->query("UPDATE {$dv}styles SET directory=REPLACE(directory, '{$tplinfo['ename']}/', '{$_POST['edit_ename']}/') WHERE parentid='{$tplinfo['styleid']}'");
				if ('' !== $deletecssfile) {
					@FileHandler::unlink($deletecssfile);
				}
				$mycache->createstylecss();
				$forum_tpl = $db->scalar("SELECT forum_tpl FROM {$dv}setup WHERE forum_tpl LIKE '%{$tplinfo['ename']}%'");
				if (false !== $forum_tpl) {
					$forum_tpl = explode('|', $forum_tpl);
					if (0 == intval($tplinfo['stype'])) {
						if( $forum_tpl[0] == $tplinfo['ename']) {
							$forum_tpl[0] = $_POST['edit_ename'];
						}
						if( false !== strpos($forum_tpl[1], $tplinfo['ename'].'/')) {
							$forum_tpl[1] = str_replace($tplinfo['ename'].'/', $_POST['edit_ename'].'/', $forum_tpl[1]);
						}
					} elseif (1 == intval($tplinfo['stype'])) {
						if ($_POST['edit_ename'] != $forum_tpl[2]) {
							$forum_tpl[2] = $_POST['edit_ename'];
						}
					} elseif (2 == intval($tplinfo['stype'])) {
						if( false !== strpos($forum_tpl[1], '/'.$tplinfo['ename'])) {
							$forum_tpl[1] = str_replace('/'.$tplinfo['ename'], '/'.$_POST['edit_ename'], $forum_tpl[1]);
						}
					}
					$db->query("UPDATE {$dv}setup SET forum_tpl='".$db->escape_string(implode('|', $forum_tpl))."'");
					$mycache->createForumSetting();
					unset($mycache);
				}
				$suc = 1;
				$actionmsg = $lang['tpl.str1'];
			}
		} else {
			print 'Failed!';
		}
	} elseif ($_POST['sub_action'] === 'addnew') {//添加模板
		if ($_POST['add_name'] !== '' AND $_POST['add_ename'] !== '' AND $_POST['add_charset'] !== '' AND $_POST['add_directory'] !== '') {
			$exists = $db->scalar("SELECT COUNT(1) FROM {$dv}styles WHERE ename='{$_POST['add_ename']}' AND stype='0'");
			if ($exists <= 0) {
				$destfile = simpleMapPath($_POST['add_directory'].'/'.$_POST['add_ename']);
				if (intval($tplinfo['stype']) == 2) {
					$destfile .= '.css';
					//print '$destfile:'.$destfile;
					if (!FileHandler::is_file($destfile) AND !FileHandler::touch($destfile)) {
						show_msg($lang['page_title'], $lang['tpl.error4']);
						footer();
						exit;
					}
				} else {
					if(!is_dir($destfile)) {
						if ($issafemode = ini_get('safe_mode')) {
							show_msg($lang['page_title'], str_replace('{$TPL_DIR}', $destfile, $lang['tpl.error12']));
							footer();
							exit;
						}
						if (!mkdir($destfile, 0777)) {
							show_msg($lang['page_title'], $lang['tpl.error4']);
							footer();
							exit;
						}
					}
				}
				if (!is_writable($destfile)) {
					show_msg($lang['page_title'], $lang['tpl.error3']);
					footer();
					exit;
				}
				$arraycontent = array();
				$add_directory = preg_replace('#(?:\\+)|(?:/+)#i', '/', $_POST['add_directory']);
				$db->query("INSERT INTO {$dv}styles(name, ename, directory, copyright, charset, stype, tpl_type, parentid, content) VALUES('{$_POST['add_name']}', '{$_POST['add_ename']}', '{$add_directory}', '{$_POST['add_copyright']}', '{$_POST['add_charset']}', '{$tplinfo['stype']}', '{$tplinfo['tpl_type']}', '{$tplinfo['parentid']}', '".$db->escape_string(base64_encode(serialize($arraycontent)))."')");
				$suc = 1;
				$actionmsg = $lang['tpl.str2'];
			} else {
				show_msg($lang['page_title'], $lang['tpl.error3']);
				footer();
				exit;
			}
		}
	} elseif ($_POST['sub_action'] === 'delete') {//删除模板
		if ($tplinfo === false) {
			show_msg($lang['page_title'], $lang['tpl.error3']);
			footer();
			exit;
		}
		$actionmsg = $lang['tpl.str6'];
		$suc = 1;
		$subdeleted = true;
		if ($query = $db->query("SELECT styleid,name,ename,content,directory,copyright,stype,tpl_type,parentid,charset FROM {$dv}styles WHERE parentid='{$tplinfo['styleid']}'")) {
			while ($rs = $db->fetch_array($query)) {
				if (1 == intval($rs['stype'])) {
					$directory = simpleMapPath($rs['directory'].'/'.$rs['ename']);
					if (false !== ($langcontent = unserialize(base64_decode($rs['content'])))) {
						foreach ($langcontent AS $langtplname => $langtpldesc) {
							if (!FileHandler::unlink($directory.'/'.$langtplname.'.lang.php')) {
								$actionmsg = $lang['tpl.error14'];
								$suc = 0;
								$subdeleted = false;
								break;
							}
						}
					}
					@rmdir($directory);
				} elseif (2 == intval($rs['stype'])) {
					$destdirectory = simpleMapPath($rs['directory']);
					$cssfile = $destdirectory.'/'.$rs['ename'].'.css';
					$cssphpfile = CACHE_PATH.'style/'.$rs['ename'].'.php';
					if (!FileHandler::unlink($cssfile) AND !FileHandler::unlink($cssphpfile)) {
						$actionmsg = $lang['tpl.error15'];
						$suc = 0;
						$subdeleted = false;
						break;
					}
				} else {
					continue;
				}
			}
			if (1 == $suc) {
				$db->query("DELETE FROM {$dv}styles WHERE parentid='{$tplinfo['styleid']}'");
			}
		}
		if (1 == $tplinfo['tpl_type'] AND 0 == intval($tplinfo['stype'])) {
			$tpldirectory = simpleMapPath($tplinfo['directory']);
		} else {
			$tpldirectory = simpleMapPath($tplinfo['directory'].'/'.$tplinfo['ename']);
		}
		if (true === $subdeleted AND false !== ($tplcontent = unserialize(base64_decode($tplinfo['content'])))) {
			foreach ($tplcontent AS $tplname => $tpldesc) {
				if (1 == intval($tplinfo['tpl_type']) AND 0 == intval($tplinfo['stype'])) {
					$unlinkfile = $tpldirectory.$tplinfo['ename'].'.'.$tplname.'.tpl.php';
				} else {
					$unlinkfile = $tpldirectory.'/'.$tplname.'.tpl.php';
				}
				if (!FileHandler::unlink($unlinkfile)) {
					$actionmsg = $lang['tpl.error16'];
					$suc = 0;
					break;
				}
			}
		}
		@rmdir($tpldirectory);
		if (1 == $suc) {
			$db->query("DELETE FROM {$dv}styles WHERE styleid='{$tplinfo['styleid']}'");
		}
	}
	show_msg($lang['page_title'], $actionmsg, $suc);
	footer();
	exit;
} else {
	require_once printout('template');
}

footer();

?>