<?php
/*
'**************************************************************************************************
' 软件名称: 搜一次 Content Management System
' 版本编号: Version 1.X
' 官方网站: http://www.syccms.com
' 官方论坛：http://bbs.syccms.com
' 版权所有: 搜一次运营团队
' 法律顾问: 杭州市元茂律师事务所 严飞律师
' 郑重声明:
'    1、任何个人或组织不得在未经授权的情况下删除、修改、拷贝本软件及其他副本上一切关于版权的信息；
'    2、搜一次网络事业部保留此软件的法律追究权利
'**************************************************************************************************
*/
include "admin_loginstate.php";
include "../include/conn.php";
include "../include/function.php";
include "admin_version.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link  href="images/style.css" rel="stylesheet" type="text/css" />
<title>模板管理</title>
<script type="text/javascript" language="javascript">var tentpath='<?php echo S_SitePath?>';</script>
<script src="js/jquery.js"></script>
<script src="js/alerts.js"></script>
<link href="images/alerts.css" rel="stylesheet" type="text/css" media="screen" />
<script language="javascript">
$(document).ready(function(){
	$("#addtemp").click(function(){
		 var S_Name=$("#S_Name").val();
		 var S_TempPath=$("#S_TempPath").val();
		 var S_TheOrder1=$("#S_TheOrder1").val();
		 $("#S_Name").css("border-color","#666 #ccc #ccc #666");
		 $("#S_TempPath").css("border-color","#666 #ccc #ccc #666");
		 $("#tishi").text("");
		 if (S_Name==""){
			 $("#S_Name").css("border","1px solid red");
			 $("#tishi").text("请填写模板名！").css("color","red");
			 return false;}
		 else if(S_TempPath==""){
			 $("#S_TempPath").css("border","1px solid red");
			 $("#tishi").text("请填写模板路径！").css("color","red");
			 return false;}
		 else if(S_Name.length > 50){
			 $("#tishi").text("模板名不能超过50个字符！").css("color","red");
		 	return false;}
		 else if(S_TempPath.length > 100){
			 $("#tishi").text("模板路径不能超过100个字符！").css("color","red");
			 return false;}
		 else if(S_TheOrder1.length > 10){
			 $("#tishi").text("排序不能超过10个字符！").css("color","red");
			 return false;}
		 
	})
})
function del(id){
	jConfirm('您确定要执行删除操作吗?', '操作提示', function(r) {
		if(r==true){
			window.location='?action=del&id='+id;
			}
				});
		
		}
function delfile(name){
	jConfirm('您确定要执行删除操作吗?', '操作提示', function(r) {
		if(r==true){
			window.location='?action=delefiles&filedir='+name;
			}
				});
		
	};
	
	
</script>
</head>

<body>
<div class="r_main">
	<div class="r_content">
		<div class="r_content_1">
