]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Text/BBCode.php
Fix: Events on Diaspora now should look fine
[friendica.git] / src / Content / Text / BBCode.php
index 8dd9305ca316f263f98503304ef59492b868eaed..da98a7ff87fddb6c0a0cdf0c47bad2cb98b394d5 100644 (file)
@@ -25,6 +25,7 @@ use Friendica\Util\ParseUrl;
 
 require_once "include/event.php";
 require_once "include/html2plain.php";
+require_once "include/html2bbcode.php";
 require_once "mod/proxy.php";
 
 class BBCode
@@ -705,9 +706,10 @@ class BBCode
                                }
 
                                if ($data["description"] != "" && $data["description"] != $data["title"]) {
-                                       $return .= sprintf('<blockquote>%s</blockquote>', trim(BBCode::convert($data["description"])));
+                                       // Sanitize the HTML by converting it to BBCode
+                                       $bbcode = html2bbcode($data["description"]);
+                                       $return .= sprintf('<blockquote>%s</blockquote>', trim(self::convert($bbcode)));
                                }
-
                                if ($data["type"] == "link") {
                                        $return .= sprintf('<sup><a href="%s">%s</a></sup>', $data['url'], parse_url($data['url'], PHP_URL_HOST));
                                }
@@ -754,7 +756,7 @@ class BBCode
                if (($data["url"] != "") && ($data["title"] != "")) {
                        $text .= "\n[url=" . $data["url"] . "]" . $data["title"] . "[/url]";
                } elseif (($data["url"] != "")) {
-                       $text .= "\n" . $data["url"];
+                       $text .= "\n[url]" . $data["url"] . "[/url]";
                }
 
                return $text . "\n" . $data["after"];
@@ -831,6 +833,48 @@ class BBCode
                }
        }
 
+       /**
+        * @brief Shortens [url] BBCodes in a format that looks less ugly than the full address. (callback function)
+        * @param array $match Array with the matching values
+        * @return string reformatted link including HTML codes
+        */
+       private static function shortenVisibleUrlCallback($match)
+       {
+               $url = $match[1];
+
+               if (isset($match[2]) && ($match[1] != $match[2])) {
+                       return $match[0];
+               }
+
+               $parts = parse_url($url);
+               if (!isset($parts['scheme'])) {
+                       return $match[0];
+               }
+
+               return self::shortenVisibleUrl($url);
+       }
+
+       /**
+        * @brief Shortens [url] BBCodes in a format that looks less ugly than the full address.
+        * @param string $url URL that is about to be reformatted
+        * @return string reformatted link including HTML codes
+        */
+       private static function shortenVisibleUrl($url)
+       {
+               $parts = parse_url($url);
+               $scheme = $parts['scheme'] . '://';
+               $styled_url = str_replace($scheme, '', $url);
+
+//             Currently deactivated, due to preview problems inside of Diaspora
+//             if (strlen($styled_url) > 30) {
+//                     $styled_url = substr($styled_url, 0, 30) . "…";
+//             }
+
+               $html = '<a href="%s" target="_blank">%s</a>';
+
+               return sprintf($html, $url, $styled_url);
+       }
+
        /*
         * [noparse][i]italic[/i][/noparse] turns into
         * [noparse][ i ]italic[ /i ][/noparse],
@@ -1123,13 +1167,13 @@ class BBCode
                                }
 
                                if (stripos(normalise_link($link), 'http://twitter.com/') === 0) {
+                                       $text .= '<br /><a href="' . $link . '">' . $link . '</a>';
+                               } else {
                                        $text .= $headline . '<blockquote>' . trim($share[3]) . "</blockquote><br />";
 
                                        if ($link != "") {
                                                $text .= '<br /><a href="' . $link . '">[l]</a>';
                                        }
-                               } else {
-                                       $text .= '<br /><a href="' . $link . '">' . $link . '</a>';
                                }
 
                                break;
@@ -1459,6 +1503,9 @@ class BBCode
                        if ($simple_html == 7) {
                                $text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism", 'self::convertUrlForMastodonCallback', $text);
                                $text = preg_replace_callback("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", 'self::convertUrlForMastodonCallback', $text);
+                       } else {
+                               $text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism", 'self::shortenVisibleUrlCallback', $text);
+                               $text = preg_replace_callback("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", 'self::shortenVisibleUrlCallback', $text);
                        }
                } else {
                        $text = preg_replace("(\[url\]([$URLSearchString]*)\[\/url\])ism", " $1 ", $text);
@@ -1558,10 +1605,8 @@ class BBCode
                if (strpos($text, '[/map]') !== false) {
                        $text = preg_replace_callback(
                                "/\[map\](.*?)\[\/map\]/ism",
-                               function ($match) {
-                                       // the extra space in the following line is intentional
-                                       // Whyyy? - @MrPetovan
-                                       return str_replace($match[0], '<div class="map"  >' . Map::byLocation($match[1]) . '</div>', $match[0]);
+                               function ($match) use ($simple_html) {
+                                       return str_replace($match[0], '<p class="map">' . Map::byLocation($match[1], $simple_html) . '</p>', $match[0]);
                                },
                                $text
                        );
@@ -1569,16 +1614,14 @@ class BBCode
                if (strpos($text, '[map=') !== false) {
                        $text = preg_replace_callback(
                                "/\[map=(.*?)\]/ism",
-                               function ($match) {
-                                       // the extra space in the following line is intentional
-                                       // Whyyy? - @MrPetovan
-                                       return str_replace($match[0], '<div class="map"  >' . Map::byCoordinates(str_replace('/', ' ', $match[1])) . '</div>', $match[0]);
+                               function ($match) use ($simple_html) {
+                                       return str_replace($match[0], '<p class="map">' . Map::byCoordinates(str_replace('/', ' ', $match[1]), $simple_html) . '</p>', $match[0]);
                                },
                                $text
                        );
                }
                if (strpos($text, '[map]') !== false) {
-                       $text = preg_replace("/\[map\]/", '<div class="map"></div>', $text);
+                       $text = preg_replace("/\[map\]/", '<p class="map"></p>', $text);
                }
 
                // Check for headers