<?php
/*
 *		Powered by YiCms 
 *
 *		mailto:yixiao@yicms.cn
 *
 */
class TempletsAction extends Action{
    public function checkUser($level){
		if(empty($_SESSION['aid'])) $this->redirect('/Admin/Index/login');
		if(!empty($level) && !in_array($level,$_SESSION['levels'])) $this->error('您没有此权限!.');
	}
    public function index(){
		$this->checkUser('5_1_1');
		$theme = $this->dir(C('ROOT').'/YiCms/Tpl/Home');
		$this->assign('theme',$theme);
		$this->display();
	}
	public function theme(){
		$this->checkUser('5_1_2');
		$config = file_get_contents('config/config.inc.php');
		$new = preg_replace('/\'DEFAULT_THEME\'=>\'(.*?)\',/','\'DEFAULT_THEME\'=>\''.$_GET['name'].'\',',$config);
		if(file_put_contents('config/config.inc.php',$new))	$this->success('更换成功!^_^');
		else $this->error('更换失败!= =重试吧!');
	}
	public function dir($path){
		$this->checkUser();
		if(!is_dir($path)) return;
		$files = array();
		$handle = opendir($path);
		$i=0;
		$config = file_get_contents('config/config.inc.php');
		preg_match('/\'DEFAULT_THEME\'=>\'(.*?)\'/',$config,$nowTheme);
		$nowTheme = $nowTheme[1];
		while(($file = readdir($handle)) != false){
			if($file != '.' && $file != '..'){
				$newPath = $path.'/'.$file;
				if(is_dir($newPath)) $dir[$i]['dir'] = $file;
				if(is_file($newPath.'/screen.jpg')) $dir[$i]['sreen'] = C('URL').'/YiCms/Tpl/Home/'.$file.'/screen.jpg';
				else $dir[$i]['sreen'] = C('URL').'/Public/source/no_screen.jpg';
				if($file == $nowTheme) $dir[$i]['text'] = '<font color="red">正使用</font>';
				else $dir[$i]['text'] = '切换到此模板';
			}
			$i++;
		}
		return($dir);
	}
	public function edit(){
		$this->checkUser('5_2_1');
		$config = file_get_contents('config/config.inc.php');
		preg_match('/\'DEFAULT_THEME\'=>\'(.*?)\'/',$config,$nowTheme);
		$nowTheme = $nowTheme[1];
		$list = $this->file(C('ROOT').'/YiCms/Tpl/Home/'.$nowTheme);
		$this->assign('list',$list);
		$this->display();
	}
	public function file($path){
		$this->checkUser();
		$desc['Index_index.html'] = '主页';
		$desc['Index_nlist.html'] = '内容列表';
		$desc['Index_article.html'] = '内容';
		$desc['Index_page.html'] = '自定义页面';
		$desc['Public_header.html'] = '公用头部';
		$desc['Public_footer.html'] = '公用底部';
		$desc['Index_add.html'] = '添加内容';
		$desc['Index_best.html'] = '热门页面';
		$desc['Index_checkAdd.html'] = '添加内容成功';
		$desc['Index_day.html'] = '24小时内精华';
		$desc['Index_history.html'] = '穿越';
		$desc['Index_month.html'] = '30天内精华';
		$desc['Index_week.html'] = '7天内精华';
		$desc['Index_tag.html'] = '标签搜索';
		$desc['Index_tagcloud.html'] = '标签聚合';
		$desc['Index_ticket.html'] = '审贴页面';
		if(!is_dir($path)) return;
		$files = array();
		$handle = opendir($path);
		$i =0;
		while(($file = readdir($handle)) != false){
			if(substr($file,-4) == 'html'){
				$templets[$i]['name'] = $file;
				$templets[$i]['size'] = filesize($path.'/'.$file);
				$templets[$i]['time'] = filemtime($path.'/'.$file);
				$templets[$i]['edit'] = '<a href="__URL__/editTemplets/name/'.$file.'" style="color:red">[修改]</a>';
				if($desc[$file]){
					$templets[$i]['desc'] = $desc[$file];
				}else{ 
					$templets[$i]['desc'] = '未知模板';
					$templets[$i]['edit'] .= '&nbsp;&nbsp;|&nbsp;&nbsp;<a href="__URL__/delTemplets/name/'.$file.'" onclick="return confirm(\'提示：您确定要删除该模板吗？删了就真的没了!\')">[删除]</a>';
				}
			}
			$i++;
		}
		return($templets);
	}
	public function editTemplets(){
		$this->checkUser('5_2_2');
		$config = file_get_contents('config/config.inc.php');
		preg_match('/\'DEFAULT_THEME\'=>\'(.*?)\'/',$config,$nowTheme);
		$nowTheme = $nowTheme[1];
		$list['name'] = $_GET['name'];
		$list['file'] = 'YiCms/Tpl/Home/'.$nowTheme.'/'.$list['name'];
		$list['content'] = @file_get_contents(C('ROOT').'/YiCms/Tpl/Home/'.$nowTheme.'/'.$list['name']);
		$list['content'] = str_replace('<textarea','##textarea',$list['content']);
		$list['content'] = str_replace('</textarea>','##/textarea>',$list['content']);
		$content = empty($list['content']) ? '<!--模板文件内暂无内容-->' : $list['content'];
		$this->assign('list',$list);
		$this->display();
	}
	public function checkTemplets(){
		$this->checkUser('5_2_2');
		$file = stripslashes($_POST['file']);
		$content = str_replace('##textarea','<textarea',stripslashes($_POST['content']));
		$content = str_replace('##/textarea>','</textarea>',$content);
		$content = empty($content) ? '<!--模板文件内暂无内容-->' : $content;
		if(file_put_contents($file,$content)) $this->success('修改成功!^_^');
		else $this->error('修改失败!= =重试吧!');
	}
	public function delTemplets(){
		$this->checkUser('5_2_3');
		$this->checkUser();
		$name = $_GET['name'];
		$this->checkUser();
		$config = file_get_contents('config/config.inc.php');
		preg_match('/\'DEFAULT_THEME\'=>\'(.*?)\'/',$config,$nowTheme);
		$nowTheme = $nowTheme[1];
		if(@unlink(C('ROOT').'/YiCms/Tpl/Home/'.$nowTheme.'/'.$name)) $this->success('删除成功!^_^');
		else $this->error('删除失败?请检查你是否有该文件的权限!= =重试吧!');
	}
}
?>