<?php
class ui_temp{
	public function templist(){
	
	if(trim(SafeRequest("type","get"))<>"")
	{
		if(file_exists("user/" . trim(SafeRequest("type","get"))))
		{
			include ("user/" . trim(SafeRequest("type","get")));
		}
	}

	if(trim(SafeRequest("file","get"))<>"")
	{
		$path = SafeRequest("path","get");
		if(file_exists($path  .trim(SafeRequest("file","get"))) && is_file($path  .trim(SafeRequest("file","get"))))
		{
?>
	<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tb_style">
	<tr class="td_title"><th height="30" colspan="2" align="left" class="td_title">&nbsp;修改模板[<a style="cursor:pointer" onclick="history.go(-1);">返回</a>]</th>
	</tr>
	  <form action="?action=save" method="post"  >
		<tr>
		  <td width="13%" height="30" class="td_border">&nbsp;模板文件名称：</td>
		  <td width="87%"><input name="FileName" type="text" size="30" value="<?php echo SafeRequest("file","get");?>" />
		</tr>
		<tr>
		  <td colspan="2" class="td_border"><textarea  name="content" style="width:99%;font-family: Arial, Helvetica, sans-serif;font-size: 14px;" rows="25"><?php echo file_get_contents($path  .trim(SafeRequest("file","get")))?></textarea></td>
		</tr>
		<tr>
		  <td></td><td ><input type="hidden" name="folder" value="<?php echo $path?>"><input name="tempname" type="hidden" value="<?php echo SafeRequest("tempname","get")?>"><input name="Submit" type="submit" class="rb1" value="修改当前模板" /> 
		 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   <input name="back" type="button" class="rb1"  onClick="javascript:location.href='admin_template.php'" value="返回模板管理" /></td>
		</tr>
	  </form>
</table>

<?php			
		}
	}
	else
	{
		if(trim($_GET['dir'])<>"")
		{
			$path = trim($_GET['dir']);
		}
		else
		{
			$path = getcwd();
		}
		//$_SESSION['dir'] = $path;
?>
        <table width="100%"   cellspacing="0" class="tb_style">
              <tr>
                <td height="30" colspan="5" class="td_title" >&nbsp;[<?php echo SafeRequest("tempname","get");?>]文件管理 </td>
              </tr>
              <tr>
                <td width="20%" height="30" class="td_border" >&nbsp; 模板文件名</td>
                <td width="22%" align="center" class="td_border" >模板类型</td>
                <td width="18%" align="center" class="td_border">文件大小</td>
                <td width="23%" align="center" class="td_border" >修改时间</td>
                <td width="17%" align="center" class="td_border" >操作</td>
              </tr>	
<?php		
		if(file_exists($path) && is_dir($path))
		{
			$d = dir($path); 
			while(false !== ($v = $d->read()))
			{
				if($v == "." || $v == "..")
				{
					continue; 
				}
				$file = $d->path."\\".$v; 
				if(is_dir($file))
				{
   				echo "<a href='?action=templist&path=".$path."&dir=$v'>[$v]</a><br />";
				}
			}
			$d->rewind();
?>
	
<?php		
			while(false !== ($v = $d->read()))
			{
				
				if($v == "." || $v == "..")
				{
					continue; 
				}
				$file = $d->path . $v; 
				if(is_dir($file))
				{
					continue;
				}
				if(is_file($file))
				{
?>
              <tr>
                <td width="20%" height="30" class="td_border" >&nbsp;<a href='?action=templist&path=<?php echo $path?>&tempname=<?php echo SafeRequest("tempname","get");?>&file=<?php echo $v?>'> <?php echo $v?></a></td>
                <td width="22%" align="center" class="td_border" >&nbsp;<?php echo getTemplateType($v);?></td>
                <td width="18%" align="center" class="td_border">&nbsp;<?php echo round(filesize($file)/1204,2)."Kb"?></td>
                <td width="23%" align="center" class="td_border" >&nbsp;<?php echo date('Y-m-d H:i:s',filemtime($file))?></td>
                <td width="17%" align="center" class="td_border" >&nbsp;<a href='?action=templist&path=<?php echo $path?>&tempname=<?php echo SafeRequest("tempname","get");?>&file=<?php echo $v?>'>编辑</a>&nbsp;&nbsp; <a style="cursor:pinter" onclick="window.location='?action=copyfile&path=<?php echo $path?>&file=<?php echo $v?>&tempname=<?php echo SafeRequest("tempname","get")?>'">复制</a>&nbsp;&nbsp; <a style="cursor:pinter" onclick="window.location='?action=delfile&path=<?php echo $path?>&file=<?php echo $v?>&tempname=<?php echo SafeRequest("tempname","get")?>'">删除</a></td>
              </tr>
<?php					
				}
			}
?>
 </table>
 <?php	
		$d->close();
		}
	}

}
	
