<?php
require_once(dirname(__FILE__)."/admin_inc.php");
/*******************************************************************************************
' Software name: DJCMS Content Management System
' Version:3.0
' Web: http://www.djcms.net
' Author: 心情(qq:1130731)
' Copyright (C) 2009-2010 DJCMS官方 版权所有
' 法律申明：DJCMS程序所有代码100%原创、未引入任何网上代码,对一切抄袭行为、坚决严肃追究法律责任
'*******************************************************************************************/
viewHead("模板管理"."-" .$menuList[2][0]);
$action = getForm("action", "get");
switch ($action) {
	case "add" : addTemplate();break;
	case "custom":CustomList();break;
	case "edit" : editTemplate();break;
	case "save" : saveTemplate();break;
	case "savenew" : saveNewTemplate();break;
	case "del" : delTemplate();break;
	default : main();break;
} 
if ($action != "custom") viewFoot();


function main() {
	global $menuList;
	$dirTemplate = "../template";
	$path=getForm("path","get") ; if (empty($path)) $path= $dirTemplate;
	if (substr($path,0,11) != "../template") alertMsg("只允许编辑template目录","admin_template.php");
	$flist = getFolderList($path);
?>
<div id="append_parent"></div>
<div class="container" id="cpcontainer">
<!--当前导航-->
<script type="text/JavaScript">if(parent.$('admincpnav')) parent.$('admincpnav').innerHTML='后台首页&nbsp;&raquo;&nbsp;<?php echo $menuList[2][0];?>&nbsp;&raquo;&nbsp;模板管理';</script>
<table class="tb" id="templatefilelist">
<tr class="thead"><th colspan="6">模板管理</th></tr>
  <tr>
    <td width="22%"><strong>文件名</strong></td>
	<td width="20%"><strong>模板类型</strong></td>
    <td width="19%"><strong>文件大小</strong></td>
    <td width="17%"><strong>修改时间</strong></td>
    <td width="22%"><strong>操作</strong></td>
  </tr>
  <tr>
  <?php
if (substr($path,strlen($path)-1) == '/') $path = substr($path,0,strlen($path)-1);
$lastLevelPath = substr($path,0,strrpos($path,'/'));
if ($path != '../template') {
?>
    <td  colspan="5">当前目录：<?php echo $path;?></td>
   </tr> 
  <tr align="left" > <td  colspan="4"><a  href="?path=<?php echo $lastLevelPath;?>"><img border=0 src='imgs/last.gif' />上一级目录</a></td>
  <td  colspan="1">
  	  
      <?php if($path=="../template/".defaultTemplate."/".templateFileFolder.""){?>
      <a href="?action=add&filedir=<?=$path?>"><div class="btn" style="width:95px;text-align:center;">添加自定义模板</div></a>
      <?php }?>
	</td>
  </tr>
   <?php
}

foreach ($flist as $finfo) {
    $folder = $path."/".$finfo['filename'];
//if (strpos($folder,"elf_")<1){
?>
	<tr align="left" ><td ><a href="<?php if($finfo['filetype']=='folder'){echo "?path=".$folder;}elseif($finfo['filetype']=='txt'){echo "?action=edit&filedir=".$folder;}else{echo $folder;}?>"><?php echo $finfo['fileicon'];?><?php echo $finfo['filename'];?></a></td><td><?php echo $finfo['fileinfo'];?></td><td><?php echo $finfo['filesize'];?></td><td><span style='font-size:10px'><?php echo $finfo['filetime'];?></span></td> <td><?php if($finfo['filetype']=='folder'){echo "<a href=\"?path=".$folder."\"><img border=0 src='imgs/next.gif' />下一级目录</a>";}elseif($finfo['filetype']=='txt'){echo "<a href='?action=edit&filedir=".$folder."' >编辑</a>&nbsp;&nbsp;&nbsp;<a href='?action=del&filedir=".$folder."' onClick=\"return confirm('确定要删除吗')\">删除</a>";}else{echo "<a href='".$folder."' target=_blank >浏览</a>&nbsp;&nbsp;&nbsp;<a href='?action=del&filedir=".$folder."' onClick=\"return confirm('确定要删除吗')\">删除</a>";}?></td></tr>
<?php
 //}
}
?>
</table>
<script>changeRowColor('templatefilelist','tableRow',4);</script>
<?php
}
function editTemplate() {
	global $menuList;
	$filedir = getForm("filedir","get");
	$fileType = substr($filedir,0,strrpos($filedir,'.'));
	$folder = substr($filedir,0,strrpos($filedir,'/'));
	if ($filetype = ".html" || $filetype = ".htm" || $filetype = ".js" || $filetype = ".css" || $filetype = ".txt"){
?><div class="container" id="cpcontainer">
    <table class="tb">
<tr class="thead"><th colspan="2">修改模板</th></tr>
  <form action="?action=save" method="post"  >
  <input type="hidden" name="refer" value="<?php echo getRefer();?>">
    <tr>
      <td width="8%">文件名称：</td>
      <td width="92%"><input name="name" type="text"  disabled="disabled" size="40" value="<?php echo substr($filedir,strrpos($filedir,'/')+1,strlen($filedir)-1);?>" /> 注意：文件名无法修改
    </tr>
    <tr>
      <td colspan="2"><textarea  name="content" style="width:100%;font-family: Arial, Helvetica, sans-serif;font-size: 14px;" rows="25" dataType="Require" msg="请填写模版内容"><?=new_htmlspecialchars(loadFile($filedir))?></textarea></td>
    </tr>
    <tr>
      <td></td><td ><input type="hidden" name="folder" value="<?php echo $folder;?>"><input name="filedir" type="hidden" value="<?php echo $filedir;?>"><input type="submit" name="Submit" value="修改模板" class="btn" /> <input type="button" name="back" value="返  回"  class="btn" onClick="javascript:history.go(-1);" /></td>
    </tr>
  </form>
</table>
<?php
	} else {
		alertMsg("操作被禁止","admin_template.php");
	}
}
function addTemplate() {
	global $menuList;
	$filedir = getForm("filedir","get");
	$last=getRefer();$ext=explode("|",".html|.htm|.xml|.js|.wml");
	$strlen=count($ext);
	if (strpos($last,"admin_template.asp")==0) $last="?action=custom";
?><div class="container" id="cpcontainer">
    <table class="tb">
<tr class="thead"><th colspan="2">添加自定义模板文件</th></tr>
  <form action="?action=savenew" method="post"  >
    <tr>
      <td width="8%">文件名称：</td>
      <td>self_<input name="name" type="text" size="40" />
      <select name="type">
	  <?
	  for ($i = 0;$i < $strlen;$i++) {
		echo "<option value=\"".$i."\">".$ext[$i]."</option>";
	  }
	  ?>
	  </select>
      <span style="margin-left:80px;">注意：左侧文本框内容为前台生成的文件名</span>
    </tr>
    <tr>
      <td colspan="2"><textarea  name="content" style="width:100%;font-family: Arial, Helvetica, sans-serif;font-size: 14px;" rows="25" dataType="Require" msg="请填写模版内容"></textarea></td>
    </tr>
    <tr>
      <td></td><td ><input name="filedir" type="hidden" value="<?php echo $filedir;?>"><input type="submit" name="Submit" value="添  加" class="btn" /> <input type="button" name="back" value="返  回"  class="btn" onClick="javascript:history.go(-1);" /></td>
    </tr>
  </form>
</table>
<?php

}

