]> 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 875674544cb174d299422039eca29107b28b40ee..ab7300da18b5c3e4f35b4bed9745374aee45bde5 100644 (file)
@@ -226,7 +226,7 @@ class BBCode
         * @param bool $keep_urls Whether to keep URLs in the resulting plaintext
         * @return string
         */
-       public static function toPlaintext(string $text, bool $keep_urls = true, bool $strip_tags = false): string
+       public static function toPlaintext(string $text, bool $keep_urls = true): string
        {
                DI::profiler()->startRecording('rendering');
                // Remove pictures in advance to avoid unneeded proxy calls
@@ -238,9 +238,6 @@ class BBCode
 
                $naked_text = HTML::toPlaintext(self::convert($text, false, BBCode::EXTERNAL, true), 0, !$keep_urls);
 
-               if ($strip_tags) {
-                       $naked_text = strip_tags($naked_text);
-               }
                DI::profiler()->stopRecording();
                return $naked_text;
        }
@@ -1161,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
         *
@@ -1658,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 {
@@ -1674,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 {