这里会显示出您选择的修订版和当前版本之间的差别。
后一修订版 | 前一修订版 | ||
zblogphp:development:plugin:interfaces:filter_plugin_postarticle_core [2016/06/20 19:56] 127.0.0.1 外部编辑 |
— (当前版本) | ||
---|---|---|---|
行 1: | 行 1: | ||
- | ====== Filter_Plugin_PostArticle_Core ====== | ||
- | ===== 接口描述 ===== | ||
- | 接管本插件接口,将提交文章数据时,在过滤数据内容及保存数据前运行插件自定义代码,即可通过该接口更改保存的文章内容。 | ||
- | |||
- | ===== 应用场景 ===== | ||
- | 过滤文章内容,替换指定数据内容等,如将文章中的远程文件链接自动换为本地URL等。 | ||
- | |||
- | ===== 使用说明 ===== | ||
- | ==== 调用方法 ==== | ||
- | <code php> | ||
- | Add_Filter_Plugin('Filter_Plugin_PostArticle_Core','demo'); | ||
- | function demo(&$article){ | ||
- | echo 'demo'; | ||
- | } | ||
- | </code> | ||
- | ==== 调用参数 ==== | ||
- | ^参数类型 ^参数 ^描述 ^ | ||
- | |[[zblogphp:pluginsinterface:post]] |$article |当前提交的文章数据实例 | | ||
- | ==== 返回参数 ==== | ||
- | 无 | ||
- | ===== 代码示例 ===== | ||
- | 以下代码来自插件[[http://app.zblogcn.com/?id=390|保存远程图片]],实现的步骤简单描述如下: | ||
- | - 找出文章中的远程图片 | ||
- | - 将远程图片保存到本地服务器中 | ||
- | - 替换文章中的图片地址为本地地址 | ||
- | <code php 1> | ||
- | #注册插件 | ||
- | 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; | ||
- | |||
- | set_time_limit(0); | ||
- | ZBlogException::ClearErrorHook(); | ||
- | $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; | ||
- | |||
- | } | ||
- | </code> |