<?php 
/**
 * 模板处理
 */
if(!defined('IN_SYS'))exit('access die'); 
if(!getAdminPri('10001'))exit(NO_AUTH_LOGIN);
$task = $_GET['task'];
require SYS_ROOT.'include/classes/cls.xml_parser.php';
function read_path($path,$all = true){
	$path = substr($path,-1,1)!='/'?$path.'/':$path;
	$fp = opendir($path);
	while (FALSE!=$file=readdir($fp)){
		if($file!='.' && $file!='..' && $file!='.svn' && $file!='install' && is_dir($path.$file)){
			$temp[] = $all?$path.$file.'/':$file;
		}
	}
	closedir($fp);
	return $temp;
}

function getTplList($file) {
	if(!is_file($file))return NULL;
	$charset = CHARSET;
	$xp = new XmlParser($file);
	$arr = $xp -> xmlToArray();
	if(!empty($arr['item']))$arr['item']['view'] = 'admincp.php?action=templete&task=view&path='.$arr['item']['folder'].'&file='.$arr['item']['view'];
	foreach ($arr['item'] as &$v){
		if($charset=='gbk')$v = iconv("utf-8",'gbk',$v);
	}
	return $arr['item'];
}
if(is_post()){
	$task = $_POST['task'];
	switch ($task){
		case 'upload':
		if(!class_exists('Upload'))require SYS_ROOT.'include/classes/upload.class.php';
		$upload = new Upload();
		$upload->upload_save_path = 'cache/template_temp/';
		$upload->upload_maxsize = 1024*1024*1;
		$upload->upload_type = array('zip');
		$upfile = $upload->up('upload_template');
		if(!$upfile){
			$msg = $upload->getMsg();
			$sp->bgmsg($msg['msg']);
		}
		//开始解析压缩包
		if(!class_exists('Zip'))require PUB_CLASS.'phpwms.zip.class.php';
		$zip = new Zip();
		$folders = read_path(SYS_ROOT.'templete/',FALSE);
		$zipfile = $upfile['end_file'];
		$extract_temp_path = SYS_ROOT.'cache/template_temp/'.time().'/';
		$pack = $zip->unpackzip($zipfile,$extract_temp_path,-1,1);
		if(!$pack){
			@unlink($pack);
			delete_folder($extract_temp_path,TRUE);
			exit($sp->bgmsg('解压缩文件失败或未完全解压.请自行上传模板到template目录'));
			//exit('1|'.$this->_lang['template']['un_pack_error_1']);//解压缩文件失败或未完全解压.请自行上传模板到template目录
		}
		$xml_file = $extract_temp_path.'wms.xml';
		if(!file_exists($xml_file) || !is_file($xml_file)){
			@unlink($zipfile);
			delete_folder($extract_temp_path,TRUE);
			exit($sp->bgmsg('缺少xml解析文件-wms.xml'));
		}
		clearstatcache();
		$xml_info = getTplList($xml_file);
		$s_f = $xml_info['folder'];
		if(empty($s_f))exit($sp->bgmsg('xml格式错误,缺少模板路径.'));
		if(in_array($s_f,$folders)){
			@unlink($zipfile);
			delete_folder($extract_temp_path,TRUE);
			exit($sp->bgmsg('已存在相同的模板目录或您已安装了本模板.'));
		}
		//移动文件
		$move_template_path = SYS_ROOT.'template/'.$s_f.'/';
		mv_dir($extract_temp_path,$move_template_path,TRUE);
		@unlink($zipfile);
		@delete_folder($extract_temp_path);
		exit($sp->bgmsg('模板安装成功'));
			break;
		default:dump($_POST);exit('invaild post');
	}
}

//只是简单处理了一下
function r($path){
	return trim(str_replace(array('.','/','\\'),'',html_encode($path)));
}
switch ($task){
	case 'delete':
			$folder = r($_GET['folder']);
			//if(empty($folder))header("location:admincp.php?action=templete");
			if($folder=='default')exit($sp->bgmsg('默认皮肤不允许删除'));
			if($folder==get_cache('templete'))exit($sp->bgmsg('正在使用该模板,不能删除'));
			$path = SYS_ROOT.'template/'.$folder.'/';
			if(!file_exists($path))header("location:admincp.php?action=templete");
			clearstatcache();
			$list = read_path(SYS_ROOT.'template/',FALSE);
			if(!in_array($folder,$list))header("location:admincp.php?action=templete");
			delete_folder($path,TRUE);
			$sp->bgmsg('删除模板成功','admincp.php?action=templete');
		break;
	case 'change'://更换模板
			$file = r($_GET['folder']);
			if(empty($file))header("location:admincp.php?action=templete");
			siteConfig('templete',$file);
			$sp->bgmsg('更换模板成功', '?action=templete');
		break;
	case 'bak'://备份模板
			$folder = r($_GET['folder']);
			if(empty($folder))header("location:admincp.php?action=templete");
			require PUB_CLASS.'phpwms.zip.class.php';
			$zip = new Zip();
			$zip->write_file = FALSE;
			$path = SYS_ROOT.'template/'.$folder.'/';
			if(!file_exists($path))header("location:admincp.php?action=templete");
			$name = $folder.'_template_backup.zip';
			$zip->packzip($path,$name);
			$code = $zip->out;
			header('Content-type: application/octet-stream');
			header('Accept-Ranges: bytes');
			header('Accept-Length: '.strlen($code));
			header('Content-Disposition: attachment;filename='.$folder.'_Files.tar.gz');
			echo $code;
			exit;
		break;
	case 'view'://查看图片
		$file = $_GET['file'];
		$path = r($_GET['path']);
		if(empty($file))exit('crack die');
		$files = SYS_ROOT.'template/'.$path.'/'.$file;
		@ob_start();
		@ob_end_clean();
		@ob_end_clean();
		header("Content-type:image/png");
		@readfile($files);
		break;
	default:
			$tpl_list = read_path(TEMPLATE_MAIN_PATH);
			$temp = array();
			foreach ($tpl_list as &$t){
				$f = $t.'wms.xml';
				if(is_file($f))$temp[] = getTplList($f);
			}
			//正在使用中的
			$cache = get_cache('templete');
			if(empty($cache) || !file_exists(SYS_ROOT.'template/'.$cache.'/wms.xml')){
				siteConfig('templete','default');
				$cache = 'default';
			}
			$ary = array_group_by($temp,'folder');
			$keys = array_keys($ary);
			if(in_array($cache,$keys)){
				$used = $ary[$cache][0];
				unset($ary[$cache]);
			}
			$q = array();foreach ($ary as $t)$q[] = $t[0];
			unset($ary);
			$assign_data = array(
				'used'=>$used,
				'other'=>$q,
			);
			$tpl->assign('use',$cache);
			$tpl->assign('tpl_list',$assign_data);
			$tpl -> assign("admin_nav",shownav('模板管理'));
			$tpl->display('templete.tpl');
	}
?>