<?php
# Name: PHP版程氏音乐CMS管理系统 v3.0
# Author: 程氏<web@chshcms.com> [QQ:848769359]
# Homepage:http://www.chshcms.cn/
include_once("../../lib/cs_conn.php");
include_once("../../lib/cs_common.php");
include "../loginstate.php";
set_time_limit(0);
?>
<!doctype html>
<html>
<head>
<meta charset="gbk">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>程氏官方数据同步</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 src="../res/js/jquery.js"></script>
<script src="../../public/js/webox.js"></script>
<script language="javascript"> 
$(document).ready(function(){
	$("#ChkAll").click(function(){
		if($(this).attr("checked")==true){
			$("input[id='CS_ID']").each(function(){
				$(this).attr("checked",true);									 
			})							
		}else{
			$("input[id='CS_ID']").each(function(){
				$(this).attr("checked",false);									 
			})							
		}
	})
})
function ShowBd(_id){
  $.get("../ajax/ajax.php?cid="+_id+"&ac=bdlist", 
	  function(obj){
		  if(obj){
			  $("#bd_"+_id).html(obj);
		  }else{
			  $("#bd_"+_id).html('<font color=#ff6600>未知</font>');
		  }
	  }
  );
}	   
function Delbd(cid,csid){
  $.get("../ajax/ajax.php?cid="+cid+"&csid="+csid+"&ac=bdcsdj", 
	  function(obj){
		  if(obj=='ok'){
			  ShowBd(cid);
		  }else{
			  alert("绑定失败!");		
		  }
	  }
  );
} 
</script>
</head>
<body>
<?php
$ac=CS_Request("ac");
$op=CS_Request("op");
$cid=CS_Request("cid");
$mark=CS_Request("mark");
$pages=CS_Request("pages");
if(empty($ac)) $ac='main';
if(empty($pages)) $pages=1;
$csid=CS_RequestBox("CS_ID");
if(empty($csid)) $csid=CS_Request("csid");