	public function main(){
		global $db;
		$sql="select * from s_skins";
		$result=$db->query($sql);
		$tempcount=$db->num_rows($result);
?>
<table width="100%"  border="0" cellpadding="0" cellspacing="0" class="tb_style">
  <tr>
    <td colspan="10" height="30" class="td_title">&nbsp;网站模版管理</td>
  </tr>
<?php
if($tempcount=="0"){
?>
  <tr>
    <td height="30" colspan="10" align="center" class="td_border">&nbsp;&nbsp;<strong>暂无模板,请先添加！</strong></td>
  </tr>
<?php	
	}
else{
	if($result){
		while($row=$db->fetch_array($result)){
?>
      <form action="?action=tedit" method="post">
  <tr>
    <td height="30" class="td_border">&nbsp;模版名称：</td>
    <td class="td_border">&nbsp;<input type="text" name="S_Name" value="<?php echo $row['S_Name']?>"></td>
    <td class="td_border">&nbsp;模版路径：
      <input type="hidden" name="S_ID" value="<?php echo $row['S_ID']?>"></td>
    <td class="td_border">&nbsp;<input type="text" name="S_TempPath" value="<?php echo $row['S_TempPath']?>"></td>
    <td class="td_border">&nbsp;排序：</td>
    <td class="td_border">&nbsp;<input type="text" size="4" id="S_TheOrder" name="S_TheOrder" value="<?php echo $row['S_TheOrder']?>" onkeyup="this.value=this.value.replace(/[^\d]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"></td>
    <td class="td_border">&nbsp;<input type="hidden" name="isde" <?php if(S_Templateurl==$row['S_TempPath']){?> value="1" <?php }else{?>value="0"<?php }?> ><input type="button"  <?php if(S_Templateurl==$row['S_TempPath']){?> value="默认模板" disabled="disabled" <?php }else{?>value="切换模板"<?php }?>   onclick="javacript:window.location.href='?action=temp&path=<?php echo $row['S_TempPath']?>'" class="rb1"/></td>
    <td class="td_border">&nbsp;<input type="submit" value="修改" class="rb1"></td>
	<td class="td_border">&nbsp;<input type="button" class="rb1" value="管理" onClick="javascript:window.location='?action=templist&tempname=<?php echo $row['S_Name']?>&dir=../<?php echo $row['S_TempPath']?>'"></td>
    <td class="td_border"><input type="button" value="删除" <?php if(S_Templateurl==$row['S_TempPath']){?> disabled="disabled"<?php }?>  onclick="del(<?php echo $row['S_ID']?>)"  class="rb1"></td>
    
  </tr>
</form>                 
<?php
			
			}
		}
	}	
?>
</table>


 <form action="?action=addsave" method="post">
 <table width="100%"  border="0" cellpadding="0" cellspacing="0"  class="tb_style">
  <tr>
    <td colspan="7" class="td_title">&nbsp;网站模版添加&nbsp;&nbsp; <label id="tishi"></label></td>
  </tr>
  <tr>
    <td height="30" class="td_border">&nbsp;模版名称：</td>
    <td class="td_border">&nbsp;<input type="text" name="S_Name" id="S_Name"></td>
    <td class="td_border">&nbsp;模版路径：</td>
    <td style="font-size:12px; color:red;" class="td_border">&nbsp;<input type="text" name="S_TempPath" id="S_TempPath">
    如：skins/default/html/</td>
    <td class="td_border">&nbsp;排序：</td>
    <td class="td_border">&nbsp;<input type="text" size="4" name="S_TheOrder1" id="S_TheOrder1" value="0" onkeyup="this.value=this.value.replace(/[^\d]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"></td>
    <td class="td_border">&nbsp;<input type="submit" value="添加模版" class="rb1" id="addtemp"></td>
  </tr>
</table>

 </form>
 		</div>
	</div>
</div> 
<?php		
		}
	}
?>        