function CustomList() {
	global $menuList;
	$path = "../template/".defaultTemplate."/".templateFileFolder;
	$fileList = getFolderList($path);
?>
<div class="container" id="cpcontainer">
<!--当前导航-->
<script type="text/JavaScript">if(parent.$('admincpnav')) parent.$('admincpnav').innerHTML='后台首页&nbsp;&raquo;&nbsp;<?php echo $menuList[2][0];?>&nbsp;&raquo;&nbsp;模板管理';</script>
<form action="admin_makehtml.php?action=custom" method="post" id="customform" target="proxy">
<input type="hidden" name="custom" id="custom" value="">
</form>
<table class="tb" id="templatefilelist">
<tr class="thead"><th colspan="5"><div style="float:left">自定义模板管理：&nbsp;&nbsp;</div><a href="?action=add&filedir=<?php echo $path;?>"><div class="btn" style="float:left;width:95px;text-align:center;font-weight:normal;padding:0; margin:0">添加自定义模板</div></a></th></tr>
  <tr>
    <td width="22%"><strong>文件名</strong></td>
	<td width="20%"><strong>模板类型</strong></td>
    <td width="19%"><strong>文件大小</strong></td>
    <td width="17%"><strong>修改时间</strong></td>
    <td width="22%"><strong>操作</strong></td>
  </tr>
<form action="" method="post" id="makehtml" target="proxy">
<?php
    foreach ($fileList as $finfo) {
		$folder = $path."/".$finfo['filename'];
		if (strpos($folder,"elf_") > 0) {
?>
<tr align="left">
	<td>
    <input type="checkbox" class="checkbox" name="custom[]" value="<?=$finfo['filename']?>" />
		<a href='?action=edit&filedir=<?php echo $folder;?>' ><?php echo $finfo['fileicon'];?><?php echo $finfo['filename'];?></a>
	</td>
	<td>
		<?php echo $finfo['fileinfo'];?></td><td><?php echo $finfo['filesize'];?></td><td><?php echo $finfo['filetime'];?></td><td>
		<a href='<?php echo $folder;?>' target=_blank >浏览</a>&nbsp;&nbsp;&nbsp;
		<a href="javascript:$('custom').value='<?php echo $finfo['filename'];?>';$('customform').submit()" >生成</a>&nbsp;&nbsp;&nbsp;<a href='?action=edit&filedir=<?php echo $folder;?>' >编辑</a>&nbsp;&nbsp;&nbsp;
		<a href='?action=del&filedir=<?php echo $folder;?>' onClick=""return confirm('确定要删除吗')"">删除</a>
	</td>
</tr>
<?php
	}
}
?>
<tr align="left">
	<td colspan="5"><div class="cuspages"><div class="pages">全选<input type="checkbox" name="chkall" id="chkall" class="checkbox" onclick="checkAll(this.checked,'input','custom[]')" />反选<input type="checkbox" name="chkothers" id="chkothers" class="checkbox" onclick="checkOthers('input','custom[]')" /><input type="submit" value="批量生成" class="btn" onclick="$('makehtml').action='admin_makehtml.php?action=custom'" /></div></div></td>
</tr>
</form>
</table>
</div>
<iframe width="100%" height="0" frameborder="0" scrolling="auto" src="about:blank" align="middle" name="proxy" onload="var _1=this.contentWindow;if(_1.document.URL!='about:blank'){this.style.height=_1.document.body.scrollHeight+'px';}"></iframe>
<?php
echo "</body></html>";
}

