]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Text/BBCode.php
Tests added
[friendica.git] / src / Content / Text / BBCode.php
index 33f91b43d11ed2b3fd31400eca4fcb19e27c9ae3..a38d2de7856ec39f7c24c38c36d7a5af6c92eee2 100644 (file)
@@ -36,14 +36,10 @@ use Friendica\Core\Renderer;
 use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Event;
-use Friendica\Model\Photo;
 use Friendica\Model\Post;
 use Friendica\Model\Tag;
 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
-use Friendica\Object\Image;
-use Friendica\Protocol\Activity;
-use Friendica\Util\Images;
 use Friendica\Util\Map;
 use Friendica\Util\ParseUrl;
 use Friendica\Util\Proxy;
@@ -1316,7 +1312,8 @@ class BBCode
                                        "\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] "
+                                       "[table]\n", "[table] ", " [table]", "\n[/table]", " [/table]", "[/table] ",
+                                       " \n", "\t\n", "[/li]\n", "\n[li]", "\n[*]", 
                                ];
                                $replace = [
                                        "[th]", "[th]", "[th]", "[/th]", "[/th]", "[/th]",
@@ -1324,7 +1321,8 @@ class BBCode
                                        "[tr]", "[tr]", "[tr]", "[tr]", "[/tr]", "[/tr]", "[/tr]", "[/tr]",
                                        "[hr]", "[hr]", "[hr]", "[hr]",
                                        "[attachment ", "[attachment ", "[/attachment]", "[/attachment]", "[/attachment]", "[/attachment]",
-                                       "[table]", "[table]", "[table]", "[/table]", "[/table]", "[/table]"
+                                       "[table]", "[table]", "[table]", "[/table]", "[/table]", "[/table]",
+                                       "\n", "\n", "[/li]", "[li]", "[*]",
                                ];
                                do {
                                        $oldtext = $text;
@@ -1342,11 +1340,11 @@ class BBCode
                                // removing multiplicated newlines
                                if (DI::config()->get('system', 'remove_multiplicated_lines')) {
                                        $search = [
-                                               "\n\n\n", "\n ", " \n", "[/quote]\n\n", "\n[/quote]", "[/li]\n", "\n[li]", "\n[*]", "\n[ul]", "[/ul]\n", "\n\n[share ", "[/attachment]\n",
+                                               "\n\n\n", "[/quote]\n\n", "\n[/quote]", "\n[ul]", "[/ul]\n", "\n[ol]", "[/ol]\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]",
+                                               "\n\n", "[/quote]\n", "[/quote]", "[ul]", "[/ul]", "[ol]", "[/ol]", "\n[share ", "[/attachment]",
                                                "[h1]", "[/h1]", "[h2]", "[/h2]", "[h3]", "[/h3]", "[h4]", "[/h4]", "[h5]", "[/h5]", "[h6]", "[/h6]"
                                        ];
                                        do {
@@ -1402,12 +1400,12 @@ class BBCode
                                }
 
                                // Check for headers
-                               $text = preg_replace("(\[h1\](.*?)\[\/h1\])ism", '<h1>$1</h1>', $text);
-                               $text = preg_replace("(\[h2\](.*?)\[\/h2\])ism", '<h2>$1</h2>', $text);
-                               $text = preg_replace("(\[h3\](.*?)\[\/h3\])ism", '<h3>$1</h3>', $text);
-                               $text = preg_replace("(\[h4\](.*?)\[\/h4\])ism", '<h4>$1</h4>', $text);
-                               $text = preg_replace("(\[h5\](.*?)\[\/h5\])ism", '<h5>$1</h5>', $text);
-                               $text = preg_replace("(\[h6\](.*?)\[\/h6\])ism", '<h6>$1</h6>', $text);
+                               $text = preg_replace("(\[h1\](.*?)\[\/h1\])ism", '</p><h1>$1</h1><p>', $text);
+                               $text = preg_replace("(\[h2\](.*?)\[\/h2\])ism", '</p><h2>$1</h2><p>', $text);
+                               $text = preg_replace("(\[h3\](.*?)\[\/h3\])ism", '</p><h3>$1</h3><p>', $text);
+                               $text = preg_replace("(\[h4\](.*?)\[\/h4\])ism", '</p><h4>$1</h4><p>', $text);
+                               $text = preg_replace("(\[h5\](.*?)\[\/h5\])ism", '</p><h5>$1</h5><p>', $text);
+                               $text = preg_replace("(\[h6\](.*?)\[\/h6\])ism", '</p><h6>$1</h6><p>', $text);
 
                                // Check for paragraph
                                $text = preg_replace("(\[p\](.*?)\[\/p\])ism", '<p>$1</p>', $text);
@@ -1460,6 +1458,7 @@ class BBCode
                                // @deprecated since 2021.12, left for backward-compatibility reasons
                                $text = preg_replace("(\[class=(.*?)\](.*?)\[\/class\])ism", '<span class="$1">$2</span>', $text);
                                // Add HTML new lines
+                               $text = str_replace("\n\n", '</p><p>', $text);
                                $text = str_replace("\n", '<br>', $text);
 
                                // handle nested lists
@@ -1469,27 +1468,27 @@ class BBCode
                                        ((strpos($text, "[/ol]") !== false) && (strpos($text, "[ol]") !== false)) ||
                                        ((strpos($text, "[/ul]") !== false) && (strpos($text, "[ul]") !== false)) ||
                                        ((strpos($text, "[/li]") !== false) && (strpos($text, "[li]") !== false))) && (++$endlessloop < 20)) {
-                                       $text = preg_replace("/\[list\](.*?)\[\/list\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $text);
-                                       $text = preg_replace("/\[list=\](.*?)\[\/list\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>', $text);
-                                       $text = preg_replace("/\[list=1\](.*?)\[\/list\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $text);
-                                       $text = preg_replace("/\[list=((?-i)i)\](.*?)\[\/list\]/ism", '<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>', $text);
-                                       $text = preg_replace("/\[list=((?-i)I)\](.*?)\[\/list\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>', $text);
-                                       $text = preg_replace("/\[list=((?-i)a)\](.*?)\[\/list\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>', $text);
-                                       $text = preg_replace("/\[list=((?-i)A)\](.*?)\[\/list\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>', $text);
-                                       $text = preg_replace("/\[ul\](.*?)\[\/ul\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $text);
-                                       $text = preg_replace("/\[ol\](.*?)\[\/ol\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $text);
+                                       $text = preg_replace("/\[list\](.*?)\[\/list\]/ism", '</p><ul class="listbullet" style="list-style-type: circle;">$1</ul><p>', $text);
+                                       $text = preg_replace("/\[list=\](.*?)\[\/list\]/ism", '</p><ul class="listnone" style="list-style-type: none;">$1</ul><p>', $text);
+                                       $text = preg_replace("/\[list=1\](.*?)\[\/list\]/ism", '</p><ul class="listdecimal" style="list-style-type: decimal;">$1</ul><p>', $text);
+                                       $text = preg_replace("/\[list=((?-i)i)\](.*?)\[\/list\]/ism", '</p><ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul><p>', $text);
+                                       $text = preg_replace("/\[list=((?-i)I)\](.*?)\[\/list\]/ism", '</p><ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul><p>', $text);
+                                       $text = preg_replace("/\[list=((?-i)a)\](.*?)\[\/list\]/ism", '</p><ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul><p>', $text);
+                                       $text = preg_replace("/\[list=((?-i)A)\](.*?)\[\/list\]/ism", '</p><ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul><p>', $text);
+                                       $text = preg_replace("/\[ul\](.*?)\[\/ul\]/ism", '</p><ul>$1</ul><p>', $text);
+                                       $text = preg_replace("/\[ol\](.*?)\[\/ol\]/ism", '</p><ol>$1</ol><p>', $text);
                                        $text = preg_replace("/\[li\](.*?)\[\/li\]/ism", '<li>$1</li>', $text);
                                }
 
                                $text = preg_replace("/\[th\](.*?)\[\/th\]/sm", '<th>$1</th>', $text);
                                $text = preg_replace("/\[td\](.*?)\[\/td\]/sm", '<td>$1</td>', $text);
                                $text = preg_replace("/\[tr\](.*?)\[\/tr\]/sm", '<tr>$1</tr>', $text);
-                               $text = preg_replace("/\[table\](.*?)\[\/table\]/sm", '<table>$1</table>', $text);
+                               $text = preg_replace("/\[table\](.*?)\[\/table\]/sm", '</p><table>$1</table><p>', $text);
 
-                               $text = preg_replace("/\[table border=1\](.*?)\[\/table\]/sm", '<table border="1" >$1</table>', $text);
-                               $text = preg_replace("/\[table border=0\](.*?)\[\/table\]/sm", '<table border="0" >$1</table>', $text);
+                               $text = preg_replace("/\[table border=1\](.*?)\[\/table\]/sm", '</p><table border="1" >$1</table><p>', $text);
+                               $text = preg_replace("/\[table border=0\](.*?)\[\/table\]/sm", '</p><table border="0" >$1</table><p>', $text);
 
-                               $text = str_replace('[hr]', '<hr />', $text);
+                               $text = str_replace('[hr]', '</p><hr /><p>', $text);
 
                                if (!$for_plaintext) {
                                        $text = self::performWithEscapedTags($text, ['url', 'img', 'audio', 'video', 'youtube', 'vimeo', 'share', 'attachment', 'iframe', 'bookmark'], function ($text) {
@@ -1523,7 +1522,7 @@ class BBCode
                                }
 
                                // Declare the format for [quote] layout
-                               $QuoteLayout = '<blockquote>$1</blockquote>';
+                               $QuoteLayout = '</p><blockquote>$1</blockquote><p>';
 
                                // Check for [quote] text
                                // handle nested quotes
@@ -1565,11 +1564,16 @@ class BBCode
                                $text = preg_replace("/\[zmg\=([0-9]*)x([0-9]*)\](.*?)\[\/zmg\]/ism", '<img class="zrl" src="$3" style="width: $1px;" >', $text);
 
                                $text = preg_replace_callback(
-                                       "/\[img\=(.*?)\](.*?)\[\/img\]/ism",
+                                       "/\[[iz]mg\=(.*?)\](.*?)\[\/[iz]mg\]/ism",
                                        function ($matches) use ($simple_html, $uriid) {
                                                $matches[1] = self::proxyUrl($matches[1], $simple_html, $uriid);
-                                               $matches[2] = htmlspecialchars($matches[2], ENT_COMPAT);
-                                               return '<img src="' . $matches[1] . '" alt="' . $matches[2] . '" title="' . $matches[2] . '">';
+                                               $alt = htmlspecialchars($matches[2], ENT_COMPAT);
+                                               // Fix for Markdown problems with Diaspora, see issue #12701
+                                               if (($simple_html != self::DIASPORA) || strpos($matches[2], '"') === false) {
+                                                       return '<img src="' . $matches[1] . '" alt="' . $alt . '" title="' . $alt . '">';
+                                               } else {
+                                                       return '<img src="' . $matches[1] . '" alt="' . $alt . '">';
+                                               }
                                        },
                                        $text
                                );
@@ -1577,7 +1581,7 @@ class BBCode
                                // Images
                                // [img]pathtoimage[/img]
                                $text = preg_replace_callback(
-                                       "/\[img\](.*?)\[\/img\]/ism",
+                                       "/\[[iz]mg\](.*?)\[\/[iz]mg\]/ism",
                                        function ($matches) use ($simple_html, $uriid) {
                                                if (strpos($matches[1], "data:image/") === 0) {
                                                        return $matches[0];
@@ -1938,6 +1942,10 @@ class BBCode
                                : []
                );
 
+               if (strpos($text, '<p>') !== false || strpos($text, '</p>') !== false) {
+                       $text = '<p>' . $text . '</p>';
+               }
+
                $text = HTML::purify($text, $allowedIframeDomains);
                DI::profiler()->stopRecording();