]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Text/BBCode.php
Add a couple of cases to DateTimeFormat::fix()
[friendica.git] / src / Content / Text / BBCode.php
index 2c4226dc1272c43e271690fb8b22f980f19f8442..43d4127ea99c51bf136283b22657681dd08c3535 100644 (file)
@@ -287,14 +287,17 @@ class BBCode
                        }
                }
 
-               // if nothing is found, it maybe having an image.
                if (!isset($post['type'])) {
-                       // Simplify image codes
-                       $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
-                       $body = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $body);
                        $post['text'] = $body;
+               }
+
+               // Simplify image codes
+               $post['text'] = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $post['text']);
+               $post['text'] = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $post['text']);
 
-                       if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) {
+               // if nothing is found, it maybe having an image.
+               if (!isset($post['type'])) {
+                       if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]#ism", $post['text'], $pictures, PREG_SET_ORDER)) {
                                if ((count($pictures) == 1) && !$has_title) {
                                        if (!empty($item['object-type']) && ($item['object-type'] == Activity\ObjectType::IMAGE)) {
                                                // Replace the preview picture with the real picture
@@ -322,14 +325,14 @@ class BBCode
                                                }
 
                                                $post['preview'] = $pictures[0][2];
-                                               $post['text'] = trim(str_replace($pictures[0][0], '', $body));
+                                               $post['text'] = trim(str_replace($pictures[0][0], '', $post['text']));
                                        } else {
                                                $imgdata = Images::getInfoFromURLCached($pictures[0][1]);
                                                if (($imgdata) && substr($imgdata['mime'], 0, 6) == 'image/') {
                                                        $post['type'] = 'photo';
                                                        $post['image'] = $pictures[0][1];
                                                        $post['preview'] = $pictures[0][2];
-                                                       $post['text'] = trim(str_replace($pictures[0][0], '', $body));
+                                                       $post['text'] = trim(str_replace($pictures[0][0], '', $post['text']));
                                                }
                                        }
                                } elseif (count($pictures) > 0) {
@@ -341,13 +344,12 @@ class BBCode
                                        }
 
                                        $post['image'] = $pictures[0][2];
-                                       $post['text'] = $body;
 
                                        foreach ($pictures as $picture) {
                                                $post['text'] = trim(str_replace($picture[0], '', $post['text']));
                                        }
                                }
-                       } elseif (preg_match_all("(\[img\](.*?)\[\/img\])ism", $body, $pictures, PREG_SET_ORDER)) {
+                       } elseif (preg_match_all("(\[img\](.*?)\[\/img\])ism", $post['text'], $pictures, PREG_SET_ORDER)) {
                                if ($has_title) {
                                        $post['type'] = 'link';
                                        $post['url'] = $plink;
@@ -356,7 +358,6 @@ class BBCode
                                }
 
                                $post['image'] = $pictures[0][1];
-                               $post['text'] = $body;
                                foreach ($pictures as $picture) {
                                        $post['text'] = trim(str_replace($picture[0], '', $post['text']));
                                }
@@ -396,7 +397,6 @@ class BBCode
 
                        if (!isset($post['type'])) {
                                $post['type'] = 'text';
-                               $post['text'] = trim($body);
                        }
 
                        if (($post['type'] == 'photo') && empty($post['images']) && !empty($post['remote_images'])) {
@@ -413,6 +413,10 @@ class BBCode
                        if (isset($data['images'][0])) {
                                $post['image'] = $data['images'][0]['src'];
                        }
+               } elseif (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]#ism", $post['text'], $pictures, PREG_SET_ORDER)) {
+                       foreach ($pictures as $picture) {
+                               $post['text'] = trim(str_replace($picture[0], '', $post['text']));
+                       }
                }
 
                DI::profiler()->stopRecording();
@@ -424,7 +428,6 @@ class BBCode
         *
         * @param string  $body
         * @param boolean $no_link_desc No link description
-        *
         * @return string with replaced body
         */
        public static function removeAttachment(string $body, bool $no_link_desc = false): string
@@ -449,7 +452,6 @@ class BBCode
         *
         * @param string $text
         * @param bool $keep_urls Whether to keep URLs in the resulting plaintext
-        *
         * @return string
         */
        public static function toPlaintext(string $text, bool $keep_urls = true): string
