zblogphp文章提交的核心接口说明
接口名称:Filter_Plugin_PostArticle_Core
接口描述:接管本插件接口,将提交文章数据时,在过滤数据内容及保存数据前运行插件自定义代码,即可通过该接口更改保存的文章内容。
应用场景:过滤文章内容,替换指定数据内容等,如将文章中的远程文件链接自动换为本地URL、添加文章时添加附件信息(如发送邮箱)等。
调用方法:
Add_Filter_Plugin('Filter_Plugin_PostArticle_Core','yt_demo');
调用参数:
参数类型:post
参数:$article
描述:当前提交的文章数据实例
返回参数:
无
调用示例:
#注册插件 RegisterPlugin("RemoteImage","ActivePlugin_RemoteImage"); #激活插件时挂接 Filter_Plugin_PostArticle_Core接口 function ActivePlugin_RemoteImage() { Add_Filter_Plugin('Filter_Plugin_PostArticle_Core','RemoteImage_Main'); } #以引用方式接管$article实例 function RemoteImage_Main(&$article) { global $zbp; $content = $article->Content; $pattern = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/"; //匹配图片文件的正则 preg_match_all($pattern,$content,$matchContent); $picArray = $matchContent[1]; //存储匹配的图片链接 if ($picArray){ foreach($picArray as $key=>$rurl){ if(substr($rurl,0,strlen($zbp->host))!=$zbp->host) { $path=$zbp->usersdir.'upload/'.date('Y').'/'.date('m'); if(!file_exists($path)) mkdir($path,0755,true); $picname=date('YmdHis').'_'.rand(10000,99999).'.'.pathinfo($rurl,PATHINFO_EXTENSION); $pic=$path.'/'.$picname; $getpic=RemoteImage_Save($rurl,$pic,$picname); //保存远程图片到本地服务器,得到返回的本地图片地址 $picUrl=str_replace($zbp->path,$zbp->host,$pic); //把图片地址替换成本地 $article->Content=str_replace($rurl,$picUrl,$article->Content); //替换文章内容中的图片地址 } } } } #保存远程图片到本地服务器,返回保存在本地的图片地址 function RemoteImage_Save($url,$filename="",$name) { global $zbp; if($url=="") return false; if($filename=="") { $ext=strrchr($url,"."); if($ext!=".gif" && $ext!=".jpg" && $ext!=".png") return false; $filename=date("YmdHis").$ext; } ob_start(); readfile($url); $img = ob_get_contents(); ob_end_clean(); $size = strlen($img); $fp2=@fopen($filename, "a"); fwrite($fp2,$img); fclose($fp2); $upload = new Upload; $upload->Name = $name; $upload->SourceName = $name; $upload->MimeType = ""; $upload->Size = $size; $upload->AuthorID = $zbp->user->ID; $upload->Save(); return true; }
示例说明:
代码来自插件保存远程图片,实现的步骤简单描述如下:
找出文章中的远程图片
将远程图片保存到本地服务器中
替换文章中的图片地址为本地地址