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), $url, $title);
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 (($title != "") AND (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]);
203 // Always embed the SSL version
204 $url = str_replace(array("http://www.youtube.com/", "http://player.vimeo.com/"),
205 array("https://www.youtube.com/", "https://player.vimeo.com/"), $url);
207 //logger("tryoembed: $url");
209 $o = oembed_fetch_url($url);
211 if (isset($match[2]))
212 $o->title = $match[2];
214 //echo "<pre>"; var_dump($match, $url, $o); killme();
216 if ($o->type=="error") return $match[0];
218 $html = oembed_format_object($o);
219 return $html; //oembed_iframe($html,$o->width,$o->height);
223 // [noparse][i]italic[/i][/noparse] turns into
224 // [noparse][ i ]italic[ /i ][/noparse],
225 // to hide them from parser.
227 function bb_spacefy($st) {
228 $whole_match = $st[0];
230 $spacefied = preg_replace("/\[(.*?)\]/", "[ $1 ]", $captured);
231 $new_str = str_replace($captured, $spacefied, $whole_match);
235 // The previously spacefied [noparse][ i ]italic[ /i ][/noparse],
236 // now turns back and the [noparse] tags are trimed
237 // returning [i]italic[/i]
239 function bb_unspacefy_and_trim($st) {
240 $whole_match = $st[0];
242 $unspacefied = preg_replace("/\[ (.*?)\ ]/", "[$1]", $captured);
246 function bb_find_open_close($s, $open, $close, $occurance = 1) {
252 for($i = 1; $i <= $occurance; $i++) {
253 if( $start_pos !== false)
254 $start_pos = strpos($s, $open, $start_pos + 1);
257 if( $start_pos === false)
260 $end_pos = strpos($s, $close, $start_pos);
262 if( $end_pos === false)
265 $res = array( 'start' => $start_pos, 'end' => $end_pos );
270 function get_bb_tag_pos($s, $name, $occurance = 1) {
276 for($i = 1; $i <= $occurance; $i++) {
277 if( $start_open !== false)
278 $start_open = strpos($s, '[' . $name, $start_open + 1); // allow [name= type tags
281 if( $start_open === false)
284 $start_equal = strpos($s, '=', $start_open);
285 $start_close = strpos($s, ']', $start_open);
287 if( $start_close === false)
292 $end_open = strpos($s, '[/' . $name . ']', $start_close);
294 if( $end_open === false)
297 $res = array( 'start' => array('open' => $start_open, 'close' => $start_close),
298 'end' => array('open' => $end_open, 'close' => $end_open + strlen('[/' . $name . ']')) );
299 if( $start_equal !== false)
300 $res['start']['equal'] = $start_equal + 1;
305 function bb_tag_preg_replace($pattern, $replace, $name, $s) {
310 $pos = get_bb_tag_pos($string, $name, $occurance);
311 while($pos !== false && $occurance < 1000) {
313 $start = substr($string, 0, $pos['start']['open']);
314 $subject = substr($string, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']);
315 $end = substr($string, $pos['end']['close']);
319 $subject = preg_replace($pattern, $replace, $subject);
320 $string = $start . $subject . $end;
323 $pos = get_bb_tag_pos($string, $name, $occurance);
329 if(! function_exists('bb_extract_images')) {
330 function bb_extract_images($body) {
332 $saved_image = array();
337 $img_start = strpos($orig_body, '[img');
338 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
339 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
340 while(($img_st_close !== false) && ($img_end !== false)) {
342 $img_st_close++; // make it point to AFTER the closing bracket
343 $img_end += $img_start;
345 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
346 // This is an embedded image
348 $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
349 $new_body = $new_body . substr($orig_body, 0, $img_start) . '[$#saved_image' . $cnt . '#$]';
354 $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
356 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
358 if($orig_body === false) // in case the body ends on a closing image tag
361 $img_start = strpos($orig_body, '[img');
362 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
363 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
366 $new_body = $new_body . $orig_body;
368 return array('body' => $new_body, 'images' => $saved_image);
371 if(! function_exists('bb_replace_images')) {
372 function bb_replace_images($body, $images) {
377 foreach($images as $image) {
378 // We're depending on the property of 'foreach' (specified on the PHP website) that
379 // it loops over the array starting from the first element and going sequentially
380 // to the last element
381 $newbody = str_replace('[$#saved_image' . $cnt . '#$]', '<img src="' . $image .'" alt="' . t('Image/photo') . '" />', $newbody);
388 function bb_ShareAttributes($share, $simplehtml) {
389 $attributes = $share[2];
392 preg_match("/author='(.*?)'/ism", $attributes, $matches);
393 if ($matches[1] != "")
394 $author = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
396 preg_match('/author="(.*?)"/ism', $attributes, $matches);
397 if ($matches[1] != "")
398 $author = $matches[1];
401 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
402 if ($matches[1] != "")
403 $profile = $matches[1];
405 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
406 if ($matches[1] != "")
407 $profile = $matches[1];
410 preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
411 if ($matches[1] != "")
412 $avatar = $matches[1];
414 preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
415 if ($matches[1] != "")
416 $avatar = $matches[1];
419 preg_match("/link='(.*?)'/ism", $attributes, $matches);
420 if ($matches[1] != "")
423 preg_match('/link="(.*?)"/ism', $attributes, $matches);
424 if ($matches[1] != "")
429 $itemcache = get_itemcachepath();
431 // relative dates only make sense when they aren't cached
432 if ($itemcache == "") {
433 preg_match("/posted='(.*?)'/ism", $attributes, $matches);
434 if ($matches[1] != "")
435 $posted = $matches[1];
437 preg_match('/posted="(.*?)"/ism', $attributes, $matches);
438 if ($matches[1] != "")
439 $posted = $matches[1];
441 $reldate = (($posted) ? " " . relative_date($posted) : '');
444 $userid = GetProfileUsername($profile,$author, false);
445 $userid_compact = GetProfileUsername($profile,$author, true);
447 $preshare = trim($share[1]);
450 $preshare .= "<br /><br />";
452 switch ($simplehtml) {
454 $text = $preshare.html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8').' <a href="'.$profile.'">'.$userid."</a>: <br />»".$share[3]."«";
457 $text = $preshare.html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8').' '.$userid_compact.": <br />".$share[3];
460 $headline = '<div class="shared_header">';
461 $headline .= '<span><b>'.html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8').$userid.':</b></span>';
462 $headline .= "</div>";
464 $text = trim($share[1]);
469 $text .= $headline.'<blockquote class="shared_content">'.trim($share[3])."</blockquote><br />";
472 $text .= '<br /><a href="'.$link.'">[l]</a>';
475 $headline = '<div class="shared_header">';
476 $headline .= '<span><b>'.html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8');
477 $headline .= sprintf(t('<a href="%1$s" target="_blank">%2$s</a> %3$s'), $link, $userid, $posted);
478 $headline .= ":</b></span></div>";
480 $text = trim($share[1]);
485 $text .= $headline.'<blockquote class="shared_content">'.trim($share[3])."</blockquote><br />";
489 $text = $preshare.html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8').' '.$userid_compact.": <br />".$share[3];
492 $text = $preshare.">> @".$userid_compact.": <br />".$share[3];
494 case 7: // statusnet/GNU Social
495 $text = $preshare.html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8')." @".$userid_compact.": ".$share[3];
498 $text = $preshare."RT @".$userid_compact.": ".$share[3];
500 case 9: // Google+/Facebook
501 $text = $preshare.html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8').' '.$userid_compact.": <br />".$share[3];
504 $text .= "<br /><br />".$link;
507 $headline = trim($share[1]).'<div class="shared_header">';
509 $headline .= '<img src="'.$avatar.'" height="32" width="32" >';
511 $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);
512 $headline .= "</div>";
513 $text = $headline.'<blockquote class="shared_content">'.trim($share[3])."</blockquote>";
519 function GetProfileUsername($profile, $username, $compact = false) {
521 $twitter = preg_replace("=https?://twitter.com/(.*)=ism", "$1@twitter.com", $profile);
522 if ($twitter != $profile) {
526 return($username." (".$twitter.")");
529 $appnet = preg_replace("=https?://alpha.app.net/(.*)=ism", "$1@alpha.app.net", $profile);
530 if ($appnet != $profile) {
534 return($username." (".$appnet.")");
537 $gplus = preg_replace("=https?://plus.google.com/(.*)=ism", "$1@plus.google.com", $profile);
538 if ($gplus != $profile) {
540 return($gplususername." (".$username.")");
542 return($username." (".$gplus.")");
545 $friendica = preg_replace("=https?://(.*)/profile/(.*)=ism", "$2@$1", $profile);
546 if ($friendica != $profile) {
550 return($username." (".$friendica.")");
553 $diaspora = preg_replace("=https?://(.*)/u/(.*)=ism", "$2@$1", $profile);
554 if ($diaspora != $profile) {
558 return($username." (".$diaspora.")");
561 $StatusnetHost = preg_replace("=https?://(.*)/user/(.*)=ism", "$1", $profile);
562 if ($StatusnetHost != $profile) {
563 $StatusnetUser = preg_replace("=https?://(.*)/user/(.*)=ism", "$2", $profile);
564 if ($StatusnetUser != $profile) {
565 $UserData = fetch_url("http://".$StatusnetHost."/api/users/show.json?user_id=".$StatusnetUser);
566 $user = json_decode($UserData);
569 return($user->screen_name."@".$StatusnetHost);
571 return($username." (".$user->screen_name."@".$StatusnetHost.")");
576 // pumpio (http://host.name/user)
577 $rest = preg_replace("=https?://([\.\w]+)/([\.\w]+)(.*)=ism", "$3", $profile);
579 $pumpio = preg_replace("=https?://([\.\w]+)/([\.\w]+)(.*)=ism", "$2@$1", $profile);
580 if ($pumpio != $profile) {
584 return($username." (".$pumpio.")");
591 function bb_RemovePictureLinks($match) {
592 $text = Cache::get($match[1]);
595 $ch = @curl_init($match[1]);
596 @curl_setopt($ch, CURLOPT_NOBODY, true);
597 @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
598 @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; ".FRIENDICA_PLATFORM." ".FRIENDICA_VERSION."-".DB_UPDATE_VERSION.")");
600 $curl_info = @curl_getinfo($ch);
602 if (substr($curl_info["content_type"], 0, 6) == "image/")
603 $text = "[url=".$match[1]."]".$match[1]."[/url]";
605 $text = "[url=".$match[2]."]".$match[2]."[/url]";
607 // if its not a picture then look if its a page that contains a picture link
608 require_once("include/network.php");
610 $body = fetch_url($match[1]);
612 $doc = new DOMDocument();
613 @$doc->loadHTML($body);
614 $xpath = new DomXPath($doc);
615 $list = $xpath->query("//meta[@name]");
616 foreach ($list as $node) {
619 if ($node->attributes->length)
620 foreach ($node->attributes as $attribute)
621 $attr[$attribute->name] = $attribute->value;
623 if (strtolower($attr["name"]) == "twitter:image")
624 $text = "[url=".$attr["content"]."]".$attr["content"]."[/url]";
627 Cache::set($match[1],$text);
632 function bb_expand_links($match) {
633 if (stristr($match[2], $match[3]) OR ($match[2] == $match[3]))
634 return ($match[1]."[url]".$match[2]."[/url]");
636 return ($match[1].$match[3]." [url]".$match[2]."[/url]");
639 function bb_CleanPictureLinksSub($match) {
640 $text = Cache::get($match[1]);
643 $ch = @curl_init($match[1]);
644 @curl_setopt($ch, CURLOPT_NOBODY, true);
645 @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
646 @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; ".FRIENDICA_PLATFORM." ".FRIENDICA_VERSION."-".DB_UPDATE_VERSION.")");
648 $curl_info = @curl_getinfo($ch);
650 // if its a link to a picture then embed this picture
651 if (substr($curl_info["content_type"], 0, 6) == "image/")
652 $text = "[img]".$match[1]."[/img]";
654 $text = "[img]".$match[2]."[/img]";
656 // if its not a picture then look if its a page that contains a picture link
657 require_once("include/network.php");
659 $body = fetch_url($match[1]);
661 $doc = new DOMDocument();
662 @$doc->loadHTML($body);
663 $xpath = new DomXPath($doc);
664 $list = $xpath->query("//meta[@name]");
665 foreach ($list as $node) {
668 if ($node->attributes->length)
669 foreach ($node->attributes as $attribute)
670 $attr[$attribute->name] = $attribute->value;
672 if (strtolower($attr["name"]) == "twitter:image")
673 $text = "[img]".$attr["content"]."[/img]";
676 Cache::set($match[1],$text);
681 function bb_CleanPictureLinks($text) {
682 $text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'bb_CleanPictureLinksSub', $text);
686 // BBcode 2 HTML was written by WAY2WEB.net
687 // extended to work with Mistpark/Friendica - Mike Macgirvin
689 function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = false, $forplaintext = false) {
691 $stamp1 = microtime(true);
695 // Hide all [noparse] contained bbtags by spacefying them
696 // POSSIBLE BUG --> Will the 'preg' functions crash if there's an embedded image?
698 $Text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'bb_spacefy',$Text);
699 $Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_spacefy',$Text);
700 $Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_spacefy',$Text);
703 // Move all spaces out of the tags
704 $Text = preg_replace("/\[(\w*)\](\s*)/ism", '$2[$1]', $Text);
705 $Text = preg_replace("/(\s*)\[\/(\w*)\]/ism", '[/$2]$1', $Text);
707 // Extract the private images which use data urls since preg has issues with
708 // large data sizes. Stash them away while we do bbcode conversion, and then put them back
709 // in after we've done all the regex matching. We cannot use any preg functions to do this.
711 $extracted = bb_extract_images($Text);
712 $Text = $extracted['body'];
713 $saved_image = $extracted['images'];
715 // If we find any event code, turn it into an event.
716 // After we're finished processing the bbcode we'll
717 // replace all of the event code with a reformatted version.
719 $ev = bbtoevent($Text);
722 // Replace any html brackets with HTML Entities to prevent executing HTML or script
723 // Don't use strip_tags here because it breaks [url] search by replacing & with amp
725 $Text = str_replace("<", "<", $Text);
726 $Text = str_replace(">", ">", $Text);
728 // remove some newlines before the general conversion
729 $Text = preg_replace("/\s?\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","[share$1]$2[/share]",$Text);
730 $Text = preg_replace("/\s?\[quote(.*?)\]\s?(.*?)\s?\[\/quote\]\s?/ism","[quote$1]$2[/quote]",$Text);
732 $Text = preg_replace("/\n\[code\]/ism", "[code]", $Text);
733 $Text = preg_replace("/\[\/code\]\n/ism", "[/code]", $Text);
735 // Handle attached links or videos
736 $Text = bb_attachment($Text, ($simplehtml != 4) AND ($simplehtml != 0), $tryoembed);
738 // Rearrange shared links
739 if (get_config("system", "rearrange_shared_links") AND (!$simplehtml OR $tryoembed))
740 $Text = preg_replace_callback("(\[class=(.*?)\](.*?)\[\/class\])ism","bb_rearrange_link",$Text);
742 // when the content is meant exporting to other systems then remove the avatar picture since this doesn't really look good on these systems
744 $Text = preg_replace("/\[share(.*?)avatar\s?=\s?'.*?'\s?(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","\n[share$1$2]$3[/share]",$Text);
746 // Convert new line chars to html <br /> tags
748 // nlbr seems to be hopelessly messed up
749 // $Text = nl2br($Text);
754 $Text = str_replace("\r\n","\n", $Text);
756 // removing multiplicated newlines
757 if (get_config("system", "remove_multiplicated_lines")) {
758 $search = array("\n\n\n", "\n ", " \n", "[/quote]\n\n", "\n[/quote]", "[/li]\n", "\n[li]", "\n[ul]", "[/ul]\n", "\n\n[share ");
759 $replace = array("\n\n", "\n", "\n", "[/quote]\n", "[/quote]", "[/li]", "[li]", "[ul]", "[/ul]", "\n[share ");
762 $Text = str_replace($search, $replace, $Text);
763 } while ($oldtext != $Text);
766 $Text = str_replace(array("\r","\n"), array('<br />','<br />'), $Text);
769 $Text = str_replace(array("\n","\r"), array('',''),$Text);
773 // Set up the parameters for a URL search string
774 $URLSearchString = "^\[\]";
775 // Set up the parameters for a MAIL search string
776 $MAILSearchString = $URLSearchString;
778 // Remove all hashtag addresses
779 if (!$tryoembed OR $simplehtml)
780 $Text = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $Text);
782 // Bookmarks in red - will be converted to bookmarks in friendica
783 $Text = preg_replace("/#\^\[url\]([$URLSearchString]*)\[\/url\]/ism", '[bookmark=$1]$1[/bookmark]', $Text);
784 $Text = preg_replace("/#\^\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '[bookmark=$1]$2[/bookmark]', $Text);
785 $Text = preg_replace("/#\[url\=[$URLSearchString]*\]\^\[\/url\]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/i",
786 "[bookmark=$1]$2[/bookmark]", $Text);
788 if (in_array($simplehtml, array(2, 6, 7, 8, 9))) {
789 $Text = preg_replace_callback("/([^#@])\[url\=([^\]]*)\](.*?)\[\/url\]/ism","bb_expand_links",$Text);
790 //$Text = preg_replace("/[^#@]\[url\=([^\]]*)\](.*?)\[\/url\]/ism",' $2 [url]$1[/url]',$Text);
791 $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",' $2 [url]$1[/url]',$Text);
794 if ($simplehtml == 5)
795 $Text = preg_replace("/[^#@]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '[url]$1[/url]', $Text);
797 // Perform URL Search
799 $Text = preg_replace_callback("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'tryoembed',$Text);
801 if ($simplehtml == 5)
802 $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url]$1[/url]',$Text);
804 $Text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url=$1]$2[/url]',$Text);
806 // if the HTML is used to generate plain text, then don't do this search, but replace all URL of that kind to text
808 $Text = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1<a href="$2" target="_blank">$2</a>', $Text);
810 $Text = preg_replace("(\[url\]([$URLSearchString]*)\[\/url\])ism"," $1 ",$Text);
811 $Text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'bb_RemovePictureLinks', $Text);
815 $Text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$Text);
817 $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$1</a>', $Text);
818 $Text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
819 //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
821 // Red compatibility, though the link can't be authenticated on Friendica
822 $Text = preg_replace("/\[zrl\=([$URLSearchString]*)\](.*?)\[\/zrl\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
825 // we may need to restrict this further if it picks up too many strays
826 // link acct:user@host to a webfinger profile redirector
828 $Text = preg_replace('/acct:(.*?)@(.*?)([ ,])/', '<a href="' . $a->get_baseurl() . '/acctlink?addr=' . "$1@$2"
829 . '" target="extlink" >acct:' . "$1@$2$3" . '</a>',$Text);
831 // Perform MAIL Search
832 $Text = preg_replace("/\[mail\]([$MAILSearchString]*)\[\/mail\]/", '<a href="mailto:$1">$1</a>', $Text);
833 $Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.*?)\[\/mail\]/", '<a href="mailto:$1">$2</a>', $Text);
835 // Check for bold text
836 $Text = preg_replace("(\[b\](.*?)\[\/b\])ism",'<strong>$1</strong>',$Text);
838 // Check for Italics text
839 $Text = preg_replace("(\[i\](.*?)\[\/i\])ism",'<em>$1</em>',$Text);
841 // Check for Underline text
842 $Text = preg_replace("(\[u\](.*?)\[\/u\])ism",'<u>$1</u>',$Text);
844 // Check for strike-through text
845 $Text = preg_replace("(\[s\](.*?)\[\/s\])ism",'<strike>$1</strike>',$Text);
847 // Check for over-line text
848 $Text = preg_replace("(\[o\](.*?)\[\/o\])ism",'<span class="overline">$1</span>',$Text);
850 // Check for colored text
851 $Text = preg_replace("(\[color=(.*?)\](.*?)\[\/color\])ism","<span style=\"color: $1;\">$2</span>",$Text);
853 // Check for sized text
854 // [size=50] --> font-size: 50px (with the unit).
855 $Text = preg_replace("(\[size=(\d*?)\](.*?)\[\/size\])ism","<span style=\"font-size: $1px; line-height: initial;\">$2</span>",$Text);
856 $Text = preg_replace("(\[size=(.*?)\](.*?)\[\/size\])ism","<span style=\"font-size: $1; line-height: initial;\">$2</span>",$Text);
858 // Check for centered text
859 $Text = preg_replace("(\[center\](.*?)\[\/center\])ism","<div style=\"text-align:center;\">$1</div>",$Text);
861 // Check for list text
862 $Text = str_replace("[*]", "<li>", $Text);
864 // Check for style sheet commands
865 $Text = preg_replace_callback("(\[style=(.*?)\](.*?)\[\/style\])ism","bb_cleanstyle",$Text);
867 // Check for CSS classes
868 $Text = preg_replace_callback("(\[class=(.*?)\](.*?)\[\/class\])ism","bb_cleanclass",$Text);
870 // handle nested lists
873 while ((((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false)) ||
874 ((strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false)) ||
875 ((strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false)) ||
876 ((strpos($Text, "[/li]") !== false) && (strpos($Text, "[li]") !== false))) && (++$endlessloop < 20)) {
877 $Text = preg_replace("/\[list\](.*?)\[\/list\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
878 $Text = preg_replace("/\[list=\](.*?)\[\/list\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>' ,$Text);
879 $Text = preg_replace("/\[list=1\](.*?)\[\/list\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
880 $Text = preg_replace("/\[list=((?-i)i)\](.*?)\[\/list\]/ism",'<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>' ,$Text);
881 $Text = preg_replace("/\[list=((?-i)I)\](.*?)\[\/list\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>' ,$Text);
882 $Text = preg_replace("/\[list=((?-i)a)\](.*?)\[\/list\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>' ,$Text);
883 $Text = preg_replace("/\[list=((?-i)A)\](.*?)\[\/list\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>' ,$Text);
884 $Text = preg_replace("/\[ul\](.*?)\[\/ul\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
885 $Text = preg_replace("/\[ol\](.*?)\[\/ol\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
886 $Text = preg_replace("/\[li\](.*?)\[\/li\]/ism", '<li>$1</li>' ,$Text);
889 $Text = preg_replace("/\[th\](.*?)\[\/th\]/sm", '<th>$1</th>' ,$Text);
890 $Text = preg_replace("/\[td\](.*?)\[\/td\]/sm", '<td>$1</td>' ,$Text);
891 $Text = preg_replace("/\[tr\](.*?)\[\/tr\]/sm", '<tr>$1</tr>' ,$Text);
892 $Text = preg_replace("/\[table\](.*?)\[\/table\]/sm", '<table>$1</table>' ,$Text);
894 $Text = preg_replace("/\[table border=1\](.*?)\[\/table\]/sm", '<table border="1" >$1</table>' ,$Text);
895 $Text = preg_replace("/\[table border=0\](.*?)\[\/table\]/sm", '<table border="0" >$1</table>' ,$Text);
897 $Text = str_replace('[hr]','<hr />', $Text);
899 // This is actually executed in prepare_body()
901 $Text = str_replace('[nosmile]','',$Text);
903 // Check for font change text
904 $Text = preg_replace("/\[font=(.*?)\](.*?)\[\/font\]/sm","<span style=\"font-family: $1;\">$2</span>",$Text);
906 // Declare the format for [code] layout
908 // $Text = preg_replace_callback("/\[code\](.*?)\[\/code\]/ism",'stripcode_br_cb',$Text);
910 $CodeLayout = '<code>$1</code>';
911 // Check for [code] text
912 $Text = preg_replace("/\[code\](.*?)\[\/code\]/ism","$CodeLayout", $Text);
914 // Declare the format for [spoiler] layout
915 $SpoilerLayout = '<blockquote class="spoiler">$1</blockquote>';
917 // Check for [spoiler] text
918 // handle nested quotes
920 while ((strpos($Text, "[/spoiler]") !== false) and (strpos($Text, "[spoiler]") !== false) and (++$endlessloop < 20))
921 $Text = preg_replace("/\[spoiler\](.*?)\[\/spoiler\]/ism","$SpoilerLayout", $Text);
923 // Check for [spoiler=Author] text
925 $t_wrote = t('$1 wrote:');
927 // handle nested quotes
929 while ((strpos($Text, "[/spoiler]")!== false) and (strpos($Text, "[spoiler=") !== false) and (++$endlessloop < 20))
930 $Text = preg_replace("/\[spoiler=[\"\']*(.*?)[\"\']*\](.*?)\[\/spoiler\]/ism",
931 "<br /><strong class=".'"spoiler"'.">" . $t_wrote . "</strong><blockquote class=".'"spoiler"'.">$2</blockquote>",
934 // Declare the format for [quote] layout
935 $QuoteLayout = '<blockquote>$1</blockquote>';
937 // Check for [quote] text
938 // handle nested quotes
940 while ((strpos($Text, "[/quote]") !== false) and (strpos($Text, "[quote]") !== false) and (++$endlessloop < 20))
941 $Text = preg_replace("/\[quote\](.*?)\[\/quote\]/ism","$QuoteLayout", $Text);
943 // Check for [quote=Author] text
945 $t_wrote = t('$1 wrote:');
947 // handle nested quotes
949 while ((strpos($Text, "[/quote]")!== false) and (strpos($Text, "[quote=") !== false) and (++$endlessloop < 20))
950 $Text = preg_replace("/\[quote=[\"\']*(.*?)[\"\']*\](.*?)\[\/quote\]/ism",
951 "<br /><strong class=".'"author"'.">" . $t_wrote . "</strong><blockquote>$2</blockquote>",
954 // [img=widthxheight]image source[/img]
955 //$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="height: $2px; width: $1px;" >', $Text);
956 $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="width: $1px;" >', $Text);
957 $Text = preg_replace("/\[zmg\=([0-9]*)x([0-9]*)\](.*?)\[\/zmg\]/ism", '<img class="zrl" src="$3" style="width: $1px;" >', $Text);
960 // [img]pathtoimage[/img]
961 $Text = preg_replace("/\[img\](.*?)\[\/img\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
962 $Text = preg_replace("/\[zmg\](.*?)\[\/zmg\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
965 $Text = preg_replace_callback("/(.*?)\[share(.*?)\](.*?)\[\/share\]/ism",
966 function ($match) use ($simplehtml){
967 return(bb_ShareAttributes($match, $simplehtml));
970 $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);
971 $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);
972 //$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);
977 $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);
978 $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);
980 $Text = preg_replace_callback("/\[video\](.*?)\[\/video\]/ism", 'tryoembed', $Text);
981 $Text = preg_replace_callback("/\[audio\](.*?)\[\/audio\]/ism", 'tryoembed', $Text);
983 $Text = preg_replace("/\[video\](.*?)\[\/video\]/",
984 '<a href="$1" target="_blank">$1</a>', $Text);
985 $Text = preg_replace("/\[audio\](.*?)\[\/audio\]/",
986 '<a href="$1" target="_blank">$1</a>', $Text);
989 // html5 video and audio
993 $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<iframe src="$1" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="$1">$1</a></iframe>', $Text);
995 $Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<a href="$1">$1</a>', $Text);
997 // Youtube extensions
999 $Text = preg_replace_callback("/\[youtube\](https?:\/\/www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);
1000 $Text = preg_replace_callback("/\[youtube\](www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", 'tryoembed', $Text);
1001 $Text = preg_replace_callback("/\[youtube\](https?:\/\/youtu.be\/.*?)\[\/youtube\]/ism",'tryoembed',$Text);
1004 $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
1005 $Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
1006 $Text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism",'[youtube]$1[/youtube]',$Text);
1009 $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);
1011 $Text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism",
1012 '<a href="https://www.youtube.com/watch?v=$1" target="_blank">https://www.youtube.com/watch?v=$1</a>', $Text);
1015 $Text = preg_replace_callback("/\[vimeo\](https?:\/\/player.vimeo.com\/video\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text);
1016 $Text = preg_replace_callback("/\[vimeo\](https?:\/\/vimeo.com\/[0-9]+).*?\[\/vimeo\]/ism",'tryoembed',$Text);
1019 $Text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text);
1020 $Text = preg_replace("/\[vimeo\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/vimeo\]/ism",'[vimeo]$1[/vimeo]',$Text);
1023 $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);
1025 $Text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism",
1026 '<a href="https://vimeo.com/$1" target="_blank">https://vimeo.com/$1</a>', $Text);
1028 // $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);
1032 $Text = oembed_bbcode2html($Text);
1034 // Avoid triple linefeeds through oembed
1035 $Text = str_replace("<br style='clear:left'></span><br /><br />", "<br style='clear:left'></span><br />", $Text);
1037 // If we found an event earlier, strip out all the event code and replace with a reformatted version.
1038 // Replace the event-start section with the entire formatted event. The other bbcode is stripped.
1039 // Summary (e.g. title) is required, earlier revisions only required description (in addition to
1040 // start which is always required). Allow desc with a missing summary for compatibility.
1042 if((x($ev,'desc') || x($ev,'summary')) && x($ev,'start')) {
1043 $sub = format_event_html($ev);
1045 $Text = preg_replace("/\[event\-summary\](.*?)\[\/event\-summary\]/ism",'',$Text);
1046 $Text = preg_replace("/\[event\-description\](.*?)\[\/event\-description\]/ism",'',$Text);
1047 $Text = preg_replace("/\[event\-start\](.*?)\[\/event\-start\]/ism",$sub,$Text);
1048 $Text = preg_replace("/\[event\-finish\](.*?)\[\/event\-finish\]/ism",'',$Text);
1049 $Text = preg_replace("/\[event\-location\](.*?)\[\/event\-location\]/ism",'',$Text);
1050 $Text = preg_replace("/\[event\-adjust\](.*?)\[\/event\-adjust\]/ism",'',$Text);
1053 // Unhide all [noparse] contained bbtags unspacefying them
1054 // and triming the [noparse] tag.
1056 $Text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'bb_unspacefy_and_trim',$Text);
1057 $Text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'bb_unspacefy_and_trim',$Text);
1058 $Text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'bb_unspacefy_and_trim',$Text);
1061 $Text = preg_replace('/\[\&\;([#a-z0-9]+)\;\]/','&$1;',$Text);
1062 $Text = preg_replace('/\&\#039\;/','\'',$Text);
1063 $Text = preg_replace('/\"\;/','"',$Text);
1065 // fix any escaped ampersands that may have been converted into links
1066 $Text = preg_replace("/\<([^>]*?)(src|href)=(.*?)\&\;(.*?)\>/ism",'<$1$2=$3&$4>',$Text);
1067 $Text = preg_replace("/\<([^>]*?)(src|href)=\"(?!http|ftp|mailto|cid)(.*?)\>/ism",'<$1$2="">',$Text);
1070 $Text = bb_replace_images($Text, $saved_image);
1072 // Clean up the HTML by loading and saving the HTML with the DOM.
1073 // Bad structured html can break a whole page.
1074 // For performance reasons do it only with ativated item cache or at export.
1075 if (!$tryoembed OR (get_itemcachepath() != "")) {
1076 $doc = new DOMDocument();
1077 $doc->preserveWhiteSpace = false;
1079 $Text = mb_convert_encoding($Text, 'HTML-ENTITIES', "UTF-8");
1081 $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
1082 $encoding = '<?xml encoding="UTF-8">';
1083 @$doc->loadHTML($encoding.$doctype."<html><body>".$Text."</body></html>");
1084 $doc->encoding = 'UTF-8';
1085 $Text = $doc->saveHTML();
1086 $Text = str_replace(array("<html><body>", "</body></html>", $doctype, $encoding), array("", "", "", ""), $Text);
1088 $Text = str_replace('<br></li>','</li>', $Text);
1090 //$Text = mb_convert_encoding($Text, "UTF-8", 'HTML-ENTITIES');
1093 // Clean up some useless linebreaks in lists
1094 //$Text = str_replace('<br /><ul','<ul ', $Text);
1095 //$Text = str_replace('</ul><br />','</ul>', $Text);
1096 //$Text = str_replace('</li><br />','</li>', $Text);
1097 //$Text = str_replace('<br /><li>','<li>', $Text);
1098 // $Text = str_replace('<br /><ul','<ul ', $Text);
1100 // Remove all hashtag addresses
1101 /* if (!$tryoembed AND get_config("system", "remove_hashtags_on_export")) {
1102 $pattern = '/#<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
1103 $Text = preg_replace($pattern, '#$2', $Text);
1106 call_hooks('bbcode',$Text);
1108 $a->save_timestamp($stamp1, "parser");