function saveTemplate() {
	$ref = getForm("refer","post");
	$content = getForm("content","post");
	$filedir = getForm("filedir","post");
	$folder = getForm("folder","post");
	$fileType = substr($filedir, 0, strrpos($filedir,'.'));
	if (strpos($filedir,";")>0){ alertMsg("操作被禁止",$ref);die();}
	if ($filetype = ".html" || $filetype = ".htm" || $filetype = ".js" || $filetype = ".css" || $filetype = ".txt" || $filetype = ".xml" || $filetype = ".wml"){
		createTextFile(new_stripslashes($content), $filedir);
		alertMsg("操作成功", $ref);
	} else {
		alertMsg("操作被禁止", $ref);
	}
}

function delTemplate() {
	$filedir = getForm("filedir","both");
	$folderstr = substr($filedir, 0, strrpos($filedir,'/'));
	delFile($filedir);
	alertMsg("成功删除", getRefer());
} 

function saveNewTemplate() {
	$file = explode("|",".html|.htm|.xml|.js|.wml");
	$type = getForm("type","both");
	$filetype = $file[$type];
	$content = getForm("content","post");
	$filedir = getForm("filedir","post");
	if ($filedir!="../template/".defaultTemplate."/".templateFileFolder) die("只能把模板添加在"."template/".defaultTemplate."/".templateFileFolder."文件夹");
	$fileName = getForm("name","post");
	if (empty($fileName)){ alertMsg("请填写文件名","");last();die();}
	if (strpos($filedir."/self_".$fileName.$filetype,";")>0){ alertMsg("操作被禁止","");last();}
	if (isExistFile($filedir."/self_".$fileName.$filetype)){
		alertMsg("已存在该文件请更换名称","");last();
	} else {
		createTextFile(new_stripslashes($content), $filedir."/self_".$fileName.$filetype);
		alertMsg("操作成功","admin_template.php?action=custom");
	}
}

function getTemplateType($filename) {
	switch (strtolower($filename)) {
		case "index.html":
			$getTemplateType = "首页模板";break;
		case "head.html":
			$getTemplateType = "模板头文件";break;
		case "foot.html":
			$getTemplateType = "模板尾文件";break;
		case "channel.html":
			$getTemplateType = "分类页模板";break;
		case "content.html":
			$getTemplateType = "内容页模板";break;
		case "play.html":
			$getTemplateType = "播放页模板";break;
		case "openplay.html":
			$getTemplateType = "播放页模板(弹窗模式)";break;
		case "map.html":
			$getTemplateType = "HTML地图页模板";break;
		case "search.html":
			$getTemplateType = "搜索页模板";break;
		case "topic.html":
			$getTemplateType = "专题页模板";break;
		case "topicindex.html":
			$getTemplateType = "专题首页模板";break;
		case "news.html":
			$getTemplateType = "新闻内容页模板";break;
		case "newsindex.html":
			$getTemplateType = "新闻首页模板";break;
		case "js.html":
			$getTemplateType = "JS远程调用页模板";break;
		case "gbook.html":
			$getTemplateType = "留言本页面模板";break;
		default:
			if(stristr($filename, '.gif') || stristr($filename, '.jpg') || stristr($filename, '.png')) {
				$getTemplateType = "图片文件";
			} else if (stristr($filename, '.css')) {
				 $getTemplateType = "样式文件";
			} else if (stristr($filename, '.js')) {
				 $getTemplateType = "脚本文件";
			} else if (stristr($filename, 'self')) {
				 $getTemplateType = "自定义模板";
			} elseif (stristr($filename, 'http.txt')) {
				 $getTemplateType = "伪静态配置模板";
			} else if (stristr($filename, '.html') || stristr($filename, '.htm')) {
				 $getTemplateType = "静态页面文件";
			} else {
				$getTemplateType = "其它文件";
			}
	}
	return $getTemplateType;
}

