2 require_once("include/oembed.php");
3 require_once('include/event.php');
5 function bb_attachment($Text, $plaintext = false, $tryoembed = true) {
6 $Text = preg_replace_callback("/\[attachment(.*?)\](.*?)\[\/attachment\]/ism",
7 function ($match) use ($plaintext){
9 $attributes = $match[1];
12 preg_match("/type='(.*?)'/ism", $attributes, $matches);
13 if ($matches[1] != "")
16 preg_match('/type="(.*?)"/ism', $attributes, $matches);
17 if ($matches[1] != "")
23 if (!in_array($type, array("link", "audio", "video")))
27 preg_match("/url='(.*?)'/ism", $attributes, $matches);
28 if ($matches[1] != "")
31 preg_match('/url="(.*?)"/ism', $attributes, $matches);
32 if ($matches[1] != "")
36 preg_match("/title='(.*?)'/ism", $attributes, $matches);
37 if ($matches[1] != "")
40 preg_match('/title="(.*?)"/ism', $attributes, $matches);
41 if ($matches[1] != "")
45 preg_match("/image='(.*?)'/ism", $attributes, $matches);
46 if ($matches[1] != "")
49 preg_match('/image="(.*?)"/ism', $attributes, $matches);
50 if ($matches[1] != "")
54 preg_match("/preview='(.*?)'/ism", $attributes, $matches);
55 if ($matches[1] != "")
56 $preview = $matches[1];
58 preg_match('/preview="(.*?)"/ism', $attributes, $matches);
59 if ($matches[1] != "")
60 $preview = $matches[1];
63 $text = sprintf('<a href="%s" target="_blank">%s</a>', $url, $title);
65 $text = sprintf('<span class="type-%s">', $type);
67 $bookmark = array(sprintf('[bookmark=%s]%s[/bookmark]', $url, $title), $title, $url);
69 $oembed = tryoembed($bookmark);
71 $oembed = $bookmark[0];
73 if (($image != "") AND !strstr(strtolower($oembed), "<img "))
74 $text .= sprintf('<img src="%s" alt="%s" class="attachment-image" />', $image, $title); // To-Do: Anführungszeichen in "alt"
75 elseif (($preview != "") AND !strstr(strtolower($oembed), "<img "))
76 $text .= sprintf('<img src="%s" alt="%s" class="attachment-preview" />', $preview, $title); // To-Do: Anführungszeichen in "alt"
80 $text .= sprintf('<blockquote>%s</blockquote></span>', trim($match[2]));
89 function bb_rearrange_link($shared) {
90 if ($shared[1] != "type-link")
93 $newshare = trim($shared[2]);
94 $newshare = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $newshare);
96 if (!strpos($shared[0], "[bookmark"))
97 $newshare = preg_replace("/\[url\=(.*?)\](.*?)\[\/url\]/ism", '[bookmark=$1]$2[/bookmark]', $newshare, 1);
99 preg_match("/\[img\](.*?)\[\/img\]/ism", $newshare, $matches);
102 $newshare = str_replace($matches[0], '', $newshare);
103 $newshare = "[img]".$matches[1]."[/img]\n".$newshare;
106 $search = array("\n\n", "\n ", " \n");
107 $replace = array("\n", "\n", "\n");
109 $oldtext = $newshare;
110 $newshare = str_replace($search, $replace, $newshare);
111 } while ($oldtext != $newshare);
113 $newshare = "[class=type-link]".$newshare."[/class]";
118 function bb_remove_share_information($Text, $plaintext = false, $nolink = false) {
119 $Text = preg_replace_callback("((.*?)\[class=(.*?)\](.*?)\[\/class\])ism",
120 function ($match) use ($plaintext, $nolink){
121 return(bb_cleanup_share($match, $plaintext, $nolink));
126 function bb_cleanup_share($shared, $plaintext, $nolink) {
127 if (!in_array($shared[2], array("type-link", "type-video")))
131 $shared[3] = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism","[bookmark=$1]$1[/bookmark]", $shared[3]);
133 if (!preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",$shared[3], $bookmark))
137 return(trim($shared[1]));
142 if (isset($bookmark[2][0]))
143 $title = $bookmark[2][0];
145 if (isset($bookmark[1][0]))
146 $link = $bookmark[1][0];
148 if (strpos($shared[1],$title) !== false)
151 // if (strpos($shared[1],$link) !== false)
154 $text = trim($shared[1]);
156 if (($text == "") AND ($title != "") AND ($link == ""))
157 $text .= "\n\n".trim($title);
159 if (($link != "") AND ($title != ""))
160 $text .= "\n[url=".trim($link)."]".trim($title)."[/url]";
161 elseif (($link != ""))
162 $text .= "\n".trim($link);
168 function bb_cleanstyle($st) {
169 return "<span style=\"".cleancss($st[1]).";\">".$st[2]."</span>";
172 function bb_cleanclass($st) {
173 return "<span class=\"".cleancss($st[1])."\">".$st[2]."</span>";
176 function cleancss($input) {
180 $input = strtolower($input);
182 for ($i = 0; $i < strlen($input); $i++) {
183 $char = substr($input, $i, 1);
185 if (($char >= "a") and ($char <= "z"))
188 if (!(strpos(" #;:0123456789-_", $char) === false))
195 function stripcode_br_cb($s) {
196 return '[code]' . str_replace('<br />', '', $s[1]) . '[/code]';
199 function tryoembed($match){
200 $url = ((count($match)==2)?$match[1]:$match[2]);
202 // Always embed the SSL version
203 $url = str_replace(array("http://www.youtube.com/", "http://player.vimeo.com/"),
204 array("https://www.youtube.com/", "https://player.vimeo.com/"), $url);
206 //logger("tryoembed: $url");
208 $o = oembed_fetch_url($url);
210 //echo "<pre>"; var_dump($match, $url, $o); killme();
212 if ($o->type=="error") return $match[0];
214 $html = oembed_format_object($o);
215 return $html; //oembed_iframe($html,$o->width,$o->height);
219 // [noparse][i]italic[/i][/noparse] turns into
220 // [noparse][ i ]italic[ /i ][/noparse],
221 // to hide them from parser.
223 function bb_spacefy($st) {
224 $whole_match = $st[0];
226 $spacefied = preg_replace("/\[(.*?)\]/", "[ $1 ]", $captured);
227 $new_str = str_replace($captured, $spacefied, $whole_match);
231 // The previously spacefied [noparse][ i ]italic[ /i ][/noparse],
232 // now turns back and the [noparse] tags are trimed
233 // returning [i]italic[/i]
235 function bb_unspacefy_and_trim($st) {
236 $whole_match = $st[0];
238 $unspacefied = preg_replace("/\[ (.*?)\ ]/", "[$1]", $captured);
242 function bb_find_open_close($s, $open, $close, $occurance = 1) {
248 for($i = 1; $i <= $occurance; $i++) {
249 if( $start_pos !== false)
250 $start_pos = strpos($s, $open, $start_pos + 1);
253 if( $start_pos === false)
256 $end_pos = strpos($s, $close, $start_pos);
258 if( $end_pos === false)
261 $res = array( 'start' => $start_pos, 'end' => $end_pos );
266 function get_bb_tag_pos($s, $name, $occurance = 1) {
272 for($i = 1; $i <= $occurance; $i++) {
273 if( $start_open !== false)
274 $start_open = strpos($s, '[' . $name, $start_open + 1); // allow [name= type tags
277 if( $start_open === false)
280 $start_equal = strpos($s, '=', $start_open);
281 $start_close = strpos($s, ']', $start_open);
283 if( $start_close === false)
288 $end_open = strpos($s, '[/' . $name . ']', $start_close);
290 if( $end_open === false)
293 $res = array( 'start' => array('open' => $start_open, 'close' => $start_close),
294 'end' => array('open' => $end_open, 'close' => $end_open + strlen('[/' . $name . ']')) );
295 if( $start_equal !== false)
296 $res['start']['equal'] = $start_equal + 1;
301 function bb_tag_preg_replace($pattern, $replace, $name, $s) {
306 $pos = get_bb_tag_pos($string, $name, $occurance);
307 while($pos !== false && $occurance < 1000) {
309 $start = substr($string, 0, $pos['start']['open']);
310 $subject = substr($string, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']);
311 $end = substr($string, $pos['end']['close']);
315 $subject = preg_replace($pattern, $replace, $subject);
316 $string = $start . $subject . $end;
319 $pos = get_bb_tag_pos($string, $name, $occurance);
325 if(! function_exists('bb_extract_images')) {
326 function bb_extract_images($body) {
328 $saved_image = array();
333 $img_start = strpos($orig_body, '[img');
334 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
335 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
336 while(($img_st_close !== false) && ($img_end !== false)) {
338 $img_st_close++; // make it point to AFTER the closing bracket
339 $img_end += $img_start;
341 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
342 // This is an embedded image
344 $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
345 $new_body = $new_body . substr($orig_body, 0, $img_start) . '[$#saved_image' . $cnt . '#$]';
350 $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
352 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
354 if($orig_body === false) // in case the body ends on a closing image tag
357 $img_start = strpos($orig_body, '[img');
358 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
359 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
362 $new_body = $new_body . $orig_body;
364 return array('body' => $new_body, 'images' => $saved_image);
367 if(! function_exists('bb_replace_images')) {
368 function bb_replace_images($body, $images) {
373 foreach($images as $image) {
374 // We're depending on the property of 'foreach' (specified on the PHP website) that
375 // it loops over the array starting from the first element and going sequentially
376 // to the last element
377 $newbody = str_replace('[$#saved_image' . $cnt . '#$]', '<img src="' . $image .'" alt="' . t('Image/photo') . '" />', $newbody);
384 function bb_ShareAttributes($share, $simplehtml) {
385 $attributes = $share[2];
388 preg_match("/author='(.*?)'/ism", $attributes, $matches);
389 if ($matches[1] != "")
390 $author = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
392 preg_match('/author="(.*?)"/ism', $attributes, $matches);
393 if ($matches[1] != "")
394 $author = $matches[1];
397 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
398 if ($matches[1] != "")
399 $profile = $matches[1];
401 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
402 if ($matches[1] != "")
403 $profile = $matches[1];
406 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
407 if ($matches[1] != "")
408 $avatar = $matches[1];
410 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
411 if ($matches[1] != "")
412 $avatar = $matches[1];
415 preg_match("/link='(.*?)'/ism", $attributes, $matches);
416 if ($matches[1] != "")
419 preg_match('/link="(.*?)"/ism', $attributes, $matches);
420 if ($matches[1] != "")
425 $itemcache = get_itemcachepath();
427 // relative dates only make sense when they aren't cached
428 if ($itemcache == "") {
429 preg_match("/posted='(.*?)'/ism", $attributes, $matches);
430 if ($matches[1] != "")
431 $posted = $matches[1];
433 preg_match('/posted="(.*?)"/ism', $attributes, $matches);
434 if ($matches[1] != "")
435 $posted = $matches[1];
437 $reldate = (($posted) ? " " . relative_date($posted) : '');
440 $userid = GetProfileUsername($profile,$author, false);
441 $userid_compact = GetProfileUsername($profile,$author, true);
443 $preshare = trim($share[1]);
446 $preshare .= "<br /><br />";
448 switch ($simplehtml) {
450 $text = $preshare.html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8').' <a href="'.$profile.'">'.$userid."</a>: <br />»".$share[3]."«";
453 $text = $preshare.html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8').' '.$userid_compact.": <br />".$share[3];
456 $headline = '<div class="shared_header">';
457 $headline .= '<span><b>'.html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8').$userid.':</b></span>';
458 $headline .= "</div>";
460 $text = trim($share[1]);
465 $text .= $headline.'<blockquote class="shared_content">'.trim($share[3])."</blockquote><br />";
468 $text .= '<br /><a href="'.$link.'">[l]</a>';
471 $headline = '<div class="shared_header">';
472 $headline .= '<span><b>'.html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8');
473 $headline .= sprintf(t('<a href="%1$s" target="_blank">%2$s</a> %3$s'), $link, $userid, $posted);
474 $headline .= ":</b></span></div>";
476 $text = trim($share[1]);
481 $text .= $headline.'<blockquote class="shared_content">'.trim($share[3])."</blockquote><br />";
485 $text = $preshare.html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8').' '.$userid_compact.": <br />".$share[3];
488 $text = $preshare.">> @".$userid_compact.": <br />".$share[3];
490 case 7: // statusnet/GNU Social
491 $text = $preshare.html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8')." @".$userid_compact.": ".$share[3];
494 $text = $preshare."RT @".$userid_compact.": ".$share[3];
496 case 9: // Google+/Facebook
497 $text = $preshare.html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8').' '.$userid_compact.": <br />".$share[3];
500 $text .= "<br /><br />".$link;
503 $headline = trim($share[1]).'<div class="shared_header">';
505 $headline .= '<img src="'.$avatar.'" height="32" width="32" >';
507 $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);
508 $headline .= "</div>";
509 $text = $headline.'<blockquote class="shared_content">'.trim($share[3])."</blockquote>";
515 function GetProfileUsername($profile, $username, $compact = false) {
517 $twitter = preg_replace("=https?://twitter.com/(.*)=ism", "$1@twitter.com", $profile);
518 if ($twitter != $profile) {
522 return($username." (".$twitter.")");
525 $appnet = preg_replace("=https?://alpha.app.net/(.*)=ism", "$1@alpha.app.net", $profile);
526 if ($appnet != $profile) {
530 return($username." (".$appnet.")");
533 $gplus = preg_replace("=https?://plus.google.com/(.*)=ism", "$1@plus.google.com", $profile);
534 if ($gplus != $profile) {
536 return($gplususername." (".$username.")");
538 return($username." (".$gplus.")");
541 $friendica = preg_replace("=https?://(.*)/profile/(.*)=ism", "$2@$1", $profile);
542 if ($friendica != $profile) {
546 return($username." (".$friendica.")");
549 $diaspora = preg_replace("=https?://(.*)/u/(.*)=ism", "$2@$1", $profile);
550 if ($diaspora != $profile) {
554 return($username." (".$diaspora.")");
557 $StatusnetHost = preg_replace("=https?://(.*)/user/(.*)=ism", "$1", $profile);
558 if ($StatusnetHost != $profile) {
559 $StatusnetUser = preg_replace("=https?://(.*)/user/(.*)=ism", "$2", $profile);
560 if ($StatusnetUser != $profile) {
561 $UserData = fetch_url("http://".$StatusnetHost."/api/users/show.json?user_id=".$StatusnetUser);
562 $user = json_decode($UserData);
565 return($user->screen_name."@".$StatusnetHost);
567 return($username." (".$user->screen_name."@".$StatusnetHost.")");
572 // pumpio (http://host.name/user)
573 $rest = preg_replace("=https?://([\.\w]+)/([\.\w]+)(.*)=ism", "$3", $profile);
575 $pumpio = preg_replace("=https?://([\.\w]+)/([\.\w]+)(.*)=ism", "$2@$1", $profile);
576 if ($pumpio != $profile) {
580 return($username." (".$pumpio.")");
587 function bb_RemovePictureLinks($match) {
588 $text = Cache::get($match[1]);
591 $ch = @curl_init($match[1]);
592 @curl_setopt($ch, CURLOPT_NOBODY, true);
593 @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
594 @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)");
596 $curl_info = @curl_getinfo($ch);
598 if (substr($curl_info["content_type"], 0, 6) == "image/")
599 $text = "[url=".$match[1]."]".$match[1]."[/url]";
601 $text = "[url=".$match[2]."]".$match[2]."[/url]";
603 // if its not a picture then look if its a page that contains a picture link
604 require_once("include/network.php");
606 $body = fetch_url($match[1]);
608 $doc = new DOMDocument();
609 @$doc->loadHTML($body);
610 $xpath = new DomXPath($doc);
611 $list = $xpath->query("//meta[@name]");
612 foreach ($list as $node) {
615 if ($node->attributes->length)
616 foreach ($node->attributes as $attribute)
617 $attr[$attribute->name] = $attribute->value;
619 if (strtolower($attr["name"]) == "twitter:image")
620 $text = "[url=".$attr["content"]."]".$attr["content"]."[/url]";
623 Cache::set($match[1],$text);
628 function bb_expand_links($match) {
629 if (stristr($match[2], $match[3]) OR ($match[2] == $match[3]))
630 return ($match[1]."[url]".$match[2]."[/url]");
632 return ($match[1].$match[3]." [url]".$match[2]."[/url]");
635 function bb_CleanPictureLinksSub($match) {
636 $text = Cache::get($match[1]);
639 $ch = @curl_init($match[1]);
640 @curl_setopt($ch, CURLOPT_NOBODY, true);
641 @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
642 @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)");
644 $curl_info = @curl_getinfo($ch);
646 // if its a link to a picture then embed this picture
647 if (substr($curl_info["content_type"], 0, 6) == "image/")
648 $text = "[img]".$match[1]."[/img]";
650 $text = "[img]".$match[2]."[/img]";
652 // if its not a picture then look if its a page that contains a picture link
653 require_once("include/network.php");
655 $body = fetch_url($match[1]);
657 $doc = new DOMDocument();
658 @$doc->loadHTML($body);
659 $xpath = new DomXPath($doc);
660 $list = $xpath->query("//meta[@name]");
661 foreach ($list as $node) {
664 if ($node->attributes->length)
665 foreach ($node->attributes as $attribute)
666 $attr[$attribute->name] = $attribute->value;
668 if (strtolower($attr["name"]) == "twitter:image")
669 $text = "[img]".$attr["content"]."[/img]";
672 Cache::set($match[1],$text);
677 function bb_CleanPictureLinks($text) {
678 $text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'bb_CleanPictureLinksSub', $text);
682 // BBcode 2 HTML was written by WAY2WEB.net
683 // extended to work with Mistpark/Friendica - Mike Macgirvin
685 function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = false, $forplaintext = false) {
687 $stamp1 = microtime(true);
691 // Hide all [noparse] contained bbtags by spacefying them
692 // POSSIBLE BUG --> Will the 'preg' functions crash if there's an embedded image?
694 $Text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'bb_spacefy',$Text);
695 $Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_spacefy',$Text);
696 $Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_spacefy',$Text);
699 // Move all spaces out of the tags
700 $Text = preg_replace("/\[(\w*)\](\s*)/ism", '$2[$1]', $Text);
701 $Text = preg_replace("/(\s*)\[\/(\w*)\]/ism", '[/$2]$1', $Text);
703 // Extract the private images which use data urls since preg has issues with
704 // large data sizes. Stash them away while we do bbcode conversion, and then put them back
705 // in after we've done all the regex matching. We cannot use any preg functions to do this.
707 $extracted = bb_extract_images($Text);
708 $Text = $extracted['body'];
709 $saved_image = $extracted['images'];
711 // If we find any event code, turn it into an event.
712 // After we're finished processing the bbcode we'll
713 // replace all of the event code with a reformatted version.
715 $ev = bbtoevent($Text);
718 // Replace any html brackets with HTML Entities to prevent executing HTML or script
719 // Don't use strip_tags here because it breaks [url] search by replacing & with amp
721 $Text = str_replace("<", "<", $Text);
722 $Text = str_replace(">", ">", $Text);
724 // remove some newlines before the general conversion
725 $Text = preg_replace("/\s?\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","[share$1]$2[/share]",$Text);
726 $Text = preg_replace("/\s?\[quote(.*?)\]\s?(.*?)\s?\[\/quote\]\s?/ism","[quote$1]$2[/quote]",$Text);
728 $Text = preg_replace("/\n\[code\]/ism", "[code]", $Text);
729 $Text = preg_replace("/\[\/code\]\n/ism", "[/code]", $Text);
731 // Handle attached links or videos
732 $Text = bb_attachment($Text, ($simplehtml != 4) AND ($simplehtml != 0), $tryoembed);
734 // Rearrange shared links
735 if (get_config("system", "rearrange_shared_links") AND (!$simplehtml OR $tryoembed))
736 $Text = preg_replace_callback("(\[class=(.*?)\](.*?)\[\/class\])ism","bb_rearrange_link",$Text);
738 // when the content is meant exporting to other systems then remove the avatar picture since this doesn't really look good on these systems
740 $Text = preg_replace("/\[share(.*?)avatar\s?=\s?'.*?'\s?(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","\n[share$1$2]$3[/share]",$Text);
742 // Convert new line chars to html <br /> tags
744 // nlbr seems to be hopelessly messed up
745 // $Text = nl2br($Text);
750 $Text = str_replace("\r\n","\n", $Text);
752 // removing multiplicated newlines
753 if (get_config("system", "remove_multiplicated_lines")) {
754 $search = array("\n\n\n", "\n ", " \n", "[/quote]\n\n", "\n[/quote]", "[/li]\n", "\n[li]", "\n[ul]", "[/ul]\n", "\n\n[share ");
755 $replace = array("\n\n", "\n", "\n", "[/quote]\n", "[/quote]", "[/li]", "[li]", "[ul]", "[/ul]", "\n[share ");
758 $Text = str_replace($search, $replace, $Text);
759 } while ($oldtext != $Text);
762 $Text = str_replace(array("\r","\n"), array('<br />','<br />'), $Text);
765 $Text = str_replace(array("\n","\r"), array('',''),$Text);
769 // Set up the parameters for a URL search string
770 $URLSearchString = "^\[\]";
771 // Set up the parameters for a MAIL search string
772 $MAILSearchString = $URLSearchString;
774 // Remove all hashtag addresses
775 if (!$tryoembed OR $simplehtml)
776 $Text = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $Text);
778 // Bookmarks in red - will be converted to bookmarks in friendica
779 $Text = preg_replace("/#\^\[url\]([$URLSearchString]*)\[\/url\]/ism", '[bookmark=$1]$1[/bookmark]', $Text);
780 $Text = preg_replace("/#\^\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '[bookmark=$1]$2[/bookmark]', $Text);
781 $Text = preg_replace("/#\[url\=[$URLSearchString]*\]\^\[\/url\]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/i",
782 "[bookmark=$1]$2[/bookmark]", $Text);
784 if (in_array($simplehtml, array(2, 6, 7, 8, 9))) {
785 $Text = preg_replace_callback("/([^#@])\[url\=([^\]]*)\](.*?)\[\/url\]/ism","bb_expand_links",$Text);
786 //$Text = preg_replace("/[^#@]\[url\=([^\]]*)\](.*?)\[\/url\]/ism",' $2 [url]$1[/url]',$Text);
787 $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",' $2 [url]$1[/url]',$Text);
790 if ($simplehtml == 5)
791 $Text = preg_replace("/[^#@]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '[url]$1[/url]', $Text);
793 // Perform URL Search
795 $Text = preg_replace_callback("/\[bookmark\=([^\]]*)\].*?\[\/bookmark\]/ism",'tryoembed',$Text);
797 if ($simplehtml == 5)
798 $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url]$1[/url]',$Text);
800 $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url=$1]$2[/url]',$Text);
802 // if the HTML is used to generate plain text, then don't do this search, but replace all URL of that kind to text
804 $Text = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1<a href="$2" target="_blank">$2</a>', $Text);
806 $Text = preg_replace("(\[url\]([$URLSearchString]*)\[\/url\])ism"," $1 ",$Text);
807 $Text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'bb_RemovePictureLinks', $Text);
811 $Text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$Text);
813 $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$1</a>', $Text);
814 $Text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
815 //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
817 // Red compatibility, though the link can't be authenticated on Friendica
818 $Text = preg_replace("/\[zrl\=([$URLSearchString]*)\](.*?)\[\/zrl\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
821 // we may need to restrict this further if it picks up too many strays
822 // link acct:user@host to a webfinger profile redirector
824 $Text = preg_replace('/acct:(.*?)@(.*?)([ ,])/', '<a href="' . $a->get_baseurl() . '/acctlink?addr=' . "$1@$2"
825 . '" target="extlink" >acct:' . "$1@$2$3" . '</a>',$Text);
827 // Perform MAIL Search
828 $Text = preg_replace("/\[mail\]([$MAILSearchString]*)\[\/mail\]/", '<a href="mailto:$1">$1</a>', $Text);
829 $Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.*?)\[\/mail\]/", '<a href="mailto:$1">$2</a>', $Text);
831 // Check for bold text
832 $Text = preg_replace("(\[b\](.*?)\[\/b\])ism",'<strong>$1</strong>',$Text);
834 // Check for Italics text
835 $Text = preg_replace("(\[i\](.*?)\[\/i\])ism",'<em>$1</em>',$Text);
837 // Check for Underline text
838 $Text = preg_replace("(\[u\](.*?)\[\/u\])ism",'<u>$1</u>',$Text);
840 // Check for strike-through text
841 $Text = preg_replace("(\[s\](.*?)\[\/s\])ism",'<strike>$1</strike>',$Text);
843 // Check for over-line text
844 $Text = preg_replace("(\[o\](.*?)\[\/o\])ism",'<span class="overline">$1</span>',$Text);
846 // Check for colored text
847 $Text = preg_replace("(\[color=(.*?)\](.*?)\[\/color\])ism","<span style=\"color: $1;\">$2</span>",$Text);
849 // Check for sized text
850 // [size=50] --> font-size: 50px (with the unit).
851 $Text = preg_replace("(\[size=(\d*?)\](.*?)\[\/size\])ism","<span style=\"font-size: $1px; line-height: initial;\">$2</span>",$Text);
852 $Text = preg_replace("(\[size=(.*?)\](.*?)\[\/size\])ism","<span style=\"font-size: $1; line-height: initial;\">$2</span>",$Text);
854 // Check for centered text
855 $Text = preg_replace("(\[center\](.*?)\[\/center\])ism","<div style=\"text-align:center;\">$1</div>",$Text);
857 // Check for list text
858 $Text = str_replace("[*]", "<li>", $Text);
860 // Check for style sheet commands
861 $Text = preg_replace_callback("(\[style=(.*?)\](.*?)\[\/style\])ism","bb_cleanstyle",$Text);
863 // Check for CSS classes
864 $Text = preg_replace_callback("(\[class=(.*?)\](.*?)\[\/class\])ism","bb_cleanclass",$Text);
866 // handle nested lists
869 while ((((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false)) ||
870 ((strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false)) ||
871 ((strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false)) ||
872 ((strpos($Text, "[/li]") !== false) && (strpos($Text, "[li]") !== false))) && (++$endlessloop < 20)) {
873 $Text = preg_replace("/\[list\](.*?)\[\/list\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
874 $Text = preg_replace("/\[list=\](.*?)\[\/list\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>' ,$Text);
875 $Text = preg_replace("/\[list=1\](.*?)\[\/list\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
876 $Text = preg_replace("/\[list=((?-i)i)\](.*?)\[\/list\]/ism",'<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>' ,$Text);
877 $Text = preg_replace("/\[list=((?-i)I)\](.*?)\[\/list\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>' ,$Text);
878 $Text = preg_replace("/\[list=((?-i)a)\](.*?)\[\/list\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>' ,$Text);
879 $Text = preg_replace("/\[list=((?-i)A)\](.*?)\[\/list\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>' ,$Text);
880 $Text = preg_replace("/\[ul\](.*?)\[\/ul\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
881 $Text = preg_replace("/\[ol\](.*?)\[\/ol\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
882 $Text = preg_replace("/\[li\](.*?)\[\/li\]/ism", '<li>$1</li>' ,$Text);
885 $Text = preg_replace("/\[th\](.*?)\[\/th\]/sm", '<th>$1</th>' ,$Text);
886 $Text = preg_replace("/\[td\](.*?)\[\/td\]/sm", '<td>$1</td>' ,$Text);
887 $Text = preg_replace("/\[tr\](.*?)\[\/tr\]/sm", '<tr>$1</tr>' ,$Text);
888 $Text = preg_replace("/\[table\](.*?)\[\/table\]/sm", '<table>$1</table>' ,$Text);
890 $Text = preg_replace("/\[table border=1\](.*?)\[\/table\]/sm", '<table border="1" >$1</table>' ,$Text);
891 $Text = preg_replace("/\[table border=0\](.*?)\[\/table\]/sm", '<table border="0" >$1</table>' ,$Text);
893 $Text = str_replace('[hr]','<hr />', $Text);
895 // This is actually executed in prepare_body()
897 $Text = str_replace('[nosmile]','',$Text);
899 // Check for font change text
900 $Text = preg_replace("/\[font=(.*?)\](.*?)\[\/font\]/sm","<span style=\"font-family: $1;\">$2</span>",$Text);
902 // Declare the format for [code] layout
904 // $Text = preg_replace_callback("/\[code\](.*?)\[\/code\]/ism",'stripcode_br_cb',$Text);
906 $CodeLayout = '<code>$1</code>';
907 // Check for [code] text
908 $Text = preg_replace("/\[code\](.*?)\[\/code\]/ism","$CodeLayout", $Text);
910 // Declare the format for [spoiler] layout
911 $SpoilerLayout = '<blockquote class="spoiler">$1</blockquote>';
913 // Check for [spoiler] text
914 // handle nested quotes
916 while ((strpos($Text, "[/spoiler]") !== false) and (strpos($Text, "[spoiler]") !== false) and (++$endlessloop < 20))
917 $Text = preg_replace("/\[spoiler\](.*?)\[\/spoiler\]/ism","$SpoilerLayout", $Text);
919 // Check for [spoiler=Author] text
921 $t_wrote = t('$1 wrote:');
923 // handle nested quotes
925 while ((strpos($Text, "[/spoiler]")!== false) and (strpos($Text, "[spoiler=") !== false) and (++$endlessloop < 20))
926 $Text = preg_replace("/\[spoiler=[\"\']*(.*?)[\"\']*\](.*?)\[\/spoiler\]/ism",
927 "<br /><strong class=".'"spoiler"'.">" . $t_wrote . "</strong><blockquote class=".'"spoiler"'.">$2</blockquote>",
930 // Declare the format for [quote] layout
931 $QuoteLayout = '<blockquote>$1</blockquote>';
933 // Check for [quote] text
934 // handle nested quotes
936 while ((strpos($Text, "[/quote]") !== false) and (strpos($Text, "[quote]") !== false) and (++$endlessloop < 20))
937 $Text = preg_replace("/\[quote\](.*?)\[\/quote\]/ism","$QuoteLayout", $Text);
939 // Check for [quote=Author] text
941 $t_wrote = t('$1 wrote:');
943 // handle nested quotes
945 while ((strpos($Text, "[/quote]")!== false) and (strpos($Text, "[quote=") !== false) and (++$endlessloop < 20))
946 $Text = preg_replace("/\[quote=[\"\']*(.*?)[\"\']*\](.*?)\[\/quote\]/ism",
947 "<br /><strong class=".'"author"'.">" . $t_wrote . "</strong><blockquote>$2</blockquote>",
950 // [img=widthxheight]image source[/img]
951 //$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="height: $2px; width: $1px;" >', $Text);
952 $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="width: $1px;" >', $Text);
953 $Text = preg_replace("/\[zmg\=([0-9]*)x([0-9]*)\](.*?)\[\/zmg\]/ism", '<img class="zrl" src="$3" style="width: $1px;" >', $Text);
956 // [img]pathtoimage[/img]
957 $Text = preg_replace("/\[img\](.*?)\[\/img\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
958 $Text = preg_replace("/\[zmg\](.*?)\[\/zmg\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
961 $Text = preg_replace_callback("/(.*?)\[share(.*?)\](.*?)\[\/share\]/ism",
962 function ($match) use ($simplehtml){
963 return(bb_ShareAttributes($match, $simplehtml));
966 $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);
967 $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);
968 //$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);
973 $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);
974 $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);
976 $Text = preg_replace_callback("/\[video\](.*?)\[\/video\]/ism", 'tryoembed', $Text);
977 $Text = preg_replace_callback("/\[audio\](.*?)\[\/audio\]/ism", 'tryoembed', $Text);
979 $Text = preg_replace("/\[video\](.*?)\[\/video\]/",
980 '<a href="$1" target="_blank">$1</a>', $Text);
981 $Text = preg_replace("/\[audio\](.*?)\[\/audio\]/",
982 '<a href="$1" target="_blank">$1</a>', $Text);
985 // html5 video and audio
989 $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<iframe src="$1" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="$1">$1</a></iframe>', $Text);
991 $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<a href="$1">$1</a>', $Text);
993 // Youtube extensions
995 $Text = preg_replace_callback("/\[youtube\](https?:\/\/www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);
996 $Text = preg_replace_callback("/\[youtube\](www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);
997 $Text = preg_replace_callback("/\[youtube\](https?:\/\/youtu.be\/.*?)\[\/youtube\]/ism",'tryoembed',$Text);
1000 $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
1001 $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
1002 $Text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
1005 $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);
1007 $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism",
1008 '<a href="https://www.youtube.com/watch?v=$1" target="_blank">https://www.youtube.com/watch?v=$1</a>', $Text);
1011 $Text = preg_replace_callback("/\[vimeo\](https?:\/\/player.vimeo.com\/video\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text);
1012 $Text = preg_replace_callback("/\[vimeo\](https?:\/\/vimeo.com\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text);
1015 $Text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text);
1016 $Text = preg_replace("/\[vimeo\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text);
1019 $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);
1021 $Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism",
1022 '<a href="https://vimeo.com/$1" target="_blank">https://vimeo.com/$1</a>', $Text);
1024 // $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);
1028 $Text = oembed_bbcode2html($Text);
1030 // Avoid triple linefeeds through oembed
1031 $Text = str_replace("<br style='clear:left'></span><br /><br />", "<br style='clear:left'></span><br />", $Text);
1033 // If we found an event earlier, strip out all the event code and replace with a reformatted version.
1034 // Replace the event-start section with the entire formatted event. The other bbcode is stripped.
1035 // Summary (e.g. title) is required, earlier revisions only required description (in addition to
1036 // start which is always required). Allow desc with a missing summary for compatibility.
1038 if((x($ev,'desc') || x($ev,'summary')) && x($ev,'start')) {
1039 $sub = format_event_html($ev);
1041 $Text = preg_replace("/\[event\-summary\](.*?)\[\/event\-summary\]/ism",'',$Text);
1042 $Text = preg_replace("/\[event\-description\](.*?)\[\/event\-description\]/ism",'',$Text);
1043 $Text = preg_replace("/\[event\-start\](.*?)\[\/event\-start\]/ism",$sub,$Text);
1044 $Text = preg_replace("/\[event\-finish\](.*?)\[\/event\-finish\]/ism",'',$Text);
1045 $Text = preg_replace("/\[event\-location\](.*?)\[\/event\-location\]/ism",'',$Text);
1046 $Text = preg_replace("/\[event\-adjust\](.*?)\[\/event\-adjust\]/ism",'',$Text);
1049 // Unhide all [noparse] contained bbtags unspacefying them
1050 // and triming the [noparse] tag.
1052 $Text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'bb_unspacefy_and_trim',$Text);
1053 $Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_unspacefy_and_trim',$Text);
1054 $Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_unspacefy_and_trim',$Text);
1057 $Text = preg_replace('/\[\&\;([#a-z0-9]+)\;\]/','&$1;',$Text);
1058 $Text = preg_replace('/\&\#039\;/','\'',$Text);
1059 $Text = preg_replace('/\"\;/','"',$Text);
1061 // fix any escaped ampersands that may have been converted into links
1062 $Text = preg_replace("/\<([^>]*?)(src|href)=(.*?)\&\;(.*?)\>/ism",'<$1$2=$3&$4>',$Text);
1063 $Text = preg_replace("/\<([^>]*?)(src|href)=\"(?!http|ftp|mailto|cid)(.*?)\>/ism",'<$1$2="">',$Text);
1066 $Text = bb_replace_images($Text, $saved_image);
1068 // Clean up the HTML by loading and saving the HTML with the DOM
1069 // Only do it when it has to be done - for performance reasons
1070 // Update: Now it is done every time - since bad structured html can break a whole page
1071 //if (!$tryoembed) {
1072 // $doc = new DOMDocument();
1073 // $doc->preserveWhiteSpace = false;
1075 // $Text = mb_convert_encoding($Text, 'HTML-ENTITIES', "UTF-8");
1077 // $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
1078 // @$doc->loadHTML($doctype."<html><body>".$Text."</body></html>");
1080 // $Text = $doc->saveHTML();
1081 // $Text = str_replace(array("<html><body>", "</body></html>", $doctype), array("", "", ""), $Text);
1083 // $Text = str_replace('<br></li>','</li>', $Text);
1085 // $Text = mb_convert_encoding($Text, "UTF-8", 'HTML-ENTITIES');
1088 // Clean up some useless linebreaks in lists
1089 //$Text = str_replace('<br /><ul','<ul ', $Text);
1090 //$Text = str_replace('</ul><br />','</ul>', $Text);
1091 //$Text = str_replace('</li><br />','</li>', $Text);
1092 //$Text = str_replace('<br /><li>','<li>', $Text);
1093 // $Text = str_replace('<br /><ul','<ul ', $Text);
1095 // Remove all hashtag addresses
1096 /* if (!$tryoembed AND get_config("system", "remove_hashtags_on_export")) {
1097 $pattern = '/#<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
1098 $Text = preg_replace($pattern, '#$2', $Text);
1101 call_hooks('bbcode',$Text);
1103 $a->save_timestamp($stamp1, "parser");