<?php
@set_time_limit(0);
@ob_implicit_flush();
require_once(dirname(__FILE__)."/../../inc/common.php");

function downPic($pg,$dt){
	session_start();
	$downType=$dt;$page = $pg;
	$sitePic = sitePic."/".date('Y')."-".date('m');
	$isDownOk = false;$pagesize=30;
	$sql="SELECT m_id,m_name,m_pic FROM ".DB::table('data')." WHERE LEFT(m_pic,7) = 'http://' ORDER BY m_addtime DESC";
	$allRecordset = db::getCount($sql);
	$page=ifthen(empty($page),1,$page);
	$totalpage = ceil($allRecordset/$pagesize);
	if ($page == 1) $_SESSION["m_pic_page"] = $totalpage;
	if ($allRecordset == 0 || $page > $totalpage) {
		alertMsg("恭喜，所有图片已经成功下载到本地!","admin_video.php");exit();
	}
	echo "<font color=red>共".$_SESSION["m_pic_page"]."页,正在开始下载第".$page."页数据的的图片</font><br>";
    $query = DB::query("$sql LIMIT 0,$pagesize");
	while ($row = DB::fetch($query)) {
		$ps = explode("/",$row['m_pic']);
		$filename = $ps[count($ps)-1];
		if (strpos(" ".$filename,"?") > 0){$filenamet=explode('?',$filename);$filename=$filenamet[0];}
		$filePath = "pic/".$sitePic."/".$filename;
		if (!file_exists($filePath)) {
			$isDownOk = downSinglePic($row['m_pic'],$row['m_id'],$row['m_name'],$filePath,"down");
		} else {
			echo "数据<font color=red>".$row['m_name']."</font>的图片已经存在 <a target=_blank href=".$filePath.">预览图片</a><br>";
			$isDownOk=true;
		}
		if ($isDownOk) {
			updatePicUrl($row['m_id'],replaceStr($filePath,"../",""));
			if (watermark == 1) writeFontWaterPrint($filePath);
		} else {
			updatePicUrl($row['m_id'],"");
		}
	}
	downPic($page+1,"all");
}
	
function updatePicUrl($id,$pic) {
	DB::update("data", array('m_pic' => $pic),array('m_id' => $id));
}

function downSinglePic($picUrl,$vid,$vname,$filePath,$infotype) {
	if ($infotype=="") $spanstr=""; else $spanstr="<br/>";
	if (empty($picUrl) || substr($picUrl,0,7) != 'http://') {
		echo "数据<font color=red>".$vname."</font>的图片路径错误1,请检查是否有效  ".$spanstr;
		return false;
	}
	$fileext = getFileFormat($filePath);
	$ps = explode("/",$picUrl);
	$filename = $ps[count($ps)-1];
	if (strpos($picUrl, ".ykimg.com/") > 0) {
		$fileext = ".gif";
		$filePath = $filePath.$fileext;
	}
	if (!empty($fileext) && strpos("|.jpg|.gif|.png|.bmp|.jpeg|",strtolower($fileext)) > 0) {
		if (!(strpos($picUrl, ".ykimg.com/") > 0)) {
			if (empty($filename) || strpos(' '.$filename,'www.') > 0 || strpos($filename,".") == 0) {
				echo "数据<font color=red>".$vname."</font>的图片路径错误2,请检查是否有效  ".$spanstr;
				return false;
			}
		}
		$imgStream = getRemoteContent($picUrl);
		$createStreamFileFlag = createTextFile($imgStream,$filePath);
		if ($createStreamFileFlag) {
			$streamLen = strlen($imgStream);
			if ($streamLen < 2048) {
				echo "数据<font color=red>".$vname."</font>的图片下载发生错误5,请检查是否有效  ".$spanstr;
				return false;
			} else {
				echo "数据<font color=red>".$vname."</font>的图片下载成功,大小为<font color=red>".number_format($streamLen/1024,2)."</font>KB <a target=_blank href=../".$filePath.">预览图片</a>  ".$spanstr;
				return true;
			}
		} else {
			if (empty($vid)) {
				echo "数据<font color=red>".$vname."</font>的图片下载发生错误3,请检查是否有效  ".$spanstr;
				return false;
			} else {
				echo "数据<font color=red>".$vname."</font>的图片下载发生错误4,id为<font color=red>".$vid."</font>,请检查是否有效  ".$spanstr;
				return false;
			}
		}
	} else {
		echo "数据<font color=red>".$vname."</font>的图片下载发生错误6,图片格式不对  ".$spanstr;
		return false;
	}
}

downPic(1,"all");
?>