<?php

/*

'**************************************************************************************************

' 软件名称: 黑色幻想舞曲管理系统CMS2.0

' 版本编号: hshxsdj 2.0

' 官方网站: http://www..hshxs.com

' 官方论坛：http://bbs.hshxs.com

' 版权所有: 黑色幻想开发团队    噬己翼(QQ:532917920)

' 郑重声明:

'    1、任何个人或组织不得在未经授权的情况下删除、修改、拷贝本软件及其他副本上一切关于版权的信息；

'    2、黑色幻想网络事业部保留此软件的法律追究权利

'**************************************************************************************************

*/

include "../include/conn.php";

include "../include/function.php";

include "admin_version.php";

include "admin_loginstate.php";

@set_time_limit(0);

$SID=SafeRequest("sid","get");

$page=SafeRequest("pages","get");

if(!IsNum($page)){$page=1;}



		$sql="Select S_ID,S_Name,S_Vdata from s_video where Lower(Left(S_Vdata,7))='http://' and  Lower(Left(S_Vdata,18))<>'http://www.rayfile' order by S_ID desc";

		$fieldir="../upload/url/";

		

	$Arr=getpagerow($sql,15);//sql,每页显示条数		

	$result=$db->query($Arr[2]);

	$num=$db->num_rows($result);

	echo("<div style=' width:100%; background: #F9FBFD; border-top:4px solid #DEEFFA; border-bottom:1px solid #DEEFFA;color:#008EBD; font-size:12px; height:30px; line-height:30px;font-weight:bold; margin:0 auto;'>&nbsp;正在开始同步第<font style='color:red; font-size:12px; font-style:italic'>".$page."</font>页，剩<font style='color:red; font-size:12px; font-style:italic'>".$Arr[3]."</font>页</div>");

	if($num==0){

		die("<font style=font-size:12px;><br/>&nbsp;所有远程音乐数据已经同步到本地.....</font>");

		}

	else{

		if($result){

			 while ($row = $db ->fetch_array($result)){



				 	 $nowdate="../upload/url/".date('Ymd');



				 if(!file_exists($nowdate)){

					@mkdir($nowdate,0777);

					}

				 if(file_exists($nowdate)){

				 	$datefolder=date('Ymd')."/";

				 }else{

					$datefolder='';

					}

					$filename=$datefolder.date('Ymd').time().mt_rand(1,100).substr($row['S_Vdata'],-4,4);		

					$filename1="/upload/url/".$filename;		



				if(substr($row['S_Vdata'],-4)!=".mp3" && substr($row['S_Vdata'],-4)!=".wma"){

					$filename="".$datefolder.date('Ymd').time().mt_rand(1,100).".mp3";

					$filename1="/upload/url/".$filename;

				}	



				$filesavepath=$fieldir.$filename;

				if(!remote_file_exists($row['S_Vdata'])){

					echo "<font color=red style=font-size:12px;>".$row['S_Name']."</font>的远程数据不存在!</br>";

				}else{

					$grabInfo=GrabDj($row['S_Vdata'],$filesavepath,$filename);

					if($grabInfo=="ok"){



							$sqlstr="update s_video set S_Vdata='".$filename1."',S_Remark='".$filename1."' where S_ID=".$row['S_ID']."";



						$db->query($sqlstr);

						echo "<font style=font-size:12px;>同步<font color=red>".$row['S_Name']."</font>的远程数据成功!<a href=\"".$filesavepath."\" target=_blank>".$filename."</a></font></br>";						

						}

					elseif($grabInfo=="nodj"){



							  $sqlstr="update s_video set S_Vdata='' where S_ID=".$row['S_ID']."";



						  $db->query($sqlstr);

						  echo "<font color=red style=font-size:12px;>".$row['S_Name']."</font>的远程数据不存在!</br>";

						

						}	

					else{						

						echo "<font style=font-size:12px;>同步<font color=red>".$row['S_Name']."</font>的远程数据失败!</br>";		

						}	

					}

			 	}

				mysql_free_result($result);

				echo "<font style=font-size:12px;>第".$page."页数据同步完毕,暂停".S_StopTime."秒后继续同步．．．．．．</font><script language='javascript'>setTimeout('ReadGo();',".(S_StopTime*1000).");function ReadGo(){location.href='?sid=".$SID."&pages=".($page+1)."';}</script>";	

			}

		}	





function GrabDj($url,$filename="",$name) { 

//方法1

if($url==""){

	return "nodj";

	}

$ext=strtolower(strrchr($url,".")); 

ob_start(); 

for($i=0;$i<3;$i++){

	$dj=@file_get_contents($url);	

    if($dj) break;

}

//方法2

if(!IsNul($dj)){

	$dj=geturl($url);

	}

ob_end_clean(); 

$size = strlen($dj); 

if(($size*1024)<1){

	return "error";

	}

else{

	$fp2=@fopen($filename, "wb+"); 

	fwrite($fp2,$dj); 

	fclose($fp2); 

	return "ok";

	}

} 



?>