/
_bbcode.plugin.php
392 lines (348 loc) · 10.7 KB
/
_bbcode.plugin.php
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
/**
* This file implements the BBcode plugin for b2evolution
*
* BB style formatting, like [b]bold[/b]
*
* b2evolution - {@link http://b2evolution.net/}
* Released under GNU GPL License - {@link http://b2evolution.net/about/gnu-gpl-license}
* @copyright (c)2003-2020 by Francois Planque - {@link http://fplanque.com/}
*
* @package plugins
*/
if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
load_class( '../plugins/_custom_tags.plugin.php', 'custom_tags_plugin' );
/**
* @package plugins
*/
class bbcode_plugin extends custom_tags_plugin
{
var $code = 'b2evBBco';
var $name = 'BB code';
var $priority = 50;
var $version = '7.2.5';
var $group = 'rendering';
var $short_desc;
var $long_desc;
var $help_topic = 'bb-code-plugin';
var $number_of_installs = 1;
/*
* Internal
*/
var $toolbar_label = 'BB code:';
var $configurable_post_list = true;
var $configurable_comment_list = true;
var $configurable_message_list = true;
var $configurable_email_list = true;
var $post_search_list;
var $post_replace_list;
var $comment_search_list;
var $comment_replace_list;
var $msg_search_list;
var $msg_replace_list;
var $email_search_list;
var $email_replace_list;
var $default_search_list = '[b] #\[b](.+?)\[/b]#is
[i] #\[i](.+?)\[/i]#is
[s] #\[s](.+?)\[/s]#is
[color] !\[color=(#?[A-Za-z0-9]+?)](.+?)\[/color]!is
[size] #\[size=([0-9]+?)](.+?)\[/size]#is
[font] #\[font=([A-Za-z0-9 ;\-]+?)](.+?)\[/font]#is
#\[quote(=?)](.+?)\[/quote]#is
#\[quote=([^\]\#]*?)\#([cp][0-9]+)](.+?)\[/quote]#is
[quote] #\[quote=([^\]]*?)](.+?)\[/quote]#is
[indent] #\[indent](.+?)\[/indent]#is
[list=1] #\[list=1](.+?)\[/list]#is
[list=a] #\[list=a](.+?)\[/list]#is
[list] #\[list](.+?)\[/list]#is
[*] #\[\*](.+?)(\n|\[/list\])#is
[bg] !\[bg=(#?[A-Za-z0-9]+?)](.+?)\[/bg]!is
[clear] #\[clear]#is';
var $default_replace_list = '<strong>$1</strong>
<em>$1</em>
<span style="text-decoration:line-through">$1</span>
<span style="color:$1">$2</span>
<span style="font-size:$1px">$2</span>
<span style="font-family:$1">$2</span>
<blockquote>$2</blockquote>
<strong class="quote_author">$1 wrote <a href="#$2">earlier</a>:</strong><blockquote>$3</blockquote>
<strong class="quote_author">$1 wrote:</strong><blockquote>$2</blockquote>
<div class="indented">$1</div>
<ol type="1">$1</ol>
<ol type="a">$1</ol>
<ul>$1</ul>
<li>$1</li>
<span style="background-color:$1">$2</span>
<div class="clear"></div>';
/**
* Init
*/
function PluginInit( & $params )
{
$this->short_desc = T_('BB formatting e-g [b]bold[/b]');
$this->long_desc = T_('Supported tags and the BB code toolbar are configurable.
Supported tags by default are: [b] [i] [s] [color=...] [size=...] [font=...] [quote] [list=1] [list=a] [list] [*] [bg=] [clear]');
}
/**
* Parse BB code
* ( The main purpose of this function is to parsing multilevel lists tags,
* i.e. when one [list] is contained inside other [list] )
*
* @param string Content
* @param array Search list
* @param array Replace list
* @return string Content
*/
function parse_bbcode( $content, $search_list, $replace_list )
{
if( empty( $content ) )
{ // No content, Exit here
return $content;
}
$complex = array();
$simple = array();
foreach( $search_list as $i => $search_text )
{ // Split the masks to separate things
preg_match( '#(\\\\\[.+\])\(.+\)(\\\\\[.+\])#is', $search_text, $search_tags );
preg_match( '#(<.+>)\$.+(<.+>)#is', $replace_list[ $i ], $replace_tags );
if( !empty( $search_tags ) )
{
$complex[] = array(
's_full' => $search_text,
's_start' => '!^'.$search_tags[1].'!is',
's_end' => '!^'.$search_tags[2].'!is',
'r_full' => $replace_list[ $i ],
'r_start' => $replace_tags[1],
'r_end' => $replace_tags[2],
);
}
else
{
$simple[] = array(
's_full' => $search_text,
'r_full' => $replace_list[ $i ],
);
}
}
//pre_dump($complex);
foreach( $simple as $tag_info )
{ // Make simple replacements
$content = preg_replace( $tag_info['s_full'], $tag_info['r_full'], $content );
}
// Complex parsing with multilevel tags structure
$c_pos = 0;
$content_length = strlen( $content );
$opened_tags = array();
$new_content = $content;
while( $c_pos < $content_length - 2 )
{
if( $content[ $c_pos ].$content[ $c_pos + 1 ] == '[/' )
{ // Closed tag
if( !empty( $opened_tags ) )
{ // Get closed tag that was opened as last
$sub_content = substr( $content, $c_pos );
$closed_tag_info = $complex[ $opened_tags[ count( $opened_tags ) - 1 ] ];
if( preg_match( $closed_tag_info['s_end'], $sub_content ) )
{
$first_part = str_replace( $sub_content, '', $new_content );
$sub_content = preg_replace( $closed_tag_info['s_end'], $closed_tag_info['r_end'], $sub_content );
$new_content = $first_part.$sub_content;
//pre_dump( 'CLOSE', $opened_tags, $closed_tag_info['r_end'], $closed_tag_info['s_end'], $new_content );
// Remove this tag from opened tags array
array_pop( $opened_tags );
}
}
}
elseif( $content[ $c_pos ] == '[' )
{ // Opened tag
$sub_content = substr( $content, $c_pos );
foreach( $complex as $tag_num => $tag_info )
{
if( preg_match( $tag_info['s_start'], $sub_content ) )
{
$first_part = str_replace( $sub_content, '', $new_content );
$sub_content = preg_replace( $tag_info['s_start'], $tag_info['r_start'], $sub_content );
$new_content = $first_part.$sub_content;
// Add this tag to know what tag was opened as last
$opened_tags[] = $tag_num;
//pre_dump( 'OPEN', $opened_tags, $tag_info['s_start'], $new_content );
break;
}
}
}
$c_pos++;
}
//pre_dump($new_content);
$new_content = $this->parse_anchor_links( $new_content );
return $new_content;
}
/**
* Parse anchor links, Set absolute path for each link with relative anchor like <a href="#">
*
* @param string Content
* @return string Content
*/
function parse_anchor_links( $content )
{
if( preg_match_all( '/ href="#(c|p)([0-9]+)"/i', $content, $matches ) )
{
$CommentCache = & get_CommentCache();
$ItemCache = & get_ItemCache();
foreach( $matches[0] as $m => $full_match )
{
$object_ID = $matches[2][$m];
$new_url = '';
switch( $matches[1][$m] )
{ // Object type:
case 'p':
// Item
if( $Item = & $ItemCache->get_by_ID( $object_ID, false, false ) )
{ // Replace anchor url with item permanent url
$new_url = $Item->get_permanent_url().'#p'.$object_ID;
}
break;
case 'c':
// Comment
if( $Comment = & $CommentCache->get_by_ID( $object_ID, false, false ) )
{ // Replace anchor url with comment permanent url
$new_url = $Comment->get_permanent_url();
}
break;
default:
// Incorrect object type, Skip this url
continue 2;
}
if( !empty( $new_url ) )
{ // Replace relative anchor url with new absolute url
$content = str_replace( $full_match, ' href="'.$new_url.'"', $content );
}
}
}
return $content;
}
/**
* The following function are here so the events will be registered
* @see Plugins_admin::get_registered_events()
*/
function RenderItemAsHtml( & $params )
{
parent::RenderItemAsHtml( $params );
}
function RenderMessageAsHtml( & $params )
{
parent::RenderMessageAsHtml( $params );
}
function RenderEmailAsHtml( & $params )
{
parent::RenderItemAsHtml( $params );
}
/**
* Do the same as for HTML.
*
* @see RenderItemAsHtml()
*/
function RenderItemAsXml( & $params )
{
$this->RenderItemAsHtml( $params );
}
/**
*
* Render comments if required
*
* @see Plugin::FilterCommentContent()
*/
function FilterCommentContent( & $params )
{
$Comment = & $params['Comment'];
$comment_Item = & $Comment->get_Item();
$item_Blog = & $comment_Item->get_Blog();
if( in_array( $this->code, $Comment->get_renderers_validated() ) )
{ // apply_comment_rendering is set to always render
$content = & $params['data'];
if( !isset( $this->comment_search_list ) )
{ // Init comment search list only first time:
$this->comment_search_list = $this->prepare_search_list( $this->get_coll_setting( 'coll_comment_search_list', $item_Blog ) );
}
if( !isset( $this->comment_replace_list ) )
{ // Init comment replace list only first time:
$this->comment_replace_list = $this->prepare_replace_list( $this->get_coll_setting( 'coll_comment_replace_list', $item_Blog ) );
}
$content = replace_outside_code_and_short_tags( $this->comment_search_list, $this->comment_replace_list, $content, array( $this, 'parse_bbcode' ) );
}
}
/**
* Event handler: Called when displaying editor toolbars on post/item form.
*
* This is for post/item edit forms only. Comments, PMs and emails use different events.
*
* @param array Associative array of parameters
* @return boolean did we display a toolbar?
*/
function AdminDisplayToolbar( & $params )
{
$params['target_type'] = 'Item';
return $this->DisplayCodeToolbar( $params );
}
/**
* Event handler: Called when displaying editor toolbars on comment form.
*
* @param array Associative array of parameters
* @return boolean did we display a toolbar?
*/
function DisplayCommentToolbar( & $params )
{
$params['target_type'] = 'Comment';
return $this->DisplayCodeToolbar( $params );
}
/**
* Event handler: Called when displaying editor toolbars for message.
*
* @param array Associative array of parameters
* @return boolean did we display a toolbar?
*/
function DisplayMessageToolbar( & $params )
{
$params['target_type'] = 'Message';
return $this->DisplayCodeToolbar( $params );
}
/**
* Event handler: Called when displaying editor toolbars for email.
*
* @param array Associative array of parameters
* @return boolean did we display a toolbar?
*/
function DisplayEmailToolbar( & $params )
{
$apply_rendering = $this->get_email_setting( 'email_apply_rendering' );
if( ! empty( $apply_rendering ) && $apply_rendering != 'never' )
{
$params['target_type'] = 'EmailCampaign';
return $this->DisplayCodeToolbar( $params );
}
return false;
}
/**
* Prepare a search list
*
* @param string String value of a search list
* @return array The search list as array
*/
function prepare_search_list( $search_list_string )
{
$search_list_array = explode( "\n", str_replace( "\r", '', $search_list_string ) );
foreach( $search_list_array as $l => $line )
{ // Remove button name from regexp string
$line = explode( ' ', $line, 2 );
if( empty( $line[1] ) )
{ // Bad format of search string
unset( $search_list_array[ $l ] );
}
else
{ // Replace this line with regexp value (to delete a button name)
$search_list_array[ $l ] = $line[1];
}
}
return $search_list_array;
}
}
?>