<?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('5_5');
$ac=CS_Request("ac");
@set_time_limit(0);
$page=CS_Request("pages");
if(empty($page)){$page=1;}

	$sql="Select CS_ID,CS_Name,CS_Pic from ".Getdbname('dance')." where Lower(Left(CS_Pic,7))='http://'  order by CS_ID desc";
	$fieldir="../../annex/dance/";

	$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="../../annex/dance/".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).".jpg";	
				$filesavepath=$fieldir.$filename;
				if(!remote_file_exists($row['CS_Pic'])){
					$sqlstr="update ".Getdbname('dance')." set CS_Pic='' where CS_ID=".$row['CS_ID']."";
					$db->query($sqlstr);
					echo "<font color=red style=font-size:12px;>".$row['S_Name']."</font>的远程图片不存在!</br>";
				}else{
					$grabInfo=GrabImage($row['CS_Pic'],$filesavepath,$filename);
					if($grabInfo=="ok"){

						$sqlstr="update ".Getdbname('dance')." set CS_Pic='".$filename."' where CS_ID=".$row['CS_ID']."";
						$db->query($sqlstr);
						echo "<font style=font-size:12px;>同步<font color=red>".$row['CS_Name']."</font>的图片成功!<a href=\"".$filesavepath."\" target=_blank>".$filename."</a></font></br>";						
						}
					elseif($grabInfo=="nopic"){
						  $sqlstr="update ".Getdbname('dance')." set CS_Pic='' where CS_ID=".$row['CS_ID']."";
						  $db->query($sqlstr);
						  echo "<font color=red style=font-size:12px;>".$row['CS_Name']."</font>的远程图片不存在!</br>";
						
						}	
					else{						
						echo "<font style=font-size:12px;>同步<font color=red>".$row['CS_Name']."</font>的图片失败!</br>";		
					}	
				}
			 }
				mysql_free_result($result);
				echo "<font style=font-size:12px;>第".$page."页图片同步完毕,暂停3秒后继续同步．．．．．．</font><script language='javascript'>setTimeout('ReadGo();',".(3000).");function ReadGo(){location.href='?pages=".($page+1)."';}</script>";	
			}
		}	


function GrabImage($url,$filename="",$name) { 
    if($url==""){
	return "nopic";
    }
   $ext=strtolower(strrchr($url,".")); 
   ob_start(); 
   for($i=0;$i<3;$i++){
	$img=@file_get_contents($url);	
        if($img) break;
   }
   if(empty($img)){
	$img=geturl($url);
   }
   ob_end_clean(); 
   $size = strlen($img); 
   if(($size*1024)<1){
	return "error";
   }else{
	$fp2=@fopen($filename, "wb+"); 
	fwrite($fp2,$img); 
	fclose($fp2); 
	return "ok";
   }
} 
?>
