]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Text/BBCode.php
Blanks replaced
[friendica.git] / src / Content / Text / BBCode.php
index 1f638c2f52353bd0e7e342945d79bfc75cdb09e0..4f7ff1f2411f9a78945627df8402470e72ed71ec 100644 (file)
@@ -39,6 +39,7 @@ 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;
@@ -501,11 +502,13 @@ class BBCode
                                        continue;
                                }
 
-                               $curlResult = DI::httpClient()->get($mtch[1]);
+                               $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);
@@ -1096,7 +1099,7 @@ class BBCode
                                        $attributes[$field] = html_entity_decode($matches[2] ?? '', ENT_QUOTES, 'UTF-8');
                                }
 
-                               $img_str = '<img src="' . 
+                               $img_str = '<img src="' .
                                self::proxyUrl($match[2], $simplehtml, $uriid) . '"';
                                foreach ($attributes as $key => $value) {
                                        if (!empty($value)) {
@@ -1135,7 +1138,7 @@ class BBCode
                        case self::API:
                                $text = ($is_quote_share? '<br>' : '') .
                                '<b><a href="' . $attributes['link'] . '">' . html_entity_decode('&#x2672;', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . "</a>:</b><br>\n" .
-                               '<blockquote class="shared_content">' . $content . '</blockquote>';
+                               '<blockquote class="shared_content" dir="auto">' . $content . '</blockquote>';
                                break;
                        case self::DIASPORA:
                                if (stripos(Strings::normaliseLink($attributes['link']), 'http://twitter.com/') === 0) {
@@ -1160,7 +1163,7 @@ class BBCode
                                $headline .= DI::l10n()->t('<a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a> %3$s', $attributes['link'], $mention, $attributes['posted']);
                                $headline .= ':</b></p>' . "\n";
 
-                               $text = ($is_quote_share? '<hr />' : '') . $headline . '<blockquote class="shared_content">' . trim($content) . '</blockquote>' . "\n";
+                               $text = ($is_quote_share? '<hr />' : '') . $headline . '<blockquote class="shared_content" dir="auto">' . trim($content) . '</blockquote>' . "\n";
 
                                break;
                        case self::OSTATUS:
@@ -1214,7 +1217,7 @@ class BBCode
                                $text = "[url=" . $match[2] . ']' . $match[2] . "[/url]";
 
                                // if its not a picture then look if its a page that contains a picture link
-                               $body = DI::httpClient()->fetch($match[1]);
+                               $body = DI::httpClient()->fetch($match[1], HttpClientAccept::HTML, 0);
                                if (empty($body)) {
                                        DI::cache()->set($cache_key, $text);
                                        return $text;
@@ -1290,7 +1293,7 @@ class BBCode
                        }
 
                        // if its not a picture then look if its a page that contains a picture link
-                       $body = DI::httpClient()->fetch($match[1]);
+                       $body = DI::httpClient()->fetch($match[1], HttpClientAccept::HTML, 0);
                        if (empty($body)) {
                                DI::cache()->set($cache_key, $text);
                                return $text;
@@ -1780,7 +1783,7 @@ class BBCode
 
                                $text = preg_replace("/\[img\](.*?)\[\/img\]/ism", '<img src="$1" alt="' . DI::l10n()->t('Image/photo') . '" />', $text);
                                $text = preg_replace("/\[zmg\](.*?)\[\/zmg\]/ism", '<img src="$1" alt="' . DI::l10n()->t('Image/photo') . '" />', $text);
-       
+
                                $text = self::convertImages($text, $simple_html, $uriid);
 
                                $text = preg_replace("/\[crypt\](.*?)\[\/crypt\]/ism", '<br><img src="' .DI::baseUrl() . '/images/lock_icon.gif" alt="' . DI::l10n()->t('Encrypted content') . '" title="' . DI::l10n()->t('Encrypted content') . '" /><br>', $text);
@@ -1894,7 +1897,7 @@ class BBCode
                                } else {
                                        $text = preg_replace("/([#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", '$1$3', $text);
                                }
-                               
+
                                if (!$for_plaintext) {
                                        if (in_array($simple_html, [self::OSTATUS, self::API, self::ACTIVITYPUB])) {
                                                $text = preg_replace_callback("/\[url\](.*?)\[\/url\]/ism", 'self::convertUrlForActivityPubCallback', $text);
@@ -2083,11 +2086,15 @@ class BBCode
         * @param string $text The text with BBCode
         * @return string The same text - but without "abstract" element
         */
-       public static function stripAbstract($text)
+       public static function stripAbstract(string $text): string
        {
                DI::profiler()->startRecording('rendering');
-               $text = preg_replace("/[\s|\n]*\[abstract\].*?\[\/abstract\][\s|\n]*/ism", '', $text);
-               $text = preg_replace("/[\s|\n]*\[abstract=.*?\].*?\[\/abstract][\s|\n]*/ism", '', $text);
+
+               $text = BBCode::performWithEscapedTags($text, ['code', 'noparse', 'nobb', 'pre'], function ($text) {
+                       $text = preg_replace("/[\s|\n]*\[abstract\].*?\[\/abstract\][\s|\n]*/ism", ' ', $text);
+                       $text = preg_replace("/[\s|\n]*\[abstract=.*?\].*?\[\/abstract][\s|\n]*/ism", ' ', $text);
+                       return $text;
+               });
 
                DI::profiler()->stopRecording();
                return $text;
@@ -2096,30 +2103,26 @@ class BBCode
        /**
         * Returns the value of the "abstract" element
         *
-        * @param string $text The text that maybe contains the element
+        * @param string $text  The text that maybe contains the element
         * @param string $addon The addon for which the abstract is meant for
         * @return string The abstract
         */
-       public static function getAbstract($text, $addon = '')
+       public static function getAbstract(string $text, string $addon = ''): string
        {
                DI::profiler()->startRecording('rendering');
-               $abstract = '';
-               $abstracts = [];
                $addon = strtolower($addon);
 
-               if (preg_match_all("/\[abstract=(.*?)\](.*?)\[\/abstract\]/ism", $text, $results, PREG_SET_ORDER)) {
-                       foreach ($results as $result) {
-                               $abstracts[strtolower($result[1])] = $result[2];
+               $abstract = BBCode::performWithEscapedTags($text, ['code', 'noparse', 'nobb', 'pre'], function ($text) use ($addon) {
+                       if ($addon && preg_match('#\[abstract=' . preg_quote($addon, '#') . '](.*?)\[/abstract]#ism', $text, $matches)) {
+                               return $matches[1];
                        }
-               }
 
-               if (isset($abstracts[$addon])) {
-                       $abstract = $abstracts[$addon];
-               }
+                       if (preg_match("#\[abstract](.*?)\[/abstract]#ism", $text, $matches)) {
+                               return $matches[1];
+                       }
 
-               if ($abstract == '' && preg_match("/\[abstract\](.*?)\[\/abstract\]/ism", $text, $result)) {
-                       $abstract = $result[1];
-               }
+                       return '';
+               });
 
                DI::profiler()->stopRecording();
                return $abstract;
@@ -2334,11 +2337,9 @@ class BBCode
         * @param array    $tagList A list of tag names, e.g ['noparse', 'nobb', 'pre']
         * @param callable $callback
         * @return string
-        * @throws Exception
-        *@see Strings::performWithEscapedBlocks
-        *
+        * @see Strings::performWithEscapedBlocks
         */
-       public static function performWithEscapedTags(string $text, array $tagList, callable $callback)
+       public static function performWithEscapedTags(string $text, array $tagList, callable $callback): string
        {
                $tagList = array_map('preg_quote', $tagList);
 
@@ -2346,7 +2347,7 @@ class BBCode
        }
 
        /**
-        * Replaces mentions in the provided message body for the provided user and network if any
+        * Replaces mentions in the provided message body in BBCode links for the provided user and network if any
         *
         * @param $body
         * @param $profile_uid
@@ -2358,11 +2359,10 @@ class BBCode
        public static function setMentions($body, $profile_uid = 0, $network = '')
        {
                DI::profiler()->startRecording('rendering');
-               self::performWithEscapedTags($body, ['noparse', 'pre', 'code', 'img'], function ($body) use ($profile_uid, $network) {
+               $body = self::performWithEscapedTags($body, ['noparse', 'pre', 'code', 'img'], function ($body) use ($profile_uid, $network) {
                        $tags = self::getTags($body);
 
                        $tagged = [];
-                       $inform = '';
 
                        foreach ($tags as $tag) {
                                $tag_type = substr($tag, 0, 1);
@@ -2381,7 +2381,7 @@ class BBCode
                                        }
                                }
 
-                               if (($success = Item::replaceTag($body, $inform, $profile_uid, $tag, $network)) && $success['replaced']) {
+                               if (($success = Item::replaceTag($body, $profile_uid, $tag, $network)) && $success['replaced']) {
                                        $tagged[] = $tag;
                                }
                        }