]> git.mxchange.org Git - friendica.git/blob - include/bbcode.php
get rid of 'Friendika'
[friendica.git] / include / bbcode.php
1 <?php
2
3 require_once("include/oembed.php");
4 require_once('include/event.php');
5
6
7
8 function stripcode_br_cb($s) {
9         return '[code]' . str_replace('<br />', '', $s[1]) . '[/code]';
10 }
11
12 function tryoembed($match){
13         $url = ((count($match)==2)?$match[1]:$match[2]);
14 //      logger("tryoembed: $url");
15
16         $o = oembed_fetch_url($url);
17
18         //echo "<pre>"; var_dump($match, $url, $o); killme();
19
20         if ($o->type=="error") return $match[0];
21
22         $html = oembed_format_object($o);
23         return $html; //oembed_iframe($html,$o->width,$o->height);
24
25 }
26
27 // [noparse][i]italic[/i][/noparse] turns into
28 // [noparse][ i ]italic[ /i ][/noparse],
29 // to hide them from parser.
30
31 function bb_spacefy($st) {
32   $whole_match = $st[0];
33   $captured = $st[1];
34   $spacefied = preg_replace("/\[(.*?)\]/", "[ $1 ]", $captured);
35   $new_str = str_replace($captured, $spacefied, $whole_match);
36   return $new_str;
37 }
38
39 // The previously spacefied [noparse][ i ]italic[ /i ][/noparse],
40 // now turns back and the [noparse] tags are trimed
41 // returning [i]italic[/i]
42
43 function bb_unspacefy_and_trim($st) {
44   $whole_match = $st[0];
45   $captured = $st[1];
46   $unspacefied = preg_replace("/\[ (.*?)\ ]/", "[$1]", $captured);
47   return $unspacefied;
48 }
49
50 function bb_find_open_close($s, $open, $close, $occurance = 1) {
51
52         if($occurance < 1)
53                 $occurance = 1;
54
55         $start_pos = -1;
56         for($i = 1; $i <= $occurance; $i++) {
57                 if( $start_pos !== false)
58                         $start_pos = strpos($s, $open, $start_pos + 1);
59         }
60
61         if( $start_pos === false)
62                 return false;
63
64         $end_pos = strpos($s, $close, $start_pos);
65
66         if( $end_pos === false)
67                 return false;
68
69         $res = array( 'start' => $start_pos, 'end' => $end_pos );
70
71         return $res;
72 }
73
74 function get_bb_tag_pos($s, $name, $occurance = 1) {
75
76         if($occurance < 1)
77                 $occurance = 1;
78
79         $start_open = -1;
80         for($i = 1; $i <= $occurance; $i++) {
81                 if( $start_open !== false)
82                         $start_open = strpos($s, '[' . $name, $start_open + 1); // allow [name= type tags
83         }
84
85         if( $start_open === false)
86                 return false;
87
88         $start_equal = strpos($s, '=', $start_open);
89         $start_close = strpos($s, ']', $start_open);
90
91         if( $start_close === false)
92                 return false;
93
94         $start_close++;
95
96         $end_open = strpos($s, '[/' . $name . ']', $start_close);
97
98         if( $end_open === false)
99                 return false;
100
101         $res = array( 'start' => array('open' => $start_open, 'close' => $start_close),
102                       'end' => array('open' => $end_open, 'close' => $end_open + strlen('[/' . $name . ']')) );
103         if( $start_equal !== false)
104                 $res['start']['equal'] = $start_equal + 1;
105
106         return $res;
107 }
108
109 function bb_tag_preg_replace($pattern, $replace, $name, $s) {
110
111         $string = $s;
112
113         $occurance = 1;
114         $pos = get_bb_tag_pos($string, $name, $occurance);
115         while($pos !== false && $occurance < 1000) {
116
117                 $start = substr($string, 0, $pos['start']['open']);
118                 $subject = substr($string, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']);
119                 $end = substr($string, $pos['end']['close']);
120                 if($end === false)
121                         $end = '';
122
123                 $subject = preg_replace($pattern, $replace, $subject);
124                 $string = $start . $subject . $end;
125
126                 $occurance++;
127                 $pos = get_bb_tag_pos($string, $name, $occurance);
128         }
129
130         return $string;
131 }
132
133 if(! function_exists('bb_extract_images')) {
134 function bb_extract_images($body) {
135
136         $saved_image = array();
137         $orig_body = $body;
138         $new_body = '';
139
140         $cnt = 0;
141         $img_start = strpos($orig_body, '[img');
142         $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
143         $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
144         while(($img_st_close !== false) && ($img_end !== false)) {
145
146                 $img_st_close++; // make it point to AFTER the closing bracket
147                 $img_end += $img_start;
148
149                 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
150                         // This is an embedded image
151
152                         $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
153                         $new_body = $new_body . substr($orig_body, 0, $img_start) . '[$#saved_image' . $cnt . '#$]';
154
155                         $cnt++;
156                 }
157                 else
158                         $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
159
160                 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
161
162                 if($orig_body === false) // in case the body ends on a closing image tag
163                         $orig_body = '';
164
165                 $img_start = strpos($orig_body, '[img');
166                 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
167                 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
168         }
169
170         $new_body = $new_body . $orig_body;
171
172         return array('body' => $new_body, 'images' => $saved_image);
173 }}
174
175 if(! function_exists('bb_replace_images')) {
176 function bb_replace_images($body, $images) {
177
178         $newbody = $body;
179
180         $cnt = 0;
181         foreach($images as $image) {
182                 // We're depending on the property of 'foreach' (specified on the PHP website) that
183                 // it loops over the array starting from the first element and going sequentially
184                 // to the last element
185                 $newbody = str_replace('[$#saved_image' . $cnt . '#$]', '<img src="' . $image .'" alt="' . t('Image/photo') . '" />', $newbody);
186                 $cnt++;
187         }
188
189         return $newbody;
190 }}
191
192
193
194         // BBcode 2 HTML was written by WAY2WEB.net
195         // extended to work with Mistpark/Friendica - Mike Macgirvin
196
197 function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
198
199         $a = get_app();
200
201         // Hide all [noparse] contained bbtags by spacefying them
202         // POSSIBLE BUG --> Will the 'preg' functions crash if there's an embedded image?
203
204         $Text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'bb_spacefy',$Text);
205         $Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_spacefy',$Text);
206         $Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_spacefy',$Text);
207
208
209         // Move all spaces out of the tags
210         $Text = preg_replace("/\[(\w*)\](\s*)/ism", '$2[$1]', $Text);
211         $Text = preg_replace("/(\s*)\[\/(\w*)\]/ism", '[/$2]$1', $Text);
212
213         // Extract the private images which use data url's since preg has issues with
214         // large data sizes. Stash them away while we do bbcode conversion, and then put them back
215         // in after we've done all the regex matching. We cannot use any preg functions to do this.
216
217         $extracted = bb_extract_images($Text);
218         $Text = $extracted['body'];
219         $saved_image = $extracted['images'];
220
221         // If we find any event code, turn it into an event.
222         // After we're finished processing the bbcode we'll
223         // replace all of the event code with a reformatted version.
224
225         $ev = bbtoevent($Text);
226
227
228         // Replace any html brackets with HTML Entities to prevent executing HTML or script
229         // Don't use strip_tags here because it breaks [url] search by replacing & with amp
230
231         $Text = str_replace("<", "&lt;", $Text);
232         $Text = str_replace(">", "&gt;", $Text);
233
234         // Convert new line chars to html <br /> tags
235
236         // nlbr seems to be hopelessly messed up
237         //      $Text = nl2br($Text);
238
239         // We'll emulate it.
240
241         $Text = str_replace("\r\n","\n", $Text);
242         $Text = str_replace(array("\r","\n"), array('<br />','<br />'), $Text);
243
244         if($preserve_nl)
245                 $Text = str_replace(array("\n","\r"), array('',''),$Text);
246
247
248
249         // Set up the parameters for a URL search string
250         $URLSearchString = "^\[\]";
251         // Set up the parameters for a MAIL search string
252         $MAILSearchString = $URLSearchString;
253
254
255         // Perform URL Search
256
257         $Text = preg_replace("/([^\]\=]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1<a href="$2" target="external-link">$2</a>', $Text);
258
259         if ($tryoembed)
260                 $Text = preg_replace_callback("/\[bookmark\=([^\]]*)\].*?\[\/bookmark\]/ism",'tryoembed',$Text);
261
262         $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url=$1]$2[/url]',$Text);
263
264         if ($tryoembed)
265                 $Text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$Text);
266
267         $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="external-link">$1</a>', $Text);
268         $Text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '<a href="$1" target="external-link">$2</a>', $Text);
269         //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
270
271         // we may need to restrict this further if it picks up too many strays
272         // link acct:user@host to a webfinger profile redirector
273
274         $Text = preg_replace('/acct:(.*?)@(.*?)([ ,])/', '<a href="' . $a->get_baseurl() . '/acctlink?addr=' . "$1@$2" 
275                 . '" target="extlink" >acct:' . "$1@$2$3" . '</a>',$Text);
276
277         // Perform MAIL Search
278         $Text = preg_replace("/\[mail\]([$MAILSearchString]*)\[\/mail\]/", '<a href="mailto:$1">$1</a>', $Text);
279         $Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.*?)\[\/mail\]/", '<a href="mailto:$1">$2</a>', $Text);
280
281         // Check for bold text
282         $Text = preg_replace("(\[b\](.*?)\[\/b\])ism",'<strong>$1</strong>',$Text);
283
284         // Check for Italics text
285         $Text = preg_replace("(\[i\](.*?)\[\/i\])ism",'<em>$1</em>',$Text);
286
287         // Check for Underline text
288         $Text = preg_replace("(\[u\](.*?)\[\/u\])ism",'<u>$1</u>',$Text);
289
290         // Check for strike-through text
291         $Text = preg_replace("(\[s\](.*?)\[\/s\])ism",'<strike>$1</strike>',$Text);
292
293         // Check for over-line text
294         $Text = preg_replace("(\[o\](.*?)\[\/o\])ism",'<span class="overline">$1</span>',$Text);
295
296         // Check for colored text
297         $Text = preg_replace("(\[color=(.*?)\](.*?)\[\/color\])ism","<span style=\"color: $1;\">$2</span>",$Text);
298
299         // Check for sized text
300         // [size=50] --> font-size: 50px (with the unit).
301         $Text = preg_replace("(\[size=(\d*?)\](.*?)\[\/size\])ism","<span style=\"font-size: $1px;\">$2</span>",$Text);
302         $Text = preg_replace("(\[size=(.*?)\](.*?)\[\/size\])ism","<span style=\"font-size: $1;\">$2</span>",$Text);
303
304         // Check for centered text
305         $Text = preg_replace("(\[center\](.*?)\[\/center\])ism","<div style=\"text-align:center;\">$1</div>",$Text);
306
307         // Check for list text
308         $Text = str_replace("[*]", "<li>", $Text);
309
310         // Check for style sheet commands
311         $Text = preg_replace("(\[style=(.*?)\](.*?)\[\/style\])ism","<span style=\"$1;\">$2</span>",$Text);
312
313         // Check for CSS classes
314         $Text = preg_replace("(\[class=(.*?)\](.*?)\[\/class\])ism","<span class=\"$1\">$2</span>",$Text);
315
316         // handle nested lists
317         $endlessloop = 0;
318
319         while ((((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false)) ||
320                ((strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false)) || 
321                ((strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false)) || 
322                ((strpos($Text, "[/li]") !== false) && (strpos($Text, "[li]") !== false))) && (++$endlessloop < 20)) {
323                 $Text = preg_replace("/\[list\](.*?)\[\/list\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
324                 $Text = preg_replace("/\[list=\](.*?)\[\/list\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>' ,$Text);
325                 $Text = preg_replace("/\[list=1\](.*?)\[\/list\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
326                 $Text = preg_replace("/\[list=((?-i)i)\](.*?)\[\/list\]/ism",'<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>' ,$Text);
327                 $Text = preg_replace("/\[list=((?-i)I)\](.*?)\[\/list\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>' ,$Text);
328                 $Text = preg_replace("/\[list=((?-i)a)\](.*?)\[\/list\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>' ,$Text);
329                 $Text = preg_replace("/\[list=((?-i)A)\](.*?)\[\/list\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>' ,$Text);
330                 $Text = preg_replace("/\[ul\](.*?)\[\/ul\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
331                 $Text = preg_replace("/\[ol\](.*?)\[\/ol\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
332                 $Text = preg_replace("/\[li\](.*?)\[\/li\]/ism", '<li>$1</li>' ,$Text);
333         }
334
335         $Text = preg_replace("/\[th\](.*?)\[\/th\]/sm", '<th>$1</th>' ,$Text);
336         $Text = preg_replace("/\[td\](.*?)\[\/td\]/sm", '<td>$1</td>' ,$Text);
337         $Text = preg_replace("/\[tr\](.*?)\[\/tr\]/sm", '<tr>$1</tr>' ,$Text);
338         $Text = preg_replace("/\[table\](.*?)\[\/table\]/sm", '<table>$1</table>' ,$Text);
339
340         $Text = preg_replace("/\[table border=1\](.*?)\[\/table\]/sm", '<table border="1" >$1</table>' ,$Text);
341         $Text = preg_replace("/\[table border=0\](.*?)\[\/table\]/sm", '<table border="0" >$1</table>' ,$Text);
342
343         $Text = str_replace('[hr]','<hr />', $Text);
344
345         // This is actually executed in prepare_body()
346
347         $Text = str_replace('[nosmile]','',$Text);
348
349         // Check for font change text
350         $Text = preg_replace("/\[font=(.*?)\](.*?)\[\/font\]/sm","<span style=\"font-family: $1;\">$2</span>",$Text);
351
352         // Declare the format for [code] layout
353
354 //      $Text = preg_replace_callback("/\[code\](.*?)\[\/code\]/ism",'stripcode_br_cb',$Text);
355
356         $CodeLayout = '<code>$1</code>';
357         // Check for [code] text
358         $Text = preg_replace("/\[code\](.*?)\[\/code\]/ism","$CodeLayout", $Text);
359
360         // Declare the format for [spoiler] layout
361         $SpoilerLayout = '<blockquote class="spoiler">$1</blockquote>';
362
363         // Check for [spoiler] text
364         // handle nested quotes
365         $endlessloop = 0;
366         while ((strpos($Text, "[/spoiler]") !== false) and (strpos($Text, "[spoiler]") !== false) and (++$endlessloop < 20))
367                 $Text = preg_replace("/\[spoiler\](.*?)\[\/spoiler\]/ism","$SpoilerLayout", $Text);
368
369         // Check for [spoiler=Author] text
370
371         $t_wrote = t('$1 wrote:');
372
373         // handle nested quotes
374         $endlessloop = 0;
375         while ((strpos($Text, "[/spoiler]")!== false)  and (strpos($Text, "[spoiler=") !== false) and (++$endlessloop < 20))
376                 $Text = preg_replace("/\[spoiler=[\"\']*(.*?)[\"\']*\](.*?)\[\/spoiler\]/ism",
377                                      "<br /><strong class=".'"spoiler"'.">" . $t_wrote . "</strong><blockquote class=".'"spoiler"'.">$2</blockquote>",
378                                      $Text);
379
380         // Declare the format for [quote] layout
381         $QuoteLayout = '<blockquote>$1</blockquote>';
382
383         // Check for [quote] text
384         // handle nested quotes
385         $endlessloop = 0;
386         while ((strpos($Text, "[/quote]") !== false) and (strpos($Text, "[quote]") !== false) and (++$endlessloop < 20))
387                 $Text = preg_replace("/\[quote\](.*?)\[\/quote\]/ism","$QuoteLayout", $Text);
388
389         // Check for [quote=Author] text
390
391         $t_wrote = t('$1 wrote:');
392
393         // handle nested quotes
394         $endlessloop = 0;
395         while ((strpos($Text, "[/quote]")!== false)  and (strpos($Text, "[quote=") !== false) and (++$endlessloop < 20))
396                 $Text = preg_replace("/\[quote=[\"\']*(.*?)[\"\']*\](.*?)\[\/quote\]/ism",
397                                      "<br /><strong class=".'"author"'.">" . $t_wrote . "</strong><blockquote>$2</blockquote>",
398                                      $Text);
399
400         // [img=widthxheight]image source[/img]
401         //$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="height: $2px; width: $1px;" >', $Text);
402         $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="width: $1px;" >', $Text);
403
404         // Images
405         // [img]pathtoimage[/img]
406         $Text = preg_replace("/\[img\](.*?)\[\/img\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
407
408
409
410         $Text = preg_replace("/\[crypt\](.*?)\[\/crypt\]/ism",'<br/><img src="' .$a->get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . t('Encrypted content') . '" /><br />', $Text);
411         $Text = preg_replace("/\[crypt=(.*?)\](.*?)\[\/crypt\]/ism",'<br/><img src="' .$a->get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . '$1' . ' ' . t('Encrypted content') . '" /><br />', $Text);
412
413
414         // Try to Oembed
415         if ($tryoembed) {
416                 $Text = preg_replace("/\[video\](.*?\.(ogg|ogv|oga|ogm|webm|mp4))\[\/video\]/ism", '<video src="$1" controls="controls" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="$1">$1</a></video>', $Text);
417                 $Text = preg_replace("/\[audio\](.*?\.(ogg|ogv|oga|ogm|webm|mp4|mp3))\[\/audio\]/ism", '<audio src="$1" controls="controls"><a href="$1">$1</a></audio>', $Text);
418
419                 $Text = preg_replace_callback("/\[video\](.*?)\[\/video\]/ism", 'tryoembed', $Text);
420                 $Text = preg_replace_callback("/\[audio\](.*?)\[\/audio\]/ism", 'tryoembed', $Text);
421         } else {
422                 $Text = preg_replace("/\[video\](.*?)\[\/video\]/", '$1', $Text);
423                 $Text = preg_replace("/\[audio\](.*?)\[\/audio\]/", '$1', $Text);
424         }
425
426         // html5 video and audio
427
428
429         if ($tryoembed)
430                 $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<iframe src="$1" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="$1">$1</a></iframe>', $Text);
431         else
432                 $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<a href="$1">$1</a>', $Text);
433
434         // Youtube extensions
435         if ($tryoembed) {
436                 $Text = preg_replace_callback("/\[youtube\](https?:\/\/www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);        
437                 $Text = preg_replace_callback("/\[youtube\](www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);        
438                 $Text = preg_replace_callback("/\[youtube\](https?:\/\/youtu.be\/.*?)\[\/youtube\]/ism",'tryoembed',$Text); 
439         }
440
441         $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text); 
442         $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text); 
443         $Text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
444
445         if ($tryoembed)
446                 $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="http://www.youtube.com/embed/$1" frameborder="0" ></iframe>', $Text);
447         else
448                 $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", "http://www.youtube.com/watch?v=$1", $Text);
449
450
451         if ($tryoembed) {
452                 $Text = preg_replace_callback("/\[vimeo\](https?:\/\/player.vimeo.com\/video\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text); 
453                 $Text = preg_replace_callback("/\[vimeo\](https?:\/\/vimeo.com\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text); 
454         }
455
456         $Text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text); 
457         $Text = preg_replace("/\[vimeo\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text);
458
459         if ($tryoembed)
460                 $Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="http://player.vimeo.com/video/$1" frameborder="0" ></iframe>', $Text);
461         else
462                 $Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", "http://vimeo.com/$1", $Text);
463
464 //      $Text = preg_replace("/\[youtube\](.*?)\[\/youtube\]/", '<object width="425" height="350" type="application/x-shockwave-flash" data="http://www.youtube.com/v/$1" ><param name="movie" value="http://www.youtube.com/v/$1"></param><!--[if IE]><embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" width="425" height="350" /><![endif]--></object>', $Text);
465
466
467         // oembed tag
468         $Text = oembed_bbcode2html($Text);
469
470         // Avoid triple linefeeds through oembed
471         $Text = str_replace("<br style='clear:left'></span><br /><br />", "<br style='clear:left'></span><br />", $Text);
472
473         // If we found an event earlier, strip out all the event code and replace with a reformatted version.
474         // Replace the event-start section with the entire formatted event. The other bbcode is stripped.
475         // Summary (e.g. title) is required, earlier revisions only required description (in addition to 
476         // start which is always required). Allow desc with a missing summary for compatibility.
477
478         if((x($ev,'desc') || x($ev,'summary')) && x($ev,'start')) {
479                 $sub = format_event_html($ev);
480
481                 $Text = preg_replace("/\[event\-summary\](.*?)\[\/event\-summary\]/ism",'',$Text);
482                 $Text = preg_replace("/\[event\-description\](.*?)\[\/event\-description\]/ism",'',$Text);
483                 $Text = preg_replace("/\[event\-start\](.*?)\[\/event\-start\]/ism",$sub,$Text); 
484                 $Text = preg_replace("/\[event\-finish\](.*?)\[\/event\-finish\]/ism",'',$Text);
485                 $Text = preg_replace("/\[event\-location\](.*?)\[\/event\-location\]/ism",'',$Text);
486                 $Text = preg_replace("/\[event\-adjust\](.*?)\[\/event\-adjust\]/ism",'',$Text);
487         }
488
489         // Unhide all [noparse] contained bbtags unspacefying them 
490         // and triming the [noparse] tag.
491
492         $Text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'bb_unspacefy_and_trim',$Text);
493         $Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_unspacefy_and_trim',$Text);
494         $Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_unspacefy_and_trim',$Text);
495
496
497         $Text = preg_replace('/\[\&amp\;([#a-z0-9]+)\;\]/','&$1;',$Text);
498         $Text = preg_replace('/\&\#039\;/','\'',$Text);
499         $Text = preg_replace('/\&quot\;/','"',$Text);
500
501         // fix any escaped ampersands that may have been converted into links
502         $Text = preg_replace("/\<(.*?)(src|href)=(.*?)\&amp\;(.*?)\>/ism",'<$1$2=$3&$4>',$Text);
503         $Text = preg_replace("/\<(.*?)(src|href)=\"[^hfm](.*?)\>/ism",'<$1$2="">',$Text);
504
505         if($saved_image)
506                 $Text = bb_replace_images($Text, $saved_image);
507
508         // Clean up the HTML by loading and saving the HTML with the DOM
509         // Only do it when it has to be done - for performance reasons
510         if (!$tryoembed) {
511                 $doc = new DOMDocument();
512                 $doc->preserveWhiteSpace = false;
513
514                 $Text = mb_convert_encoding($Text, 'HTML-ENTITIES', "UTF-8");
515
516                 $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
517                 @$doc->loadHTML($doctype."<html><body>".$Text."</body></html>");
518
519                 $Text = $doc->saveHTML();
520                 $Text = str_replace(array("<html><body>", "</body></html>", $doctype), array("", "", ""), $Text);
521
522                 $Text = str_replace('<br></li>','</li>', $Text);
523
524                 $Text = mb_convert_encoding($Text, "UTF-8", 'HTML-ENTITIES');
525         }
526
527         call_hooks('bbcode',$Text);
528
529         return $Text;
530 }
531