]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Text/BBCode.php
API: We now can post statuses via API
[friendica.git] / src / Content / Text / BBCode.php
index ea818d7f31c2511fd36d8f5f393d9f122bf137c9..32cd818cac690c29364802f70bc7df59dc9008d7 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -27,12 +27,12 @@ use Exception;
 use Friendica\Content\ContactSelector;
 use Friendica\Content\Item;
 use Friendica\Content\OEmbed;
+use Friendica\Content\PageInfo;
 use Friendica\Content\Smilies;
 use Friendica\Core\Hook;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
-use Friendica\Core\System;
 use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Event;
@@ -50,9 +50,10 @@ use Friendica\Util\XML;
 class BBCode
 {
        // Update this value to the current date whenever changes are made to BBCode::convert
-       const VERSION = '2020-12-18-video-embeds';
+       const VERSION = '2021-05-01';
 
        const INTERNAL = 0;
+       const EXTERNAL = 1;
        const API = 2;
        const DIASPORA = 3;
        const CONNECTORS = 4;
@@ -61,6 +62,8 @@ class BBCode
        const BACKLINK = 8;
        const ACTIVITYPUB = 9;
 
+       const TOP_ANCHOR = '<br class="top-anchor">';
+       const BOTTOM_ANCHOR = '<br class="button-anchor">';
        /**
         * Fetches attachment data that were generated the old way
         *
@@ -154,6 +157,8 @@ class BBCode
                        'after'         => '',
                        'image'         => null,
                        'url'           => '',
+                       'author_name'   => '',
+                       'author_url'    => '',
                        'provider_name' => '',
                        'provider_url'  => '',
                        'title'         => '',
@@ -168,103 +173,61 @@ class BBCode
 
                $data['text'] = trim($match[1]);
 
-               $type = '';
-               preg_match("/type='(.*?)'/ism", $attributes, $matches);
-               if (!empty($matches[1])) {
-                       $type = strtolower($matches[1]);
-               }
-
-               preg_match('/type="(.*?)"/ism', $attributes, $matches);
-               if (!empty($matches[1])) {
-                       $type = strtolower($matches[1]);
-               }
-
-               if ($type == '') {
-                       return [];
+               foreach (['type', 'url', 'title', 'image', 'preview', 'publisher_name', 'publisher_url', 'author_name', 'author_url'] as $field) {
+                       preg_match('/' . preg_quote($field, '/') . '=("|\')(.*?)\1/ism', $attributes, $matches);
+                       $value = $matches[2] ?? '';
+
+                       if ($value != '') {
+                               switch ($field) {
+                                       case 'publisher_name':
+                                               $data['provider_name'] = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
+                                               break;
+                                       case 'publisher_url':
+                                               $data['provider_url'] = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
+                                               break;
+                                       case 'author_name':
+                                               $data['author_name'] = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
+                                               if ($data['provider_name'] == $data['author_name']) {
+                                                       $data['author_name'] = '';
+                                               }
+                                               break;
+                                       case 'author_url':
+                                               $data['author_url'] = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
+                                               if ($data['provider_url'] == $data['author_url']) {
+                                                       $data['author_url'] = '';
+                                               }
+                                               break;
+                                       case 'title':
+                                               $value = self::convert(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), false, true);
+                                               $value = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
+                                               $value = str_replace(['[', ']'], ['&#91;', '&#93;'], $value);
+                                               $data['title'] = $value;
+                                       default:
+                                               $data[$field] = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
+                                               break;
+                               }
+                       }
                }
 
-               if (!in_array($type, ['link', 'audio', 'photo', 'video'])) {
+               if (!in_array($data['type'], ['link', 'audio', 'photo', 'video'])) {
                        return [];
                }
 
-               if ($type != '') {
-                       $data['type'] = $type;
-               }
-
-               $url = '';
-               preg_match("/url='(.*?)'/ism", $attributes, $matches);
-               if (!empty($matches[1])) {
-                       $url = $matches[1];
-               }
-
-               preg_match('/url="(.*?)"/ism', $attributes, $matches);
-               if (!empty($matches[1])) {
-                       $url = $matches[1];
-               }
-
-               if ($url != '') {
-                       $data['url'] = html_entity_decode($url, ENT_QUOTES, 'UTF-8');
-               }
-
-               $title = '';
-               preg_match("/title='(.*?)'/ism", $attributes, $matches);
-               if (!empty($matches[1])) {
-                       $title = $matches[1];
-               }
-
-               preg_match('/title="(.*?)"/ism', $attributes, $matches);
-               if (!empty($matches[1])) {
-                       $title = $matches[1];
-               }
-
-               if ($title != '') {
-                       $title = self::convert(html_entity_decode($title, ENT_QUOTES, 'UTF-8'), false, true);
-                       $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
-                       $title = str_replace(['[', ']'], ['&#91;', '&#93;'], $title);
-                       $data['title'] = $title;
-               }
-
-               $image = '';
-               preg_match("/image='(.*?)'/ism", $attributes, $matches);
-               if (!empty($matches[1])) {
-                       $image = $matches[1];
-               }
-
-               preg_match('/image="(.*?)"/ism', $attributes, $matches);
-               if (!empty($matches[1])) {
-                       $image = $matches[1];
-               }
-
-               if ($image != '') {
-                       $data['image'] = html_entity_decode($image, ENT_QUOTES, 'UTF-8');
-               }
-
-               $preview = '';
-               preg_match("/preview='(.*?)'/ism", $attributes, $matches);
-               if (!empty($matches[1])) {
-                       $preview = $matches[1];
-               }
-
-               preg_match('/preview="(.*?)"/ism', $attributes, $matches);
-               if (!empty($matches[1])) {
-                       $preview = $matches[1];
-               }
-
-               if ($preview != '') {
-                       $data['preview'] = html_entity_decode($preview, ENT_QUOTES, 'UTF-8');
-               }
-
                $data['description'] = trim($match[3]);
 
                $data['after'] = trim($match[4]);
 
                $parts = parse_url($data['url']);
                if (!empty($parts['scheme']) && !empty($parts['host'])) {
-                       $data['provider_name'] = $parts['host'];
-                       $data['provider_url'] = $parts['scheme'] . '://' . $parts['host'];
+                       if (empty($data['provider_name'])) {
+                               $data['provider_name'] = $parts['host'];
+                       }
+                       if (empty($data['provider_url']) || empty(parse_url($data['provider_url'], PHP_URL_SCHEME))) {
+                               $data['provider_url'] = $parts['scheme'] . '://' . $parts['host'];
 
-                       if (!empty($parts['port'])) {
-                               $data['provider_url'] .= ':' . $parts['port'];
+                               if (!empty($parts['port'])) {
+                                       $data['provider_url'] .= ':' . $parts['port'];
+                               }
                        }
                }
 
@@ -292,6 +255,8 @@ class BBCode
                        foreach ($pictures as $picture) {
                                if (Photo::isLocal($picture[1])) {
                                        $post['images'][] = ['url' => str_replace('-1.', '-0.', $picture[1]), 'description' => $picture[2]];
+                               } else {
+                                       $post['remote_images'][] = ['url' => $picture[1], 'description' => $picture[2]];
                                }
                        }
                        if (!empty($post['images']) && !empty($post['images'][0]['description'])) {
@@ -303,6 +268,8 @@ class BBCode
                        foreach ($pictures as $picture) {
                                if (Photo::isLocal($picture[1])) {
                                        $post['images'][] = ['url' => str_replace('-1.', '-0.', $picture[1]), 'description' => ''];
+                               } else {
+                                       $post['remote_images'][] = ['url' => $picture[1], 'description' => ''];
                                }
                        }
                }
@@ -322,14 +289,14 @@ class BBCode
                                                $data = ['url' => $url, 'type' => 'photo'];
                                        } else {
                                                // Checking, if the link goes to a picture
-                                               $data = ParseUrl::getSiteinfoCached($pictures[0][1], true);
+                                               $data = ParseUrl::getSiteinfoCached($pictures[0][1]);
                                        }
 
                                        // Workaround:
                                        // Sometimes photo posts to the own album are not detected at the start.
                                        // So we seem to cannot use the cache for these cases. That's strange.
                                        if (($data['type'] != 'photo') && strstr($pictures[0][1], "/photos/")) {
-                                               $data = ParseUrl::getSiteinfo($pictures[0][1], true);
+                                               $data = ParseUrl::getSiteinfo($pictures[0][1]);
                                        }
 
                                        if ($data['type'] == 'photo') {
@@ -363,19 +330,17 @@ class BBCode
                                        }
                                }
                        } elseif (preg_match_all("(\[img\](.*?)\[\/img\])ism", $body, $pictures, PREG_SET_ORDER)) {
-                               if ((count($pictures) == 1) && !$has_title) {
-                                       $post['type'] = 'photo';
-                                       $post['image'] = $pictures[0][1];
-                                       $post['text'] = str_replace($pictures[0][0], '', $body);
-                               } elseif (count($pictures) > 0) {
+                               if ($has_title) {
                                        $post['type'] = 'link';
                                        $post['url'] = $plink;
-                                       $post['image'] = $pictures[0][1];
-                                       $post['text'] = $body;
+                               } else {
+                                       $post['type'] = 'photo';
+                               }
 
-                                       foreach ($pictures as $picture) {
-                                               $post['text'] = trim(str_replace($picture[0], '', $post['text']));
-                                       }
+                               $post['image'] = $pictures[0][1];
+                               $post['text'] = $body;
+                               foreach ($pictures as $picture) {
+                                       $post['text'] = trim(str_replace($picture[0], '', $post['text']));
                                }
                        }
 
@@ -415,8 +380,17 @@ class BBCode
                                $post['type'] = "text";
                                $post['text'] = trim($body);
                        }
+
+                       if (($post['type'] == 'photo') && empty($post['images']) && !empty($post['remote_images'])) {
+                               $post['images'] = $post['remote_images'];
+                               $post['image'] = $post['images'][0]['url'];
+                               if (!empty($post['images']) && !empty($post['images'][0]['description'])) {
+                                       $post['image_description'] = $post['images'][0]['description'];
+                               }
+                       }
+                       unset($post['remote_images']);
                } elseif (isset($post['url']) && ($post['type'] == 'video')) {
-                       $data = ParseUrl::getSiteinfoCached($post['url'], true);
+                       $data = ParseUrl::getSiteinfoCached($post['url']);
 
                        if (isset($data['images'][0])) {
                                $post['image'] = $data['images'][0]['src'];
@@ -436,7 +410,7 @@ class BBCode
         */
        public static function removeAttachment($body, $no_link_desc = false)
        {
-               return preg_replace_callback("/\s*\[attachment (.*)\](.*?)\[\/attachment\]\s*/ism",
+               return preg_replace_callback("/\s*\[attachment (.*?)\](.*?)\[\/attachment\]\s*/ism",
                        function ($match) use ($no_link_desc) {
                                $attach_data = self::getAttachmentData($match[0]);
                                if (empty($attach_data['url'])) {
@@ -493,7 +467,7 @@ class BBCode
                $c = preg_match_all('/\[img.*?\](.*?)\[\/img\]/ism', $s, $matches, PREG_SET_ORDER);
                if ($c) {
                        foreach ($matches as $mtch) {
-                               Logger::log('scale_external_image: ' . $mtch[1]);
+                               Logger::info('scale_external_image', ['image' => $mtch[1]]);
 
                                $hostname = str_replace('www.', '', substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3));
                                if (stristr($mtch[1], $hostname)) {
@@ -633,16 +607,19 @@ class BBCode
         * @param string  $text
         * @param integer $simplehtml
         * @param bool    $tryoembed
+        * @param array   $data
         * @return string
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private static function convertAttachment($text, $simplehtml = self::INTERNAL, $tryoembed = true)
+       public static function convertAttachment($text, $simplehtml = self::INTERNAL, $tryoembed = true, array $data = [])
        {
-               $data = self::getAttachmentData($text);
+               $data = $data ?: self::getAttachmentData($text);
                if (empty($data) || empty($data['url'])) {
                        return $text;
                }
 
+               $stamp1 = microtime(true);
+
                if (isset($data['title'])) {
                        $data['title'] = strip_tags($data['title']);
                        $data['title'] = str_replace(['http://', 'https://'], '', $data['title']);
@@ -683,13 +660,16 @@ class BBCode
                        }
 
                        if (!empty($data['description']) && $data['description'] != $data['title']) {
-                               // Sanitize the HTML by converting it to BBCode
-                               $bbcode = HTML::toBBCode($data['description']);
-                               $return .= sprintf('<blockquote>%s</blockquote>', trim(self::convert($bbcode)));
+                               // Sanitize the HTML
+                               $return .= sprintf('<blockquote>%s</blockquote>', trim(HTML::purify($data['description'])));
                        }
 
-                       if (!empty($data['url'])) {
-                               $return .= sprintf('<sup><a href="%s">%s</a></sup>', $data['url'], parse_url($data['url'], PHP_URL_HOST));
+                       if (!empty($data['provider_url']) && !empty($data['provider_name'])) {
+                               if (!empty($data['author_name'])) {
+                                       $return .= sprintf('<sup><a href="%s">%s (%s)</a></sup>', $data['provider_url'], $data['author_name'], $data['provider_name']);
+                               } else {
+                                       $return .= sprintf('<sup><a href="%s">%s</a></sup>', $data['provider_url'], $data['provider_name']);
+                               }
                        }
 
                        if ($simplehtml != self::CONNECTORS) {
@@ -697,6 +677,7 @@ class BBCode
                        }
                }
 
+               DI::profiler()->saveTimestamp($stamp1, 'rendering');
                return trim(($data['text'] ?? '') . ' ' . $return . ' ' . ($data['after'] ?? ''));
        }
 
@@ -965,6 +946,26 @@ class BBCode
                return $newbody;
        }
 
+       /**
+        *
+        * @param  string   $text     A BBCode string
+        * @return array share attributes
+        */
+       public static function fetchShareAttributes($text)
+       {
+               $attributes = [];
+               if (!preg_match("/(.*?)\[share(.*?)\](.*)\[\/share\]/ism", $text, $matches)) {
+                       return $attributes;
+               }
+
+               $attribute_string = $matches[2];
+               foreach (['author', 'profile', 'avatar', 'link', 'posted', 'guid'] as $field) {
+                       preg_match("/$field=(['\"])(.+?)\\1/ism", $attribute_string, $matches);
+                       $attributes[$field] = html_entity_decode($matches[2] ?? '', ENT_QUOTES, 'UTF-8');
+               }
+               return $attributes;
+       }
+
        /**
         * This function converts a [share] block to text according to a provided callback function whose signature is:
         *
@@ -1085,12 +1086,12 @@ class BBCode
                                        '$avatar'       => $attributes['avatar'],
                                        '$author'       => $attributes['author'],
                                        '$link'         => $attributes['link'],
-                                       '$link_title'   => DI::l10n()->t('link to source'),
+                                       '$link_title'   => DI::l10n()->t('Link to source'),
                                        '$posted'       => $attributes['posted'],
                                        '$guid'         => $attributes['guid'],
                                        '$network_name' => ContactSelector::networkToName($network, $attributes['profile']),
                                        '$network_icon' => ContactSelector::networkToIcon($network, $attributes['profile']),
-                                       '$content'      => self::setMentions(trim($content), 0, $network),
+                                       '$content'      => self::TOP_ANCHOR . self::setMentions(trim($content), 0, $network) . self::BOTTOM_ANCHOR,
                                ]);
                                break;
                }
@@ -1104,26 +1105,24 @@ class BBCode
                $text = DI::cache()->get($cache_key);
 
                if (is_null($text)) {
-                       $a = DI::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, DI::httpRequest()->getUserAgent());
-                       @curl_exec($ch);
-                       $curl_info = @curl_getinfo($ch);
-
-                       DI::profiler()->saveTimestamp($stamp1, "network");
+                       $curlResult = DI::httpRequest()->head($match[1], ['timeout' => DI::config()->get('system', 'xrd_timeout')]);
+                       if ($curlResult->isSuccess()) {
+                               $mimetype = $curlResult->getHeader('Content-Type');
+                       } else {
+                               $mimetype = '';
+                       }
 
-                       if (substr($curl_info['content_type'], 0, 6) == 'image/') {
+                       if (substr($mimetype, 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 = DI::httpRequest()->fetch($match[1]);
+                               if (empty($body)) {
+                                       DI::cache()->set($cache_key, $text);
+                                       return $text;
+                               }
 
                                $doc = new DOMDocument();
                                @$doc->loadHTML($body);
@@ -1160,8 +1159,6 @@ class BBCode
 
        private static function cleanPictureLinksCallback($match)
        {
-               $a = DI::app();
-
                // When the picture link is the own photo path then we can avoid fetching the link
                $own_photo_url = preg_quote(Strings::normaliseLink(DI::baseUrl()->get()) . '/photos/');
                if (preg_match('|' . $own_photo_url . '.*?/image/|', Strings::normaliseLink($match[1]))) {
@@ -1179,20 +1176,15 @@ class BBCode
                        return $text;
                }
 
-               // Only fetch the header, not the content
-               $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, DI::httpRequest()->getUserAgent());
-               @curl_exec($ch);
-               $curl_info = @curl_getinfo($ch);
-
-               DI::profiler()->saveTimestamp($stamp1, "network");
+               $curlResult = DI::httpRequest()->head($match[1], ['timeout' => DI::config()->get('system', 'xrd_timeout')]);
+               if ($curlResult->isSuccess()) {
+                       $mimetype = $curlResult->getHeader('Content-Type');
+               } else {
+                       $mimetype = '';
+               }
 
                // if its a link to a picture then embed this picture
-               if (substr($curl_info['content_type'], 0, 6) == 'image/') {
+               if (substr($mimetype, 0, 6) == 'image/') {
                        $text = '[img]' . $match[1] . '[/img]';
                } else {
                        if (!empty($match[3])) {
@@ -1203,6 +1195,10 @@ class BBCode
 
                        // if its not a picture then look if its a page that contains a picture link
                        $body = DI::httpRequest()->fetch($match[1]);
+                       if (empty($body)) {
+                               DI::cache()->set($cache_key, $text);
+                               return $text;
+                       }
 
                        $doc = new DOMDocument();
                        @$doc->loadHTML($body);
@@ -1350,10 +1346,14 @@ class BBCode
                                $search = ["\n[th]", "[th]\n", " [th]", "\n[/th]", "[/th]\n", "[/th] ",
                                        "\n[td]", "[td]\n", " [td]", "\n[/td]", "[/td]\n", "[/td] ",
                                        "\n[tr]", "[tr]\n", " [tr]", "[tr] ", "\n[/tr]", "[/tr]\n", " [/tr]", "[/tr] ",
+                                       "\n[hr]", "[hr]\n", " [hr]", "[hr] ",
+                                       "\n[attachment ", " [attachment ", "\n[/attachment]", "[/attachment]\n", " [/attachment]", "[/attachment] ",
                                        "[table]\n", "[table] ", " [table]", "\n[/table]", " [/table]", "[/table] "];
                                $replace = ["[th]", "[th]", "[th]", "[/th]", "[/th]", "[/th]",
                                        "[td]", "[td]", "[td]", "[/td]", "[/td]", "[/td]",
                                        "[tr]", "[tr]", "[tr]", "[tr]", "[/tr]", "[/tr]", "[/tr]", "[/tr]",
+                                       "[hr]", "[hr]", "[hr]", "[hr]",
+                                       "[attachment ", "[attachment ", "[/attachment]", "[/attachment]", "[/attachment]", "[/attachment]",
                                        "[table]", "[table]", "[table]", "[/table]", "[/table]", "[/table]"];
                                do {
                                        $oldtext = $text;
@@ -1387,12 +1387,22 @@ class BBCode
                                // Handle attached links or videos
                                if ($simple_html == self::ACTIVITYPUB) {
                                        $text = self::removeAttachment($text);
-                               } elseif (!in_array($simple_html, [self::INTERNAL, self::CONNECTORS])) {
+                               } elseif (!in_array($simple_html, [self::INTERNAL, self::EXTERNAL, self::CONNECTORS])) {
                                        $text = self::removeAttachment($text, true);
                                } else {
                                        $text = self::convertAttachment($text, $simple_html, $try_oembed);
                                }
 
+                               $nosmile = strpos($text, '[nosmile]') !== false;
+                               $text = str_replace('[nosmile]', '', $text);
+
+                               // Replace non graphical smilies for external posts
+                               if (!$nosmile) {
+                                       $text = self::performWithEscapedTags($text, ['img'], function ($text) use ($simple_html, $for_plaintext) {
+                                               return Smilies::replace($text, ($simple_html != self::INTERNAL) || $for_plaintext);
+                                       });
+                               }
+
                                // leave open the posibility of [map=something]
                                // this is replaced in Item::prepareBody() which has knowledge of the item location
                                if (strpos($text, '[/map]') !== false) {
@@ -1506,11 +1516,6 @@ class BBCode
                                        });
                                }
 
-                               // This is actually executed in Item::prepareBody()
-
-                               $nosmile = strpos($text, '[nosmile]') !== false;
-                               $text = str_replace('[nosmile]', '', $text);
-
                                // Check for font change text
                                $text = preg_replace("/\[font=(.*?)\](.*?)\[\/font\]/sm", "<span style=\"font-family: $1;\">$2</span>", $text);
 
@@ -1605,18 +1610,19 @@ class BBCode
                                //$Text = preg_replace("/\[crypt=(.*?)\](.*?)\[\/crypt\]/ism", '<br><img src="' .DI::baseUrl() . '/images/lock_icon.gif" alt="' . DI::l10n()->t('Encrypted content') . '" title="' . '$1' . ' ' . DI::l10n()->t('Encrypted content') . '" /><br>', $Text);
 
                                // Simplify "video" element
-                               $text = preg_replace('(\[video.*?\ssrc\s?=\s?([^\s\]]+).*?\].*?\[/video\])ism', '[video]$1[/video]', $text);
+                               $text = preg_replace('(\[video[^\]]*?\ssrc\s?=\s?([^\s\]]+)[^\]]*?\].*?\[/video\])ism', '[video]$1[/video]', $text);
 
                                if ($try_oembed) {
                                        // html5 video and audio
                                        $text = preg_replace("/\[video\](.*?\.(ogg|ogv|oga|ogm|webm|mp4).*?)\[\/video\]/ism",
-                                               '<video src="$1" controls width="' . $a->videowidth . '" height="' . $a->videoheight . '" loop="true"><a href="$1">$1</a></video>', $text);
-                                       $text = preg_replace("/\[video\](.*?)\[\/video\]/ism",
-                                               '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>', $text);
-                                       $text = preg_replace("/\[audio\](.*?)\[\/audio\]/ism", '<audio src="$1" controls><a href="$1">$1</a></audio>', $text);
+                                               '<video src="$1" controls width="100%" height="auto"><a href="$1">$1</a></video>', $text);
 
                                        $text = preg_replace_callback("/\[video\](.*?)\[\/video\]/ism", $try_oembed_callback, $text);
                                        $text = preg_replace_callback("/\[audio\](.*?)\[\/audio\]/ism", $try_oembed_callback, $text);
+
+                                       $text = preg_replace("/\[video\](.*?)\[\/video\]/ism",
+                                               '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>', $text);
+                                       $text = preg_replace("/\[audio\](.*?)\[\/audio\]/ism", '<audio src="$1" controls><a href="$1">$1</a></audio>', $text);
                                } else {
                                        $text = preg_replace("/\[video\](.*?)\[\/video\]/ism",
                                                '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>', $text);
@@ -1683,13 +1689,6 @@ class BBCode
                                        $text = preg_replace("/\[event\-id\](.*?)\[\/event\-id\]/ism", '', $text);
                                }
 
-                               // Replace non graphical smilies for external posts
-                               if (!$nosmile && !$for_plaintext) {
-                                       $text = self::performWithEscapedTags($text, ['img'], function ($text) {
-                                               return Smilies::replace($text);
-                                       });
-                               }
-
                                if (!$for_plaintext && DI::config()->get('system', 'big_emojis') && ($simple_html != self::DIASPORA)) {
                                        $conv = html_entity_decode(str_replace([' ', "\n", "\r"], '', $text));
                                        // Emojis are always 4 byte Unicode characters
@@ -1722,7 +1721,7 @@ class BBCode
                                                $text);
                                } elseif (!$simple_html) {
                                        $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
-                                               '$1<a href="$2" class="userinfo mention" title="$3">$3</a>',
+                                               '$1<a href="$2" class="userinfo mention" title="$3"><bdi>$3</bdi></a>',
                                                $text);
                                }
 
@@ -1876,28 +1875,25 @@ class BBCode
                        $text
                );
 
-               $config = \HTMLPurifier_HTML5Config::createDefault();
-               $config->set('HTML.Doctype', 'HTML5');
-               $config->set('HTML.SafeIframe', true);
-               $config->set('URI.SafeIframeRegexp', '%^(?:
-                       https://www.youtube.com/embed/
-                       |
-                       https://player.vimeo.com/video/
-                       |
-                       ' . DI::baseUrl() . '/oembed/ # Has to change with the source in Content\Oembed::iframe
-               )%xi');
-               $config->set('Attr.AllowedRel', [
-                       'noreferrer' => true,
-                       'noopener' => true,
-               ]);
-               $config->set('Attr.AllowedFrameTargets', [
-                       '_blank' => true,
-               ]);
-
-               $HTMLPurifier = new \HTMLPurifier($config);
-               $text = $HTMLPurifier->purify($text);
+               // Default iframe allowed domains/path
+               $allowedIframeDomains = [
+                       DI::baseUrl()->getHostname()
+                       . (DI::baseUrl()->getUrlPath() ? '/' . DI::baseUrl()->getUrlPath() : '')
+                       . '/oembed/', # The path part has to change with the source in Content\Oembed::iframe
+                       'www.youtube.com/embed/',
+                       'player.vimeo.com/video/',
+               ];
 
-               return $text;
+               $allowedIframeDomains = array_merge(
+                       $allowedIframeDomains,
+                       DI::config()->get('system', 'allowed_oembed') ?
+                               explode(',', DI::config()->get('system', 'allowed_oembed'))
+                               : []
+               );
+
+               $text = HTML::purify($text, $allowedIframeDomains);
+
+               return trim($text);
        }
 
        /**
@@ -2068,7 +2064,7 @@ class BBCode
        {
                $ret = [];
 
-               BBCode::performWithEscapedTags($string, ['noparse', 'pre', 'code'], function ($string) use (&$ret) {
+               BBCode::performWithEscapedTags($string, ['noparse', 'pre', 'code', 'img'], function ($string) use (&$ret) {
                        // Convert hashtag links to hashtags
                        $string = preg_replace('/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism', '#$2 ', $string);
 
@@ -2106,11 +2102,6 @@ class BBCode
                                                continue;
                                        }
 
-                                       // ignore strictly numeric tags like #1
-                                       if ((strpos($match, '#') === 0) && ctype_digit(substr($match, 1))) {
-                                               continue;
-                                       }
-
                                        // try not to catch url fragments
                                        if (strpos($string, $match) && preg_match('/[a-zA-z0-9\/]/', substr($string, strpos($string, $match) - 1, 1))) {
                                                continue;
@@ -2124,6 +2115,32 @@ class BBCode
                return array_unique($ret);
        }
 
+       /**
+        * Expand tags to URLs
+        *
+        * @param string $body 
+        * @return string body with expanded tags 
+        */
+       public static function expandTags(string $body)
+       {
+               return preg_replace_callback("/([!#@])([^\^ \x0D\x0A,;:?\']*[^\^ \x0D\x0A,;:?!\'.])/",
+                       function ($match) {
+                               switch ($match[1]) {
+                                       case '!':
+                                       case '@':
+                                               $contact = Contact::getByURL($match[2]);
+                                               if (!empty($contact)) {
+                                                       return $match[1] . '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
+                                               } else {
+                                                       return $match[1] . $match[2];
+                                               }
+                                               break;
+                                       case '#':
+                                               return $match[1] . '[url=' . 'https://' . DI::baseUrl() . '/search?tag=' . $match[2] . ']' . $match[2] . '[/url]';
+                               }
+                       }, $body);
+       }
+
        /**
         * Perform a custom function on a text after having escaped blocks enclosed in the provided tag list.
         *
@@ -2216,4 +2233,75 @@ class BBCode
 
                return $header;
        }
+
+       /**
+        * Returns the BBCode relevant to embed the provided URL in a post body.
+        * For media type, it will return [img], [video] and [audio] tags.
+        * For regular web pages, it will either output a [bookmark] tag if title and description were provided,
+        * an [attachment] tag or a simple [url] tag depending on $tryAttachment.
+        *
+        * @param string      $url
+        * @param bool        $tryAttachment
+        * @param string|null $title
+        * @param string|null $description
+        * @param string|null $tags
+        * @return string
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        *@see ParseUrl::getSiteinfoCached
+        *
+        */
+       public static function embedURL(string $url, bool $tryAttachment = true, string $title = null, string $description = null, string $tags = null): string
+       {
+               DI::logger()->info($url);
+
+               // If there is already some content information submitted we don't
+               // need to parse the url for content.
+               if (!empty($title) && !empty($description)) {
+                       $title = str_replace(["\r", "\n"], ['', ''], $title);
+
+                       $description = '[quote]' . trim($description) . '[/quote]' . "\n";
+
+                       $str_tags = '';
+                       if (!empty($tags)) {
+                               $arr_tags = ParseUrl::convertTagsToArray($tags);
+                               if (count($arr_tags)) {
+                                       $str_tags = "\n" . implode(' ', $arr_tags) . "\n";
+                               }
+                       }
+
+                       $result = sprintf('[bookmark=%s]%s[/bookmark]%s', $url, ($title) ? $title : $url, $description) . $str_tags;
+
+                       DI::logger()->info('(unparsed): returns: ' . $result);
+
+                       return $result;
+               }
+
+               $siteinfo = ParseUrl::getSiteinfoCached($url);
+
+               if (in_array($siteinfo['type'], ['image', 'video', 'audio'])) {
+                       switch ($siteinfo['type']) {
+                               case 'video':
+                                       $bbcode = "\n" . '[video]' . $url . '[/video]' . "\n";
+                                       break;
+                               case 'audio':
+                                       $bbcode = "\n" . '[audio]' . $url . '[/audio]' . "\n";
+                                       break;
+                               default:
+                                       $bbcode = "\n" . '[img]' . $url . '[/img]' . "\n";
+                                       break;
+                       }
+
+                       return $bbcode;
+               }
+
+               unset($siteinfo['keywords']);
+
+               // Bypass attachment if parse url for a comment
+               if (!$tryAttachment) {
+                       return "\n" . '[url=' . $url . ']' . $siteinfo['title'] . '[/url]';
+               }
+
+               // Format it as BBCode attachment
+               return "\n" . PageInfo::getFooterFromData($siteinfo);
+       }
 }