<?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("../../lib/cs_config_ftp.php");
include_once("../loginstate.php");
SystemPer('2_1');
$ac=CS_Request("ac");
$op=CS_Request("op");
$ftppass=substr(CS_Ftppwd,0,1)."*******".substr(CS_Ftppwd,-1);
@set_time_limit(0);

if($ac=='saomiao'){
   echo("<link href=\"../res/css/admin_style.css\" rel=\"stylesheet\" /><div class=\"h_a\">&nbsp;正在扫描硬盘歌曲...</div>");
      $hz=CS_Request("hz");
		$CS_CID=CS_Request("CS_CID");
		$CS_TID=CS_Request("CS_TID");
		$CS_Singer=CS_Request("CS_Singer");
		$CS_User=CS_Request("CS_User");
		$CS_PID=CS_Request("CS_PID");
		$CS_FID=CS_Request("CS_FID");
		$CS_Year=CS_Request("CS_Year");
		$CS_Reco=CS_Request("CS_Reco");
		$CS_Coin=CS_Request("CS_Coin");
		$CS_Vip=CS_Request("CS_Vip");
		$CS_Tags=$_POST["CS_Tags"];
                $CS_Template=CS_Request("CS_Template");

    $path=CS_Request("path");
    if(substr($path,0,1)!='/') $path="/".$path;
    if(substr($path,-1)!='/') $path=$path."/";
    if(empty($path)) $path='/';
    $conn_id = ftp_connect(CS_Ftpip,CS_Ftpdk) or die("FTP 连接失败 !");
    $login_result = ftp_login($conn_id, CS_Ftpuid, CS_Ftppwd);
    if ((!$conn_id) || (!$login_result)) {
        die("FTP用户名或密码错误 !");
    }
    $contents = ftp_nlist($conn_id, $path);
             for($i=0;$i<count($contents);$i++){
                                      $file=$contents[$i];
	                              $sys=substr($file,-3);//获取文件名后缀
	                              if(is_int(strpos($hz,$sys)) !== false){  //判断是否是音乐文件

                                          $CS_Name=substr($file,0,-4);
                                          $CS_PlayUrl=$path.$file;
                                          $CS_PlayUrl=str_replace("//","/",$CS_PlayUrl);
                                          $CS_DownUrl=$CS_PlayUrl;
                                          $CS_Dx=formatsize(ftp_size($conn_id, $path.$contents[$i]));

				          //判断数据是否相同
				          $CS_ID= $db->Getone("select CS_ID from ".Getdbname('dance')." where CS_Name='".$CS_Name."' and CS_PlayUrl='".$CS_PlayUrl."'");
				          if($CS_ID){

					    echo "<font style=font-size:13px;>&nbsp;&nbsp;歌曲&nbsp;&nbsp;<font color=red>".$file."</font>&nbsp;&nbsp;<font color=#0000ff>已经存在，跳过入库!</font>．．．<br/>";

				          }else{
					          $sql="Insert ".Getdbname('dance')."  (CS_PlayUrl,CS_DownUrl1,CS_Tags,CS_Name,CS_CID,CS_User,CS_TID,CS_Singer,CS_PID,CS_FID,CS_Year,CS_Reco,CS_Coin,CS_Vip,CS_Template,CS_Dx,CS_AddTime) values ('".$CS_PlayUrl."','".$CS_DownUrl."','".$CS_Tags."','".$CS_Name."',".$CS_CID.",'".$CS_User."',".$CS_TID.",'".$CS_Singer."',".$CS_PID.",".$CS_FID.",'".$CS_Year."',".$CS_Reco.",".$CS_Coin.",".$CS_Vip.",'".$CS_Template."','".$CS_Dx."','".date('Y-m-d H:i:s')."')";
					          if($db->query($sql)){
					          	echo "<font style=font-size:13px;>&nbsp;&nbsp;歌曲&nbsp;&nbsp;<font color=red>".$file."</font>&nbsp;&nbsp;<font color=#026E12>入库成功</font>．．．<br/>";
					          }else{
					                echo "<font style=font-size:13px;>&nbsp;&nbsp;歌曲&nbsp;&nbsp;<font color=red>".$file."</font>&nbsp;&nbsp;<font color=#ff6600>入库失败，未知错误</font>．．．<br/>";
                                                  }
				          }

                                      }

             } 

    echo "<script>setTimeout(\"window.location='javascript:history.go(-1);'\",4000);</script>";
    exit("&nbsp;&nbsp;<font color=#0000ff><b>恭喜你！所有歌曲扫描入库完毕．．．</b></font><br><br>");
}
if($ac=='ftp'){
     $CS_Ftpip=CS_Request("CS_Ftpip");
     $CS_Ftpdk=CS_Request("CS_Ftpdk");
     $CS_Ftpuid=CS_Request("CS_Ftpuid");
     $CS_Ftppwd=CS_Request("CS_Ftppwd");

        if($CS_Ftppwd==$ftppass){
	     $CS_Ftppwd=CS_Ftppwd;
        }

	$strs="<?php"."\r\n";
	$strs=$strs."define(\"CS_Ftpip\",\"".$CS_Ftpip."\");\r\n";
	$strs=$strs."define(\"CS_Ftpdk\",\"".$CS_Ftpdk."\");\r\n";
	$strs=$strs."define(\"CS_Ftpuid\",\"".$CS_Ftpuid."\");\r\n";
	$strs=$strs."define(\"CS_Ftppwd\",\"".$CS_Ftppwd."\");\r\n";
	$strs=$strs."?>";
	//写文件
	$ifile = new iFile('../../lib/cs_config_ftp.php','w');
	if($ifile->WriteFile($strs,3)){
            die(Admin_Error('恭喜您，更新成功！','javascript:history.go(-1);'));
        }else{
            die(Admin_Error('更新失败，请检查lib文件夹是否有写入权限！','javascript:history.go(-1);'));
        } 
}
?>
<!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 src="../../public/js/webox.js"></script>
<script type="text/javascript"> 
$(document).ready(function(){
	$("#adddance").click(function(){
		var CS_CID=document.getElementById('CS_CID').value;
		var CS_User=document.getElementById('CS_User').value;
		var path=document.getElementById('path').value;
		var CS_Coin=document.getElementById('CS_Coin').value;
	if(CS_CID=="0"){
		$("#Msg").text("请选择歌曲分类！").css("color","red");
		return false;
		}
	else if(CS_User==""){
		$("#Msg").text("请填写歌曲所属会员！").css("color","red");
		return false;
		}
	else if(CS_Coin==""){
		$("#Msg").text("请填写歌曲下载金币！").css("color","red");
		return false;
		}
	else if(path==""){
		$("#Msg").text("请填写歌曲目录路径！").css("color","red");
		return false;
		}
	});
});
function saomiao(_path){
      if(_path){
          Popsboxif('浏览FTP歌曲文件',540,418,'?ac=look&path='+_path+'');
      }else{
            alert("请先填写路径！");
      }
}
</script>
</head>
<?php
if($ac=='look'){
    $path=$_GET['path'];
    if(substr($path,0,1)!='/') $path="/".$path;
    if(substr($path,-1)!='/') $path=$path."/";
    if(empty($path)) $path='/';
    $conn_id = ftp_connect(CS_Ftpip,CS_Ftpdk) or die("FTP 连接失败 !");
    $login_result = ftp_login($conn_id, CS_Ftpuid, CS_Ftppwd);
    if ((!$conn_id) || (!$login_result)) {
        die("FTP用户名或密码错误 !");
    }
    $contents = ftp_nlist($conn_id, $path);

echo '<body class="body_none">
      <div>
      <div class="pop_cont pop_table">
	<table width="100%" style="table-layout:fixed;">
		<col width="400" />
		<col />
				<tr>
					<td>&nbsp;<img src=../res/images/dir2.gif border=0 width=16 height=16 align=absmiddle>&nbsp;当前目录: <font color=red>'.$path.'</font></td>
					<td><a href="javascript:history.back(1);">返回上一页</a>&nbsp;</td>
				</tr>';
                        for($i=0;$i<count($contents);$i++){
                                      $file=$contents[$i];
	                              $sys=substr($file,-3);//获取文件名后缀
	                              if($sys =="mp3" || $sys =="wma" || $sys =="mpg" || $sys =="mp4" || $sys =="avi" || $sys =="flv"){  //判断是否是音乐文件
					echo "<tr>";
					echo "<td><input type='checkbox' value='0' class='checkbox' disabled />&nbsp;".$file."</td>";
					echo "<td>".formatsize(ftp_size($conn_id,$path.$file))."</td>";
					echo "</tr>";
                                      }

			} 
echo "</table></div></div>";
exit();
}
?>
<body>
<div class="wrap">
<div class="h_a">FTP扫描配置</div>
<div class="table_full">
<form class="J_ajaxForm" data-role="edit" name="form2" action="?ac=ftp" method="post">
	<table width="100%" class="J_check_wrap">
		<col class="th" />
		<col width="400" />
		<col />
		<tr>
			<th>FTP服务器IP</th>
			<td>
				<input name="CS_Ftpip" id="CS_Ftpip" value="<?php echo CS_Ftpip?>" type="text" class="input input_hd length_3"> &nbsp; &nbsp;
                                端口：<input name="CS_Ftpdk" id="CS_Ftpdk" value="<?php echo CS_Ftpdk?>" type="text" class="input input_hd length_2">
			</td>
			<td><div class="fun_tips">填写FTP服务器IP、端口</div></td>
		</tr>
		<tr>
			<th>FTP用户名</th>
			<td>
				<input name="CS_Ftpuid" id="CS_Ftpuid" value="<?php echo CS_Ftpuid?>" type="text" class="input input_hd length_3"> &nbsp; &nbsp;
                                密码：<input name="CS_Ftppwd" id="CS_Ftppwd" value="<?php echo $ftppass?>" type="text" class="input input_hd length_2">
			</td>
			<td><div class="fun_tips">填写FTP用户名、密码</div></td>
		</tr>
		<tr>
			<th></th>
			<td>
				<button type="submit" id="ftpgo" class="btn btn_submit mr10 J_ajax_submit_btn">提交保存</button>
			</td>
			<td></td>
		</tr>
	</table>