if($ac=='caiji'){
    $Danceneir=Danceurl($CSDJCMS_URL."&ac=ruku&op=".$op."&cid=".$cid."&csid=".$csid."&pages=".$pages);
    if(empty($Danceneir)){
         die("<script>alert('对不起，入库出错，请联系程氏官方客服！');window.location='javascript:history.go(-1);'</script>");
    }
    //获取管理员ID会员名称
    $rowu=$db->getrow("select * from ".Getdbname('user')." where CS_ID=1");
    if(!$rowu){
         $CS_User='未知';
    }else{
         $CS_User=$rowu['CS_Name'];
    }
    $danceall=explode("===cscms===",$Danceneir);
    $csdance=explode("$$$",$danceall[1]);
    $pagesjs=$danceall[0];
    $danceall=explode('##cms##',$csdance[0]);
    $num=count($danceall);
    if($op=='id') $num=3;
		for($j=1;$j<$num-1;$j++){	
                   $cids=explode("##cms##",$csdance[0]);
                   $CS_CID=$cids[$j];
                   $name=explode("##cms##",$csdance[1]);
                   $CS_Name=$name[$j];
                   $playform=explode("##cms##",$csdance[2]);
                   $CS_PID=Getplay($playform[$j]);
                   $pic=explode("##cms##",$csdance[3]);
                   $CS_Pic=$pic[$j];
                   $ddata=explode("##cms##",$csdance[4]);
                   if($CS_CID=='11'){
                         $CS_PlayUrl=$ddata[$j];
                         $CS_DownUrl="csdjzy_".base64_encode($ddata[$j]).".mp3";
                         $CS_Template='dance-sp-play.html';
                   }else{
                         $CS_Url=base64_encode($ddata[$j]);
                         if($CS_PID==1){
                             $CS_PlayUrl="csdjzy_".$CS_Url.".mp4";
                         }else{
                             $CS_PlayUrl="csdjzy_".$CS_Url.".wma";
                         }
                         $CS_DownUrl="csdjzy_".$CS_Url.".mp3";
                         $CS_Template='dance-play.html';
                   }

                  //判断是否填加官方视听服务器
                   $rowp=$db->getrow("select * from ".Getdbname('fwqip')." where CS_Playip='http://dj.chshcms.com/play/?mp3='");
                   if(!$rowp){
                          exit("<script>alert('对不起，您还没有填加官方视听服务器组！');window.location='javascript:history.go(-1);'</script>");
                   }else{
			//判断绑定的分类
			$isbind="0";
			$sqlstr="select * from ".Getdbname('class')." where CS_SID=1";
			$res=$db->query($sqlstr);
			if($res){
				while($row=$db->fetch_array($res)){
					if(stristr($row['CS_CSID'],",".$CS_CID.",")){
						$isbind=$row['CS_ID'];
					}
				}
			}
			$CS_CID=$isbind;
                        if($CS_CID==0){
			          echo "&nbsp;&nbsp;<font color=red>".$CS_Name."</font>&nbsp;&nbsp;数据没有绑定分类，不进行入库处理！<br/>";
                        }else{
			    //开始处理数据
			    if(empty($CS_Name)){
			          echo "&nbsp;&nbsp;<font color=red>".$CS_Name."</font>&nbsp;&nbsp;数据不完整，不进行入库处理！<br/>";
			    }else{
				//判断数据是否相同
				$CS_ID= $db->Getone("select CS_ID from ".Getdbname('dance')." where CS_Name='".$CS_Name."' and CS_PlayUrl='".$CS_PlayUrl."'");
				if($CS_ID){
					echo "&nbsp;&nbsp;<font color=#ff6600>".$CS_Name."</font>&nbsp;&nbsp;数据相同，暂无不需要更新<br/>";
				}else{
					$sql="Insert ".Getdbname('dance')."  (CS_PlayUrl,CS_DownUrl1,CS_Name,CS_Pic,CS_CID,CS_User,CS_PID,CS_FID,CS_Template,CS_AddTime) values ('".$CS_PlayUrl."','".$CS_DownUrl."','".$CS_Name."','".$CS_Pic."',".$CS_CID.",'".$CS_User."',".$CS_PID.",".$rowp['CS_ID'].",'".$CS_Template."','".date('Y-m-d H:i:s')."')";
					if($db->query($sql)){
						echo "<font color=#ff00ff>&nbsp;&nbsp;".$CS_Name."</font>数据库中没有记录，已采集完成！<br/>";
					}else{
						echo "<font color=#0000ff>&nbsp;&nbsp;".$CS_Name."</font>入库失败，未知错误！<br/>";
                                        }
				}	
			    }	
                        }
                   }
                }
		$pages=$pages+1;
		if($pages>$pagesjs){
                     if(!empty($csid)){
			echo("</br>&nbsp;&nbsp;<b>恭喜您，全部入库完成啦。。。。。</b><script>setTimeout('updatenext();',2000);function updatenext(){location.href='javascript:history.go(-1);';}</script>");
                     }else{
			echo("</br>&nbsp;&nbsp;<b>恭喜您，全部入库完成啦。。。。。</b><script>setTimeout('updatenext();',3000);function updatenext(){location.href='?mark=php&cid=".$cid."';}</script>");
                     }
		}else{
			echo("</br>&nbsp;&nbsp;<b><img src='../../public/images/load.gif'>&nbsp;第<font color=red>".($pages-1)."</font>页入库完毕,暂停<font color=red>3</font>秒继续。。。。。</b><script>setTimeout('updatenext();',3000);function updatenext(){location.href='?mark=php&cid=".$cid."&op=".$op."&ac=".$ac."&pages=".$pages."';}</script>");

		}
}else{
    $ConTent=Danceurl($CSDJCMS_URL."&cid=".$cid."&pages=".$pages);
    if(empty($ConTent)){
         die("<script>alert('对不起，您的空间不支持远程采集！');window.location='javascript:history.go(-1);'</script>");
    }
    echo $ConTent;

}


function Danceurl($url){
    $ConTent=@file_get_contents($url);
    if(empty($ConTent) && function_exists('curl_init')){
         $ch = curl_init($url);
         curl_setopt($ch, CURLOPT_TIMEOUT, 1);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         $ConTent = curl_exec($ch);
         curl_close ($ch);
    }
   return $ConTent;
}

function Getplay($name){
     if($name=='mp3'){
        return 0;
     }elseif($name=='cmp'){
       return 1;
     }elseif($name=='flv'){
       return 2;
     }elseif($name=='youku'){
       return 3;
     }elseif($name=='tudou'){
       return 4;
     }elseif($name=='qiyi'){
       return 5;
     }elseif($name=='yinyt'){
       return 6;
     }elseif($name=='mp4'){
       return 7;
     }else{
       return 0;
     }
}
?>