function GetCurContentAlbum($body,$rfurl,&$firstdd) { global $cfg_multi_site,$cfg_basehost,$ddmaxwidth,$cfg_basedir; include_once(DEDEADMIN."/../include/pub_collection_functions.php"); if(empty($ddmaxwidth)) $ddmaxwidth = 150; $rsimg = ''; $cfg_uploaddir = $GLOBALS['cfg_image_dir']; $cfg_basedir = $GLOBALS['cfg_basedir']; $basehost = "http://".$_SERVER["HTTP_HOST"]; if($cfg_multi_site == 'N'){ $body = str_replace(strtolower($basehost),"",$body); $body = str_replace(strtoupper($basehost),"",$body); }else{ if($cfg_basehost!=$basehost){ $body = str_replace(strtolower($basehost),$cfg_basehost,$body); $body = str_replace(strtoupper($basehost),$cfg_basehost,$body); } } $img_array = array(); preg_match_all("/(src|SRC)=[\"|'| ]{0,}(http:\/\/(.*)\.(gif|jpg|jpeg|png))/isU",$body,$img_array); $img_array = array_unique($img_array[2]); $imgUrl = $cfg_uploaddir."/".strftime("%y%m%d",time()); $imgPath = $cfg_basedir.$imgUrl; if(!is_dir($imgPath."/")){ MkdirAll($imgPath,$GLOBALS['cfg_dir_purview']); CloseFtp(); } $milliSecond = strftime("%H%M%S",time()); foreach($img_array as $key=>$value) { if(eregi($basehost,$value)) continue; if($cfg_basehost!=$basehost && eregi($cfg_basehost,$value)) continue; if(!eregi("^http://",$value)) continue; $value = trim($value); $itype = substr($value,0,-3); if(!eregi("gif|jpg|png",$itype)) $itype = ".jpg"; $rndFileName = $imgPath."/".$milliSecond.$key.$itype; $iurl = $imgUrl."/".$milliSecond.$key.$itype; //下载并保存文件 //$rs = $htd->SaveToBin($rndFileName); $rs = DownImageKeep($value,$rfurl,$rndFileName,"",0,30); if($rs){ $litpicname = GetImageMapDD($iurl,$ddmaxwidth); if(empty($firstdd) && !empty($litpicname)){ $firstdd = $litpicname; if(!file_exists($cfg_basedir.$firstdd)) $firstdd = $iurl; } @WaterImg($rndFileName,'down'); $info = ''; $imginfos = GetImageSize($rndFileName,$info); $rsimg .= "{dede:img ddimg='$litpicname' text='' width='".$imginfos[0]."' height='".$imginfos[1]."'} $iurl {/dede:img}\r\n"; } } return $rsimg; }
function ttest() { $imgobj = Import::img(); $crawler = Import::crawler(); $iconv = Import::gz_iconv(); require_once SYS_PATH_ADMIN . 'inc' . DS . 'download.php'; //$crawler->curl_get_con('http://www.womai.com/Product-0-526333.htm');exit; //$imgobj->grabImage("http://www.21ej.com/picture/139-848-892.gif",SYS_PATH."test.gif"); DownImageKeep("http://www.21ej.com/picture/139-848-d892.gif", "http://www.baidu.com", SYS_PATH . "test.gif", "", 0, 1); exit; $sql = "SELECT tb1.url,tb2.goods_preg_1,tb2.goods_preg_2,tb2.goods_preg_3,tb2.goods_preg_4,tb2.goods_preg_5 FROM `{$this->App->prefix()}goods_cache_url` AS tb1 LEFT JOIN `{$this->App->prefix()}goods_cache_site` AS tb2 ON tb2.gcid = tb1.gcid WHERE tb1.gcid='1' LIMIT 3"; $rt = $this->App->find($sql); $str = ""; if (!empty($rt)) { foreach ($rt as $row) { $url = $row['url']; echo $con = $crawler->curl_get_con($url); exit; if (empty($con)) { continue; } $con = $iconv->ec_iconv('GB2312', 'UTF8', $con); //分类 $goods_preg_1 = $row['goods_preg_1']; @preg_match($goods_preg_1, $con, $arr1); $catetitle = isset($arr1[1]) ? $arr1[1] : ""; //标题 $goods_preg_2 = $row['goods_preg_2']; @preg_match($goods_preg_2, $con, $arr2); $goodstitle = isset($arr2[1]) ? $arr2[1] : ""; //价格 $goods_preg_3 = $row['goods_preg_3']; @preg_match($goods_preg_3, $con, $arr3); $goodsprice = isset($arr3[1]) ? $arr3[1] : ""; $goods_preg_4 = $row['goods_preg_4']; if (!empty($goods_preg_4)) { $goods_preg_4 = str_replace('"', "'", $goods_preg_4); @preg_match($goods_preg_4, $con, $arr4); $goodsimg = isset($arr4[1]) ? $arr4[1] : ""; $simg = "photos/g/" . date('Ym', mktime()) . "/ej" . mktime() . substr($goodsimg, -4); $imgobj->imagescopy("http://www.21ej.com/" . $goodsimg, SYS_PATH . $simg); } $goods_preg_5 = $row['goods_preg_5']; if (!empty($goods_preg_5)) { @preg_match($goods_preg_5, $con, $arr5); $goodsdesc = isset($arr5[1]) ? $arr5[1] : ""; } } } }
/** * 下载图片 * * @access public * @param string $gurl 地址 * @param string $rfurl 来源地址 * @param string $filename 文件名 * @param string $gcookie 调整cookie * @param string $JumpCount 跳转计数 * @param string $maxtime 最大次数 * @return string */ function DownImageKeep($gurl, $rfurl, $filename, $gcookie = "", $JumpCount = 0, $maxtime = 30) { $urlinfos = GetHostInfo($gurl); $ghost = trim($urlinfos['host']); if ($ghost == '') { return FALSE; } $gquery = $urlinfos['query']; if ($gcookie == "" && !empty($rfurl)) { $gcookie = RefurlCookie($rfurl); } $sessionQuery = "GET {$gquery} HTTP/1.1\r\n"; $sessionQuery .= "Host: {$ghost}\r\n"; $sessionQuery .= "Referer: {$rfurl}\r\n"; $sessionQuery .= "Accept: */*\r\n"; $sessionQuery .= "User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\r\n"; if ($gcookie != "" && !preg_match("/[\r\n]/", $gcookie)) { $sessionQuery .= $gcookie . "\r\n"; } $sessionQuery .= "Connection: Keep-Alive\r\n\r\n"; $errno = ""; $errstr = ""; $m_fp = fsockopen($ghost, 80, $errno, $errstr, 10); fwrite($m_fp, $sessionQuery); $lnum = 0; //获取详细应答头 $m_httphead = array(); $httpstas = explode(" ", fgets($m_fp, 256)); $m_httphead["http-edition"] = trim($httpstas[0]); $m_httphead["http-state"] = trim($httpstas[1]); while (!feof($m_fp)) { $line = trim(fgets($m_fp, 256)); if ($line == "" || $lnum > 100) { break; } $hkey = ""; $hvalue = ""; $v = 0; for ($i = 0; $i < strlen($line); $i++) { if ($v == 1) { $hvalue .= $line[$i]; } if ($line[$i] == ":") { $v = 1; } if ($v == 0) { $hkey .= $line[$i]; } } $hkey = trim($hkey); if ($hkey != "") { $m_httphead[strtolower($hkey)] = trim($hvalue); } } //分析返回记录 if (preg_match("/^3/", $m_httphead["http-state"])) { if (isset($m_httphead["location"]) && $JumpCount < 3) { $JumpCount++; DownImageKeep($gurl, $rfurl, $filename, $gcookie, $JumpCount); } else { return FALSE; } } if (!preg_match("/^2/", $m_httphead["http-state"])) { return FALSE; } if (!isset($m_httphead)) { return FALSE; } $contentLength = $m_httphead['content-length']; //保存文件 $fp = fopen($filename, "w") or die("写入文件:{$filename} 失败!"); $i = 0; $okdata = ""; $starttime = time(); while (!feof($m_fp)) { $okdata .= fgetc($m_fp); $i++; //超时结束 if (time() - $starttime > $maxtime) { break; } //到达指定大小结束 if ($i >= $contentLength) { break; } } if ($okdata != "") { fwrite($fp, $okdata); } fclose($fp); if ($okdata == "") { @unlink($filename); fclose($m_fp); return FALSE; } fclose($m_fp); return TRUE; }
/** * 下载特定资源,并保存为指定文件 * * @access public * @param string $dourl 操作地址 * @param string $mtype 附件类型 * @param string $islitpic 是否缩略图 * @return string */ function DownMedia($dourl, $mtype = 'img', $islitpic = FALSE) { global $notckpic; if (empty($notckpic)) { $notckpic = 0; } //检测是否已经下载此文件 $wi = FALSE; $tofile = $filename = ''; if ($notckpic == 0) { $row = $this->dsql->GetOne("SELECT hash,tofile FROM `#@__co_mediaurls` WHERE nid='{$this->noteId}' AND hash='" . md5($dourl) . "' "); if (isset($row['tofile'])) { $tofile = $filename = $row['tofile']; } } //如果不存在,下载文件 if ($tofile == '' || !file_exists($GLOBALS['cfg_basedir'] . $filename)) { $filename = $this->GetRndName($dourl, $mtype); if (!preg_match("#^\\/#", $filename)) { $filename = "/" . $filename; } //防盗链模式 if ($this->noteInfos['isref'] == 'yes' && $this->noteInfos['refurl'] != '') { if ($this->noteInfos['exptime'] == '') { $this->noteInfos['exptime'] = 10; } DownImageKeep($dourl, $this->noteInfos['refurl'], $GLOBALS['cfg_basedir'] . $filename, '', 0, $this->Item['exptime']); } else { $this->cHttpDown->OpenUrl($dourl); $this->cHttpDown->SaveToBin($GLOBALS['cfg_basedir'] . $filename); $this->cHttpDown->Close(); } //下载文件成功,保存记录 if (file_exists($GLOBALS['cfg_basedir'] . $filename)) { if ($tofile == '') { $query = "INSERT INTO `#@__co_mediaurls`(nid,hash,tofile) VALUES ('" . $this->noteId . "', '" . md5($dourl) . "', '" . addslashes($filename) . "');"; } else { $query = "UPDATE `#@__co_mediaurls` SET tofile='" . addslashes($filename) . "' WHERE hash='" . md5($dourl) . "' "; } $this->dsql->ExecuteNoneQuery($query); } } //如果下载图片失败或图片不存在,返回网址 if (!file_exists($GLOBALS['cfg_basedir'] . $filename)) { return $dourl; } //生成缩略图 if ($mtype == 'img' && !$islitpic && $this->breImage == '') { $this->breImage = $filename; if (!preg_match("#^http:\\/\\/#", $this->breImage) && file_exists($GLOBALS['cfg_basedir'] . $filename)) { $filenames = explode('/', $filename); $filenamed = $filenames[count($filenames) - 1]; $nfilename = str_replace('.', '_lit.', $filenamed); $nfilename = str_replace($filenamed, $nfilename, $filename); if (@copy($GLOBALS['cfg_basedir'] . $filename, $GLOBALS['cfg_basedir'] . $nfilename)) { ImageResize($GLOBALS['cfg_basedir'] . $nfilename, $GLOBALS['cfg_ddimg_width'], $GLOBALS['cfg_ddimg_height']); $this->breImage = $nfilename; } } } if ($mtype == 'img' && !$islitpic) { @WaterImg($GLOBALS['cfg_basedir'] . $filename, 'collect'); } return $filename; }
/** * 获得HTML里的外部资源,针对图集 * * @access public * @param string $body 文档内容 * @param string $rfurl 来源地址 * @param string $firstdd 开始标记 * @return string */ function GetCurContentAlbum($body, $rfurl, &$firstdd) { global $dsql, $cfg_multi_site, $cfg_basehost, $cfg_ddimg_width; global $cfg_basedir, $pagestyle, $cuserLogin, $cfg_addon_savetype; require_once DEDEINC . '/dedecollection.func.php'; if (empty($cfg_ddimg_width)) { $cfg_ddimg_width = 320; } $rsimg = ''; $cfg_uploaddir = $GLOBALS['cfg_image_dir']; $cfg_basedir = $GLOBALS['cfg_basedir']; $basehost = 'http://' . $_SERVER['HTTP_HOST']; $img_array = array(); preg_match_all("/(src)=[\"|'| ]{0,}(http:\\/\\/([^>]*)\\.(gif|jpg|png))/isU", $body, $img_array); $img_array = array_unique($img_array[2]); $imgUrl = $cfg_uploaddir . '/' . MyDate($cfg_addon_savetype, time()); $imgPath = $cfg_basedir . $imgUrl; if (!is_dir($imgPath . '/')) { MkdirAll($imgPath, $GLOBALS['cfg_dir_purview']); CloseFtp(); } $milliSecond = 'co' . dd2char(MyDate('ymdHis', time())); foreach ($img_array as $key => $value) { $value = trim($value); if (preg_match("#" . $basehost . "#i", $value) || !preg_match("#^http:\\/\\/#i", $value) || $cfg_basehost != $basehost && preg_match("#" . $cfg_basehost . "#i", $value)) { continue; } $itype = substr($value, -4, 4); if (!preg_match("#\\.(gif|jpg|png)#", $itype)) { $itype = ".jpg"; } $rndFileName = $imgPath . '/' . $milliSecond . '-' . $key . $itype; $iurl = $imgUrl . '/' . $milliSecond . '-' . $key . $itype; //下载并保存文件 $rs = DownImageKeep($value, $rfurl, $rndFileName, '', 0, 30); if ($rs) { $info = ''; $imginfos = GetImageSize($rndFileName, $info); $fsize = filesize($rndFileName); $filename = $milliSecond . '-' . $key . $itype; //保存图片附件信息 $inquery = "INSERT INTO `#@__uploads`(arcid,title,url,mediatype,width,height,playtime,filesize,uptime,mid)\n VALUES ('0','{$filename}','{$iurl}','1','{$imginfos[0]}','{$imginfos['1']}','0','{$fsize}','" . time() . "','" . $cuserLogin->getUserID() . "'); "; $dsql->ExecuteNoneQuery($inquery); $fid = $dsql->GetLastID(); AddMyAddon($fid, $iurl); if ($pagestyle > 2) { $litpicname = GetImageMapDD($iurl, $cfg_ddimg_width); } else { $litpicname = $iurl; } if (empty($firstdd) && !empty($litpicname)) { $firstdd = $litpicname; if (!file_exists($cfg_basedir . $firstdd)) { $firstdd = $iurl; } } @WaterImg($rndFileName, 'down'); $rsimg .= "{dede:img ddimg='{$litpicname}' text='' width='" . $imginfos[0] . "' height='" . $imginfos[1] . "'} {$iurl} {/dede:img}\r\n"; } } return $rsimg; }
/** * 获得HTML里的外部资源,针对图集 * * @param string $body 内容 * @param string $rfurl 地址 * @param string $firstdd 第一个缩略图 * @return string */ function GetCurContentAlbum($body, $rfurl, &$firstdd) { global $cfg_multi_site, $cfg_basehost, $ddmaxwidth, $cfg_basedir, $pagestyle, $cfg_mb_rmdown, $title, $cfg_ml, $cfg_user_dir; include_once DEDEINC . "/dedecollection.func.php"; if (empty($ddmaxwidth)) { $ddmaxwidth = 240; } $rsimg = ''; $basehost = "http://" . $_SERVER["HTTP_HOST"]; $img_array = array(); preg_match_all("/(src|SRC)=[\"|'| ]{0,}(http:\\/\\/([^>]*)\\.(gif|jpg|png))/isU", $body, $img_array); $img_array = array_unique($img_array[2]); $imgUrl = $cfg_user_dir . "/" . $cfg_ml->M_ID; $imgPath = $cfg_basedir . $imgUrl; if (!is_dir($imgPath . "/")) { MkdirAll($imgPath, $GLOBALS['cfg_dir_purview']); CloseFtp(); } $milliSecond = MyDate("ymdHis", time()); foreach ($img_array as $key => $value) { if (preg_match("#" . $basehost . "#i", $value)) { continue; } if ($cfg_basehost != $basehost && preg_match("#" . $cfg_basehost . "#i", $value)) { continue; } if (!preg_match("#^http:\\/\\/#i", $value)) { continue; } if ($cfg_mb_rmdown == 'Y') { $value = trim($value); $itype = substr($value, -4, 4); if (!preg_match("#\\.(gif|jpg|png)#i", $itype)) { $itype = ".jpg"; } $rndFileName = $imgPath . "/" . $milliSecond . $key . $itype; $iurl = $imgUrl . "/" . $milliSecond . $key . $itype; //下载并保存文件 //$rs = $htd->SaveToBin($rndFileName); $rs = DownImageKeep($value, $rfurl, $rndFileName, '', 0, 30); if ($rs) { if ($pagestyle > 2) { $litpicname = GetImageMapDD($iurl, $ddmaxwidth); if ($litpicname != '') { SaveUploadInfo($title, $litpicname, 1, $addinfos); } } else { $litpicname = ''; } if (empty($firstdd)) { $firstdd = $litpicname; if (!file_exists($cfg_basedir . $firstdd)) { $firstdd = $iurl; } } @WaterImg($rndFileName, 'down'); $info = ''; $imginfos = GetImageSize($rndFileName, $info); SaveUploadInfo($title, $iurl, 1, $imginfos); $rsimg .= "{dede:img ddimg='{$litpicname}' text='' width='" . $imginfos[0] . "' height='" . $imginfos[1] . "'} {$iurl} {/dede:img}\r\n"; } } else { $rsimg .= "{dede:img ddimg='{$value}' text='' width='0' height='0'} {$value} {/dede:img}\r\n"; } } return $rsimg; }
function DownMedia($dourl,$mtype='img'){ //检测是否已经下载此文件 $isError = false; $errfile = $GLOBALS['cfg_phpurl'].'/img/etag.gif'; $row = $this->dsql->GetOne("Select nurl from #@__co_mediaurl where rurl like '$dourl'"); $wi = false; if(!empty($row['nurl'])){ $filename = $row['nurl']; return $filename; }else{ //如果不存在,下载该文件 $filename = $this->GetRndName($dourl,$mtype); if(!ereg("^/",$filename)) $filename = "/".$filename; //反盗链模式 if($this->Item["isref"]=='yes' && $this->Item["refurl"]!=''){ if($this->Item["exptime"]=='') $this->Item["exptime"] = 10; $rs = DownImageKeep($dourl,$this->Item["refurl"],$GLOBALS['cfg_basedir'].$filename,"",0,$this->Item["exptime"]); if($rs){ $inquery = "INSERT INTO #@__co_mediaurl(nid,rurl,nurl) VALUES ('".$this->NoteId."', '".addslashes($dourl)."', '".addslashes($filename)."');"; $this->dsql->ExecuteNoneQuery($inquery); }else{ $inquery = "INSERT INTO #@__co_mediaurl(nid,rurl,nurl) VALUES ('".$this->NoteId."', '".addslashes($dourl)."', '".addslashes($errfile)."');"; $this->dsql->ExecuteNoneQuery($inquery); $isError = true; } if($mtype=='img'){ $wi = true; } //常规模式 }else{ $this->CHttpDown->OpenUrl($dourl); $this->CHttpDown->SaveToBin($GLOBALS['cfg_basedir'].$filename); $inquery = "INSERT INTO #@__co_mediaurl(nid,rurl,nurl) VALUES ('".$this->NoteId."', '".addslashes($dourl)."', '".addslashes($filename)."');"; $this->dsql->ExecuteNoneQuery($inquery); if($mtype=='img'){ $wi = true; } $this->CHttpDown->Close(); } } //生成缩略图 if($mtype=='img' && $this->breImage=='' && !$isError){ $this->breImage = $filename; if(!eregi("^http://",$this->breImage) && file_exists($GLOBALS['cfg_basedir'].$filename)){ $filenames = explode('/',$filename); $filenamed = $filenames[count($filenames)-1]; $nfilename = "lit_".$filenamed; $nfilename = str_replace($filenamed,$nfilename,$filename); if(file_exists($GLOBALS['cfg_basedir'].$nfilename)){ $this->breImage = $nfilename; }else if(copy($GLOBALS['cfg_basedir'].$filename,$GLOBALS['cfg_basedir'].$nfilename)){ ImageResize($GLOBALS['cfg_basedir'].$nfilename,$GLOBALS['cfg_ddimg_width'],$GLOBALS['cfg_ddimg_height']); $this->breImage = $nfilename; } } } if($wi && !$isError) @WaterImg($GLOBALS['cfg_basedir'].$filename,'up'); if(!$isError) return $filename; else return $errfile; }