+ } else {
+ $text = preg_replace("(\[url\]([$URLSearchString]*)\[\/url\])ism", " $1 ", $text);
+ $text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'self::removePictureLinksCallback', $text);
+ }
+
+
+ // Handle attached links or videos
+ $text = self::convertAttachment($text, $simple_html, $try_oembed);
+
+ $text = str_replace(["\r","\n"], ['<br />', '<br />'], $text);
+
+ // Remove all hashtag addresses
+ if ((!$try_oembed || $simple_html) && !in_array($simple_html, [3, 7])) {
+ $text = preg_replace("/([#@!])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $text);
+ } elseif ($simple_html == 3) {
+ // The ! is converted to @ since Diaspora only understands the @
+ $text = preg_replace("/([@!])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
+ '@<a href="$2">$3</a>',
+ $text);
+ } elseif ($simple_html == 7) {
+ $text = preg_replace("/([@!])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
+ '$1<span class="vcard"><a href="$2" class="url" title="$3"><span class="fn nickname mention">$3</span></a></span>',
+ $text);
+ } elseif (!$simple_html) {
+ $text = preg_replace("/([@!])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
+ '$1<a href="$2" class="userinfo mention" title="$3">$3</a>',
+ $text);
+ }
+
+ // Bookmarks in red - will be converted to bookmarks in friendica
+ $text = preg_replace("/#\^\[url\]([$URLSearchString]*)\[\/url\]/ism", '[bookmark=$1]$1[/bookmark]', $text);
+ $text = preg_replace("/#\^\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '[bookmark=$1]$2[/bookmark]', $text);
+ $text = preg_replace("/#\[url\=[$URLSearchString]*\]\^\[\/url\]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/i",
+ "[bookmark=$1]$2[/bookmark]", $text);
+
+ if (in_array($simple_html, [2, 6, 7, 8, 9])) {
+ $text = preg_replace_callback("/([^#@!])\[url\=([^\]]*)\](.*?)\[\/url\]/ism", "self::expandLinksCallback", $text);
+ //$Text = preg_replace("/[^#@!]\[url\=([^\]]*)\](.*?)\[\/url\]/ism", ' $2 [url]$1[/url]', $Text);
+ $text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", ' $2 [url]$1[/url]',$text);
+ }
+
+ if ($simple_html == 5) {
+ $text = preg_replace("/[^#@!]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '[url]$1[/url]', $text);
+ }
+
+ // Perform URL Search
+ if ($try_oembed) {
+ $text = preg_replace_callback("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", $try_oembed_callback, $text);
+ }
+
+ if ($simple_html == 5) {
+ $text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", '[url]$1[/url]', $text);
+ } else {
+ $text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", '[url=$1]$2[/url]', $text);
+ }
+
+ // Handle Diaspora posts
+ $text = preg_replace_callback(
+ "&\[url=/posts/([^\[\]]*)\](.*)\[\/url\]&Usi",
+ function ($match) {
+ return "[url=" . System::baseUrl() . "/display/" . $match[1] . "]" . $match[2] . "[/url]";
+ }, $text
+ );
+
+ $text = preg_replace_callback(
+ "&\[url=/people\?q\=(.*)\](.*)\[\/url\]&Usi",
+ function ($match) {
+ return "[url=" . System::baseUrl() . "/search?search=%40" . $match[1] . "]" . $match[2] . "[/url]";
+ }, $text
+ );
+
+ // Server independent link to posts and comments
+ // See issue: https://github.com/diaspora/diaspora_federation/issues/75
+ $expression = "=diaspora://.*?/post/([0-9A-Za-z\-_@.:]{15,254}[0-9A-Za-z])=ism";
+ $text = preg_replace($expression, System::baseUrl()."/display/$1", $text);
+
+ $text = preg_replace("/([#])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
+ '$1<a href="' . System::baseUrl() . '/search?tag=$3" class="tag" title="$3">$3</a>', $text);
+
+ $text = preg_replace("/\[url\=([$URLSearchString]*)\]#(.*?)\[\/url\]/ism",
+ '#<a href="' . System::baseUrl() . '/search?tag=$2" class="tag" title="$2">$2</a>', $text);
+
+ $text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$1</a>', $text);
+ $text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $text);
+ //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
+
+ // Red compatibility, though the link can't be authenticated on Friendica
+ $text = preg_replace("/\[zrl\=([$URLSearchString]*)\](.*?)\[\/zrl\]/ism", '<a href="$1" target="_blank">$2</a>', $text);
+
+
+ // we may need to restrict this further if it picks up too many strays
+ // link acct:user@host to a webfinger profile redirector
+
+ $text = preg_replace('/acct:([^@]+)@((?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63})/', '<a href="' . System::baseUrl() . '/acctlink?addr=$1@$2" target="extlink">acct:$1@$2</a>', $text);
+
+ // Perform MAIL Search
+ $text = preg_replace("/\[mail\]([$MAILSearchString]*)\[\/mail\]/", '<a href="mailto:$1">$1</a>', $text);
+ $text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.*?)\[\/mail\]/", '<a href="mailto:$1">$2</a>', $text);
+
+ // leave open the posibility of [map=something]
+ // this is replaced in prepare_body() which has knowledge of the item location
+
+ if (strpos($text, '[/map]') !== false) {
+ $text = preg_replace_callback(
+ "/\[map\](.*?)\[\/map\]/ism",
+ function ($match) use ($simple_html) {
+ return str_replace($match[0], '<p class="map">' . Map::byLocation($match[1], $simple_html) . '</p>', $match[0]);
+ },
+ $text
+ );
+ }
+ if (strpos($text, '[map=') !== false) {
+ $text = preg_replace_callback(
+ "/\[map=(.*?)\]/ism",
+ function ($match) use ($simple_html) {
+ return str_replace($match[0], '<p class="map">' . Map::byCoordinates(str_replace('/', ' ', $match[1]), $simple_html) . '</p>', $match[0]);
+ },
+ $text
+ );
+ }
+ if (strpos($text, '[map]') !== false) {
+ $text = preg_replace("/\[map\]/", '<p class="map"></p>', $text);
+ }
+
+ // Check for headers
+ $text = preg_replace("(\[h1\](.*?)\[\/h1\])ism", '<h1>$1</h1>', $text);
+ $text = preg_replace("(\[h2\](.*?)\[\/h2\])ism", '<h2>$1</h2>', $text);
+ $text = preg_replace("(\[h3\](.*?)\[\/h3\])ism", '<h3>$1</h3>', $text);
+ $text = preg_replace("(\[h4\](.*?)\[\/h4\])ism", '<h4>$1</h4>', $text);
+ $text = preg_replace("(\[h5\](.*?)\[\/h5\])ism", '<h5>$1</h5>', $text);
+ $text = preg_replace("(\[h6\](.*?)\[\/h6\])ism", '<h6>$1</h6>', $text);
+
+ // Check for paragraph
+ $text = preg_replace("(\[p\](.*?)\[\/p\])ism", '<p>$1</p>', $text);
+
+ // Check for bold text
+ $text = preg_replace("(\[b\](.*?)\[\/b\])ism", '<strong>$1</strong>', $text);
+
+ // Check for Italics text
+ $text = preg_replace("(\[i\](.*?)\[\/i\])ism", '<em>$1</em>', $text);
+
+ // Check for Underline text
+ $text = preg_replace("(\[u\](.*?)\[\/u\])ism", '<u>$1</u>', $text);
+
+ // Check for strike-through text
+ $text = preg_replace("(\[s\](.*?)\[\/s\])ism", '<s>$1</s>', $text);
+
+ // Check for over-line text
+ $text = preg_replace("(\[o\](.*?)\[\/o\])ism", '<span class="overline">$1</span>', $text);
+
+ // Check for colored text
+ $text = preg_replace("(\[color=(.*?)\](.*?)\[\/color\])ism", "<span style=\"color: $1;\">$2</span>", $text);
+
+ // Check for sized text
+ // [size=50] --> font-size: 50px (with the unit).
+ $text = preg_replace("(\[size=(\d*?)\](.*?)\[\/size\])ism", "<span style=\"font-size: $1px; line-height: initial;\">$2</span>", $text);
+ $text = preg_replace("(\[size=(.*?)\](.*?)\[\/size\])ism", "<span style=\"font-size: $1; line-height: initial;\">$2</span>", $text);
+
+ // Check for centered text
+ $text = preg_replace("(\[center\](.*?)\[\/center\])ism", "<div style=\"text-align:center;\">$1</div>", $text);
+
+ // Check for list text
+ $text = str_replace("[*]", "<li>", $text);
+
+ // Check for style sheet commands
+ $text = preg_replace_callback(
+ "(\[style=(.*?)\](.*?)\[\/style\])ism",
+ function ($match) {
+ return "<span style=\"" . HTML::sanitizeCSS($match[1]) . ";\">" . $match[2] . "</span>";
+ },
+ $text
+ );
+
+ // Check for CSS classes
+ $text = preg_replace_callback(
+ "(\[class=(.*?)\](.*?)\[\/class\])ism",
+ function ($match) {
+ return "<span class=\"" . HTML::sanitizeCSS($match[1]) . "\">" . $match[2] . "</span>";
+ },
+ $text
+ );
+
+ // handle nested lists
+ $endlessloop = 0;
+
+ while ((((strpos($text, "[/list]") !== false) && (strpos($text, "[list") !== false)) ||
+ ((strpos($text, "[/ol]") !== false) && (strpos($text, "[ol]") !== false)) ||
+ ((strpos($text, "[/ul]") !== false) && (strpos($text, "[ul]") !== false)) ||
+ ((strpos($text, "[/li]") !== false) && (strpos($text, "[li]") !== false))) && (++$endlessloop < 20)) {
+ $text = preg_replace("/\[list\](.*?)\[\/list\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $text);
+ $text = preg_replace("/\[list=\](.*?)\[\/list\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>', $text);
+ $text = preg_replace("/\[list=1\](.*?)\[\/list\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $text);
+ $text = preg_replace("/\[list=((?-i)i)\](.*?)\[\/list\]/ism", '<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>', $text);
+ $text = preg_replace("/\[list=((?-i)I)\](.*?)\[\/list\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>', $text);
+ $text = preg_replace("/\[list=((?-i)a)\](.*?)\[\/list\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>', $text);
+ $text = preg_replace("/\[list=((?-i)A)\](.*?)\[\/list\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>', $text);
+ $text = preg_replace("/\[ul\](.*?)\[\/ul\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $text);
+ $text = preg_replace("/\[ol\](.*?)\[\/ol\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $text);
+ $text = preg_replace("/\[li\](.*?)\[\/li\]/ism", '<li>$1</li>', $text);
+ }
+
+ $text = preg_replace("/\[th\](.*?)\[\/th\]/sm", '<th>$1</th>', $text);
+ $text = preg_replace("/\[td\](.*?)\[\/td\]/sm", '<td>$1</td>', $text);
+ $text = preg_replace("/\[tr\](.*?)\[\/tr\]/sm", '<tr>$1</tr>', $text);
+ $text = preg_replace("/\[table\](.*?)\[\/table\]/sm", '<table>$1</table>', $text);
+
+ $text = preg_replace("/\[table border=1\](.*?)\[\/table\]/sm", '<table border="1" >$1</table>', $text);
+ $text = preg_replace("/\[table border=0\](.*?)\[\/table\]/sm", '<table border="0" >$1</table>', $text);
+
+ $text = str_replace('[hr]', '<hr />', $text);
+
+ // This is actually executed in prepare_body()
+
+ $text = str_replace('[nosmile]', '', $text);
+
+ // Check for font change text
+ $text = preg_replace("/\[font=(.*?)\](.*?)\[\/font\]/sm", "<span style=\"font-family: $1;\">$2</span>", $text);
+
+ // Declare the format for [code] layout
+
+ $CodeLayout = '<code>$1</code>';
+ // Check for [code] text
+ $text = preg_replace("/\[code\](.*?)\[\/code\]/ism", "$CodeLayout", $text);
+
+ // Declare the format for [spoiler] layout
+ $SpoilerLayout = '<blockquote class="spoiler">$1</blockquote>';
+
+ // Check for [spoiler] text
+ // handle nested quotes
+ $endlessloop = 0;
+ while ((strpos($text, "[/spoiler]") !== false) && (strpos($text, "[spoiler]") !== false) && (++$endlessloop < 20)) {
+ $text = preg_replace("/\[spoiler\](.*?)\[\/spoiler\]/ism", "$SpoilerLayout", $text);
+ }
+
+ // Check for [spoiler=Author] text
+
+ $t_wrote = L10n::t('$1 wrote:');
+
+ // handle nested quotes
+ $endlessloop = 0;
+ while ((strpos($text, "[/spoiler]")!== false) && (strpos($text, "[spoiler=") !== false) && (++$endlessloop < 20)) {
+ $text = preg_replace("/\[spoiler=[\"\']*(.*?)[\"\']*\](.*?)\[\/spoiler\]/ism",
+ "<br /><strong class=".'"spoiler"'.">" . $t_wrote . "</strong><blockquote class=".'"spoiler"'.">$2</blockquote>",
+ $text);
+ }
+
+ // Declare the format for [quote] layout
+ $QuoteLayout = '<blockquote>$1</blockquote>';
+
+ // Check for [quote] text
+ // handle nested quotes
+ $endlessloop = 0;
+ while ((strpos($text, "[/quote]") !== false) && (strpos($text, "[quote]") !== false) && (++$endlessloop < 20)) {
+ $text = preg_replace("/\[quote\](.*?)\[\/quote\]/ism", "$QuoteLayout", $text);
+ }
+
+ // Check for [quote=Author] text
+
+ $t_wrote = L10n::t('$1 wrote:');
+
+ // handle nested quotes
+ $endlessloop = 0;
+ while ((strpos($text, "[/quote]")!== false) && (strpos($text, "[quote=") !== false) && (++$endlessloop < 20)) {
+ $text = preg_replace("/\[quote=[\"\']*(.*?)[\"\']*\](.*?)\[\/quote\]/ism",
+ "<p><strong class=".'"author"'.">" . $t_wrote . "</strong></p><blockquote>$2</blockquote>",
+ $text);
+ }
+
+
+ // [img=widthxheight]image source[/img]
+ $text = preg_replace_callback(
+ "/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism",
+ function ($matches) use ($simple_html) {
+ if (strpos($matches[3], "data:image/") === 0) {
+ return $matches[0];
+ }
+
+ $matches[3] = self::proxyUrl($matches[3], $simple_html);
+ return "[img=" . $matches[1] . "x" . $matches[2] . "]" . $matches[3] . "[/img]";
+ },
+ $text
+ );
+
+ $text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="width: $1px;" >', $text);
+ $text = preg_replace("/\[zmg\=([0-9]*)x([0-9]*)\](.*?)\[\/zmg\]/ism", '<img class="zrl" src="$3" style="width: $1px;" >', $text);
+
+ $text = preg_replace_callback("/\[img\=([$URLSearchString]*)\](.*?)\[\/img\]/ism",
+ function ($matches) use ($simple_html) {
+ $matches[1] = self::proxyUrl($matches[1], $simple_html);
+ $matches[2] = htmlspecialchars($matches[2], ENT_COMPAT);
+ return '<img src="' . $matches[1] . '" alt="' . $matches[2] . '">';
+ },
+ $text);
+
+ // Images
+ // [img]pathtoimage[/img]
+ $text = preg_replace_callback(
+ "/\[img\](.*?)\[\/img\]/ism",
+ function ($matches) use ($simple_html) {
+ if (strpos($matches[1], "data:image/") === 0) {
+ return $matches[0];
+ }
+
+ $matches[1] = self::proxyUrl($matches[1], $simple_html);
+ return "[img]" . $matches[1] . "[/img]";
+ },
+ $text
+ );
+
+ $text = preg_replace("/\[img\](.*?)\[\/img\]/ism", '<img src="$1" alt="' . L10n::t('Image/photo') . '" />', $text);
+ $text = preg_replace("/\[zmg\](.*?)\[\/zmg\]/ism", '<img src="$1" alt="' . L10n::t('Image/photo') . '" />', $text);
+
+ // Shared content
+ $text = preg_replace_callback("/(.*?)\[share(.*?)\](.*?)\[\/share\]/ism",
+ function ($match) use ($simple_html) {
+ return self::convertShare($match, $simple_html);
+ }, $text);
+
+ $text = preg_replace("/\[crypt\](.*?)\[\/crypt\]/ism", '<br/><img src="' .System::baseUrl() . '/images/lock_icon.gif" alt="' . L10n::t('Encrypted content') . '" title="' . L10n::t('Encrypted content') . '" /><br />', $text);
+ $text = preg_replace("/\[crypt(.*?)\](.*?)\[\/crypt\]/ism", '<br/><img src="' .System::baseUrl() . '/images/lock_icon.gif" alt="' . L10n::t('Encrypted content') . '" title="' . '$1' . ' ' . L10n::t('Encrypted content') . '" /><br />', $text);
+ //$Text = preg_replace("/\[crypt=(.*?)\](.*?)\[\/crypt\]/ism", '<br/><img src="' .System::baseUrl() . '/images/lock_icon.gif" alt="' . L10n::t('Encrypted content') . '" title="' . '$1' . ' ' . L10n::t('Encrypted content') . '" /><br />', $Text);
+
+ // Try to Oembed
+ if ($try_oembed) {
+ $text = preg_replace("/\[video\](.*?\.(ogg|ogv|oga|ogm|webm|mp4).*?)\[\/video\]/ism", '<video src="$1" controls="controls" width="' . $a->videowidth . '" height="' . $a->videoheight . '" loop="true"><a href="$1">$1</a></video>', $text);
+ $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);
+
+ $text = preg_replace_callback("/\[video\](.*?)\[\/video\]/ism", $try_oembed_callback, $text);
+ $text = preg_replace_callback("/\[audio\](.*?)\[\/audio\]/ism", $try_oembed_callback, $text);
+ } else {
+ $text = preg_replace("/\[video\](.*?)\[\/video\]/ism",
+ '<a href="$1" target="_blank">$1</a>', $text);
+ $text = preg_replace("/\[audio\](.*?)\[\/audio\]/ism",
+ '<a href="$1" target="_blank">$1</a>', $text);
+ }
+
+ // html5 video and audio
+
+
+ if ($try_oembed) {
+ $text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<iframe src="$1" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="$1">$1</a></iframe>', $text);
+ } else {
+ $text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<a href="$1">$1</a>', $text);
+ }
+
+ // Youtube extensions
+ if ($try_oembed) {
+ $text = preg_replace_callback("/\[youtube\](https?:\/\/www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", $try_oembed_callback, $text);
+ $text = preg_replace_callback("/\[youtube\](www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", $try_oembed_callback, $text);
+ $text = preg_replace_callback("/\[youtube\](https?:\/\/youtu.be\/.*?)\[\/youtube\]/ism", $try_oembed_callback, $text);
+ }
+
+ $text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
+ $text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
+ $text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
+
+ if ($try_oembed) {
+ $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);
+ } else {
+ $text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism",
+ '<a href="https://www.youtube.com/watch?v=$1" target="_blank">https://www.youtube.com/watch?v=$1</a>', $text);
+ }
+
+ if ($try_oembed) {
+ $text = preg_replace_callback("/\[vimeo\](https?:\/\/player.vimeo.com\/video\/[0-9]+).*?\[\/vimeo\]/ism", $try_oembed_callback, $text);
+ $text = preg_replace_callback("/\[vimeo\](https?:\/\/vimeo.com\/[0-9]+).*?\[\/vimeo\]/ism", $try_oembed_callback, $text);
+ }
+
+ $text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism", '[vimeo]$1[/vimeo]', $text);
+ $text = preg_replace("/\[vimeo\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/vimeo\]/ism", '[vimeo]$1[/vimeo]', $text);
+
+ if ($try_oembed) {
+ $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);
+ } else {
+ $text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism",
+ '<a href="https://vimeo.com/$1" target="_blank">https://vimeo.com/$1</a>', $text);
+ }
+
+ // oembed tag
+ $text = OEmbed::BBCode2HTML($text);
+
+ // Avoid triple linefeeds through oembed
+ $text = str_replace("<br style='clear:left'></span><br /><br />", "<br style='clear:left'></span><br />", $text);
+
+ // If we found an event earlier, strip out all the event code and replace with a reformatted version.
+ // Replace the event-start section with the entire formatted event. The other bbcode is stripped.
+ // Summary (e.g. title) is required, earlier revisions only required description (in addition to
+ // start which is always required). Allow desc with a missing summary for compatibility.
+
+ if ((x($ev, 'desc') || x($ev, 'summary')) && x($ev, 'start')) {
+ $sub = Event::getHTML($ev, $simple_html);
+
+ $text = preg_replace("/\[event\-summary\](.*?)\[\/event\-summary\]/ism", '', $text);
+ $text = preg_replace("/\[event\-description\](.*?)\[\/event\-description\]/ism", '', $text);
+ $text = preg_replace("/\[event\-start\](.*?)\[\/event\-start\]/ism", $sub, $text);
+ $text = preg_replace("/\[event\-finish\](.*?)\[\/event\-finish\]/ism", '', $text);
+ $text = preg_replace("/\[event\-location\](.*?)\[\/event\-location\]/ism", '', $text);
+ $text = preg_replace("/\[event\-adjust\](.*?)\[\/event\-adjust\]/ism", '', $text);
+ $text = preg_replace("/\[event\-id\](.*?)\[\/event\-id\]/ism", '', $text);
+ }
+
+ // Replace non graphical smilies for external posts
+ if ($simple_html) {
+ $text = Smilies::replace($text, false, true);
+ }
+
+ // Replace inline code blocks
+ $text = preg_replace_callback("|(?!<br[^>]*>)<code>([^<]*)</code>(?!<br[^>]*>)|ism",
+ function ($match) use ($simple_html) {
+ $return = '<key>' . $match[1] . '</key>';
+ // Use <code> for Diaspora inline code blocks
+ if ($simple_html === 3) {
+ $return = '<code>' . $match[1] . '</code>';
+ }
+ return $return;
+ }
+ , $text);
+
+ // Unhide all [noparse] contained bbtags unspacefying them
+ // and triming the [noparse] tag.
+
+ $text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'self::unescapeNoparseCallback', $text);
+ $text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'self::unescapeNoparseCallback', $text);
+ $text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'self::unescapeNoparseCallback', $text);
+
+ /// @todo What is the meaning of these lines?
+ $text = preg_replace('/\[\&\;([#a-z0-9]+)\;\]/', '&$1;', $text);
+ $text = preg_replace('/\&\#039\;/', '\'', $text);
+
+ // Currently deactivated, it made problems with " inside of alt texts.
+ //$text = preg_replace('/\"\;/', '"', $text);
+
+ // fix any escaped ampersands that may have been converted into links
+ $text = preg_replace('/\<([^>]*?)(src|href)=(.*?)\&\;(.*?)\>/ism', '<$1$2=$3&$4>', $text);
+
+ // sanitizes src attributes (http and redir URLs for displaying in a web page, cid used for inline images in emails)
+ $allowed_src_protocols = ['http', 'redir', 'cid'];
+ $text = preg_replace('#<([^>]*?)(src)="(?!' . implode('|', $allowed_src_protocols) . ')(.*?)"(.*?)>#ism',
+ '<$1$2=""$4 data-original-src="$3" class="invalid-src" title="' . L10n::t('Invalid source protocol') . '">', $text);
+
+ // sanitize href attributes (only whitelisted protocols URLs)
+ // default value for backward compatibility
+ $allowed_link_protocols = Config::get('system', 'allowed_link_protocols', ['ftp', 'mailto', 'gopher', 'cid']);
+
+ // Always allowed protocol even if config isn't set or not including it
+ $allowed_link_protocols[] = 'http';
+ $allowed_link_protocols[] = 'redir/';
+
+ $regex = '#<([^>]*?)(href)="(?!' . implode('|', $allowed_link_protocols) . ')(.*?)"(.*?)>#ism';
+ $text = preg_replace($regex, '<$1$2="javascript:void(0)"$4 data-original-href="$3" class="invalid-href" title="' . L10n::t('Invalid link protocol') . '">', $text);
+
+ if ($saved_image) {
+ $text = self::interpolateSavedImagesIntoItemBody($text, $saved_image);
+ }
+
+ // Restore code blocks
+ $text = preg_replace_callback('/#codeblock-([0-9]+)#/iU',
+ function ($matches) use ($codeblocks) {
+ $return = $matches[0];
+ if (isset($codeblocks[intval($matches[1])])) {
+ $return = $codeblocks[$matches[1]];
+ }
+ return $return;
+ },
+ $text
+ );
+
+ // Clean up the HTML by loading and saving the HTML with the DOM.
+ // Bad structured html can break a whole page.
+ // For performance reasons do it only with ativated item cache or at export.
+ if (!$try_oembed || (get_itemcachepath() != "")) {
+ $doc = new DOMDocument();
+ $doc->preserveWhiteSpace = false;
+
+ $text = mb_convert_encoding($text, 'HTML-ENTITIES', "UTF-8");
+
+ $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
+ $encoding = '<?xml encoding="UTF-8">';
+ @$doc->loadHTML($encoding.$doctype."<html><body>".$text."</body></html>");
+ $doc->encoding = 'UTF-8';
+ $text = $doc->saveHTML();
+ $text = str_replace(["<html><body>", "</body></html>", $doctype, $encoding], ["", "", "", ""], $text);
+
+ $text = str_replace('<br></li>', '</li>', $text);
+
+ //$Text = mb_convert_encoding($Text, "UTF-8", 'HTML-ENTITIES');
+ }
+
+ // Clean up some useless linebreaks in lists
+ //$Text = str_replace('<br /><ul', '<ul ', $Text);
+ //$Text = str_replace('</ul><br />', '</ul>', $Text);
+ //$Text = str_replace('</li><br />', '</li>', $Text);
+ //$Text = str_replace('<br /><li>', '<li>', $Text);
+ //$Text = str_replace('<br /><ul', '<ul ', $Text);
+
+ Addon::callHooks('bbcode', $text);
+
+ return trim($text);
+ }
+
+ /**
+ * @brief Strips the "abstract" tag from the provided text
+ *
+ * @param string $text The text with BBCode
+ * @return string The same text - but without "abstract" element
+ */
+ public static function stripAbstract($text)
+ {
+ $text = preg_replace("/[\s|\n]*\[abstract\].*?\[\/abstract\][\s|\n]*/ism", '', $text);
+ $text = preg_replace("/[\s|\n]*\[abstract=.*?\].*?\[\/abstract][\s|\n]*/ism", '', $text);
+
+ return $text;
+ }
+
+ /**
+ * @brief Returns the value of the "abstract" element
+ *
+ * @param string $text The text that maybe contains the element
+ * @param string $addon The addon for which the abstract is meant for
+ * @return string The abstract
+ */
+ public static function getAbstract($text, $addon = "")
+ {
+ $abstract = "";
+ $abstracts = [];
+ $addon = strtolower($addon);
+
+ if (preg_match_all("/\[abstract=(.*?)\](.*?)\[\/abstract\]/ism", $text, $results, PREG_SET_ORDER)) {
+ foreach ($results AS $result) {
+ $abstracts[strtolower($result[1])] = $result[2];
+ }
+ }
+
+ if (isset($abstracts[$addon])) {
+ $abstract = $abstracts[$addon];
+ }
+
+ if ($abstract == "" && preg_match("/\[abstract\](.*?)\[\/abstract\]/ism", $text, $result)) {
+ $abstract = $result[1];
+ }
+
+ return $abstract;
+ }
+
+ /**
+ * @brief Callback function to replace a Friendica style mention in a mention for Diaspora
+ *
+ * @param array $match Matching values for the callback
+ * @return string Replaced mention
+ */
+ private static function bbCodeMention2DiasporaCallback($match)
+ {
+ $contact = Contact::getDetailsByURL($match[3]);
+
+ if (empty($contact['addr'])) {
+ $contact = Probe::uri($match[3]);
+ }
+
+ if (empty($contact['addr'])) {
+ return $match[0];
+ }
+
+ $mention = '@{' . $match[2] . '; ' . $contact['addr'] . '}';
+ return $mention;
+ }
+
+ /**
+ * @brief Converts a BBCode text into Markdown
+ *
+ * This function converts a BBCode item body to be sent to Markdown-enabled
+ * systems like Diaspora and Libertree
+ *
+ * @param string $text
+ * @param bool $for_diaspora Diaspora requires more changes than Libertree
+ * @return string
+ */
+ public static function toMarkdown($text, $for_diaspora = true)
+ {
+ $a = self::getApp();
+
+ $original_text = $text;
+
+ // Since Diaspora is creating a summary for links, this function removes them before posting
+ if ($for_diaspora) {
+ $text = self::removeShareInformation($text);
+ }
+
+ /**
+ * Transform #tags, strip off the [url] and replace spaces with underscore
+ */
+ $url_search_string = "^\[\]";
+ $text = preg_replace_callback("/#\[url\=([$url_search_string]*)\](.*?)\[\/url\]/i",
+ function ($matches) {
+ return '#' . str_replace(' ', '_', $matches[2]);
+ },
+ $text
+ );
+
+ // Converting images with size parameters to simple images. Markdown doesn't know it.
+ $text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $text);
+
+ // Convert it to HTML - don't try oembed
+ if ($for_diaspora) {
+ $text = self::convert($text, false, 3);
+
+ // Add all tags that maybe were removed
+ if (preg_match_all("/#\[url\=([$url_search_string]*)\](.*?)\[\/url\]/ism", $original_text, $tags)) {
+ $tagline = "";
+ foreach ($tags[2] as $tag) {
+ $tag = html_entity_decode($tag, ENT_QUOTES, 'UTF-8');
+ if (!strpos(html_entity_decode($text, ENT_QUOTES, 'UTF-8'), '#' . $tag)) {
+ $tagline .= '#' . $tag . ' ';
+ }
+ }
+ $text = $text . " " . $tagline;
+ }
+ } else {
+ $text = self::convert($text, false, 4);
+ }
+
+ // mask some special HTML chars from conversation to markdown
+ $text = str_replace(['<', '>', '&'], ['&_lt_;', '&_gt_;', '&_amp_;'], $text);
+
+ // If a link is followed by a quote then there should be a newline before it
+ // Maybe we should make this newline at every time before a quote.
+ $text = str_replace(["</a><blockquote>"], ["</a><br><blockquote>"], $text);
+
+ $stamp1 = microtime(true);
+
+ // Now convert HTML to Markdown
+ $text = HTML::toMarkdown($text);
+
+ // unmask the special chars back to HTML
+ $text = str_replace(['&\_lt\_;', '&\_gt\_;', '&\_amp\_;'], ['<', '>', '&'], $text);
+
+ $a->saveTimestamp($stamp1, "parser");
+
+ // Libertree has a problem with escaped hashtags.
+ $text = str_replace(['\#'], ['#'], $text);
+
+ // Remove any leading or trailing whitespace, as this will mess up
+ // the Diaspora signature verification and cause the item to disappear
+ $text = trim($text);
+
+ if ($for_diaspora) {
+ $url_search_string = "^\[\]";
+ $text = preg_replace_callback(
+ "/([@]\[(.*?)\])\(([$url_search_string]*?)\)/ism",
+ ['self', 'bbCodeMention2DiasporaCallback'],
+ $text
+ );