|
|
为了大家灌水方便,特修改几个文件,达到自动将网页内的html代码解析为UBB代码,同时将远程图片保存在论坛所在服务器。加快访问速度。
本人在discuz 4.0版本下调试测试用过
首先修改模板文件
./templates/default/post_newthread.htm找到
- <textarea rows="18" name="message" style="width: 80%; word-break: break-all" tabindex="4" onSelect="javascript: storeCaret(this);" onClick="javascript: storeCaret(this);" onKeyUp="javascript: storeCaret(this);" onKeyDown="ctlent(event);" >$message</textarea>
复制代码
修改为
- <textarea rows="18" name="message" style="width: 80%; word-break: break-all" tabindex="4" onSelect="javascript: storeCaret(this);" onClick="javascript: storeCaret(this);" onKeyUp="javascript: storeCaret(this);" onKeyDown="ctlent(event);" onbeforepaste="if(document.getElementById('x_paste').checked){window.clipboardData.setData('textID',trans());this.focus();}" id="textID">$message</textarea>
- <BR>
- <br>
- <INPUT onclick="document.getElementById('textID').value += trans_java()" type=button value="转换为ubb代码"> <INPUT onclick="document.getElementById('textID').value =''" type=button value="清除数据">
- <IFRAME id=rtf style="WIDTH: 0px; HEIGHT: 0px" marginWidth=0
- marginHeight=0 src="about:blank" scrolling=no></IFRAME><LABEL
- for=x_paste></LABEL>
- <SCRIPT>
- rtf.document.designMode="On";
- </SCRIPT>
复制代码
将
./templates/default/post_newreply.htm
中的
- <textarea rows="18" name="message" style="width: 80%; word-break: break-all" tabindex="4" onSelect="javascript: storeCaret(this);" onClick="javascript: storeCaret(this);" onKeyUp="javascript: storeCaret(this);" onKeyDown="ctlent(event);" >$message</textarea>
复制代码
修改为
- <textarea rows="18" name="message" style="width: 80%; word-break: break-all" tabindex="4" onSelect="javascript: storeCaret(this);" onClick="javascript: storeCaret(this);" onKeyUp="javascript: storeCaret(this);" onKeyDown="ctlent(event);" onbeforepaste="if(document.getElementById('x_paste').checked){window.clipboardData.setData('textID',trans());this.focus();}" id="textID">$message</textarea>
- <BR>
- <br>
- <INPUT onclick="document.getElementById('textID').value += trans_java()" type=button value="转换为ubb代码"> <INPUT onclick="document.getElementById('textID').value =''" type=button value="清除数据">
- <IFRAME id=rtf style="WIDTH: 0px; HEIGHT: 0px" marginWidth=0
- marginHeight=0 src="about:blank" scrolling=no></IFRAME><LABEL
- for=x_paste></LABEL>
- <SCRIPT>
- rtf.document.designMode="On";
- </SCRIPT>
复制代码
然后修改./include/common.js
在最后添加
- //------------------------------------------------------------------------------------
- function SymError()
- {
- return true;
- }
- window.onerror = SymError;
- function trans_java() {
- var str = "";
- rtf.focus();
- rtf.document.body.innerHTML = "";
- rtf.document.execCommand("paste");
- str = rtf.document.body.innerHTML;
- if(str.length == 0) {
- alert("剪贴板不存在超文本数据!");
- return "";
- }
- str = str.replace(/\r/g,"");
- str = str.replace(" ","");
-
- str = str.replace(/on(load|click|dbclick|mouseover|mousedown|mouseup)="[^"]+"/ig,"");
- str = str.replace(/<script[^>]*?>([\w\W]*?)<\/script>/ig,"");
- str = str.replace(/<a[^>]+href="([^"]+)"[^>]*>(.*?)<\/a>/ig,"[url=$1]$2[/url]");
- str = str.replace(/<font[^>]+color=([^ >]+)[^>]*>(.*?)<\/font>/ig,"[color=$1]$2[/color]");
-
- str = str.replace(/<img[^>]+src="([^"]+)"[^>]*>/ig,"[img]$1[/img]\n\n");
-
- str = str.replace(/<([\/]?)b>/ig,"[$1b]");
- str = str.replace(/<([\/]?)strong>/ig,"[$1b]");
- str = str.replace(/<([\/]?)u>/ig,"[$1u]");
- str = str.replace(/<([\/]?)i>/ig,"[$1i]");
-
- str = str.replace(/ /g," ");
- str = str.replace(/&/g,"&");
- str = str.replace(/"/g,""");
- str = str.replace(/</g,"<");
- str = str.replace(/>/g,">");
-
- str = str.replace(/<br>/ig,"\n");
- str = str.replace(/<[^>]*?>/g,"");
- str = str.replace(/\[url=([^\]]+)\]\n(\[img\]\1\[\/img\])\n\[\/url\]/g,"$2");
- str = str.replace(/\n+/g,"\n");
- str = str.replace(/QUOTE:/ig,"");
- str = str.replace("","");
- return str;
- }
- //------------------------------------------------------------------------------------------------
复制代码
到这里就可以实现剪贴板 ubb代码的转换,为了能使远程图片保存到本地服务器,还需要做如下修改,修改前请先备份相关文件。
打开 ./include/newthread.inc.php
找到
- $db->query("INSERT INTO {$tablepre}threads (fid, readperm, price, iconid, typeid, author, authorid, subject, dateline, lastpost, lastposter, displayorder, digest, blog, poll, attachment, moderated)
- VALUES ('$fid', '$readperm', '$price', '$iconid', '$typeid', '$discuz_user', '$discuz_uid', '$subject', '$timestamp', '$timestamp', '$discuz_user', '$displayorder', '$digest', '$blog', '$poll', '$attachment', '$moderated')");
复制代码
在其上面添加如下代码
- //获取$message中的远程图片
- $content = $message;
- $saveremoteimg = 1;
- $imgPath = './attachments' ;
- if(!empty($saveremoteimg)) {
- $body = stripslashes($content);
- $img_array = array();
- preg_match_all("/(\[img\]|\[IMG\])(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png))(\[\/img\]|\[\/IMG\])/isU",$body,$img_array);
- $img_array = array_unique($img_array[2]);
- set_time_limit(0);
- //$imgUrl = $img_dir."/".strftime("%Y%m%d",time());
- //$imgPath = $base_dir.$imgUrl;
- $milliSecond = strftime("%Y%m%d",time()).strftime("%H%M%S",time());
- //if(!is_dir($imgPath)) @mkdir($imgPath,0777);
- foreach($img_array as $key =>$value){
- $value = trim($value);
- $get_file = @file_get_contents($value);
- $rndFileName = $imgPath."/".$milliSecond.$key.".".substr($value,-3,3);
- $fileurl = $boardurl.'attachments/'.$milliSecond.$key.".".substr($value,-3,3);
- if($get_file) {
- $fp = @fopen($rndFileName,"w");
- @fwrite($fp,$get_file);
- @fclose($fp);
- }
- $body = ereg_replace($value,$fileurl,$body);
- }
- $content = addslashes($body);
- }
- $message = $content;
- //--------------------------------------------------
复制代码
修改./include/newreply.inc.php
找到
- $pinvisible = $modnewreplies ? 2 : 0;
复制代码
在其上面添加
- //获取文章body中的远程图片
- $content = $message;
- $saveremoteimg = 1;
- $imgPath = './attachments' ;
- if(!empty($saveremoteimg)) {
- $body = stripslashes($content);
- $img_array = array();
- preg_match_all("/(\[img\]|\[IMG\])(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png))(\[\/img\]|\[\/IMG\])/isU",$body,$img_array);
- $img_array = array_unique($img_array[2]);
- set_time_limit(0);
- $milliSecond = strftime("%Y%m%d",time()).strftime("%H%M%S",time());
- foreach($img_array as $key =>$value){
- $value = trim($value);
- $get_file = @file_get_contents($value);
- $rndFileName = $imgPath."/".$milliSecond.$key.".".substr($value,-3,3);
- $fileurl = $boardurl.'attachments/'.$milliSecond.$key.".".substr($value,-3,3);
- if($get_file) {
- $fp = @fopen($rndFileName,"w");
- @fwrite($fp,$get_file);
- @fclose($fp);
- }
- $body = ereg_replace($value,$fileurl,$body);
- }
- $content = addslashes($body);
- }
- $message = $content;
- //--------------------------------------------------
复制代码
至此修改完毕
默认图片的保存路径在./attachments目录下,由于时间有限,没有将其做在后台,有兴趣的朋友可以修改,将其放到后台设置里面,使其更加灵活,适应性更高。
演示论坛请参考金山新人社区
(2006 帖子)
|
|