class_bbcode.php
3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: class_bbcode.php 27449 2012-02-01 05:32:35Z zhangguosheng $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class bbcode {
var $search_exp = array();
var $replace_exp = array();
var $search_str = array();
var $replace_str = array();
var $html_s_exp = array();
var $html_r_exp = array();
var $html_s_str = array();
var $html_r_str = array();
function &instance() {
static $object;
if(empty($object)) {
$object = new bbcode();
}
return $object;
}
function bbcode() {
}
function bbcode2html($message, $parseurl=0) {
if(empty($this->search_exp)) {
$this->search_exp = array(
"/\s*\[quote\][\n\r]*(.+?)[\n\r]*\[\/quote\]\s*/is",
"/\[url\]\s*(https?:\/\/|ftp:\/\/|gopher:\/\/|news:\/\/|telnet:\/\/|rtsp:\/\/|mms:\/\/|callto:\/\/|ed2k:\/\/){1}([^\[\"']+?)\s*\[\/url\]/i",
"/\[em:([0-9]+):\]/i",
);
$this->replace_exp = array(
"<div class=\"quote\"><blockquote>\\1</blockquote></div>",
"<a href=\"\\1\\2\" target=\"_blank\">\\1\\2</a>",
" <img src=\"".STATICURL."image/smiley/comcom/\\1.gif\" class=\"vm\"> "
);
$this->replace_exp[] = '$this->bb_img(\'\\1\')';
$this->search_str = array('[b]', '[/b]','[i]', '[/i]', '[u]', '[/u]');
$this->replace_str = array('<b>', '</b>', '<i>','</i>', '<u>', '</u>');
}
if($parseurl==2) {
$this->search_exp[] = "/\[img\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/ies";
$this->replace_exp[] = '$this->bb_img(\'\\1\')';
$message = bbcode::parseurl($message);
}
@$message = str_replace($this->search_str, $this->replace_str,preg_replace($this->search_exp, $this->replace_exp, $message, 20));
return nl2br(str_replace(array("\t", ' ', ' '), array(' ', ' ', ' '), $message));
}
function parseurl($message) {
return preg_replace("/(?<=[^\]a-z0-9-=\"'\\/])((https?|ftp|gopher|news|telnet|mms|rtsp):\/\/)([a-z0-9\/\-_+=.~!%@?#%&;:$\\()|]+)/i", "[url]\\1\\3[/url]", ' '.$message);
}
function html2bbcode($message) {
if(empty($this->html_s_exp)) {
$this->html_s_exp = array(
"/\<div class=\"quote\"\>\<blockquote\>(.*?)\<\/blockquote\>\<\/div\>/is",
"/\<a href=\"(.+?)\".*?\<\/a\>/is",
"/(\r\n|\n|\r)/",
"/<br.*>/siU",
"/[ \t]*\<img src=\"static\/image\/smiley\/comcom\/(.+?).gif\".*?\>[ \t]*/is",
"/\s*\<img src=\"(.+?)\".*?\>\s*/is"
);
$this->html_r_exp = array(
"[quote]\\1[/quote]",
"\\1",
'',
"\n",
"[em:\\1:]",
"\n[img]\\1[/img]\n"
);
$this->html_s_str = array('<b>', '</b>', '<i>','</i>', '<u>', '</u>', ' ', ' ', ' ', '<', '>', '&');
$this->html_r_str = array('[b]', '[/b]','[i]', '[/i]', '[u]', '[/u]', "\t", ' ', ' ', '<', '>', '&');
}
@$message = str_replace($this->html_s_str, $this->html_r_str,
preg_replace($this->html_s_exp, $this->html_r_exp, $message));
$message = dhtmlspecialchars($message);
return trim($message);
}
function bb_img($url) {
global $_G;
if(!in_array(strtolower(substr($url, 0, 6)), array('http:/', 'https:', 'ftp://', 'rtsp:/', 'mms://'))) {
$url = isset($_G['siteurl']) && !empty($_G['siteurl']) ? $_G['siteurl'].$url : 'http://'.$url;
}
$url = addslashes($url);
return "<img src=\"$url\" class=\"vm\">";
}
}
?>