]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Text/BBCode.php
Avoid local network communication / invalid url requests
[friendica.git] / src / Content / Text / BBCode.php
index ae21d05e13bdfa8ed134410cbfbc8a229863de98..83056b2692eb07d37d338b6725b34876818d41d4 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -273,8 +273,8 @@ class BBCode
                // Get all linked images with alternative image description
                if (preg_match_all("/\[img=(http[^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
                        foreach ($pictures as $picture) {
-                               if (Photo::isLocal($picture[1])) {
-                                       $post['images'][] = ['url' => str_replace('-1.', '-0.', $picture[1]), 'description' => $picture[2]];
+                               if ($id = Photo::getIdForName($picture[1])) {
+                                       $post['images'][] = ['url' => str_replace('-1.', '-0.', $picture[1]), 'description' => $picture[2], 'id' => $id];
                                } else {
                                        $post['remote_images'][] = ['url' => $picture[1], 'description' => $picture[2]];
                                }
@@ -286,8 +286,8 @@ class BBCode
 
                if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
                        foreach ($pictures as $picture) {
-                               if (Photo::isLocal($picture[1])) {
-                                       $post['images'][] = ['url' => str_replace('-1.', '-0.', $picture[1]), 'description' => ''];
+                               if ($id = Photo::getIdForName($picture[1])) {
+                                       $post['images'][] = ['url' => str_replace('-1.', '-0.', $picture[1]), 'description' => '', 'id' => $id];
                                } else {
                                        $post['remote_images'][] = ['url' => $picture[1], 'description' => ''];
                                }
@@ -305,7 +305,7 @@ class BBCode
                // 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 ((count($pictures) == 1) && !$has_title && !Photo::isLocal($pictures[0][2])) {
                                        if (!empty($item['object-type']) && ($item['object-type'] == Activity\ObjectType::IMAGE)) {
                                                // Replace the preview picture with the real picture
                                                $url = str_replace('-1.', '-0.', $pictures[0][2]);
@@ -489,72 +489,6 @@ class BBCode
                }
        }
 
-       /**
-        * This function changing the visual size (not the real size) of images.
-        * The function does not work for pictures with an alternate text description.
-        * This could only be changed by using some new "img" BBCode format.
-        *
-        * @param string $srctext The body with images
-        * @return string The body with possibly scaled images
-        */
-       public static function scaleExternalImages(string $srctext): string
-       {
-               DI::profiler()->startRecording('rendering');
-               $s = $srctext;
-
-               // Simplify image links
-               $s = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $s);
-
-               $matches = null;
-               $c = preg_match_all('/\[img.*?\](.*?)\[\/img\]/ism', $s, $matches, PREG_SET_ORDER);
-               if ($c) {
-                       foreach ($matches as $mtch) {
-                               Logger::debug('scale_external_image', ['image' => $mtch[1]]);
-
-                               $hostname = str_replace('www.', '', substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3));
-                               if (stristr($mtch[1], $hostname)) {
-                                       continue;
-                               }
-
-                               $curlResult = DI::httpClient()->get($mtch[1], HttpClientAccept::IMAGE);
-                               if (!$curlResult->isSuccess()) {
-                                       continue;
-                               }
-
-                               Logger::debug('Got picture', ['Content-Type' => $curlResult->getHeader('Content-Type'), 'url' => $mtch[1]]);
-
-                               $i = $curlResult->getBody();
-                               $type = $curlResult->getContentType();
-                               $type = Images::getMimeTypeByData($i, $mtch[1], $type);
-
-                               if ($i) {
-                                       $Image = new Image($i, $type);
-                                       if ($Image->isValid()) {
-                                               $orig_width = $Image->getWidth();
-                                               $orig_height = $Image->getHeight();
-
-                                               if ($orig_width > 640 || $orig_height > 640) {
-                                                       $Image->scaleDown(640);
-                                                       $new_width = $Image->getWidth();
-                                                       $new_height = $Image->getHeight();
-                                                       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::debug('New string', ['image' => $s]);
-                                               }
-                                       }
-                               }
-                       }
-               }
-
-               DI::profiler()->stopRecording();
-               return $s;
-       }
-
        /**
         * Truncates imported message body string length to max_import_size
         *
@@ -1415,8 +1349,8 @@ class BBCode
        public static function cleanPictureLinks(string $text): string
        {
                DI::profiler()->startRecording('rendering');
-               $return = preg_replace_callback("&\[url=([^\[\]]*)\]\[img=(.*)\](.*)\[\/img\]\[\/url\]&Usi", 'self::cleanPictureLinksCallback', $text);
-               $return = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'self::cleanPictureLinksCallback', $return);
+               $return = preg_replace_callback("&\[url=([^\[\]]*)\]\[img=(.*)\](.*)\[\/img\]\[\/url\]&Usi", [self::class, 'cleanPictureLinksCallback'], $text);
+               $return = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", [self::class, 'cleanPictureLinksCallback'], $return);
                DI::profiler()->stopRecording();
                return $return;
        }
@@ -1450,7 +1384,7 @@ class BBCode
        {
                DI::profiler()->startRecording('rendering');
                $regexp = "/([@!])\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
-               $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
+               $body = preg_replace_callback($regexp, [self::class, 'mentionCallback'], $body);
                DI::profiler()->stopRecording();
                return $body;
        }
@@ -2002,12 +1936,12 @@ class BBCode
 
                                if (!$for_plaintext) {
                                        if (in_array($simple_html, [self::OSTATUS, self::MASTODON_API, self::TWITTER_API, self::ACTIVITYPUB])) {
-                                               $text = preg_replace_callback("/\[url\](.*?)\[\/url\]/ism", 'self::convertUrlForActivityPubCallback', $text);
-                                               $text = preg_replace_callback("/\[url\=(.*?)\](.*?)\[\/url\]/ism", 'self::convertUrlForActivityPubCallback', $text);
+                                               $text = preg_replace_callback("/\[url\](.*?)\[\/url\]/ism", [self::class, 'convertUrlForActivityPubCallback'], $text);
+                                               $text = preg_replace_callback("/\[url\=(.*?)\](.*?)\[\/url\]/ism", [self::class, 'convertUrlForActivityPubCallback'], $text);
                                        }
                                } else {
                                        $text = preg_replace("(\[url\](.*?)\[\/url\])ism", " $1 ", $text);
-                                       $text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'self::removePictureLinksCallback', $text);
+                                       $text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", [self::class, 'removePictureLinksCallback'], $text);
                                }
 
                                // Bookmarks in red - will be converted to bookmarks in friendica
@@ -2017,7 +1951,7 @@ class BBCode
                                                        "[bookmark=$1]$2[/bookmark]", $text);
 
                                if (in_array($simple_html, [self::OSTATUS, self::TWITTER])) {
-                                       $text = preg_replace_callback("/([^#@!])\[url\=([^\]]*)\](.*?)\[\/url\]/ism", "self::expandLinksCallback", $text);
+                                       $text = preg_replace_callback("/([^#@!])\[url\=([^\]]*)\](.*?)\[\/url\]/ism", [self::class, 'expandLinksCallback'], $text);
                                        //$text = preg_replace("/[^#@!]\[url\=([^\]]*)\](.*?)\[\/url\]/ism", ' $2 [url]$1[/url]', $text);
                                        $text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", ' $2 [url]$1[/url]', $text);
                                }
@@ -2327,7 +2261,7 @@ class BBCode
                        $url_search_string = "^\[\]";
                        $text = preg_replace_callback(
                                "/([@!])\[(.*?)\]\(([$url_search_string]*?)\)/ism",
-                               ['self', 'bbCodeMention2DiasporaCallback'],
+                               [self::class, 'bbCodeMention2DiasporaCallback'],
                                $text
                        );
                }
@@ -2355,7 +2289,7 @@ class BBCode
                DI::profiler()->startRecording('rendering');
                $ret = [];
 
-               self::performWithEscapedTags($string, ['noparse', 'pre', 'code', 'img'], function ($string) use (&$ret) {
+               self::performWithEscapedTags($string, ['noparse', 'pre', 'code', 'img', 'attachment'], function ($string) use (&$ret) {
                        // Convert hashtag links to hashtags
                        $string = preg_replace('/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism', '#$2 ', $string);
 
@@ -2599,7 +2533,7 @@ class BBCode
                // Bypass attachment if parse url for a comment
                if (!$tryAttachment) {
                        DI::profiler()->stopRecording();
-                       return "\n" . '[url=' . $url . ']' . $siteinfo['title'] . '[/url]';
+                       return "\n" . '[url=' . $url . ']' . ($siteinfo['title'] ?? $url) . '[/url]';
                }
 
                // Format it as BBCode attachment