<?php
# Name: PHP版程氏音乐CMS管理系统 v3.0
# Author: 程氏<web@chshcms.com> [QQ:848769359]
# Homepage:http://www.chshcms.cn/
include_once("../../lib/cs_label.php");
include_once("../loginstate.php");
SystemPer('7_1');
$ac=CS_Request("ac");
$op=CS_Request("op");
?>
<!doctype html>
<html>
<head>
<meta charset="gbk">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>chshdjcms management</title>
<link href="../res/css/admin_style.css" rel="stylesheet" />
<link href="../../public/css/webox.css" rel="stylesheet" />
<script type="text/javascript" src="../res/js/ajax.js"></script>
<script language="javascript" src="../res/js/jquery.js"></script>
<script type="text/javascript" src="../../public/js/webox.js"></script>
<script language="javascript"> 
function del(_id){ 
  if (confirm("确定删除吗？")){
    window.location='?op=del&id='+_id;
    return true;
  }else{
     return false;
  }		
}
function delmb(_id){ 
  if (confirm("确定删除吗？")){
    window.location='?ac=show&op=del&mb='+_id;
    return true;
  }else{
     return false;
  }		
}
$(document).ready(function(){
	$("#mbedit").click(function(){
		 var name=$("#name").val();
		 var content=$("#content").val();
		 $("#name").css("border-color","#666 #ccc #ccc #666");
		 $("#content").css("border-color","#666 #ccc #ccc #666");
		 if (name==""){
			 $("#name").css("border","1px solid red");
			 $("#Msg").text("请填写模板文件名称！").css("color","red");
			 return false;
                 }else if(content==""){
			 $("#content").css("border","1px solid red");
			 $("#Msg").text("请填写模板内容！").css("color","red");
			 return false;
                 }
	})
})
</script>
</head>
<body>
<div class="wrap">
<!-- start -->
<div class="nav">
	<ul class="cc">
			<li class="current"><a href="?">模版管理</a></li>
		        <li><a href="javascript:Popsboxif('添加模版',450,310,'skins_add.php');">添加模版</a></li>
	</ul>
