]> 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 3635ba874cf7ee5253a68db4e2147182e0998dee..ab7300da18b5c3e4f35b4bed9745374aee45bde5 100644 (file)
@@ -142,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(['[', ']'], ['[', ']'], $value);
                                                $data['title'] = $value;
@@ -236,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;
@@ -1158,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
         *
@@ -1655,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 {
@@ -1671,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 {
@@ -2065,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)) {
@@ -2079,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