-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbcode.inc
214 lines (194 loc) · 5.89 KB
/
bbcode.inc
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* COPYRIGHT INFORMATION
* This class is part of a website framework developped by
* Simon UYTTENDAELE. It may be used, copied, modified, and
* redistributed only for non-commercial activities.
*
* This copyright notice should remain visible on top of every file.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* CLASS INFORMATION
- Description:
This class manages custom bbcode encoding / decoding / display.
- Usage:
All functions are static, to use them, simply call 'bbcode::display', 'bbcode::encode' or
'bbcode::edit'.
*/
class bbcode
{
const BBCODE = 1;
const WIKI = 2;
public static function display($txt, $mode = self::BBCODE)
{
$search = array();
// converts a bbcode text to HTML display
if( $mode == self::BBCODE )
{
$search = array(
'/</', '/>/',
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[s\](.*?)\[\/s\]/is',
'/\[color\=(.*?)\](.*?)\[\/color\]/is',
'/\[small\](.*?)\[\/small\]/is',
'/\[big\](.*?)\[\/big\]/is',
'/\[tab\]/i',
'/\[br\]/i',
'/\[code\](.*?)\[\/code\]/is',
'/\[quote\](.*?)\[\/quote\]/is',
'/\[img\](.*?)\[\/img\]/is',
'/\[link\=(.*?)\](.*?)\[\/link\]|\[url\](.*?)\[\/url\]|(\s+https?:\/\/[^\s\[<>"]*)/is',
);
}
else if( $mode == self::WIKI )
{
$search = array(
'/</', '/>/',
'/(^|\s)\*(\S|\S.*?\S)\*(\s|$)/is',
'/(^|\s)_(\S|\S.*?\S)_(\s|$)/is',
'/(^|\s)\+(\S|\S.*?\S)\+(\s|$)/is',
'/(^|\s)-(\S|\S.*?\S)-(\s|$)/is',
'/(^|\s)>(\s|$)/',
'/(^|\s)@(\S|\S.*?\S)@(\s|$)/is',
'/(^|\s)\?\?(\S|\S.*?\S)\?\?(\s|$)/is',
'/(^|\s)!(\S|\S.*?\S)!(\s|$)/is',
'/"(.*?)":(https?:\/\/\S*?)/is',
'/(\r\n|\r|\n)/i'
);
}
$replace = array();
if( $mode == self::BBCODE )
{
$replace = array(
'<', '>',
'<strong>$1</strong>',
'<i>$1</i>',
'<span style="text-decoration:underline;">$1</span>',
'<span style="text-decoration:line-through;">$1</span>',
'<span style="color:$1;">$2</span>',
'<span style="font-size:0.7em;">$1</span>',
'<span style="font-size:1.3em;">$1</span>',
'<span style="margin-right:25px;"> </span>',
'<br />',
'<div class="forum_code">$1<br /> </div>',
'<div class="forum_quote">$1<br /> </div>',
'<img src="$1" alt="" />',
'<a href="$1$3$4">$2$3$4</a>'
);
}
else if( $mode == self::WIKI )
{
$replace = array(
'<', '>',
'$1<strong>$2</strong>$3',
'$1<i>$2</i>$3',
'$1<span style="text-decoration:underline;">$2</span>$3',
'$1<span style="text-decoration:line-through;">$2</span>$3',
'$1<span style="margin-right:25px;"> </span>$2',
'$1<div class="forum_code">$2<br /> </div>$3',
'$1<div class="forum_quote">$2<br /> </div>$3',
'$1<img src="$2" alt="" style="float:left;" />$3',
'<a href="$2">$1</a>',
'<br />'
);
}
return preg_replace($search, $replace, $txt);
}
public static function edit($txt, $mode = self::BBCODE)
{
// converts a bbcode text to an editable textfield value
$search = array();
$replace = array();
if( $mode == self::BBCODE )
{
$search = array('/\[br\]/', '/\[tab\]/');
$replace = array("\n", ' ');
}
return preg_replace($search, $replace, $txt);
}
public static function convert($txt, $from = self::BBCODE, $to = self::WIKI)
{
$search = array();
// converts a bbcode text to HTML display
if( $from == self::BBCODE )
{
$search = array(
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[s\](.*?)\[\/s\]/is',
'/\[tab\]/i',
'/\[code\](.*?)\[\/code\]/is',
'/\[quote\](.*?)\[\/quote\]/is',
'/\[img\](.*?)\[\/img\]/is',
'/\[link\=(.*?)\](.*?)\[\/link\]/is'
);
}
else if( $from == self::WIKI )
{
$search = array(
'/(^|\s)\*(\S|\S.*?\S)\*(\s|$)/is',
'/(^|\s)_(\S|\S.*?\S)_(\s|$)/is',
'/(^|\s)\+(\S|\S.*?\S)\+(\s|$)/is',
'/(^|\s)-(\S|\S.*?\S)-(\s|$)/is',
'/(^|\s)>(\s|$)/',
'/(^|\s)@(\S|\S.*?\S)@(\s|$)/is',
'/(^|\s)\?\?(\S|\S.*?\S)\?\?(\s|$)/is',
'/(^|\s)!(\S|\S.*?\S)!(\s|$)/is',
'/"(.*?)":(https?:\/\/\S*?)/is'
);
}
$replace = array();
if( $to == self::BBCODE )
{
$replace = array(
'$1[b]$2[/b]$3',
'$1[i]$2[/i]$3',
'$1[u]$2[/u]$3',
'$1[s]$2[/s]$3',
'$1[tab]$2',
'$1[code]$2[/code]$3',
'$1[quote]$2[/quote]$3',
'$1[img]$2[/img]$3',
'[link=$2]$1[/link]'
);
}
else if( $to == self::WIKI )
{
$replace = array(
' *$1* ',
' _$1_ ',
' +$1+ ',
' -$1- ',
'> ',
' @$1@ ',
' ??$1?? ',
' !$1! ',
'"$2":$1'
);
}
return preg_replace($search, $replace, $txt);
}
public static function encode($txt, $mode = self::BBCODE, $escape = true)
{
if( $mode == self::BBCODE )
{
$search = array('/\r/', '/\n/', '/\t/', '/\s{3,5}/', '/\s{2,}/');
$replace = array('', '[br]', '[tab]', '[tab]', ' ');
$txt = preg_replace($search, $replace, $txt);
return security::encode($txt, false);
}
else
{
// WARNING WIKI is assumed NOT to require
// escaped \'s, so we REMOVE them if they were added by the magic_quotes_gpc
$search = array('/&/', '/</', '/>/', '/\'/', '/"/');
$replace = array('&', '<', '>', ''', '"');
if( get_magic_quotes_gpc() )
$txt = stripslashes($txt);
return preg_replace($search, $replace, trim($txt));
}
}
}
?>