function getFileType($filedir) {
	if (!preg_match("/\./", $filedir)) {
		return "folder";
	} else {
		$filetype = strtolower(fileext($filedir));
		$imgFileStr = ".jpg|.jpeg|.gif|.bmp|.png";
		$pageFileStr = ".html|.htm|.js|.css|.txt|.xml|.wml";
		if (strpos($imgFileStr, $filetype) > 0) return "img";
		if (strpos($pageFileStr, $filetype) > 0) return "txt";
	}
}

function viewIcon($filename) {
	if (!preg_match("/\./", $filename)) {
		$icon="<img  border=0 src='imgs/folder.gif' width='16' height='16' align='absmiddle' /> ";
	} else {
	$fileType = ".".strtolower(fileext($filename));
	if (strpos(" ".".js,.css",$fileType)>0){
		$icon="<img  border=0 src='imgs/".replaceStr($fileType,".","").".gif' width='16' height='16' align='absmiddle' /> ";
    }else{
		if ($fileType == ".jpg" or $fileType == ".jpeg"){
			$icon="<img border=0 src='imgs/jpg.gif' width='16' height='16' align='absmiddle' /> ";
		}elseif ($fileType == ".htm" || $fileType == ".html" || $fileType == ".shtml" || $fileType == ".xml"){
			$icon="<img border=0 src='imgs/html.gif' width='16' height='16' align='absmiddle' /> ";
        }elseif ($fileType == ".gif" || $fileType == ".png"){
			$icon="<img border=0 src='imgs/gif.gif' width='16' height='16' align='absmiddle' /> ";
        }else{
			$icon="<img border=0 src='imgs/file.gif' width='16' height='16' align='absmiddle' /> ";
        }
    }
	}
	return $icon;
}

function fileext($filename) { 
	$extend = explode(".", $filename);
	$va = count($extend)-1;
	return $extend[$va];
}

function getFolderList($cDir) {
	$dh = dir($cDir);
	$k=0;
	while ($filename = $dh->read()) {
		if ($filename == '.' || $filename == '..' || preg_match("/\.inc/", $filename)) continue;
		$filetime = filemtime($cDir.'/'.$filename);
		$f[$k]['filetime'] = isCurrentDay($filetime);
		$f[$k]['filename'] = $filename;
		if (!preg_match("/\./", $filename)) {
			$f[$k]['fileinfo'] = "文件夹";
		} else {
			$f[$k]['fileinfo'] = getTemplateType($filename);
		}
		if (!preg_match("/\./", $filename)) {
			$f[$k]['filesize'] = getRealSize(getDirSize($cDir.'/'.$filename));
		} else {
			$f[$k]['filesize'] = getRealSize(filesize($cDir.'/'.$filename));
		}
		$f[$k]['fileicon'] = viewIcon($filename);
		$f[$k]['filetype'] = getFileType($filename);
		$k++;
	}
	return $f;
}

function getDirSize($dir) {
	$dh = opendir($dir);
	$size = 0;
	while ($file = readdir($dh)) {
		if ($file != '.' and $file != '..') {
			$path = $dir."/".$file;
			if (@is_dir($path)) {
				$size += getDirSize($path);
			} else {
				$size += filesize($path);
			}
		}
	}
	@closedir($dh);
	return $size;
}

function getRealSize($bytes) {
	if (!preg_match("/^[0-9]+$/", $bytes)) return 0;
	$sizes = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB' );
	$extension = $sizes[0];
	for ( $i = 1; ( ( $i < count( $sizes ) ) && ( $bytes >= 1024 ) ); $i++ ) {
		$bytes /= 1024;
		$extension = $sizes[$i];
	}
	return round( $bytes, 2 ) . ' ' . $extension;
}
?>
