]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Text/BBCode.php
Merge pull request #13291 from Quix0r/features/config-blocklist-published
[friendica.git] / src / Content / Text / BBCode.php
index e321b13e70378c25c5431c1df97f6bbf21b6b430..ab7300da18b5c3e4f35b4bed9745374aee45bde5 100644 (file)
@@ -64,7 +64,8 @@ class BBCode
        const ACTIVITYPUB  = 9;
        const BLUESKY      = 10;
 
-       const TOP_ANCHOR = '<br class="top-anchor">';
+       const SHARED_ANCHOR = '<hr class="shared-anchor">';
+       const TOP_ANCHOR    = '<br class="top-anchor">';
        const BOTTOM_ANCHOR = '<br class="button-anchor">';
 
        const PREVIEW_NONE     = 0;
@@ -141,7 +142,7 @@ class BBCode
                                                break;
 
                                        case 'title':
-                                               $value = self::convert(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), false, true);
+                                               $value = self::toPlaintext(html_entity_decode($value, ENT_QUOTES, 'UTF-8'));
                                                $value = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
                                                $value = str_replace(['[', ']'], ['&#91;', '&#93;'], $value);
                                                $data['title'] = $value;
@@ -235,7 +236,7 @@ class BBCode
                // Remove attachment
                $text = self::replaceAttachment($text);
 
-               $naked_text = HTML::toPlaintext(self::convert($text, false, 0, true), 0, !$keep_urls);
+               $naked_text = HTML::toPlaintext(self::convert($text, false, BBCode::EXTERNAL, true), 0, !$keep_urls);
 
                DI::profiler()->stopRecording();
                return $naked_text;
@@ -930,7 +931,7 @@ class BBCode
                                $network = $contact['network'] ?? Protocol::PHANTOM;
 
                                $tpl = Renderer::getMarkupTemplate('shared_content.tpl');
-                               $text .= Renderer::replaceMacros($tpl, [
+                               $text .= BBCode::SHARED_ANCHOR . Renderer::replaceMacros($tpl, [
                                        '$profile'      => $attributes['profile'],
                                        '$avatar'       => $attributes['avatar'],
                                        '$author'       => $attributes['author'],
@@ -1157,6 +1158,40 @@ class BBCode
                return $match[1] . '[url=' . $data['url'] . ']' . $data['nick'] . '[/url]';
        }
 
+       /**
+        * Normalize links to Youtube and Vimeo to a unified format.
+        *
+        * @param string $text
+        * @return string
+        */
+       private static function normalizeVideoLinks(string $text): string
+       {
+               $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?:\/\/www.youtube.com\/shorts\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
+               $text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $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);
+
+               return $text;
+       }
+
+       /**
+        * Expand Youtube and Vimeo links to 
+        *
+        * @param string $text
+        * @return string
+        */
+       public static function expandVideoLinks(string $text): string
+       {
+               $text = self::normalizeVideoLinks($text);
+               $text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", '[url=https://www.youtube.com/watch?v=$1]https://www.youtube.com/watch?v=$1[/url]', $text);
+               $text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", '[url=https://vimeo.com/$1]https://vimeo.com/$1[/url]', $text);
+
+               return $text;
+       }
+
        /**
         * Converts a BBCode message for a given URI-ID to a HTML message
         *
@@ -1654,12 +1689,9 @@ class BBCode
                                // Backward compatibility, [iframe] support has been removed in version 2020.12
                                $text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<a href="$1">$1</a>', $text);
 
-                               // Youtube extensions
-                               $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?:\/\/www.youtube.com\/shorts\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
-                               $text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
+                               $text = self::normalizeVideoLinks($text);
 
+                               // Youtube extensions
                                if ($try_oembed) {
                                        $text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", '<iframe width="' . $a->getThemeInfoValue('videowidth') . '" height="' . $a->getThemeInfoValue('videoheight') . '" src="https://www.youtube.com/embed/$1" frameborder="0" ></iframe>', $text);
                                } else {
@@ -1670,9 +1702,7 @@ class BBCode
                                        );
                                }
 
-                               $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);
-
+                               // Vimeo extensions
                                if ($try_oembed) {
                                        $text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", '<iframe width="' . $a->getThemeInfoValue('videowidth') . '" height="' . $a->getThemeInfoValue('videoheight') . '" src="https://player.vimeo.com/video/$1" frameborder="0" ></iframe>', $text);
                                } else {
@@ -2064,7 +2094,7 @@ class BBCode
 
                // Convert it to HTML - don't try oembed
                if ($for_diaspora) {
-                       $text = self::convert($text, false, self::DIASPORA);
+                       $text = self::convertForUriId(0, $text, self::DIASPORA);
 
                        // Add all tags that maybe were removed
                        if (preg_match_all("/#\[url\=([$url_search_string]*)\](.*?)\[\/url\]/ism", $original_text, $tags)) {
@@ -2078,7 +2108,7 @@ class BBCode
                                $text = $text . ' ' . $tagline;
                        }
                } else {
-                       $text = self::convert($text, false, self::CONNECTORS);
+                       $text = self::convertForUriId(0, $text, self::CONNECTORS);
                }
 
                // If a link is followed by a quote then there should be a newline before it