<?php
/*
'**************************************************************************************************
' 软件名称: 搜一次 Content Management System
' 版本编号: Version 1.X
' 官方网站: http://www.syccms.com
' 官方论坛：http://bbs.syccms.com
' 版权所有: 搜一次运营团队
' 法律顾问: 杭州市元茂律师事务所 严飞律师
' 郑重声明:
'    1、任何个人或组织不得在未经授权的情况下删除、修改、拷贝本软件及其他副本上一切关于版权的信息；
'    2、搜一次网络事业部保留此软件的法律追究权利
'**************************************************************************************************
*/
include "admin_loginstate.php";
include "../include/conn.php";
include "../include/function.php";
include '../include/class/http.class.php';
include "admin_version.php";
@set_time_limit(0);
$SID=SafeRequest("sid","get");
$page=SafeRequest("pages","get");
$mid = isset($_GET['mid']) ? intval($_GET['mid']) : 0;
if(!IsNum($page)){$page=1;}
if($SID==1){
	$sql="Select S_ID,S_Name,S_Pic from s_video where S_ID>$mid and Lower(Left(S_Pic,7))='http://' order by S_ID asc limit 15";
	$fieldir="../images/".S_Vimgfolder."/";
}else{
	$sql="Select S_ID,S_Name,S_Pic from s_article where S_ID>$mid and Lower(Left(S_Pic,7))='http://' order by S_ID asc limit 15";
	$fieldir="../images/".S_Aimgfolder."/";
}
	//$Arr=getpagerow($sql,15);//sql,每页显示条数
	$result=$db->query($sql);
	$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>页</div>");
	if($num==0){
		die("<font style=font-size:12px;><br/>&nbsp;所有远程图片已经同步到本地.....</font>");
	}else{
		if($result){
			while ($row = $db ->fetch_array($result)){
				$mid = $row['S_ID'];
				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);		
				} 
						
				$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."&mid=".$mid."&pages=".($page+1)."';}</script>";	
			}
		}	

function GrabImage($url,$filename="",$name) {
	if($url==""){
		return "nopic";
	}
	$ext=strtolower(strrchr($url,".")); 
	if($ext!=".gif" && $ext!=".jpg" && $ext!=".png" && $ext!=".bmp" && $ext!=".jpeg"){
		return "nopic";
	} 
	$img = http::remotehttp($url);
	$size = strlen($img); 
	if(($size*1024)<1){
		return "error";
	}else{
		$fp2=fopen($filename, "wb+"); 
		fwrite($fp2,$img); 
		fclose($fp2); 
		return "ok";
	}
}
?>