]> git.mxchange.org Git - friendica.git/blob - include/bbcode.php
4ccab8db5d9ffb276a17874a1ff3bd9edd680296
[friendica.git] / include / bbcode.php
1 <?php
2 require_once("include/oembed.php");
3 require_once('include/event.php');
4
5 function bb_rearrange_link($shared) {
6         if ($shared[1] != "type-link")
7                 return($shared[0]);
8
9         $newshare = trim($shared[2]);
10         $newshare = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $newshare);
11
12         if (!strpos($shared[0], "[bookmark"))
13                 $newshare = preg_replace("/\[url\=(.*?)\](.*?)\[\/url\]/ism", '[bookmark=$1]$2[/bookmark]', $newshare, 1);
14
15         preg_match("/\[img\](.*?)\[\/img\]/ism", $newshare, $matches);
16
17         if ($matches) {
18                 $newshare = str_replace($matches[0], '', $newshare);
19                 $newshare = "[img]".$matches[1]."[/img]\n".$newshare;
20         }
21
22         $search = array("\n\n", "\n ", " \n");
23         $replace = array("\n", "\n", "\n");
24         do {
25                 $oldtext = $newshare;
26                 $newshare = str_replace($search, $replace, $newshare);
27         } while ($oldtext != $newshare);
28
29         $newshare = "[class=type-link]".$newshare."[/class]";
30
31         return($newshare);
32 }
33
34 function bb_remove_share_information($Text, $plaintext = false) {
35         if ($plaintext)
36                 $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism","[bookmark=$1]$1[/bookmark]", $Text);
37
38         $Text = preg_replace_callback("((.*?)\[class=(.*?)\](.*?)\[\/class\])ism","bb_cleanup_share",$Text);
39         return($Text);
40 }
41
42 function bb_cleanup_share($shared) {
43         if (!in_array($shared[2], array("type-link", "type-video")))
44                 return($shared[0]);
45
46         if (!preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",$shared[3], $bookmark))
47                 return($shared[0]);
48
49         $title = "";
50         $link = "";
51
52         if (isset($bookmark[2][0]))
53                 $title = $bookmark[2][0];
54
55         if (isset($bookmark[1][0]))
56                 $link = $bookmark[1][0];
57
58         if (strpos($shared[1],$title) !== false)
59                 $title = "";
60
61 //        if (strpos($shared[1],$link) !== false)
62 //                $link = "";
63
64         $text = trim($shared[1]);
65
66         if (($text == "") AND ($title != "") AND ($link == ""))
67                 $text .= "\n\n".trim($title);
68
69         if (($link != "") AND ($title != ""))
70                 $text .= "\n[url=".trim($link)."]".trim($title)."[/url]";
71         elseif (($link != ""))
72                 $text .= "\n".trim($link);
73
74         return(trim($text));
75 }
76
77
78 function bb_cleanstyle($st) {
79   return "<span style=\"".cleancss($st[1]).";\">".$st[2]."</span>";
80 }
81
82 function bb_cleanclass($st) {
83   return "<span class=\"".cleancss($st[1])."\">".$st[2]."</span>";
84 }
85
86 function cleancss($input) {
87
88         $cleaned = "";
89
90         $input = strtolower($input);
91
92         for ($i = 0; $i < strlen($input); $i++) {
93                 $char = substr($input, $i, 1);
94
95                 if (($char >= "a") and ($char <= "z"))
96                         $cleaned .= $char;
97
98                 if (!(strpos(" #;:0123456789-_", $char) === false))
99                         $cleaned .= $char;
100         }
101
102         return($cleaned);
103 }
104
105 function stripcode_br_cb($s) {
106         return '[code]' . str_replace('<br />', '', $s[1]) . '[/code]';
107 }
108
109 function tryoembed($match){
110         $url = ((count($match)==2)?$match[1]:$match[2]);
111
112         // Always embed the SSL version
113         $url = str_replace(array("http://www.youtube.com/", "http://player.vimeo.com/"),
114                                 array("https://www.youtube.com/", "https://player.vimeo.com/"), $url);
115
116         //logger("tryoembed: $url");
117
118         $o = oembed_fetch_url($url);
119
120         //echo "<pre>"; var_dump($match, $url, $o); killme();
121
122         if ($o->type=="error") return $match[0];
123
124         $html = oembed_format_object($o);
125         return $html; //oembed_iframe($html,$o->width,$o->height);
126
127 }
128
129 // [noparse][i]italic[/i][/noparse] turns into
130 // [noparse][ i ]italic[ /i ][/noparse],
131 // to hide them from parser.
132
133 function bb_spacefy($st) {
134   $whole_match = $st[0];
135   $captured = $st[1];
136   $spacefied = preg_replace("/\[(.*?)\]/", "[ $1 ]", $captured);
137   $new_str = str_replace($captured, $spacefied, $whole_match);
138   return $new_str;
139 }
140
141 // The previously spacefied [noparse][ i ]italic[ /i ][/noparse],
142 // now turns back and the [noparse] tags are trimed
143 // returning [i]italic[/i]
144
145 function bb_unspacefy_and_trim($st) {
146   $whole_match = $st[0];
147   $captured = $st[1];
148   $unspacefied = preg_replace("/\[ (.*?)\ ]/", "[$1]", $captured);
149   return $unspacefied;
150 }
151
152 function bb_find_open_close($s, $open, $close, $occurance = 1) {
153
154         if($occurance < 1)
155                 $occurance = 1;
156
157         $start_pos = -1;
158         for($i = 1; $i <= $occurance; $i++) {
159                 if( $start_pos !== false)
160                         $start_pos = strpos($s, $open, $start_pos + 1);
161         }
162
163         if( $start_pos === false)
164                 return false;
165
166         $end_pos = strpos($s, $close, $start_pos);
167
168         if( $end_pos === false)
169                 return false;
170
171         $res = array( 'start' => $start_pos, 'end' => $end_pos );
172
173         return $res;
174 }
175
176 function get_bb_tag_pos($s, $name, $occurance = 1) {
177
178         if($occurance < 1)
179                 $occurance = 1;
180
181         $start_open = -1;
182         for($i = 1; $i <= $occurance; $i++) {
183                 if( $start_open !== false)
184                         $start_open = strpos($s, '[' . $name, $start_open + 1); // allow [name= type tags
185         }
186
187         if( $start_open === false)
188                 return false;
189
190         $start_equal = strpos($s, '=', $start_open);
191         $start_close = strpos($s, ']', $start_open);
192
193         if( $start_close === false)
194                 return false;
195
196         $start_close++;
197
198         $end_open = strpos($s, '[/' . $name . ']', $start_close);
199
200         if( $end_open === false)
201                 return false;
202
203         $res = array( 'start' => array('open' => $start_open, 'close' => $start_close),
204                       'end' => array('open' => $end_open, 'close' => $end_open + strlen('[/' . $name . ']')) );
205         if( $start_equal !== false)
206                 $res['start']['equal'] = $start_equal + 1;
207
208         return $res;
209 }
210
211 function bb_tag_preg_replace($pattern, $replace, $name, $s) {
212
213         $string = $s;
214
215         $occurance = 1;
216         $pos = get_bb_tag_pos($string, $name, $occurance);
217         while($pos !== false && $occurance < 1000) {
218
219                 $start = substr($string, 0, $pos['start']['open']);
220                 $subject = substr($string, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']);
221                 $end = substr($string, $pos['end']['close']);
222                 if($end === false)
223                         $end = '';
224
225                 $subject = preg_replace($pattern, $replace, $subject);
226                 $string = $start . $subject . $end;
227
228                 $occurance++;
229                 $pos = get_bb_tag_pos($string, $name, $occurance);
230         }
231
232         return $string;
233 }
234
235 if(! function_exists('bb_extract_images')) {
236 function bb_extract_images($body) {
237
238         $saved_image = array();
239         $orig_body = $body;
240         $new_body = '';
241
242         $cnt = 0;
243         $img_start = strpos($orig_body, '[img');
244         $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
245         $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
246         while(($img_st_close !== false) && ($img_end !== false)) {
247
248                 $img_st_close++; // make it point to AFTER the closing bracket
249                 $img_end += $img_start;
250
251                 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
252                         // This is an embedded image
253
254                         $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
255                         $new_body = $new_body . substr($orig_body, 0, $img_start) . '[$#saved_image' . $cnt . '#$]';
256
257                         $cnt++;
258                 }
259                 else
260                         $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
261
262                 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
263
264                 if($orig_body === false) // in case the body ends on a closing image tag
265                         $orig_body = '';
266
267                 $img_start = strpos($orig_body, '[img');
268                 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
269                 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
270         }
271
272         $new_body = $new_body . $orig_body;
273
274         return array('body' => $new_body, 'images' => $saved_image);
275 }}
276
277 if(! function_exists('bb_replace_images')) {
278 function bb_replace_images($body, $images) {
279
280         $newbody = $body;
281
282         $cnt = 0;
283         foreach($images as $image) {
284                 // We're depending on the property of 'foreach' (specified on the PHP website) that
285                 // it loops over the array starting from the first element and going sequentially
286                 // to the last element
287                 $newbody = str_replace('[$#saved_image' . $cnt . '#$]', '<img src="' . $image .'" alt="' . t('Image/photo') . '" />', $newbody);
288                 $cnt++;
289         }
290
291         return $newbody;
292 }}
293
294 function bb_ShareAttributes($match) {
295
296         $attributes = $match[1];
297
298         $author = "";
299         preg_match("/author='(.*?)'/ism", $attributes, $matches);
300         if ($matches[1] != "")
301                 $author = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
302
303         preg_match('/author="(.*?)"/ism', $attributes, $matches);
304         if ($matches[1] != "")
305                 $author = $matches[1];
306
307         $link = "";
308         preg_match("/link='(.*?)'/ism", $attributes, $matches);
309         if ($matches[1] != "")
310                 $link = $matches[1];
311
312         preg_match('/link="(.*?)"/ism', $attributes, $matches);
313         if ($matches[1] != "")
314                 $link = $matches[1];
315
316         $avatar = "";
317         preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
318         if ($matches[1] != "")
319                 $avatar = $matches[1];
320
321         preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
322         if ($matches[1] != "")
323                 $avatar = $matches[1];
324
325         $profile = "";
326         preg_match("/profile='(.*?)'/ism", $attributes, $matches);
327         if ($matches[1] != "")
328                 $profile = $matches[1];
329
330         preg_match('/profile="(.*?)"/ism', $attributes, $matches);
331         if ($matches[1] != "")
332                 $profile = $matches[1];
333
334         $posted = "";
335
336         $itemcache = get_config("system","itemcache");
337
338         // relative dates only make sense when they aren't cached
339         if ($itemcache == "") {
340                 preg_match("/posted='(.*?)'/ism", $attributes, $matches);
341                 if ($matches[1] != "")
342                         $posted = $matches[1];
343
344                 preg_match('/posted="(.*?)"/ism', $attributes, $matches);
345                 if ($matches[1] != "")
346                         $posted = $matches[1];
347
348                 $reldate = (($posted) ? " " . relative_date($posted) : '');
349         }
350
351         $headline = '<div class="shared_header">';
352         //$headline = '<br /><div class="shared_header">';
353
354         if ($avatar != "")
355                 $headline .= '<img src="'.$avatar.'" height="32" width="32" >';
356
357         $headline .= sprintf(t('<span><a href="%s" target="_blank">%s</a> wrote the following <a href="%s" target="_blank">post</a>'.$reldate.':</span>'), $profile, $author, $link);
358
359         $headline .= "</div>";
360
361         $text = $headline.'<blockquote class="shared_content">'.trim($match[2])."</blockquote>";
362
363         return($text);
364 }
365
366 // Escpecially for Diaspora (there mustn't be links in the share information)
367 function bb_ShareAttributesDiaspora($match) {
368
369         $attributes = $match[2];
370
371         $author = "";
372         preg_match("/author='(.*?)'/ism", $attributes, $matches);
373         if ($matches[1] != "")
374                 $author = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
375
376         preg_match('/author="(.*?)"/ism', $attributes, $matches);
377         if ($matches[1] != "")
378                 $author = $matches[1];
379
380         $profile = "";
381         preg_match("/profile='(.*?)'/ism", $attributes, $matches);
382         if ($matches[1] != "")
383                 $profile = $matches[1];
384
385         preg_match('/profile="(.*?)"/ism', $attributes, $matches);
386         if ($matches[1] != "")
387                 $profile = $matches[1];
388
389         $link = "";
390         preg_match("/link='(.*?)'/ism", $attributes, $matches);
391         if ($matches[1] != "")
392                 $link = $matches[1];
393
394         preg_match('/link="(.*?)"/ism', $attributes, $matches);
395         if ($matches[1] != "")
396                 $link = $matches[1];
397
398         $userid = GetProfileUsername($profile,$author);
399
400         $headline = '<div class="shared_header">';
401         $headline .= '<span><b>'.html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').$userid.':</b></span>';
402         $headline .= "</div>";
403
404         $text = trim($match[1]);
405
406         if ($text != "")
407                 $text .= "<hr />";
408
409         $text .= $headline.'<blockquote class="shared_content">'.trim($match[3])."</blockquote><br />";
410
411         if ($link != "")
412                 $text .= '<br /><a href="'.$link.'">[l]</a>';
413
414         //      $text .= '<br /><a href="'.$link.'">'.t("Link").' [l]</a>';
415
416         return($text);
417 }
418
419 // Optimized for Libertree, Wordpress, Tumblr, ...
420 function bb_ShareAttributesForExport($match) {
421
422         $attributes = $match[2];
423
424         $author = "";
425         preg_match("/author='(.*?)'/ism", $attributes, $matches);
426         if ($matches[1] != "")
427                 $author = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
428
429         preg_match('/author="(.*?)"/ism', $attributes, $matches);
430         if ($matches[1] != "")
431                 $author = $matches[1];
432
433         $profile = "";
434         preg_match("/profile='(.*?)'/ism", $attributes, $matches);
435         if ($matches[1] != "")
436                 $profile = $matches[1];
437
438         preg_match('/profile="(.*?)"/ism', $attributes, $matches);
439         if ($matches[1] != "")
440                 $profile = $matches[1];
441
442         $link = "";
443         preg_match("/link='(.*?)'/ism", $attributes, $matches);
444         if ($matches[1] != "")
445                 $link = $matches[1];
446
447         preg_match('/link="(.*?)"/ism', $attributes, $matches);
448         if ($matches[1] != "")
449                 $link = $matches[1];
450
451         if ($link == "")
452                 $link = $profile;
453
454         $userid = GetProfileUsername($profile,$author);
455
456         $headline = '<div class="shared_header">';
457         $headline .= '<span><b>'.html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
458         $headline .= sprintf(t('<a href="%1$s" target="_blank">%2$s</a> %3$s'), $link, $userid, $posted);
459         $headline .= ":</b></span></div>";
460
461         $text = trim($match[1]);
462
463         if ($text != "")
464                 $text .= "<hr />";
465
466         $text .= $headline.'<blockquote class="shared_content">'.trim($match[3])."</blockquote><br />";
467
468         return($text);
469 }
470
471 // Still in use?
472 function bb_ShareAttributesSimple($match) {
473
474         $attributes = $match[1];
475
476         $author = "";
477         preg_match("/author='(.*?)'/ism", $attributes, $matches);
478         if ($matches[1] != "")
479                 $author = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
480
481         preg_match('/author="(.*?)"/ism', $attributes, $matches);
482         if ($matches[1] != "")
483                 $author = $matches[1];
484
485         $profile = "";
486         preg_match("/profile='(.*?)'/ism", $attributes, $matches);
487         if ($matches[1] != "")
488                 $profile = $matches[1];
489
490         preg_match('/profile="(.*?)"/ism', $attributes, $matches);
491         if ($matches[1] != "")
492                 $profile = $matches[1];
493
494         $userid = GetProfileUsername($profile,$author);
495
496         $text = "<br />".html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').' <a href="'.$profile.'">'.$userid."</a>: <br />»".$match[2]."«";
497
498         return($text);
499 }
500
501 // Used for text exports (Twitter, Facebook, Google+)
502 function bb_ShareAttributesSimple2($match) {
503
504         $attributes = $match[1];
505
506         $author = "";
507         preg_match("/author='(.*?)'/ism", $attributes, $matches);
508         if ($matches[1] != "")
509                 $author = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
510
511         preg_match('/author="(.*?)"/ism', $attributes, $matches);
512         if ($matches[1] != "")
513                 $author = $matches[1];
514
515         $profile = "";
516         preg_match("/profile='(.*?)'/ism", $attributes, $matches);
517         if ($matches[1] != "")
518                 $profile = $matches[1];
519
520         preg_match('/profile="(.*?)"/ism', $attributes, $matches);
521         if ($matches[1] != "")
522                 $profile = $matches[1];
523
524         $userid = GetProfileUsername($profile,$author);
525
526         //$text = "<br />".html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').' <a href="'.$profile.'">'.$userid."</a>: <br />".$match[2];
527         $text = "<br />".html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8').' '.$userid.": <br />".$match[2];
528
529         return($text);
530 }
531
532 function GetProfileUsername($profile, $username) {
533
534         $twitter = preg_replace("=https?://twitter.com/(.*)=ism", "$1@twitter.com", $profile);
535         if ($twitter != $profile)
536                 return($username." (".$twitter.")");
537
538         $gplus = preg_replace("=https?://plus.google.com/(.*)=ism", "$1@plus.google.com", $profile);
539         if ($gplus != $profile)
540                 return($username." (".$gplus.")");
541
542         $friendica = preg_replace("=https?://(.*)/profile/(.*)=ism", "$2@$1", $profile);
543         if ($friendica != $profile)
544                 return($username." (".$friendica.")");
545
546         $diaspora = preg_replace("=https?://(.*)/u/(.*)=ism", "$2@$1", $profile);
547         if ($diaspora != $profile)
548                 return($username." (".$diaspora.")");
549
550         $StatusnetHost = preg_replace("=https?://(.*)/user/(.*)=ism", "$1", $profile);
551         if ($StatusnetHost != $profile) {
552                 $StatusnetUser = preg_replace("=https?://(.*)/user/(.*)=ism", "$2", $profile);
553                 if ($StatusnetUser != $profile) {
554                         $UserData = fetch_url("http://".$StatusnetHost."/api/users/show.json?user_id=".$StatusnetUser);
555                         $user = json_decode($UserData);
556                         if ($user)
557                                 return($username." (".$user->screen_name."@".$StatusnetHost.")");
558                 }
559         }
560
561         // pumpio (http://host.name/user)
562         $rest = preg_replace("=https?://([\.\w]+)/([\.\w]+)(.*)=ism", "$3", $profile);
563         if ($rest == "") {
564                 $pumpio = preg_replace("=https?://([\.\w]+)/([\.\w]+)(.*)=ism", "*$2@$1*", $profile);
565                 if ($pumpio != $profile)
566                         return($username." (".$pumpio.")");
567         }
568
569         return($username);
570 }
571
572 function bb_RemovePictureLinks($match) {
573         $text = Cache::get($match[1]);
574
575         if(is_null($text)){
576                 $ch = @curl_init($match[1]);
577                 @curl_setopt($ch, CURLOPT_NOBODY, true);
578                 @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
579                 @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)");
580                 @curl_exec($ch);
581                 $curl_info = @curl_getinfo($ch);
582
583                 if (substr($curl_info["content_type"], 0, 6) == "image/")
584                         $text = "[url=".$match[1]."]".$match[1]."[/url]";
585                 else {
586                         $text = "[url=".$match[2]."]".$match[2]."[/url]";
587
588                         // if its not a picture then look if its a page that contains a picture link
589                         require_once("include/network.php");
590
591                         $body = fetch_url($match[1]);
592
593                         $doc = new DOMDocument();
594                         @$doc->loadHTML($body);
595                         $xpath = new DomXPath($doc);
596                         $list = $xpath->query("//meta[@name]");
597                         foreach ($list as $node) {
598                                 $attr = array();
599
600                                 if ($node->attributes->length)
601                                         foreach ($node->attributes as $attribute)
602                                                 $attr[$attribute->name] = $attribute->value;
603
604                                 if (strtolower($attr["name"]) == "twitter:image")
605                                         $text = "[url=".$attr["content"]."]".$attr["content"]."[/url]";
606                         }
607                 }
608                 Cache::set($match[1],$text);
609         }
610         return($text);
611 }
612
613 function bb_CleanPictureLinksSub($match) {
614         $text = Cache::get($match[1]);
615
616         if(is_null($text)){
617                 $ch = @curl_init($match[1]);
618                 @curl_setopt($ch, CURLOPT_NOBODY, true);
619                 @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
620                 @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)");
621                 @curl_exec($ch);
622                 $curl_info = @curl_getinfo($ch);
623
624                 // if its a link to a picture then embed this picture
625                 if (substr($curl_info["content_type"], 0, 6) == "image/")
626                         $text = "[img]".$match[1]."[/img]";
627                 else {
628                         $text = "[img]".$match[2]."[/img]";
629
630                         // if its not a picture then look if its a page that contains a picture link
631                         require_once("include/network.php");
632
633                         $body = fetch_url($match[1]);
634
635                         $doc = new DOMDocument();
636                         @$doc->loadHTML($body);
637                         $xpath = new DomXPath($doc);
638                         $list = $xpath->query("//meta[@name]");
639                         foreach ($list as $node) {
640                                 $attr = array();
641
642                                 if ($node->attributes->length)
643                                         foreach ($node->attributes as $attribute)
644                                                 $attr[$attribute->name] = $attribute->value;
645
646                                 if (strtolower($attr["name"]) == "twitter:image")
647                                         $text = "[img]".$attr["content"]."[/img]";
648                         }
649                 }
650                 Cache::set($match[1],$text);
651         }
652         return($text);
653 }
654
655 function bb_CleanPictureLinks($text) {
656         $text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'bb_CleanPictureLinksSub', $text);
657         return ($text);
658 }
659
660         // BBcode 2 HTML was written by WAY2WEB.net
661         // extended to work with Mistpark/Friendica - Mike Macgirvin
662
663 function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = false, $forplaintext = false) {
664
665         $stamp1 = microtime(true);
666
667         $a = get_app();
668
669         // Hide all [noparse] contained bbtags by spacefying them
670         // POSSIBLE BUG --> Will the 'preg' functions crash if there's an embedded image?
671
672         $Text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'bb_spacefy',$Text);
673         $Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_spacefy',$Text);
674         $Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_spacefy',$Text);
675
676
677         // Move all spaces out of the tags
678         $Text = preg_replace("/\[(\w*)\](\s*)/ism", '$2[$1]', $Text);
679         $Text = preg_replace("/(\s*)\[\/(\w*)\]/ism", '[/$2]$1', $Text);
680
681         // Extract the private images which use data urls since preg has issues with
682         // large data sizes. Stash them away while we do bbcode conversion, and then put them back
683         // in after we've done all the regex matching. We cannot use any preg functions to do this.
684
685         $extracted = bb_extract_images($Text);
686         $Text = $extracted['body'];
687         $saved_image = $extracted['images'];
688
689         // If we find any event code, turn it into an event.
690         // After we're finished processing the bbcode we'll
691         // replace all of the event code with a reformatted version.
692
693         $ev = bbtoevent($Text);
694
695
696         // Replace any html brackets with HTML Entities to prevent executing HTML or script
697         // Don't use strip_tags here because it breaks [url] search by replacing & with amp
698
699         $Text = str_replace("<", "&lt;", $Text);
700         $Text = str_replace(">", "&gt;", $Text);
701
702         // remove some newlines before the general conversion
703         $Text = preg_replace("/\s?\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","[share$1]$2[/share]",$Text);
704         $Text = preg_replace("/\s?\[quote(.*?)\]\s?(.*?)\s?\[\/quote\]\s?/ism","[quote$1]$2[/quote]",$Text);
705
706         $Text = preg_replace("/\n\[code\]/ism", "[code]", $Text);
707         $Text = preg_replace("/\[\/code\]\n/ism", "[/code]", $Text);
708
709         // Rearrange shared links
710         if (get_config("system", "rearrange_shared_links") AND (!$simplehtml OR $tryoembed))
711                 $Text = preg_replace_callback("(\[class=(.*?)\](.*?)\[\/class\])ism","bb_rearrange_link",$Text);
712
713         // when the content is meant exporting to other systems then remove the avatar picture since this doesn't really look good on these systems
714         if (!$tryoembed)
715                 $Text = preg_replace("/\[share(.*?)avatar\s?=\s?'.*?'\s?(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","\n[share$1$2]$3[/share]",$Text);
716
717         // Convert new line chars to html <br /> tags
718
719         // nlbr seems to be hopelessly messed up
720         //      $Text = nl2br($Text);
721
722         // We'll emulate it.
723
724         $Text = trim($Text);
725         $Text = str_replace("\r\n","\n", $Text);
726
727         // removing multiplicated newlines
728         if (get_config("system", "remove_multiplicated_lines")) {
729                 $search = array("\n\n\n", "\n ", " \n", "[/quote]\n\n", "\n[/quote]", "[/li]\n", "\n[li]", "\n[ul]", "[/ul]\n");
730                 $replace = array("\n\n", "\n", "\n", "[/quote]\n", "[/quote]", "[/li]", "[li]", "[ul]", "[/ul]");
731                 do {
732                         $oldtext = $Text;
733                         $Text = str_replace($search, $replace, $Text);
734                 } while ($oldtext != $Text);
735         }
736
737         $Text = str_replace(array("\r","\n"), array('<br />','<br />'), $Text);
738
739         if($preserve_nl)
740                 $Text = str_replace(array("\n","\r"), array('',''),$Text);
741
742
743
744         // Set up the parameters for a URL search string
745         $URLSearchString = "^\[\]";
746         // Set up the parameters for a MAIL search string
747         $MAILSearchString = $URLSearchString;
748
749         // Remove all hashtag addresses
750         if (!$tryoembed OR $simplehtml)
751                 $Text = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $Text);
752
753         // Bookmarks in red - will be converted to bookmarks in friendica
754         $Text = preg_replace("/#\^\[url\]([$URLSearchString]*)\[\/url\]/ism", '[bookmark=$1]$1[/bookmark]', $Text);
755         $Text = preg_replace("/#\^\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '[bookmark=$1]$2[/bookmark]', $Text);
756         $Text = preg_replace("/#\[url\=[$URLSearchString]*\]\^\[\/url\]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/i",
757                                 "[bookmark=$1]$2[/bookmark]", $Text);
758
759         if ($simplehtml == 2) {
760                 $Text = preg_replace("/[^#@]\[url\=([^\]]*)\](.*?)\[\/url\]/ism",' $2 [url]$1[/url]',$Text);
761                 $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",' $2 [url]$1[/url]',$Text);
762         }
763
764         if ($simplehtml == 5)
765                 $Text = preg_replace("/[^#@]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '[url]$1[/url]', $Text);
766
767         // Perform URL Search
768         if ($tryoembed)
769                 $Text = preg_replace_callback("/\[bookmark\=([^\]]*)\].*?\[\/bookmark\]/ism",'tryoembed',$Text);
770
771         if ($simplehtml == 5)
772                 $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url]$1[/url]',$Text);
773         else
774                 $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url=$1]$2[/url]',$Text);
775
776         // if the HTML is used to generate plain text, then don't do this search, but replace all URL of that kind to text
777         if (!$forplaintext)
778                 $Text = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1<a href="$2" target="_blank">$2</a>', $Text);
779         else {
780                 $Text = preg_replace("(\[url\]([$URLSearchString]*)\[\/url\])ism"," $1 ",$Text);
781                 $Text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'bb_RemovePictureLinks', $Text);
782         }
783
784         if ($tryoembed)
785                 $Text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$Text);
786
787         $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$1</a>', $Text);
788         $Text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
789         //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
790
791         // Red compatibility, though the link can't be authenticated on Friendica
792         $Text = preg_replace("/\[zrl\=([$URLSearchString]*)\](.*?)\[\/zrl\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
793
794
795         // we may need to restrict this further if it picks up too many strays
796         // link acct:user@host to a webfinger profile redirector
797
798         $Text = preg_replace('/acct:(.*?)@(.*?)([ ,])/', '<a href="' . $a->get_baseurl() . '/acctlink?addr=' . "$1@$2" 
799                 . '" target="extlink" >acct:' . "$1@$2$3" . '</a>',$Text);
800
801         // Perform MAIL Search
802         $Text = preg_replace("/\[mail\]([$MAILSearchString]*)\[\/mail\]/", '<a href="mailto:$1">$1</a>', $Text);
803         $Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.*?)\[\/mail\]/", '<a href="mailto:$1">$2</a>', $Text);
804
805         // Check for bold text
806         $Text = preg_replace("(\[b\](.*?)\[\/b\])ism",'<strong>$1</strong>',$Text);
807
808         // Check for Italics text
809         $Text = preg_replace("(\[i\](.*?)\[\/i\])ism",'<em>$1</em>',$Text);
810
811         // Check for Underline text
812         $Text = preg_replace("(\[u\](.*?)\[\/u\])ism",'<u>$1</u>',$Text);
813
814         // Check for strike-through text
815         $Text = preg_replace("(\[s\](.*?)\[\/s\])ism",'<strike>$1</strike>',$Text);
816
817         // Check for over-line text
818         $Text = preg_replace("(\[o\](.*?)\[\/o\])ism",'<span class="overline">$1</span>',$Text);
819
820         // Check for colored text
821         $Text = preg_replace("(\[color=(.*?)\](.*?)\[\/color\])ism","<span style=\"color: $1;\">$2</span>",$Text);
822
823         // Check for sized text
824         // [size=50] --> font-size: 50px (with the unit).
825         $Text = preg_replace("(\[size=(\d*?)\](.*?)\[\/size\])ism","<span style=\"font-size: $1px;\">$2</span>",$Text);
826         $Text = preg_replace("(\[size=(.*?)\](.*?)\[\/size\])ism","<span style=\"font-size: $1;\">$2</span>",$Text);
827
828         // Check for centered text
829         $Text = preg_replace("(\[center\](.*?)\[\/center\])ism","<div style=\"text-align:center;\">$1</div>",$Text);
830
831         // Check for list text
832         $Text = str_replace("[*]", "<li>", $Text);
833
834         // Check for style sheet commands
835         $Text = preg_replace_callback("(\[style=(.*?)\](.*?)\[\/style\])ism","bb_cleanstyle",$Text);
836
837         // Check for CSS classes
838         $Text = preg_replace_callback("(\[class=(.*?)\](.*?)\[\/class\])ism","bb_cleanclass",$Text);
839
840         // handle nested lists
841         $endlessloop = 0;
842
843         while ((((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false)) ||
844                ((strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false)) || 
845                ((strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false)) || 
846                ((strpos($Text, "[/li]") !== false) && (strpos($Text, "[li]") !== false))) && (++$endlessloop < 20)) {
847                 $Text = preg_replace("/\[list\](.*?)\[\/list\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
848                 $Text = preg_replace("/\[list=\](.*?)\[\/list\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>' ,$Text);
849                 $Text = preg_replace("/\[list=1\](.*?)\[\/list\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
850                 $Text = preg_replace("/\[list=((?-i)i)\](.*?)\[\/list\]/ism",'<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>' ,$Text);
851                 $Text = preg_replace("/\[list=((?-i)I)\](.*?)\[\/list\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>' ,$Text);
852                 $Text = preg_replace("/\[list=((?-i)a)\](.*?)\[\/list\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>' ,$Text);
853                 $Text = preg_replace("/\[list=((?-i)A)\](.*?)\[\/list\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>' ,$Text);
854                 $Text = preg_replace("/\[ul\](.*?)\[\/ul\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
855                 $Text = preg_replace("/\[ol\](.*?)\[\/ol\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
856                 $Text = preg_replace("/\[li\](.*?)\[\/li\]/ism", '<li>$1</li>' ,$Text);
857         }
858
859         $Text = preg_replace("/\[th\](.*?)\[\/th\]/sm", '<th>$1</th>' ,$Text);
860         $Text = preg_replace("/\[td\](.*?)\[\/td\]/sm", '<td>$1</td>' ,$Text);
861         $Text = preg_replace("/\[tr\](.*?)\[\/tr\]/sm", '<tr>$1</tr>' ,$Text);
862         $Text = preg_replace("/\[table\](.*?)\[\/table\]/sm", '<table>$1</table>' ,$Text);
863
864         $Text = preg_replace("/\[table border=1\](.*?)\[\/table\]/sm", '<table border="1" >$1</table>' ,$Text);
865         $Text = preg_replace("/\[table border=0\](.*?)\[\/table\]/sm", '<table border="0" >$1</table>' ,$Text);
866
867         $Text = str_replace('[hr]','<hr />', $Text);
868
869         // This is actually executed in prepare_body()
870
871         $Text = str_replace('[nosmile]','',$Text);
872
873         // Check for font change text
874         $Text = preg_replace("/\[font=(.*?)\](.*?)\[\/font\]/sm","<span style=\"font-family: $1;\">$2</span>",$Text);
875
876         // Declare the format for [code] layout
877
878 //      $Text = preg_replace_callback("/\[code\](.*?)\[\/code\]/ism",'stripcode_br_cb',$Text);
879
880         $CodeLayout = '<code>$1</code>';
881         // Check for [code] text
882         $Text = preg_replace("/\[code\](.*?)\[\/code\]/ism","$CodeLayout", $Text);
883
884         // Declare the format for [spoiler] layout
885         $SpoilerLayout = '<blockquote class="spoiler">$1</blockquote>';
886
887         // Check for [spoiler] text
888         // handle nested quotes
889         $endlessloop = 0;
890         while ((strpos($Text, "[/spoiler]") !== false) and (strpos($Text, "[spoiler]") !== false) and (++$endlessloop < 20))
891                 $Text = preg_replace("/\[spoiler\](.*?)\[\/spoiler\]/ism","$SpoilerLayout", $Text);
892
893         // Check for [spoiler=Author] text
894
895         $t_wrote = t('$1 wrote:');
896
897         // handle nested quotes
898         $endlessloop = 0;
899         while ((strpos($Text, "[/spoiler]")!== false)  and (strpos($Text, "[spoiler=") !== false) and (++$endlessloop < 20))
900                 $Text = preg_replace("/\[spoiler=[\"\']*(.*?)[\"\']*\](.*?)\[\/spoiler\]/ism",
901                                      "<br /><strong class=".'"spoiler"'.">" . $t_wrote . "</strong><blockquote class=".'"spoiler"'.">$2</blockquote>",
902                                      $Text);
903
904         // Declare the format for [quote] layout
905         $QuoteLayout = '<blockquote>$1</blockquote>';
906
907         // Check for [quote] text
908         // handle nested quotes
909         $endlessloop = 0;
910         while ((strpos($Text, "[/quote]") !== false) and (strpos($Text, "[quote]") !== false) and (++$endlessloop < 20))
911                 $Text = preg_replace("/\[quote\](.*?)\[\/quote\]/ism","$QuoteLayout", $Text);
912
913         // Check for [quote=Author] text
914
915         $t_wrote = t('$1 wrote:');
916
917         // handle nested quotes
918         $endlessloop = 0;
919         while ((strpos($Text, "[/quote]")!== false)  and (strpos($Text, "[quote=") !== false) and (++$endlessloop < 20))
920                 $Text = preg_replace("/\[quote=[\"\']*(.*?)[\"\']*\](.*?)\[\/quote\]/ism",
921                                      "<br /><strong class=".'"author"'.">" . $t_wrote . "</strong><blockquote>$2</blockquote>",
922                                      $Text);
923
924         // [img=widthxheight]image source[/img]
925         //$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="height: $2px; width: $1px;" >', $Text);
926         $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="width: $1px;" >', $Text);
927         $Text = preg_replace("/\[zmg\=([0-9]*)x([0-9]*)\](.*?)\[\/zmg\]/ism", '<img class="zrl" src="$3" style="width: $1px;" >', $Text);
928
929         // Images
930         // [img]pathtoimage[/img]
931         $Text = preg_replace("/\[img\](.*?)\[\/img\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
932         $Text = preg_replace("/\[zmg\](.*?)\[\/zmg\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
933
934         // Shared content
935         if (!$simplehtml)
936                 $Text = preg_replace_callback("/\[share(.*?)\](.*?)\[\/share\]/ism","bb_ShareAttributes",$Text);
937         elseif ($simplehtml == 1)
938                 $Text = preg_replace_callback("/\[share(.*?)\](.*?)\[\/share\]/ism","bb_ShareAttributesSimple",$Text);
939         elseif (($simplehtml == 2) OR ($simplehtml == 5))
940                 $Text = preg_replace_callback("/\[share(.*?)\](.*?)\[\/share\]/ism","bb_ShareAttributesSimple2",$Text);
941         elseif ($simplehtml == 3)
942                 $Text = preg_replace_callback("/(.*?)\[share(.*?)\](.*?)\[\/share\]/ism","bb_ShareAttributesDiaspora",$Text);
943         elseif ($simplehtml == 4)
944                 $Text = preg_replace_callback("/(.*?)\[share(.*?)\](.*?)\[\/share\]/ism","bb_ShareAttributesForExport",$Text);
945
946         $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);
947         $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);
948         //$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);
949
950
951         // Try to Oembed
952         if ($tryoembed) {
953                 $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);
954                 $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);
955
956                 $Text = preg_replace_callback("/\[video\](.*?)\[\/video\]/ism", 'tryoembed', $Text);
957                 $Text = preg_replace_callback("/\[audio\](.*?)\[\/audio\]/ism", 'tryoembed', $Text);
958         } else {
959                 $Text = preg_replace("/\[video\](.*?)\[\/video\]/",
960                                         '<a href="$1" target="_blank">$1</a>', $Text);
961                 $Text = preg_replace("/\[audio\](.*?)\[\/audio\]/",
962                                         '<a href="$1" target="_blank">$1</a>', $Text);
963         }
964
965         // html5 video and audio
966
967
968         if ($tryoembed)
969                 $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<iframe src="$1" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="$1">$1</a></iframe>', $Text);
970         else
971                 $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<a href="$1">$1</a>', $Text);
972
973         // Youtube extensions
974         if ($tryoembed) {
975                 $Text = preg_replace_callback("/\[youtube\](https?:\/\/www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);
976                 $Text = preg_replace_callback("/\[youtube\](www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);
977                 $Text = preg_replace_callback("/\[youtube\](https?:\/\/youtu.be\/.*?)\[\/youtube\]/ism",'tryoembed',$Text);
978         }
979
980         $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
981         $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
982         $Text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
983
984         if ($tryoembed)
985                 $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="https://www.youtube.com/embed/$1" frameborder="0" ></iframe>', $Text);
986         else
987                 $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism",
988                                         '<a href="https://www.youtube.com/watch?v=$1" target="_blank">https://www.youtube.com/watch?v=$1</a>', $Text);
989
990         if ($tryoembed) {
991                 $Text = preg_replace_callback("/\[vimeo\](https?:\/\/player.vimeo.com\/video\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text); 
992                 $Text = preg_replace_callback("/\[vimeo\](https?:\/\/vimeo.com\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text); 
993         }
994
995         $Text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text); 
996         $Text = preg_replace("/\[vimeo\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text);
997
998         if ($tryoembed)
999                 $Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="https://player.vimeo.com/video/$1" frameborder="0" ></iframe>', $Text);
1000         else
1001                 $Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism",
1002                                         '<a href="https://vimeo.com/$1" target="_blank">https://vimeo.com/$1</a>', $Text);
1003
1004 //      $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);
1005
1006
1007         // oembed tag
1008         $Text = oembed_bbcode2html($Text);
1009
1010         // Avoid triple linefeeds through oembed
1011         $Text = str_replace("<br style='clear:left'></span><br /><br />", "<br style='clear:left'></span><br />", $Text);
1012
1013         // If we found an event earlier, strip out all the event code and replace with a reformatted version.
1014         // Replace the event-start section with the entire formatted event. The other bbcode is stripped.
1015         // Summary (e.g. title) is required, earlier revisions only required description (in addition to 
1016         // start which is always required). Allow desc with a missing summary for compatibility.
1017
1018         if((x($ev,'desc') || x($ev,'summary')) && x($ev,'start')) {
1019                 $sub = format_event_html($ev);
1020
1021                 $Text = preg_replace("/\[event\-summary\](.*?)\[\/event\-summary\]/ism",'',$Text);
1022                 $Text = preg_replace("/\[event\-description\](.*?)\[\/event\-description\]/ism",'',$Text);
1023                 $Text = preg_replace("/\[event\-start\](.*?)\[\/event\-start\]/ism",$sub,$Text); 
1024                 $Text = preg_replace("/\[event\-finish\](.*?)\[\/event\-finish\]/ism",'',$Text);
1025                 $Text = preg_replace("/\[event\-location\](.*?)\[\/event\-location\]/ism",'',$Text);
1026                 $Text = preg_replace("/\[event\-adjust\](.*?)\[\/event\-adjust\]/ism",'',$Text);
1027         }
1028
1029         // Unhide all [noparse] contained bbtags unspacefying them 
1030         // and triming the [noparse] tag.
1031
1032         $Text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'bb_unspacefy_and_trim',$Text);
1033         $Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_unspacefy_and_trim',$Text);
1034         $Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_unspacefy_and_trim',$Text);
1035
1036
1037         $Text = preg_replace('/\[\&amp\;([#a-z0-9]+)\;\]/','&$1;',$Text);
1038         $Text = preg_replace('/\&\#039\;/','\'',$Text);
1039         $Text = preg_replace('/\&quot\;/','"',$Text);
1040
1041         // fix any escaped ampersands that may have been converted into links
1042         $Text = preg_replace("/\<([^>]*?)(src|href)=(.*?)\&amp\;(.*?)\>/ism",'<$1$2=$3&$4>',$Text);
1043         $Text = preg_replace("/\<([^>]*?)(src|href)=\"(?!http|ftp|mailto|cid)(.*?)\>/ism",'<$1$2="">',$Text);
1044
1045         if($saved_image)
1046                 $Text = bb_replace_images($Text, $saved_image);
1047
1048         // Clean up the HTML by loading and saving the HTML with the DOM
1049         // Only do it when it has to be done - for performance reasons
1050         // Update: Now it is done every time - since bad structured html can break a whole page
1051         //if (!$tryoembed) {
1052         //      $doc = new DOMDocument();
1053         //      $doc->preserveWhiteSpace = false;
1054
1055         //      $Text = mb_convert_encoding($Text, 'HTML-ENTITIES', "UTF-8");
1056
1057         //      $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
1058         //      @$doc->loadHTML($doctype."<html><body>".$Text."</body></html>");
1059
1060         //      $Text = $doc->saveHTML();
1061         //      $Text = str_replace(array("<html><body>", "</body></html>", $doctype), array("", "", ""), $Text);
1062
1063         //      $Text = str_replace('<br></li>','</li>', $Text);
1064
1065         //      $Text = mb_convert_encoding($Text, "UTF-8", 'HTML-ENTITIES');
1066         //}
1067
1068         // Clean up some useless linebreaks in lists
1069         //$Text = str_replace('<br /><ul','<ul ', $Text);
1070         //$Text = str_replace('</ul><br />','</ul>', $Text);
1071         //$Text = str_replace('</li><br />','</li>', $Text);
1072         //$Text = str_replace('<br /><li>','<li>', $Text);
1073         //      $Text = str_replace('<br /><ul','<ul ', $Text);
1074
1075         // Remove all hashtag addresses
1076 /*      if (!$tryoembed AND get_config("system", "remove_hashtags_on_export")) {
1077                 $pattern = '/#<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
1078                 $Text = preg_replace($pattern, '#$2', $Text);
1079         }
1080 */
1081         call_hooks('bbcode',$Text);
1082
1083         $a->save_timestamp($stamp1, "parser");
1084
1085         return $Text;
1086 }
1087 ?>