@@ -500,7 +502,7 @@ class BBCode
                $c = preg_match_all('/\[img.*?\](.*?)\[\/img\]/ism', $s, $matches, PREG_SET_ORDER);
                if ($c) {
                        foreach ($matches as $mtch) {
-                               Logger::info('scale_external_image', ['image' => $mtch[1]]);
+                               Logger::debug('scale_external_image', ['image' => $mtch[1]]);
 
                                $hostname = str_replace('www.', '', substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3));
                                if (stristr($mtch[1], $hostname)) {
@@ -528,14 +530,14 @@ class BBCode
                                                        $Image->scaleDown(640);
                                                        $new_width = $Image->getWidth();
                                                        $new_height = $Image->getHeight();
-                                                       Logger::info('External images scaled', ['orig_width' => $orig_width, 'new_width' => $new_width, 'orig_height' => $orig_height, 'new_height' => $new_height, 'match' => $mtch[0]]);
+                                                       Logger::debug('External images scaled', ['orig_width' => $orig_width, 'new_width' => $new_width, 'orig_height' => $orig_height, 'new_height' => $new_height, 'match' => $mtch[0]]);
                                                        $s = str_replace(
                                                                $mtch[0],
                                                                '[img=' . $new_width . 'x' . $new_height. ']' . $mtch[1] . '[/img]'
                                                                . "\n",
                                                                $s
                                                        );
-                                                       Logger::info('New string', ['image' => $s]);
+                                                       Logger::debug('New string', ['image' => $s]);
                                                }
                                        }
                                }
