" . $link;
+ }
+ break;
+ default:
+ // Transforms quoted tweets in rich attachments to avoid nested tweets
+ if (stripos(normalise_link($link), 'http://twitter.com/') === 0 && OEmbed::isAllowedURL($link)) {
+ try {
+ $oembed = OEmbed::getHTML($link, $preshare);
+ } catch (Exception $e) {
+ $oembed = sprintf('[bookmark=%s]%s[/bookmark]', $link, $preshare);
+ }
+
+ $text = $preshare . $oembed;
+ } else {
+ $text = trim($share[1]) . "\n";
+
+ $avatar = proxy_url($avatar, false, PROXY_SIZE_THUMB);
+
+ $tpl = get_markup_template('shared_content.tpl');
+ $text .= replace_macros($tpl, [
+ '$profile' => $profile,
+ '$avatar' => $avatar,
+ '$author' => $author,
+ '$link' => $link,
+ '$posted' => $posted,
+ '$content' => trim($share[3])
+ ]);
+ }
+ break;
+ }
+
+ return $text;
+ }
+
+ private static function removePictureLinksCallback($match)
+ {
+ $text = Cache::get($match[1]);
+
+ if (is_null($text)) {
+ $a = get_app();
+
+ $stamp1 = microtime(true);
+
+ $ch = @curl_init($match[1]);
+ @curl_setopt($ch, CURLOPT_NOBODY, true);
+ @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ @curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
+ @curl_exec($ch);
+ $curl_info = @curl_getinfo($ch);
+
+ $a->save_timestamp($stamp1, "network");
+
+ if (substr($curl_info["content_type"], 0, 6) == "image/") {
+ $text = "[url=" . $match[1] . "]" . $match[1] . "[/url]";
+ } else {
+ $text = "[url=" . $match[2] . "]" . $match[2] . "[/url]";
+
+ // if its not a picture then look if its a page that contains a picture link
+ $body = Network::fetchUrl($match[1]);
+
+ $doc = new DOMDocument();
+ @$doc->loadHTML($body);
+ $xpath = new DomXPath($doc);
+ $list = $xpath->query("//meta[@name]");
+ foreach ($list as $node) {
+ $attr = [];
+
+ if ($node->attributes->length) {
+ foreach ($node->attributes as $attribute) {
+ $attr[$attribute->name] = $attribute->value;
+ }
+ }
+
+ if (strtolower($attr["name"]) == "twitter:image") {
+ $text = "[url=" . $attr["content"] . "]" . $attr["content"] . "[/url]";
+ }
+ }
+ }
+ Cache::set($match[1], $text);
+ }
+
+ return $text;
+ }
+
+ private static function expandLinksCallback($match)
+ {
+ if (($match[3] == "") || ($match[2] == $match[3]) || stristr($match[2], $match[3])) {
+ return ($match[1] . "[url]" . $match[2] . "[/url]");
+ } else {
+ return ($match[1] . $match[3] . " [url]" . $match[2] . "[/url]");
+ }
+ }
+
+ private static function cleanPictureLinksCallback($match)
+ {
+ $text = Cache::get($match[1]);
+
+ if (is_null($text)) {
+ $a = get_app();
+
+ $stamp1 = microtime(true);
+
+ $ch = @curl_init($match[1]);
+ @curl_setopt($ch, CURLOPT_NOBODY, true);
+ @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ @curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
+ @curl_exec($ch);
+ $curl_info = @curl_getinfo($ch);
+
+ $a->save_timestamp($stamp1, "network");
+
+ // if its a link to a picture then embed this picture
+ if (substr($curl_info["content_type"], 0, 6) == "image/") {
+ $text = "[img]" . $match[1] . "[/img]";
+ } else {
+ $text = "[img]" . $match[2] . "[/img]";
+
+ // if its not a picture then look if its a page that contains a picture link
+ $body = Network::fetchUrl($match[1]);
+
+ $doc = new DOMDocument();
+ @$doc->loadHTML($body);
+ $xpath = new DomXPath($doc);
+ $list = $xpath->query("//meta[@name]");
+ foreach ($list as $node) {
+ $attr = [];
+ if ($node->attributes->length) {
+ foreach ($node->attributes as $attribute) {
+ $attr[$attribute->name] = $attribute->value;
+ }
+ }
+
+ if (strtolower($attr["name"]) == "twitter:image") {
+ $text = "[img]" . $attr["content"] . "[/img]";
+ }
+ }
+ }
+ Cache::set($match[1], $text);
+ }
+
+ return $text;
+ }
+
+ public static function cleanPictureLinks($text)
+ {
+ $return = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'self::cleanPictureLinksCallback', $text);
+ return $return;
+ }
+
+ private static function textHighlightCallback($match)
+ {
+ if (in_array(strtolower($match[1]),
+ ['php', 'css', 'mysql', 'sql', 'abap', 'diff', 'html', 'perl', 'ruby',
+ 'vbscript', 'avrc', 'dtd', 'java', 'xml', 'cpp', 'python', 'javascript', 'js', 'sh'])
+ ) {
+ return text_highlight($match[2], strtolower($match[1]));
+ }
+ return $match[0];
+ }
+
+ /**
+ * @brief Converts a BBCode message to HTML message
+ *
+ * BBcode 2 HTML was written by WAY2WEB.net
+ * extended to work with Mistpark/Friendica - Mike Macgirvin
+ *
+ * Simple HTML values meaning:
+ * - 0: Friendica display
+ * - 1: Unused
+ * - 2: Used for Facebook, Google+, Windows Phone push, Friendica API
+ * - 3: Used before converting to Markdown in bb2diaspora.php
+ * - 4: Used for WordPress, Libertree (before Markdown), pump.io and tumblr
+ * - 5: Unused
+ * - 6: Used for Appnet
+ * - 7: Used for dfrn, OStatus
+ * - 8: Used for WP backlink text setting
+ *
+ * @param string $text
+ * @param bool $preserve_nl
+ * @param bool $try_oembed
+ * @param int $simple_html
+ * @param bool $for_plaintext
+ * @return string
+ */
+ public static function convert($text, $preserve_nl = false, $try_oembed = true, $simple_html = false, $for_plaintext = false)
+ {
+ $a = get_app();
+
+ /*
+ * preg_match_callback function to replace potential Oembed tags with Oembed content
+ *
+ * $match[0] = [tag]$url[/tag] or [tag=$url]$title[/tag]
+ * $match[1] = $url
+ * $match[2] = $title or absent
+ */
+ $try_oembed_callback = function ($match)
+ {
+ $url = $match[1];
+ $title = defaults($match, 2, null);
+
+ try {
+ $return = OEmbed::getHTML($url, $title);
+ } catch (Exception $ex) {
+ $return = $match[0];
+ }
+
+ return $return;
+ };
+
+ // Hide all [noparse] contained bbtags by spacefying them
+ // POSSIBLE BUG --> Will the 'preg' functions crash if there's an embedded image?
+
+ $text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'self::escapeNoparseCallback', $text);
+ $text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'self::escapeNoparseCallback', $text);
+ $text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'self::escapeNoparseCallback', $text);
+
+ // Remove the abstract element. It is a non visible element.
+ $text = remove_abstract($text);
+
+ // Move all spaces out of the tags
+ $text = preg_replace("/\[(\w*)\](\s*)/ism", '$2[$1]', $text);
+ $text = preg_replace("/(\s*)\[\/(\w*)\]/ism", '[/$2]$1', $text);
+
+ // Extract the private images which use data urls since preg has issues with
+ // large data sizes. Stash them away while we do bbcode conversion, and then put them back
+ // in after we've done all the regex matching. We cannot use any preg functions to do this.
+
+ $extracted = bb_extract_images($text);
+ $text = $extracted['body'];
+ $saved_image = $extracted['images'];
+
+ // If we find any event code, turn it into an event.
+ // After we're finished processing the bbcode we'll
+ // replace all of the event code with a reformatted version.
+
+ $ev = bbtoevent($text);
+
+ // Replace any html brackets with HTML Entities to prevent executing HTML or script
+ // Don't use strip_tags here because it breaks [url] search by replacing & with amp
+
+ $text = str_replace("<", "<", $text);
+ $text = str_replace(">", ">", $text);
+
+ // remove some newlines before the general conversion
+ $text = preg_replace("/\s?\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism", "[share$1]$2[/share]", $text);
+ $text = preg_replace("/\s?\[quote(.*?)\]\s?(.*?)\s?\[\/quote\]\s?/ism", "[quote$1]$2[/quote]", $text);
+
+ $text = preg_replace("/\n\[code\]/ism", "[code]", $text);
+ $text = preg_replace("/\[\/code\]\n/ism", "[/code]", $text);
+
+ // when the content is meant exporting to other systems then remove the avatar picture since this doesn't really look good on these systems
+ if (!$try_oembed) {
+ $text = preg_replace("/\[share(.*?)avatar\s?=\s?'.*?'\s?(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism", "\n[share$1$2]$3[/share]", $text);
+ }
+
+ // Check for [code] text here, before the linefeeds are messed with.
+ // The highlighter will unescape and re-escape the content.
+ if (strpos($text, '[code=') !== false) {
+ $text = preg_replace_callback("/\[code=(.*?)\](.*?)\[\/code\]/ism", 'self::textHighlightCallback', $text);
+ }
+ // Convert new line chars to html tags
+
+ // nlbr seems to be hopelessly messed up
+ // $Text = nl2br($Text);
+
+ // We'll emulate it.
+
+ $text = trim($text);
+ $text = str_replace("\r\n", "\n", $text);
+
+ // removing multiplicated newlines
+ if (Config::get("system", "remove_multiplicated_lines")) {
+ $search = ["\n\n\n", "\n ", " \n", "[/quote]\n\n", "\n[/quote]", "[/li]\n", "\n[li]", "\n[ul]", "[/ul]\n", "\n\n[share ", "[/attachment]\n",
+ "\n[h1]", "[/h1]\n", "\n[h2]", "[/h2]\n", "\n[h3]", "[/h3]\n", "\n[h4]", "[/h4]\n", "\n[h5]", "[/h5]\n", "\n[h6]", "[/h6]\n"];
+ $replace = ["\n\n", "\n", "\n", "[/quote]\n", "[/quote]", "[/li]", "[li]", "[ul]", "[/ul]", "\n[share ", "[/attachment]",
+ "[h1]", "[/h1]", "[h2]", "[/h2]", "[h3]", "[/h3]", "[h4]", "[/h4]", "[h5]", "[/h5]", "[h6]", "[/h6]"];
+ do {
+ $oldtext = $text;
+ $text = str_replace($search, $replace, $text);
+ } while ($oldtext != $text);
+ }
+
+ // Set up the parameters for a URL search string
+ $URLSearchString = "^\[\]";
+ // Set up the parameters for a MAIL search string
+ $MAILSearchString = $URLSearchString;
+
+ // if the HTML is used to generate plain text, then don't do this search, but replace all URL of that kind to text
+ if (!$for_plaintext) {
+ // Autolink feature (thanks to http://code.seebz.net/p/autolink-php/)
+ // Currently disabled, since the function is too greedy
+ // $autolink_regex = "`([^\]\=\"']|^)(https?\://[^\s<]+[^\s<\.\)])`ism";
+ $autolink_regex = "/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism";
+ $text = preg_replace($autolink_regex, '$1[url]$2[/url]', $text);
+ if ($simple_html == 7) {
+ $text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism", 'self::convertUrlForMastodonCallback', $text);
+ $text = preg_replace_callback("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", 'self::convertUrlForMastodonCallback', $text);
+ }
+ } 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"], [' ', ' '], $text);
+
+ if ($preserve_nl) {
+ $text = str_replace(["\n", "\r"], ['', ''], $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",
+ '@$3',
+ $text);
+ } elseif ($simple_html == 7) {
+ $text = preg_replace("/([@!])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
+ '$1$3',
+ $text);
+ } elseif (!$simple_html) {
+ $text = preg_replace("/([@!])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
+ '$1$3',
+ $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
+ );
+
+ // 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$3', $text);
+
+ $text = preg_replace("/\[url\=([$URLSearchString]*)\]#(.*?)\[\/url\]/ism",
+ '#$2', $text);
+
+ $text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '$1', $text);
+ $text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$2', $text);
+ //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '$2', $Text);
+
+ // Red compatibility, though the link can't be authenticated on Friendica
+ $text = preg_replace("/\[zrl\=([$URLSearchString]*)\](.*?)\[\/zrl\]/ism", '$2', $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})/', 'acct:$1@$2', $text);
+
+ // Perform MAIL Search
+ $text = preg_replace("/\[mail\]([$MAILSearchString]*)\[\/mail\]/", '$1', $text);
+ $text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.*?)\[\/mail\]/", '$2', $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) {
+ // the extra space in the following line is intentional
+ // Whyyy? - @MrPetovan
+ return str_replace($match[0], '
' . Map::byLocation($match[1]) . '
', $match[0]);
+ },
+ $text
+ );
+ }
+ if (strpos($text, '[map=') !== false) {
+ $text = preg_replace_callback(
+ "/\[map=(.*?)\]/ism",
+ function ($match) {
+ // the extra space in the following line is intentional
+ // Whyyy? - @MrPetovan
+ return str_replace($match[0], '