<?php
$ui=new ui_temp;
$action=SafeRequest("action","get");
switch($action){
	case 'main':
		$ui->main();
		break;
	case 'addsave':
		addsave();
		break;
	case 'templist':
		templist();
		break;
	case 'tedit':
		tedit();
		break;
	case 'temp':
		temp();
		break;
	case 'del':
		del();
		break;
	case 'save':
		save();
		break;
	case 'copyfile':
		copyfile();
		break;
	case 'delfile':
		delfile();
		break;
	default:
		$ui->main();
	}
	
function del(){
		global $db;
		$S_ID=SafeRequest("id","get");
		$sql="delete from s_skins where S_ID='".$S_ID."'";
		//die($sql);
		if($db->query($sql)){
			echo "<script>window.location='admin_template.php';</script>";
			}
	}	
//切换模板
function temp(){
	$S_Path=SafeRequest("path","get");
	$str=file_get_contents("../include/config.php"); 
	$str=preg_replace('/"S_Templateurl","(.*?)"/','"S_Templateurl","'.$S_Path.'"',$str);
	$ifile = new iFile('../include/config.php','w');
	$ifile->WriteFile($str,3); 
	echo "<script>window.location='admin_template.php';</script>";
	}

function delfile(){
	$S_Name=SafeRequest("file","get");
	$S_Path=SafeRequest("path","get");
	$S_TempName=SafeRequest("tempname","get");
	//die($S_Path.$S_Name);
	if(file_exists($S_Path.$S_Name)){
		unlink($S_Path.$S_Name);
		die("<script>alert('恭喜，模版文件删除！');window.location='?action=templist&tempname=".$S_TempName."&dir=".$S_Path."';</script>");
		}
	else{
		die("<script>alert('模板文件不存在删除失败！');window.location='?action=templist&tempname=".$S_TempName."&dir=".$S_Path."';</script>");
		}	
	}

function copyfile(){
	$S_Name=SafeRequest("file","get");
	$S_Path=SafeRequest("path","get");
	$S_TempName=SafeRequest("tempname","get");
	//die($S_Path.$S_Name);
	if(copy($S_Path.$S_Name,$S_Path."复制".$S_Name)){
		die("<script>alert('恭喜，模版文件复制！');window.location='?action=templist&tempname=".$S_TempName."&dir=".$S_Path."';</script>");
		}	
	}	
	
function save(){
	$S_Name=SafeRequest("FileName","post");
	$S_Path=SafeRequest("folder","post");
	$S_TempName=SafeRequest("tempname","post");
	$S_Content=stripslashes(SafeRequest("content","post"));
	//die($S_Content);
	$ifile = new iFile($S_Path.$S_Name,'w'); 
	$ifile->WriteFile($S_Content,3);
	//header("Location:admin_template.php?action=templist&tempname=".$S_TempName."&dir=".$S_Path."");
	echo "<script>window.location='admin_template.php?action=templist&tempname=".$S_TempName."&dir=".$S_Path."'</script>";
	}	
	
function templist(){
	global $ui;
	//session_destroy();
	$ui->templist();
	}
	
function tedit(){
	global $db;
	$S_ID=SafeRequest("S_ID","post");
	$S_Name=SafeRequest("S_Name","post");
	$S_TempPath=SafeRequest("S_TempPath","post");
	$S_TheOrder=SafeRequest("S_TheOrder","post");
	$sql="update s_skins set S_Name='".$S_Name."',S_TempPath='".$S_TempPath."',S_TheOrder=".$S_TheOrder." where S_ID=".$S_ID."";
				//下面开始替换.asp成.php
if(file_exists("../".$S_TempPath)){
			$TempArr=array("index.html","head.html","bottom.html","allmap.html","artlist.html","artshow.html","arttopic.html","gbook.html","search.html","topic.html","vodlist.html","vodplay.html","vodshow.html","vodtopic.html");
			for($i=0;$i<count($TempArr);$i++){
				if(file_exists("../".$S_TempPath.$TempArr[$i])){
						$Content=file_get_contents("../".$S_TempPath.$TempArr[$i]);
						$Content=ReplaceStr($Content,".asp",".php");
						fwrite(fopen("../".$S_TempPath.$TempArr[$i],"wb"),$Content);
					}
				}
			unset($TempArr);	
			}
	else{
		die("<script>alert('模版文件夹不存在！');history.back();</script>");
		}	

	if($db->query($sql)){
		echo "<script>window.location='admin_template.php';</script>";
		}
	else{
		die("<script>jAlert('模版编辑失败!','操作提示',function(R){history.back();})</script>");
		}	
	}