</form>
</div>
<form class="J_ajaxForm" data-role="edit" name="form" action="?ac=saomiao" method="post">
<div class="h_a">远程FTP扫描</div>
<div class="table_full">
	<table width="100%" class="J_check_wrap">
		<col class="th" />
		<col width="400" />
		<col />
		<tr>
			<th>音乐标签</th>
			<td>
				<input name="CS_Tags" id="CS_Tags" value="" type="text" class="input input_hd length_4">
                                &nbsp;<input class="btn" type="button" onclick="Popsboxif('选择标签',530,380,'../../public/tags.php?f=form.CS_Tags');" value="选择标签" />
			</td>
			<td><div class="fun_tips">填写音乐标签</div></td>
		</tr>
		<tr>
			<th>音乐分类</th>
			<td>
				<select class="select_2" name="CS_CID" id="CS_CID">
                                                <option value="0">选择分类</option>
            					 <?php
              						$sqlclass="select * from ".Getdbname('class')." where CS_YID=0 and CS_SID=1 and CS_FID=0";
									$results=$db->query($sqlclass);
									if($results){
										while ($row3=$db->fetch_array($results)){
										         echo "<option value='".$row3['CS_ID']."' >".$row3['CS_Name']."</option>";
		                                                                     //处理此分类下的子分类
		                                                                     $sql2="select * from ".Getdbname('class')." where CS_FID=".$row3['CS_ID']."";
		                                                                     $class2=$db->query($sql2);
		                                                                     if($class2){
			                                                                     while($row2=$db->fetch_array($class2)){
										                    echo "<option value='".$row2['CS_ID']."' >├ ".$row2['CS_Name']."</option>";
			                                                                     }
		                                                                     }
										}
									}
			  
			  			 ?>               
                                         </select>&nbsp;&nbsp;音乐专集：
				         <select class="select_2" name="CS_TID">
                                                <option value="0">选择专集</option>
            					 <?php
              						$sqlt="select * from ".Getdbname('topic')." where CS_YID=0 and CS_SID=1";
									$results=$db->query($sqlt);
									if($results){
										while ($row1=$db->fetch_array($results)){
										         echo "<option value='".$row1['CS_ID']."' >".$row1['CS_Name']."</option>";
										}
									}
			  
			  			 ?>             
                                         </select>
			</td>
			<td><div class="fun_tips">选择音乐分类、专集</div></td>
		</tr>
		<tr>
			<th>音乐歌手</th>
			<td>
				<input name="CS_Singer" value="" type="text" class="input input_hd length_2">
                                &nbsp;&nbsp;所属会员：
				<input id="CS_User" name="CS_User" value="" type="text" class="input input_hd length_2">
			</td>
			<td><div class="fun_tips">填写音乐歌手、会员</div></td>
		</tr>
		<tr>
			<th>音乐播放器</th>
			<td>
				<select class="select_2" name="CS_PID">
                                                <option value="0" selected='selected'>wma播放器</option>
                                                <option value="7" >mp3,mp4播放器</option>
                                                <option value="1" >cmp播放器</option>
                                                <option value="2" >flv播放器</option>
                                                <option value="3" >优库播放</option>
                                                <option value="4" >土豆播放</option>
                                                <option value="5" >SWF播放</option>
                                                <option value="6" >音乐台播放</option>
                                         </select>&nbsp;&nbsp;播放服务器：
				         <select class="select_2" name="CS_FID">
                                                <option value="0">选择服务器</option>
            					 <?php
              						$sqlf="select * from ".Getdbname('fwqip')." ";
									$results=$db->query($sqlf);
									if($results){
										while ($row2=$db->fetch_array($results)){
										         echo "<option value='".$row2['CS_ID']."' >".$row2['CS_Name']."</option>";
										}
									}
			  
			  			 ?>             
                                         </select>
			</td>
			<td><div class="fun_tips">选择音乐播放器、服务器</div></td>
		</tr>
		<tr>
			<th>发行年份</th>
			<td>
				<input name="CS_Year" value="2013" type="text" class="input input_hd length_2">
                                &nbsp;&nbsp;推荐星际：
				         <select class="select_2" name="CS_Reco">
                                                <option value="0">不推荐</option>
                                                <option value="1" >一星际</option>
                                                <option value="2" >二星际</option>
                                                <option value="3" >三星际</option>
                                                <option value="4" >四星际</option>
                                                <option value="5" >五星际</option>           
                                         </select>
			</td>
			<td><div class="fun_tips">选择发行年份、推荐星际</div></td>
		</tr>
		<tr>
			<th>下载金币</th>
			<td>
				<input id="CS_Coin" name="CS_Coin" value="1" type="text" class="input input_hd length_2">
                                &nbsp;个&nbsp;下载级别：
				         <select class="select_2" name="CS_Vip">
                                                <option value="0">游客下载</option>
                                                <option value="1" >普通会员</option>
                                                <option value="2" >VIP会员</option>          
                                         </select>
			</td>
			<td><div class="fun_tips">填写下载点数、级别</div></td>
		</tr>
		<tr>
			<th>播放页模版</th>
			<td>
				<input name="CS_Template" id="CS_Template" value="dance-play.html" type="text" class="input input_hd length_2">
                                &nbsp;&nbsp;<input class="btn" type="button" value="浏览" style="width:60px" onclick="Popsboxif('选择模版',450,400,'../skins/skin_re.php');"/>
			</td>
			<td><div class="fun_tips">可以利用这个功能支持所有播放器</div></td>
		</tr>
		<tr>
			<th>歌曲目录路径</th>
			<td>
				<input type="text" id="path" name="path" class="input length_3" value="">
                                &nbsp;<input class="btn btn_submit" type="button" onclick="saomiao(''+path.value+'');" value="浏览歌曲目录..." />
                        </td>
			<td><div class="fun_tips">硬盘路径，如：E:/舞曲/&nbsp;如果是空间运行就不要填了</div></td>
		</tr>
		<tr>
			<th>入库类型</th>
			<td>
				<input type="text" id="hz" name="hz" class="input length_3" value="mp3|wma|mp4|flv">
                        </td>
			<td><div class="fun_tips">歌曲入库类型，注意用“|”来阁开</div></td>
		</tr>
</div>
<div class="btn_wrap">
	<div class="btn_wrap_pd">
		<button type="submit" id="adddance" class="btn btn_submit mr10 J_ajax_submit_btn">提交扫描</button>
                <div class="tips_error" id="Msg"></div>
	</div>
</div>
</div>
</body>
</html>
