discuz 发布贴子远程图片url本地化
修改/souce/module/forum/forum_post.php
增加代码
//download image
$ym=date('Ym'); $d=date('d'); $his=date('His'); $remote=0; if(!is_dir($_G['setting']['attachdir']."/forum/".$ym."/")) @mkdir($_G['setting']['attachdir']."/forum/".$ym."/"); if(!is_dir($_G['setting']['attachdir']."/forum/".$ym."/".$d."/")) @mkdir($_G['setting']['attachdir']."/forum/".$ym."/".$d."/"); if(preg_match_all("/\[img[^\]]*\](.*)\[\/img\]/isU",$_GET['message'],$regs)){ @set_time_limit(0); require_once libfile('class/image'); $image = new image; $watermarkstatus=unserialize($_G['setting']['watermarkstatus']); $expdomains=explode("\n",str_replace("\r","",$expdomains)); $expdomains[]=$_G['siteurl']; foreach ($regs[1] as $key=>$url){ $url=trim($url); if(substr($url,0,8)!='https://') continue; $isexp=0; foreach ($expdomains as $domain){ $domain=trim($domain); if($domain&&strpos($url,$domain)!==false){ $isexp=1; break; } } if($isexp){ continue; } $ym=date('Ym'); $d=date('d'); $his=date('His'); $remote=0; if(!is_dir($_G['setting']['attachdir']."/forum/".$ym."/")) @mkdir($_G['setting']['attachdir']."/forum/".$ym."/"); if(!is_dir($_G['setting']['attachdir']."/forum/".$ym."/".$d."/")) @mkdir($_G['setting']['attachdir']."/forum/".$ym."/".$d."/"); $attachmenturl=$ym."/".$d."/".$his.strtolower(random(16)).".jpg"; $target=$_G['setting']['attachdir']."/forum/".$attachmenturl; $attachsave=savefile($url,$target); if($attachsave){ $width=$attachsave; $filename=$key.'.jpg'; $filetype='application/octet-stream'; $filesize=filesize($target); $isimage=1; $thumb=0; if(!$remote){ $thumb = $image->Thumb($target, '', $_G['setting']['thumbwidth'], $_G['setting']['thumbheight'], $_G['setting']['thumbstatus'], $_G['setting']['thumbsource']) ? 1 : 0; if(!$_G['setting']['thumbsource']) { $width = $image->imginfo['width']; } /* if($watermarkstatus['forum'] && empty($_G['forum']['disablewatermark'])) { $image->Watermark($target); } */ } /* DB::query("INSERT INTO ".DB::table("forum_attachment_unused")." ( `aid` , `width` , `dateline` , `filename` , `filetype` , `filesize` , `attachment` , `downloads` , `isimage` , `uid` , `thumb` , `remote` )
VALUES (NULL , '0', '0', '$width', '{$_G['timestamp']}', '0', '0', '{$filename}', '{$filetype}', '{$filesize}', '{$attachmenturl}', '0', '{$isimage}', '{$_G['uid']}', '{$thumb}', '{$remote}');");
*/
DB::query("INSERT INTO ".DB::table("forum_attachment")." ( `aid` , `tid` , `pid` , `uid` , `tableid` , `downloads` ) VALUES (NULL , '0', '0', '{$_G['uid']}', '127', '0');"); $aid=DB::insert_id(); DB::query("INSERT INTO ".DB::table("forum_attachment_unused")." ( `aid` , `uid` , `dateline` , `filename` , `filesize` , `attachment` , `remote` , `isimage` , `width` , `thumb` ) VALUES ('$aid', '{$_G['uid']}', '{$_G['timestamp']}', '{$filename}','{$filesize}', '{$attachmenturl}', '{$remote}', '{$isimage}', '{$width}', '{$thumb}');"); $_GET['attachnew'][$aid]=array(); $_GET['message']=str_replace_once($regs[0][$key],"[attachimg]{$aid}[/attachimg]",$_GET['message']);//因为当前DX不支持多次引用附件 } } } //die();
//调用函数
function savefile($src,$target){
if(file_exists($target)){
}
elseif(@copy($src,$target)){
}elseif ($content=@file_get_contents($src)){
$fp=@fopen($target,"w");
@fwrite($fp,$content);
@fclose($fp);
}elseif ($content=@xdfopen($src)){
$fp=@fopen($target,"w");
@fwrite($fp,$content);
@fclose($fp);
}
$sizeinfo=@getimagesize($target);
if(!$sizeinfo[0]||!$sizeinfo[2]){
@unlink($target);
return false;
}else{
return $sizeinfo[0];
}
return false;
}
function str_replace_once($needle, $replace, $haystack) {
$pos = strpos($haystack, $needle);
if ($pos === false) {
return $haystack;
}
return substr_replace($haystack, $replace, $pos, strlen($needle));
}