function addsave(){
	global $db;
	$S_Name=SafeRequest("S_Name","post");
	$S_TempPath=SafeRequest("S_TempPath","post");
	$S_TheOrder=SafeRequest("S_TheOrder1","post");
	$sql="Insert s_skins (S_Name,S_tempPath,S_TheOrder) values ('".$S_Name."','".$S_TempPath."',".$S_TheOrder.")";
	//die($sql);
			//下面开始替换.asp成.php
if(file_exists("../".$S_TempPath)){
			$TempArr=array("index.html","head.html","bottom.html","allmap.html","artlist.html","artshow.html","arttopic.html","gbook.html","search.html","topic.html","vodlist.html","vodplay.html","vodshow.html","vodtopic.html");
			for($i=0;$i<count($TempArr);$i++){
				if(file_exists("../".$S_TempPath.$TempArr[$i])){
						$Content=file_get_contents("../".$S_TempPath.$TempArr[$i]);
						$Content=ReplaceStr($Content,".asp",".php");
						fwrite(fopen("../".$S_TempPath.$TempArr[$i],"wb"),$Content);
					}
				}
			unset($TempArr);	
			}
	else{
		die("<script>alert('模版文件夹不存在！');history.back();</script>");
		}	
			
	if($db->query($sql)){
		echo "<script>window.location='admin_template.php';</script>";
		}
	else{
		die("<script>jAlert('模板添加失败!','操作提示',function(R){history.back();})</script>");
		}
	}
	
function getTemplateType($filename){
	switch(strtolower($filename)){
		case 'index.html':
			$getTemplateType="网站首页";
			break;
		case "head.html":
			$getTemplateType="网站顶部";
			break;
		case "bottom.html":
			$getTemplateType="网站底部";
			break;
		case "vodplay.html":
			$getTemplateType="播放页面";
			break;
		case "allmap.html":
			$getTemplateType="网站地图";
			break;
		case "search.html":
			$getTemplateType="网站搜索";
			break;
		case "topic.html":
			$getTemplateType="网站专题";
			break;
		case "arttopic.html":
			$getTemplateType="文章专题";
			break;
		case "vodtopic.html":
			$getTemplateType="视频专题";
			break;
		case "artlist.html":
			$getTemplateType="文章列表";
			break;
		case "vodlist.html":
			$getTemplateType="视频列表";
			break;
		case "artshow.html":
			$getTemplateType="文章内容";
			break;
		case "vodshow.html":
			$getTemplateType="视频内容";
			break;
		case "gbook.html":
			$getTemplateType="留言模板";
			break;
		case "artindex.html":
			$getTemplateType="文章首页";
			break;
		case "vodindex.html":
			$getTemplateType="视频首页";
			break;	
		case "loginuser.html":
			$getTemplateType="会员登陆";
			break;	
		case "logininfo.html":
			$getTemplateType="会员信息";
			break;	
		default:
			if(stristr($filename,'.gif') or stristr($filename,'.jpg') or stristr($filename,'.png')){
				$getTemplateType="图片文件";
				}
			elseif(stristr($filename,'.css')){
				 $getTemplateType="样式文件";
				}
			elseif(stristr($filename,'.js')){
				$getTemplateType="脚本文件";
				}	
			else{
				$getTemplateType="其它文件";
				}
	}
	return $getTemplateType;
}	
?>       
<?php include "admin_bottom.php"; ?>
</body>
</html>