@@ -585,7 +587,7 @@ class BBCode
 
                                        if (($textlen + $img_start) > $maxlen) {
                                                if ($textlen < $maxlen) {
-                                                       Logger::info('the limit happens before an embedded image');
+                                                       Logger::debug('the limit happens before an embedded image');
                                                        $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
                                                        $textlen = $maxlen;
                                                }
@@ -599,7 +601,7 @@ class BBCode
 
                                        if (($textlen + $img_end) > $maxlen) {
                                                if ($textlen < $maxlen) {
-                                                       Logger::info('the limit happens before the end of a non-embedded image');
+                                                       Logger::debug('the limit happens before the end of a non-embedded image');
                                                        $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
                                                        $textlen = $maxlen;
                                                }
@@ -622,11 +624,11 @@ class BBCode
 
                        if (($textlen + strlen($orig_body)) > $maxlen) {
                                if ($textlen < $maxlen) {
-                                       Logger::info('the limit happens after the end of the last image');
+                                       Logger::debug('the limit happens after the end of the last image');
                                        $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
                                }
                        } else {
-                               Logger::info('the text size with embedded images extracted did not violate the limit');
+                               Logger::debug('the text size with embedded images extracted did not violate the limit');
                                $new_body = $new_body . $orig_body;
                        }
 
@@ -664,10 +666,10 @@ class BBCode
                        $data['title'] = strip_tags($data['title']);
                        $data['title'] = str_replace(['http://', 'https://'], '', $data['title']);
                } else {
-                       $data['title'] = null;
+                       $data['title'] = '';
                }
 
-               if (((strpos($data['text'], "[img=") !== false) || (strpos($data['text'], "[img]") !== false) || DI::config()->get('system', 'always_show_preview')) && !empty($data['image'])) {
+               if (((strpos($data['text'], '[img=') !== false) || (strpos($data['text'], '[img]') !== false) || DI::config()->get('system', 'always_show_preview')) && !empty($data['image'])) {
                        $data['preview'] = $data['image'];
                        $data['image'] = '';
                }
@@ -1007,18 +1009,32 @@ class BBCode
        /**
         * @param string $text A BBCode string
         * @return array Empty array if no share tag is present or the following array, missing attributes end up empty strings:
-        *               - comment: Text before the opening share tag
-        *               - shared : Text inside the share tags
-        *               - author : (Optional) Display name of the shared author
-        *               - profile: (Optional) Profile page URL of the shared author
-        *               - avatar : (Optional) Profile picture URL of the shared author
-        *               - link   : (Optional) Canonical URL of the shared post
-        *               - posted : (Optional) Date the shared post was initially posted ("Y-m-d H:i:s" in GMT)
-        *               - guid   : (Optional) Shared post GUID if any
+        *               - comment   : Text before the opening share tag
+        *               - shared    : Text inside the share tags
+        *               - author    : (Optional) Display name of the shared author
+        *               - profile   : (Optional) Profile page URL of the shared author
+        *               - avatar    : (Optional) Profile picture URL of the shared author
+        *               - link      : (Optional) Canonical URL of the shared post
+        *               - posted    : (Optional) Date the shared post was initially posted ("Y-m-d H:i:s" in GMT)
+        *               - message_id: (Optional) Shared post URI if any
+        *               - guid      : (Optional) Shared post GUID if any
         */
        public static function fetchShareAttributes(string $text): array
        {
                DI::profiler()->startRecording('rendering');
+               if (preg_match('~(.*?)\[share](.*)\[/share]~ism', $text, $matches)) {
+                       return [
+                               'author'     => '',
+                               'profile'    => '',
+                               'avatar'     => '',
+                               'link'       => '',
+                               'posted'     => '',
+                               'guid'       => '',
+                               'message_id' => trim($matches[2]),
+                               'comment'    => trim($matches[1]),
+                               'shared'     => '',
+                       ];
+               }
                // See Issue https://github.com/friendica/friendica/issues/10454
                // Hashtags in usernames are expanded to links. This here is a quick fix.
                $text = preg_replace('~([@!#])\[url=.*?](.*?)\[/url]~ism', '$1$2', $text);
@@ -1045,7 +1061,7 @@ class BBCode
        private static function extractShareAttributes(string $shareString): array
        {
                $attributes = [];
-               foreach (['author', 'profile', 'avatar', 'link', 'posted', 'guid'] as $field) {
+               foreach (['author', 'profile', 'avatar', 'link', 'posted', 'guid', 'message_id'] as $field) {
                        preg_match("/$field=(['\"])(.+?)\\1/ism", $shareString, $matches);
                        $attributes[$field] = html_entity_decode($matches[2] ?? '', ENT_QUOTES, 'UTF-8');
                }
@@ -1513,10 +1529,10 @@ class BBCode
                                 * $match[1] = $url
                                 * $match[2] = $title or absent
                                 */
-                               $try_oembed_callback = function ($match)
+                               $try_oembed_callback = function (array $match)
                                {
                                        $url = $match[1];
-                                       $title = $match[2] ?? null;
+                                       $title = $match[2] ?? '';
 
                                        try {
                                                $return = OEmbed::getHTML($url, $title);
@@ -1869,11 +1885,13 @@ class BBCode
                                if ($try_oembed) {
                                        $text = preg_replace_callback("/\[youtube\](https?:\/\/www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", $try_oembed_callback, $text);
                                        $text = preg_replace_callback("/\[youtube\](www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", $try_oembed_callback, $text);
+                                       $text = preg_replace_callback("/\[youtube\](https?:\/\/www.youtube.com\/shorts\/.*?)\[\/youtube\]/ism", $try_oembed_callback, $text);
                                        $text = preg_replace_callback("/\[youtube\](https?:\/\/youtu.be\/.*?)\[\/youtube\]/ism", $try_oembed_callback, $text);
                                }
 
                                $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);
 
                                if ($try_oembed) {
@@ -2454,10 +2472,11 @@ class BBCode
         * @param string      $link    Post source URL
         * @param string      $posted  Post created date
         * @param string|null $guid    Post guid (if any)
+        * @param string|null $uri     Post uri (if any)
         * @return string
         * @TODO Rewrite to handle over whole record array
         */
-       public static function getShareOpeningTag(string $author, string $profile, string $avatar, string $link, string $posted, string $guid = null): string
+       public static function getShareOpeningTag(string $author, string $profile, string $avatar, string $link, string $posted, string $guid = null, string $uri = null): string
        {
                DI::profiler()->startRecording('rendering');
                $header = "[share author='" . str_replace(["'", "[", "]"], ["&#x27;", "&#x5B;", "&#x5D;"], $author) .
@@ -2470,6 +2489,10 @@ class BBCode
                        $header .= "' guid='" . str_replace(["'", "[", "]"], ["&#x27;", "&#x5B;", "&#x5D;"], $guid);
                }
 
+               if ($uri) {
+                       $header .= "' message_id='" . str_replace(["'", "[", "]"], ["&#x27;", "&#x5B;", "&#x5D;"], $uri);
+               }
+
                $header  .= "']";
 
                DI::profiler()->stopRecording();