<?php
/*
'**************************************************************************************************
' 软件名称: 盘锦DJ舞曲网CMS Content Management PjRen.Net
' 版本编号: Version 2.5
' 盘锦舞曲: http://www.pj57.com
' 官方网站: http://www.pjren.net
' 小说网站: http://www.hx39.com
' 0427DJ网: http://www.0427dj.net
' 官方论坛: http://www.tianjiazhen.com
' 版权所有: 盘锦舞曲开发团队    盘锦人(QQ:814006710)
' 郑重声明:
'    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;}
	if($SID==1){
		$sql="Select S_ID,S_Name,S_Pic from s_video where Lower(Left(S_Pic,7))='http://'  order by S_ID desc";
		$fieldir="../images/".S_Vimgfolder."/";
		}
	else{
		$sql="Select S_ID,S_Name,S_Pic from s_article where Lower(Left(S_Pic,7))='http://'  order by S_ID desc";
		$fieldir="../images/".S_Aimgfolder."/";
		}
	$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)){
				 if($SID==1){
				 	 $nowdate="../images/vodpic/".date('Ymd');
				 }else{
					 $nowdate="../images/artpic/".date('Ymd');	 
						 }
				 if(!file_exists($nowdate)){
					@mkdir($nowdate,0777);
					}
				 if(file_exists($nowdate)){
				 	$datefolder=date('Ymd')."/";
				 }else{
					$datefolder='';
					}
				 if(substr($row['S_Pic'],-5,5)==".jpeg"){
					$filename=$datefolder.date('Ymd').time().mt_rand(1,100).substr($row['S_Pic'],-5,5);	
					 }
				else{
					$filename=$datefolder.date('Ymd').time().mt_rand(1,100).substr($row['S_Pic'],-4,4);		
					}	 
					
				if(substr($row['S_Pic'],-4)!=".gif" && substr($row['S_Pic'],-4)!=".jpg" && substr($row['S_Pic'],-4)!=".png" && substr($row['S_Pic'],-4)!=".bmp"){
					$filename="".$datefolder.date('Ymd').time().mt_rand(1,100).".jpg";
				}	
				$filesavepath=$fieldir.$filename;
				if(!remote_file_exists($row['S_Pic'])){
					if($SID==1){
						$sqlstr="update s_video set S_Pic='' where S_ID=".$row['S_ID']."";
						}
					else{
						$sqlstr="update s_article set S_Pic='' where S_ID=".$row['S_ID']."";
						}
					$db->query($sqlstr);
					echo "<font color=red style=font-size:12px;>".$row['S_Name']."</font>的远程图片不存在!</br>";
				}else{
					$grabInfo=GrabImage($row['S_Pic'],$filesavepath,$filename);
					if($grabInfo=="ok"){
						if($SID==1){
							$sqlstr="update s_video set S_Pic='".$filename."' where S_ID=".$row['S_ID']."";
							}
						else{
							$sqlstr="update s_article set S_Pic='".$filename."' 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=="nopic"){
						  if($SID==1){
							  $sqlstr="update s_video set S_Pic='' where S_ID=".$row['S_ID']."";
							  }
						  else{
							  $sqlstr="update s_article set S_Pic='' 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 GrabImage($url,$filename="",$name) { 
//方法1
if($url==""){
	return "nopic";
	}
$ext=strtolower(strrchr($url,".")); 
//if($ext!=".gif" && $ext!=".jpg" && $ext!=".png" && $ext!=".bmp" && $ext!=".jpeg"){
//return "nopic";
//} 
ob_start(); 
for($i=0;$i<3;$i++){
	$img=@file_get_contents($url);	
    if($img) break;
}
if(!IsNul($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";
	}
} 

?>