<?php
	!defined('DIRCMS_MANAGE') && exit('Access Denied!');
	header("Cache-Control: no-cache, must-revalidate");
	$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
	$r=$r['template']['roleid']; 
	if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');
	$action=empty($action)?'manage':trim($action);

	$c=new cache();
	switch($action)
	{
		case 'manage':
			$t=array();
			$flag=false;
			function cmp($a, $b)
			{
				return strcmp($a["template"], $b["template"]);
			}
			$t=cache_read('template.cache.php',TPL_ROOT.TPL_NAME.'/');
			$tmp=glob(TPL_ROOT.TPL_NAME.'/*.html');
			foreach($tmp as $v)
			{
				$name=basename($v);
				$name=explode('.',$name);
				$name=$name[0];
				if(!array_key_exists($name,$t))
				{
					$flag=true;
					$t[$name]=array('name'=>$name,'template'=>$name);
				}
			}
			if($flag)
			{
				cache_write('template.cache.php',$t,TPL_ROOT.TPL_NAME.'/');
				cache_write('template.cache.php',$t,DIRCMS_ROOT.'data/cache/');
			}

			if($t)
			{
				usort($t,'cmp');
			}
			$r=array();
			if($t)foreach($t as $_t)
			{
				if(substr($_t['template'],0,4)!='tag_')
				{
					if(isset($k) && $k)
					{
						if(strpos($_t['template'],trim($k))!==FALSE || strpos($_t['name'],trim($k))!==FALSE)$r[]=$_t;
					}
					else
					{
						$r[]=$_t;
					}
				}
			}
			$total=intval(sizeof($r)/25);
			$offset=isset($offset)?min(intval(sizeof($r)/25),intval($offset)):0;
			$r=array_slice($r,$offset*25,25);
			include admin_tlp('template_manage');
			break;
		case 'add':
			if(isset($do_submit)) 
			{
				if(!$info['template'])showmsg('文件名称不能为空!',ADMIN_FILE.'?file=template&action=add');
				if(file_exists(TPL_ROOT.TPL_NAME.'/'.$info['template'].'.html'))showmsg('该模板名称已经存在!',ADMIN_FILE.'?file=template&action=add');
				@file_put_contents(TPL_ROOT.TPL_NAME.'/'.$info['template'].'.html',stripslashes($info['content']));
				unset($info['content']);
				$c->cache_template();
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=template&action=manage');
			}
			include admin_tlp('template_add');
			break;
		case 'edit':
			if(isset($do_submit)) 
			{
				$template=$info['template'];
				@file_put_contents(TPL_ROOT.TPL_NAME.'/'.$template.'.html',stripslashes($info['content']));
				unset($info['content']);
				$c->cache_template();
				@unlink($tmppath);
				showmsg($LANG['MANAGE_SUCEESS']);
			}
			
			$r=cache_read('template.cache.php',TPL_ROOT.TPL_NAME.'/');
			$r=$r[trim($id)];	
			if(!file_exists(TPL_ROOT.TPL_NAME.'/'.$r['template'].'.html'))showmsg('文件不存在!');
			$r['content']=file_get_contents(TPL_ROOT.TPL_NAME.'/'.$r['template'].'.html');
			include admin_tlp('template_edit');
			break;
		case 'view':
			$tmppath=DIRCMS_ROOT.'data/tmp/tmp_'.trim($id).$_userid.'.html';
			$tmpurl='data/tmp/tmp_'.trim($id).$_userid.'.html';
			$content=file_get_contents(TPL_ROOT.TPL_NAME.'/'.$id.'.html');
			$tmpcontent=str_replace('<base href="{{$DIRCMS[\'site_url\']}}" />','<base href="'.$DIRCMS['site_url'].'" />',$content);
			$tmpcontent=str_replace('<?php','&lt;?php',$tmpcontent);
			$tmpcontent=str_replace('?>','?&gt;',$tmpcontent);
			$tmpcontent=str_replace('{{$DIRCMS[\'site_url\']}}',$DIRCMS['site_url'],$tmpcontent);
			$tmpcontent=str_replace('{{$DIRCMS[site_url]}}',$DIRCMS['site_url'],$tmpcontent);
			$tmpcontent=str_replace('{{$DIRCMS[\'tpl_name\']}}',$DIRCMS['tpl_name'],$tmpcontent);
			$tmpcontent=str_replace('{{$DIRCMS[tpl_name]}}',$DIRCMS['tpl_name'],$tmpcontent);
			$tmpcontent=preg_replace('/\{\{label:([^\}\}]+)\}\}/ie',"'<a href=\"".ADMIN_FILE."?file=tag&action=content_edit&tagname='.urlencode('\\1').'\" target=\"_blank\" title=\"查看并修改此标签的调用数据\"><font style=\"background-color:#FFFF00\">{{label:\\1}}</font></a>'",$tmpcontent);
			$tmpcontent=preg_replace('/\{\{tlp\s+([a-z0-9_]+)\}\}/i',"<a href='".ADMIN_FILE."?file=template&action=view&id=\\1' title='模板的可视化视图' target='_blank'><font style='background-color:#FF8800'>{{tlp \\1}}</font></a>",$tmpcontent);
			$tmpcontent=preg_replace('/\{\{getTagname\(([0-9]+)\)\}\}/ie',"getTagname(\\1).' <a href=\"".ADMIN_FILE."?file=tag&action=edit_words&id=\\1\" target=\"_blank\"><font style=\"font-size:10px;font-weight:normal\">修改显示文字</font></a>'",$tmpcontent);

			$tmpcontent=preg_replace('/(\{\{dir:category[^\}]+\}\}.+?\{\{\/dir:category\}\})/ise',"'<a href=\"".ADMIN_FILE."?file=tag&action=edit&tlp=".$id."&content='.urlencode('\\1').'\" target=\"_blank\" style=\"width:70px; height:45px;background-color:#FFCC33\" title=\"修改标记\">修改标记</a>'",$tmpcontent);

			$tmpcontent=preg_replace('/\{\{dir:vote\s+action=\"[a-z]+\" id=([\$0-9a-z_]+)\}\}/i',"<a target=\"_blank\" href=\"".ADMIN_FILE."?file=other&action=vote_edit&id=\\1\" style=\"display:block;position:absolute;filter:Alpha(opacity=70);width:70px; line-height:25px;text-align:center;height:25px;color:#1b1b1b;background-color:#FFCC33;z-index:999\" title=\"修改投票\">修改投票</a>",$tmpcontent);

			$tmpcontent=preg_replace('/(\{\{dir:[^\}]+\}\}(.+?)\{\{\/dir:[^\{\}]+\}\})/ise',"'<a href=\"".ADMIN_FILE."?file=tag&action=edit&tlp=".$id."&content='.urlencode('\\1').'\" target=\"_blank\" style=\"display:block;position:absolute;filter:Alpha(opacity=70);width:70px; line-height:25px;text-align:center;height:25px;color:#1b1b1b;background-color:#FFCC33;z-index:999\" title=\"修改标记\">修改标记</a>'",$tmpcontent);
			if(!@file_put_contents($tmppath,$tmpcontent))
			{
				exit('<script language="javascript">{alert("请将您的data/tmp目录置为0777后再进行此操作!");history.back();}</script>');
			}
			include admin_tlp('template_view');
			break;
		case 'checktlp':
			echo file_exists(TPL_ROOT.TPL_NAME.'/'.$data.'.html')?'yes':'no';
			exit();
			break;
		case 'delete':
			@unlink(TPL_ROOT.TPL_NAME.'/'.trim($id).'.html');
			$c->cache_template_delete();
			showmsg($LANG['MANAGE_SUCEESS']);
			break;
		case 'save':
			$tmp=stripslashes($content);
			@file_put_contents('data/tmp.html',$tmp);
	}
?>
