<?php
	!defined('DIRCMS_MANAGE') && exit('Access Denied!');
	$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();
	$projects=cache_read('template.inc.php',TPL_ROOT);
	$dirproject=TPL_ROOT;
	switch($action)
	{
		case 'manage':
			if(isset($do_submit)) 
			{
				$i=0;
				foreach($dir as $val)
				{
					$info[$val]=$name[$i];
					$i++;
				}
				cache_write('template.inc.php',$info,TPL_ROOT);
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=templateproject&action=manage');
			}
			$list=glob($dirproject.'*');
			$files=glob($dirproject.'*.*');
			$dirs=array_diff($list,$files);
			$r=array();
			foreach($dirs as $v)
			{
				if(basename($v)!='comtmp')
				{
					
					$tr['dir']=basename($v);
					$tr['name']=isset($projects[$tr['dir']])?$projects[$tr['dir']]:'';
					$tr['mtime']=date('Y-m-d H:i:s',filemtime($v));
					$tr['isdefault']=$tr['dir']==TPL_NAME?1:0;
					$r[$tr['dir']]=$tr;
				}
			}
			unset($tr);
			include admin_tlp('templateproject_manage');
			break;
		case 'setdefault':
			@set_time_limit(0);
			$cachefiles=glob(DIRCMS_ROOT.'data/cache_template/*.tlp.php');
			foreach($cachefiles as $cachefile)
			{
				@unlink($cachefile);
			}
			
			$t=cache_read('template.inc.php',TPL_ROOT);
			$t[$project]=urldecode($projectname);
			$t=array_unique($t);
			cache_write('template.inc.php',$t,TPL_ROOT);
			if(!file_exists(TPL_ROOT.$project.'/template.cache.php'))cache_write('template.cache.php',array(),TPL_ROOT.$project.'/');
			set_config(array('TPL_NAME'=>$project,'TLP_NAME'=>$project));
			
			$cache=file_get_contents(TPL_ROOT.$project.'/template.cache.php');
			if(!$cache || !file_exists(TPL_ROOT.$project.'/template.cache.php'))
			{
				$cache="<?php\n\r\n?>";
			}
			file_put_contents(TPL_ROOT.$project.'/template.cache.php',$cache);
			$c->cache_template();
			$DIRCMS['tlp_name']=$DIRCMS['tpl_name']=$project;
			cache_config('common.cache.php',$DIRCMS);

			showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=templateproject&action=manage');
			break;
		case 'delete':
			@set_time_limit(0);
			$cachefiles=glob(DIRCMS_ROOT.'data/cache_template/*.tlp.php');
			foreach($cachefiles as $cachefile)
			{
				@unlink($cachefile);
			}
			if(!array_key_exists($project,$projects))showmsg($LANG['MANAGE_ERROR'],ADMIN_FILE.'?file=templateproject&action=manage');
			$info=$projects;
			unset($info[$project]);
			cache_write('template.inc.php',$info,TPL_ROOT);
			delete_dir($dirproject.$project);
			showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=templateproject&action=manage');
			break;
	}
?>