</div>
<?php 
if($ac=='xgmb'){
    $name=CS_Request("name");
    $path=CS_Request("path");
    $mbneir=file_get_contents($path.$name);
?>
	<div class="h_a">模版编辑</div>
	<form action="?ac=xgmb&op=go&path=<?php echo $path?>" method="post" class="J_ajaxForm" id="J_backup_form">
	<div class="table_full">
		<table width="100%">
			<tr><td>模版文件名：<input type="text" id="name" name="name" class="input length_3" value="<?php echo $name?>">&nbsp;&nbsp;</td></tr>
			<tr>
				<td>
                                 <textarea id="content" name="content" style="width:99%;height:350px;font-family: Arial, Helvetica, sans-serif;font-size: 14px;"><?php echo $mbneir?></textarea>
				</td>
			</tr>
		</table>
	</div>
	<div class="btn_wrap">
		<div class="btn_wrap_pd">
		<button class="btn btn_submit J_ajax_submit_btn" id="mbedit" type="submit">修改</button>
		<button class="btn J_ajax_submit_btn" type="button" onClick="window.location='javascript:history.go(-1);'">返回上一页</button>
		<div class="tips_error" id="Msg"></div>
		</div>
	</div>
	</form>
<?php
      if($op=='go'){
           $name=CS_Request("name");
           $path=CS_Request("path");
           $content=stripslashes(trim($_POST["content"]));
           if(write_file($path.$name, $content)){  //写入
                 echo '<script>document.getElementById("Msg").className="tips_success";$("#Msg").text("恭喜,修改成功!").css("color","#026E12");</script>';
	         echo "<script>setTimeout(\"window.location='?ac=show&path=".CS_ReplaceStr($path,'../../','')."'\",2000);</script>";
           }else{
                 echo '<script>$("#Msg").text("抱歉,修改失败!").css("color","red");</script>';
	         echo "<script>setTimeout(\"window.location='?ac=show&path=".CS_ReplaceStr($path,'../../','')."'\",2000);</script>";
           }
      }

}elseif($ac=='show'){ 
     $path=CS_Request("path");
?>
<div class="h_a">当前编辑模版路径:&nbsp;(<?php echo $path?>) &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;<a href="skins.php"><font color=#ff6600>返回模版首页</font></a></div>
<div class="table_list">
<table width="100%" id="dellist">
		<thead>
			<tr>
				<td>模版文件名</td>
				<td><span>模板类型</span></td>
				<td><span>文件大小</span></td>
				<td><span>修改时间</span></td>
				<td>操作</td>
			</tr>
		</thead>
<?php
   if(substr($path,0,5)=="skins"){
	$dir = "../../".$path;
	if(is_dir($dir)){ //是否为文件夹
		if ($dh = opendir($dir)) { //打开文件夹
			while (($file= readdir($dh)) !== false){ //读取文件夹里面的所有东西
				$nowfilepath=$dir.'/'.$file; //当前的目录
				if(!is_dir($nowfilepath)){ 
?>
                        <tr>
				<td><a href="?ac=xgmb&path=<?php echo $dir?>&name=<?php echo $file?>"><?php echo $file?></a></td>
				<td><?php echo getTemplateType($file);?></td>
				<td><?php echo round(filesize($dir.$file)/1204,2)."Kb"?></td>
				<td>
                                 <?php $xtime=date('Y-m-d H:i:s',filemtime($dir.$file));
                                       if(date("Y-m-d",strtotime($xtime))==date('Y-m-d')){
                                           echo "<font color=red>".$xtime."</font>";
                                       }else{
                                           echo $xtime;
                                       }
                                 ?>
                                </td>
				<td><a href="?ac=xgmb&path=<?php echo $dir?>&name=<?php echo $file?>">编辑</a> &nbsp;&nbsp;<a href="?ac=show&op=fz&name=<?php echo $file?>&path=<?php echo $path?>">复制</a> &nbsp;&nbsp;<a style="cursor:pointer;"  onclick="delmb('<?php echo $dir.$file?>')">删除</a></td>
			</tr>
<?php
                                      }
				} 
			} 
			closedir($dh); 
		} 
	} 
?>
</table>
</div>
<?php 
   if($op=='fz'){
         $name=CS_Request("name");
	 if(copy($dir.$name,$dir."复制".$name)){
                echo "<script>Popsbox('提示信息',250,90,'<div id=\"alert\">恭喜，模版文件复制成功！</div>');setTimeout(\"window.location='javascript:history.go(-1);'\",1000);</script>";
         }else{
                echo "<script>Popsbox('提示信息',250,90,'<div id=\"alert\">抱歉，模版文件复制失败！</div>');setTimeout(\"window.location='javascript:history.go(-1);'\",1000);</script>";
         }
   }
   if($op=='del'){
        $mb=CS_Request("mb");
	if(file_exists($mb)){
		@unlink($mb);
                echo "<script>Popsbox('提示信息',250,90,'<div id=\"alert\">恭喜，模版文件删除成功！</div>');setTimeout(\"window.location='javascript:history.go(-1);'\",1000);</script>";
	}else{
                echo "<script>Popsbox('提示信息',250,90,'<div id=\"alert\">模版文件不存在,删除失败！</div>');setTimeout(\"window.location='javascript:history.go(-1);'\",1000);</script>";
	}	
   }

}else{ 
?>

<form method="post" class="J_ajaxForm" action="?op=edit" data-role="list">
<div class="table_list">
	<table width="100%" id="J_table_list" style="table-layout:fixed;">
		<colgroup>
			<col width="30">
			<col width="250">
			<col width="200">
			<col width="180">
			<col>
		</colgroup>
		<thead>
			<tr>
				<td></td>
				<td>模版名称</td>
				<td>模版路径</td>
				<td class="tac">默认</td>
				<td>操作</td>
			</tr>
		</thead>
<?php 
	global $db;
	$sql="select * from ".Getdbname('skins')."";
	$result=$db->query($sql);
	$tempcount=$db->num_rows($result);
        if($tempcount=="0"){
		echo '<tbody>
			<tr><td colspan="5" class="tac">啊哦，没有符合条件的内容！</td></tr>
		 </tbody>';

        }else{
                if($result){
                     while($row=$db->fetch_array($result)){
?>
		<tbody>
		<tr>
			<td><span class="zero_icon"></span></td>
			<td>
                                <input name="CS_ID[]" type="hidden" value="<?php echo $row['CS_ID']?>" />
				<input name="CS_Name<?php echo $row['CS_ID']?>" type="text" class="input length_3 mr5" value="<?php echo $row['CS_Name']?>">
			</td>
			<td><input name="CS_TempPath<?php echo $row['CS_ID']?>" type="text" class="input length_4" value="<?php echo $row['CS_TempPath']?>"></td>
			<td class="tac"><li><label><input name="CS_Templateurl" type="radio" value="<?php echo $row['CS_TempPath']?>" <?php if(CS_Templateurl==$row['CS_TempPath']){echo "checked";}?>></label></li></td>
			<td>
				<a href="?ac=show&path=<?php echo $row['CS_TempPath']?>" class="mr10 J_dialog" title="模版编辑">[编辑]</a>
				<a style="cursor:pointer;"  onclick="del(<?php echo $row['CS_ID']?>);" class="mr10 J_ajax_del">[删除]</a>
			</td>
		</tr>
		</tbody>
<?php		
                     }
                }
        }
?>
        </table>
</div>
<div class="btn_wrap">
	<div class="btn_wrap_pd">
		<button class="btn btn_submit mr10 J_ajax_submit_btn" id="addlist" type="submit">批量修改</button>
                <div class="tips_error" id="Msg"></div>
	</div>
</div>	
</form>
<!-- end -->
<?php
    if($op=='edit'){
        global $db;
   	$CS_ID=CS_RequestBox("CS_ID");
	$ID=explode(",",$CS_ID);
		for($i=0;$i<count($ID);$i++){
			$CS_Templateurl=CS_Request("CS_Templateurl");
			$CS_Name=CS_Request("CS_Name".$ID[$i]);
			$CS_TempPath=CS_Request("CS_TempPath".$ID[$i]);
                        if(substr($CS_TempPath,-1)!="/") $CS_TempPath=$CS_TempPath."/";

                         //--------------------设置默认模版---------------------------------//
	                 $str=file_get_contents("../../lib/cs_config.php"); 
	                 $str=preg_replace('/"CS_Templateurl","(.*?)"/','"CS_Templateurl","'.$CS_Templateurl.'"',$str);
	                 $ifile = new iFile('../../lib/cs_config.php','w');
	                 $ifile->WriteFile($str,3); 
                         //--------------------设置默认模版---------------------------------//

                        if(!empty($CS_Name) && !empty($CS_TempPath) && file_exists(_CSDJ_ROOT_.$CS_TempPath."cs-index.html") && substr($CS_TempPath,0,5)=="skins"){
				$sql="update ".Getdbname('skins')." set CS_Name='".$CS_Name."',CS_TempPath='".$CS_TempPath."' where CS_ID=".$ID[$i]."";
				$db->query($sql);
                        }
		}

      echo '<script>document.getElementById("Msg").className="tips_success";$("#Msg").text("恭喜您,修改成功！").css("color","#026E12");</script>';
      echo "<script>setTimeout(\"window.location.href ='skins.php';\",2000);</script>";
    }
    if($op=='del'){
		global $db;
		$CS_ID=CS_Request("id");
                $row=$db->getRow("select * from ".Getdbname('skins')." where CS_ID=".$CS_ID."");
                if(CS_Templateurl==$row['CS_TempPath']){
                             echo '<script>$("#Msg").text("抱歉,该模版正在使用不能删除！").css("color","red");</script>';
                             exit("<script>setTimeout(\"window.location='skins.php'\",2000);</script>");
                }
                $sql="delete from ".Getdbname('skins')." where CS_ID=".$CS_ID;
		if($db->query($sql)){
                        echo '<script>document.getElementById("Msg").className="tips_success";$("#Msg").text("恭喜,删除成功!").css("color","#026E12");</script>';
	                echo "<script>setTimeout(\"window.location='skins.php'\",2000);</script>";
                }else{
                        echo '<script>$("#Msg").text("抱歉,删除失败！").css("color","red");</script>';
                }
    }
}
function getTemplateType($filename){
	switch(strtolower($filename)){
		case 'cs-index.html':
			$getTemplateType="网站首页";
			break;
		case "cs-head.html":
			$getTemplateType="网站顶部";
			break;
		case "cs-bottom.html":
			$getTemplateType="网站底部";
			break;
		case "so-dance.html":
			$getTemplateType="歌曲搜索";
			break;
		case "so-dance-tags.html":
			$getTemplateType="歌曲标签";
			break;
		case "so-topic.html":
			$getTemplateType="专集搜索";
			break;
		case "so-topic-tags.html":
			$getTemplateType="专集标签";
			break;
		case "so-news.html":
			$getTemplateType="文章搜索";
			break;
		case "topic-dance.html":
			$getTemplateType="音乐专集列表";
			break;
		case "topic-news-show.html":
			$getTemplateType="文章专题内容";
			break;
		case "topic-dance-show.html":
			$getTemplateType="音乐专集内容";
			break;
		case "news-list.html":
			$getTemplateType="文章列表";
			break;
		case "dance-list.html":
			$getTemplateType="歌曲列表";
			break;
		case "news-show.html":
			$getTemplateType="文章内容";
			break;
		case "dance-play.html":
			$getTemplateType="歌曲播放";
			break;
		case "dance-sp-play.html":
			$getTemplateType="视频播放";
			break;
		case "cs-gbook.html":
			$getTemplateType="留言模板";
			break;
		case "cs-ulogin.html":
			$getTemplateType="会员登陆";
			break;	
		case "cs-uinfo.html":
			$getTemplateType="会员信息";
			break;	
		Case "dance-down.html";
			$getTemplateType="下载页面";
			break;	
		Case "dance-sp-list.html";
			$getTemplateType="视频栏目";
			break;	
		Case "dance-playsong.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;
}	
?>
</div>
</body>
</html>
