立即注册 登录
汉山 返回首页

admin的个人空间 http://s541722682.onlinehome.us/?1 [收藏] [复制] [分享] [RSS]

日志

Discuz功能改进:导入博文和图片 (彼岸网,天香公主)

已有 941 次阅读2015-10-12 02:04 |系统分类:技术支持 | 宽屏 请点击显示宽屏,再点击恢复窄屏 | 动漫全图 如只见部分动漫,请点击显示全图,再点击恢复窄图

Discuz功能改进:导入博文和图片

热度 3已有 130 次阅读2015-10-12 12:45 AM |个人分类:Discuz

有很多博主都会将一篇博文发在几个网站,毕竟写篇原创不易,多发几个地方就多 些读者,也有的这样做是为了备份。但是从一个网站直接将博文拷贝到另一个网站对有的摄影博主来讲效果并不理想,因为这样做的话博文中的图片并没有和博文的 内容一起拷贝到第二个网站。这样的博文也不会出现在各种摄影模块里因为那些模块只选择含有本地图片的博文。本文讨论既相关又独立的两个问题:如何在日志编 辑器的工具栏里添加两个按钮,一个将远程日志内容拷贝到编辑器里,另一个将日志里的远程图片都下载到本地,并使得日志内容指向下载的本地图片。

1。导入图片
Discuz在论坛编辑器和文章编辑器里都提供了下载远程图片到本地的功能(需在管理中心对选定的用户组设置后才能使用论坛编辑器:用户->用户组->选择用户组编辑->论坛相关->贴子相关->允许下载远程图片: 远程图片本地化保存),唯独日志编辑器里缺少这个功能。下面我们介绍下在日志编辑器里添加这个功能的步骤。


1)在日志编辑器的工具栏里添加"下载远程图片"按钮: 将文件 source/module/home/home_editor.php 里的这段 <?php if ($isportal) { ?> <a href="javascript:;" class="icoPage" id="icoPage" onClick="pageBreak(event, 1);return false;" title="<?php echo lang('home/editor', 'editor_pagebreak'); ?>"></a> <a href="javascript:;" class="icoDown" id="icoDown" onClick="parent.downRemoteFile();return false;" title="<?php echo lang('home/editor', 'editor_download_remote'); ?>"></a> <?php } ?> 改为: <?php if ($isportal) { ?> <a href="javascript:;" class="icoPage" id="icoPage" onClick="pageBreak(event, 1);return false;" title="<?php echo lang('home/editor', 'editor_pagebreak'); ?>"></a> <?php } ?> <a href="javascript:;" class="icoDown" id="icoDown" onClick="parent.downRemoteFile();return false;" title="<?php echo lang('home/editor', 'editor_download_remote'); ?>"></a> 2)修改对点击该按钮的事件处理代码使之兼顾文章编辑器和日志编辑器。将文件 static/image/editor/editor_function.js 中的函数 downRemoteFile function downRemoteFile() { edit_save(); var formObj = $("articleform"); var oldAction = formObj.action; formObj.action = "portal.php?mod=portalcp&ac=upload&op=downremotefile"; formObj.onSubmit = ""; formObj.target = "uploadframe"; formObj.submit(); formObj.action = oldAction; formObj.target = ""; } 改为: function downRemoteFile() { edit_save(); var formObj = $("articleform"); if (formObj != null) { var oldAction = formObj.action; formObj.action = "portal.php?mod=portalcp&ac=upload&op=downremotefile"; } else { formObj = $("ttHtmlEditor"); var oldAction = formObj.action; formObj.action = "home.php?mod=spacecp&ac=upload&op=downremotefile"; } formObj.onSubmit = ""; formObj.target = "uploadframe"; formObj.submit(); formObj.action = oldAction; formObj.target = ""; } 注意这里设定了在日志编辑器里点击该按钮后将编辑器里的内容提交给了服务器的 home.php?mod=spacecp&ac=upload&op=downremotefile 来处理,并将处理结果交给编辑器所在网页的一个名叫 uploadframe 的 iframe。接下来两步设置了与之配套的代码。
3)添加服务器对在日志编辑器里点击该按钮后提交的编辑器里的内容的处理。这是这个功能的关键部分,在这里下载了远程图片并换了日志里远程图片的链接。在文件 source/include/spacecp/spacecp_upload.php 开始部分 if(!defined('IN_DISCUZ')) { exit('Access Denied'); } 后添加一段: if($_GET['op']=='downremotefile') { if(submitcheck('blogsubmit') && helper_access::check_module('blog')) { $upload = new discuz_upload(); $arrayimageurl = $temp = $imagereplace = array(); $string = $_GET['message']; preg_match_all("/\<img.+src=('|\"|)?(.*)(\\1)([\s].*)?\>/ismUe", $string, $temp, PREG_SET_ORDER); if(is_array($temp) && !empty($temp)) { foreach($temp as $tempvalue) { $tempvalue[2] = str_replace('\"', '', $tempvalue[2]); if(strlen($tempvalue[2])){ $arrayimageurl[] = $tempvalue[2]; } } $arrayimageurl = array_unique($arrayimageurl); if($arrayimageurl) { foreach($arrayimageurl as $tempvalue) { $imageurl = $tempvalue; $imagereplace['oldimageurl'][] = $imageurl; $attach['ext'] = $upload->fileext($imageurl); if(!$upload->is_image_ext($attach['ext'])) { continue; } $content = ''; if(preg_match('/^(http:\/\/|\.)/i', $imageurl)) { $content = dfsockopen($imageurl); } if(empty($content)) continue; $temp = explode('/', $imageurl); $attach['name'] = trim($temp[count($temp)-1]); $_G['setting']['allowwatermark'] = 0; $attach = stream_save($content, 0, 'jpg', $attach['name'], '', 0); $attachs[] = daddslashes($attach); $attach['url'] = ($attach['remote'] ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']).'album/'; $imagereplace['newimageurl'][] = $attach['url'].$attach['filepath']; } } } if($imagereplace) { $string = preg_replace(array("/\<(script|style|iframe)[^\>]*?\>.*?\<\/(\\1)\>/si", "/\<!*(--|doctype|html|head|meta|link|body)[^\>]*?\>/si"), '', $string); $string = str_replace($imagereplace['oldimageurl'], $imagereplace['newimageurl'], $string); $string = str_replace(array("\r", "\n", "\r\n"), '', addcslashes($string, '/"\\\'')); print <<<EOF <script type="text/javascript"> var f = parent.window.frames["uchome-ifrHtmlEditor"].window.frames["HtmlEditor"]; f.document.body.innerHTML = '$string'; </script> EOF; } exit(); } } 4)在日志编辑器所在网页添加一个 iframe 来接受服务器处理结果。在文件 template/default/home/spacecp_blog.htm 的最下方的 javascript 代码 <script type="text/javascript"> if($('subject')) { $('subject').focus(); } </script> 前添加 <iframe id="uploadframe" name="uploadframe" width="0" height="0" marginwidth="0" frameborder="0" src="about:blank"></iframe>
2。导入日志


1)添加新按钮所用汉字和相应对话框里的汉字。在文件 source/language/home/lang_editor.php 里添加: 'editor_download_blog' => '下载远程日志', 'editor_prompt_bloglink' => '请输入日志 URL 地址', 2)添加新按钮所用图标。在文件夹 static/image/editor 里加入图片文件 download_blog.gif:
3)在日志编辑器的工具栏里添加新按钮。在文件 source/module/home/home_editor.php 里加入 (可以加在前述"下载远程图片"按钮的后面): <?php if (!$isportal) { ?> <a href="javascript:;" id="icoDownBlog" onClick="downloadBlog(event, 1);return false;" title="<?php echo lang('home/editor', 'editor_download_blog'); ?>" style="background-image: url(static/image/editor/download_blog.gif)"></a> <?php } ?> 4)添加点击该按钮后出现的填写远程日志URL的对话框。在文件 source/module/home/home_editor.php 里这段 <div id="createUrl" class="eMenu" style="display:none;top:35px;left:26px;width:300px;font-size:12px"> <?php echo lang('home/editor', 'editor_prompt_textlink'); ?>:<br/> <input type="text" id="insertUrl" name="url" value="http://" onfocus="checkURL(this, 1);" onblur="checkURL(this, 0);" class="t_input" style="width: 190px;"> <input type="button" onclick="createLink();" name="createURL" value="<?php echo lang('home/editor', 'editor_ok'); ?>" class="submit" /> <a href="javascript:;" onclick="fHide($('createUrl'));return false;" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank"><?php echo lang('home/editor', 'editor_cancel'); ?></a> </div> 后添加: <div id="downloadBlogBox" class="eMenu" style="display:none;top:35px;left:26px;width:300px;font-size:12px"> <?php echo lang('home/editor', 'editor_prompt_bloglink'); ?>:<br /> <input type="text" id="insertBlogUrl" name="url" value="http://" onfocus="checkURL(this, 1);" onblur="checkURL(this, 0);" class="t_input" style="width: 190px;"> <input type="button" onclick="downloadBlog();" name="downloadBlogBox" value="<?php echo lang('home/editor', 'editor_ok'); ?>" class="submit" /> <a href="javascript:;" onclick="fHide($('downloadBlogBox'));return false;" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank"><?php echo lang('home/editor', 'editor_cancel'); ?></a> </div> 5)添加在点击该按钮后显示对话框的代码。在文件 static/image/editor/editor_base.js 里添加下面的函数: function downloadBlog(e, show) { if (typeof show == 'undefined') { var urlObj = $('insertBlogUrl'); var sURL = urlObj.value; if ((sURL != null) && (sURL != "http://")) { parent.downRemoteBlog(sURL); } fHide($('downloadBlogBox')); urlObj.value = 'http://'; } else { if (gIsIE) { var e = window.event; } getCaret(); var dvUrlBox = $("downloadBlogBox"); var iX = e.clientX; var iY = e.clientY; dvUrlBox.style.display = ""; dvUrlBox.style.left = (iX - 300) + "px"; dvUrlBox.style.top = 33 + "px"; } } 6)添加当用户在对话框里填写了远程日志URL并点击确定按钮后,提交该URL给服务器的代码。在文件 static/image/editor/editor_function.js 里添加下面这个函数: function downRemoteBlog(url) { edit_save(); var formObj = $("ttHtmlEditor"); var oldAction = formObj.action; formObj.action = "home.php?mod=spacecp&ac=upload&op=downremoteblog&blogurl=" + encodeURIComponent(url); formObj.onSubmit = ""; formObj.target = "uploadframe"; formObj.submit(); formObj.action = oldAction; formObj.target = ""; } 7)添加服务器对在日志编辑器里点击该按钮后提交的编辑器里的内容的处理。在文件 source/include/spacecp/spacecp_upload.php 开始部分 if(!defined('IN_DISCUZ')) { exit('Access Denied'); } 后添加一段: if($_GET['op']=='downremoteblog') { if(submitcheck('blogsubmit') && helper_access::check_module('blog')) { $subject = $_GET['subject']; $string = $_GET['message']; $blogurl = $_GET['blogurl']; $doc = new DOMDocument; $doc->loadHTMLFile($blogurl); $xpath = new DOMXPath($doc); $entries = $xpath->query('//h1[@class="ph"]'); foreach($entries as $entry) { $blogSubject = $entry->nodeValue; } $entries = $xpath->query('//div[@id="blog_article"]'); foreach($entries as $entry) { $blogContent = get_inner_html($entry); } if($blogContent) { $subject .= $blogSubject; $subject = str_replace(array("\r", "\n", "\r\n"), '', addcslashes($subject, '/"\\\'')); $string .= preg_replace("/\<img.+src=('|\"|)?(.*)(\\1)([\s].*)?\>/ismUe", "get_image_tag('\\2', '$blogurl')", $blogContent); $string = str_replace(array("\r", "\n", "\r\n"), '', addcslashes($string, '/"\\\'')); print <<<EOF <script type="text/javascript"> parent.window.document.getElementById('subject').value = "$subject"; var f = parent.window.frames["uchome-ifrHtmlEditor"].window.frames["HtmlEditor"]; f.document.body.innerHTML = "$string"; </script> EOF; } exit(); } } 8)在文件 source/include/spacecp/spacecp_upload.php 里添加几个上面用到的函数: // http://php.net/manual/de/class.domelement.php function get_inner_html( $node ) { $innerHTML= ''; $children = $node->childNodes; foreach ($children as $child) { $innerHTML .= $child->ownerDocument->saveXML( $child ); } return $innerHTML; } // http://99webtools.com/blog/convert-relative-path-into-absolute-url/ function rel2abs($rel, $base) { if(strpos($rel,"//")===0) return "http:".$rel; if (parse_url($rel, PHP_URL_SCHEME) != '') return $rel; if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel; extract(parse_url($base)); $path = preg_replace('#/[^/]*$#', '', $path); if ($rel[0] == '/') $path = ''; $abs = "$host$path/$rel"; $re = array('#(/.?/)#', '#/(?!..)[^/]+/../#'); for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {} return $scheme.'://'.$abs; } function get_image_tag($imageurl, $baseurl) { return "<img src='".rel2abs($imageurl, $baseurl)."'>"; } 9)在日志编辑器所在网页添加一个 iframe 来接受服务器处理结果(与第一部分的4)相同)。

最后总结一下。在日志编辑器的工具栏里添加了"下载远程日志"和"下载远程图片"这两个按钮后,博主可以这样来将发表在其它网站的博文拷贝到本网站:
1)在博文列表页里点击"发表新日志"按钮
2)点击"下载远程日志"按钮,在对话框里填写要下载的其它网站的博文的网址
3)点击"下载远程图片"按钮
4)点击"保存发布"按钮


鲜花

真棒

玩闹

同情

看看

困惑

震惊

bad

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

Archiver|手机版|小黑屋|汉山网    

GMT-5, 2023-2-1 18:06 , Processed in 0.053886 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

返回顶部