From b572b8989fa7119711db7ed4dd1a0fddb67c0ff4 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 24 Feb 2024 15:11:27 +0000 Subject: [PATCH] Use media link instead of proxy for pictures --- src/Content/OEmbed.php | 54 +++++++++++++++++++------------------ src/Content/Text/BBCode.php | 10 +++---- src/Util/Proxy.php | 19 +++++++++---- 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/src/Content/OEmbed.php b/src/Content/OEmbed.php index a30f6701fe..9af01b76da 100644 --- a/src/Content/OEmbed.php +++ b/src/Content/OEmbed.php @@ -24,6 +24,7 @@ namespace Friendica\Content; use DOMDocument; use DOMXPath; use Exception; +use Friendica\Content\Text\BBCode; use Friendica\Core\Cache\Enum\Duration; use Friendica\Core\Hook; use Friendica\Core\Renderer; @@ -47,21 +48,6 @@ use Friendica\Util\Strings; */ class OEmbed { - /** - * Callback for fetching URL, checking allowance and returning formatted HTML - * - * @param array $matches - * @return string Formatted HTML - */ - public static function replaceCallback(array $matches): string - { - $embedurl = $matches[1]; - $j = self::fetchURL($embedurl); - $s = self::formatObject($j); - - return $s; - } - /** * Get data from an URL to embed its content. * @@ -213,9 +199,10 @@ class OEmbed * Returns a formatted string from OEmbed object * * @param \Friendica\Object\OEmbed $oembed + * @param int $uriid * @return string */ - private static function formatObject(\Friendica\Object\OEmbed $oembed): string + private static function formatObject(\Friendica\Object\OEmbed $oembed, int $uriid): string { $ret = '
'; @@ -235,7 +222,7 @@ class OEmbed '$escapedhtml' => base64_encode($oembed->html), '$tw' => $tw, '$th' => $th, - '$turl' => $oembed->thumbnail_url, + '$turl' => BBCode::proxyUrl($oembed->thumbnail_url, BBCode::INTERNAL, $uriid, Proxy::SIZE_SMALL), ]); } else { $ret = $oembed->html; @@ -243,14 +230,14 @@ class OEmbed break; case 'photo': - $ret .= ''; + $ret .= ''; break; case 'link': break; case 'rich': - $ret .= Proxy::proxifyHtml($oembed->html); + $ret .= Proxy::proxifyHtml($oembed->html, $uriid); break; } @@ -288,12 +275,21 @@ class OEmbed $ret .= '' . $oembed->embed_url . ''; } $ret .= ""; + if ($oembed->type == 'link') { + if (!empty($oembed->thumbnail_url)) { + $ret .= ''; + } + if (!empty($oembed->description)) { + $ret .= '

' . $oembed->description . '

'; + } + } } elseif (!strpos($oembed->html, $oembed->embed_url)) { // add for html2bbcode conversion $ret .= '' . $oembed->title . ''; } $ret .= '
'; +$test = Proxy::proxifyHtml($ret, $uriid); return str_replace("\n", "", $ret); } @@ -302,14 +298,19 @@ class OEmbed * Converts BBCode to HTML code * * @param string $text + * @param int $uriid * @return string */ - public static function BBCode2HTML(string $text): string + public static function BBCode2HTML(string $text, int $uriid): string { - if (DI::config()->get('system', 'no_oembed')) { - return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "" . DI::l10n()->t('Embedding disabled') . " : $1", $text); + if (!preg_match_all("/\[embed\](.+?)\[\/embed\]/is", $text, $matches, PREG_SET_ORDER)) { + return $text; } - return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", [self::class, 'replaceCallback'], $text); + foreach ($matches as $match) { + $data = self::fetchURL($match[1]); + $text = str_replace($match[0], self::formatObject($data, $uriid), $text); + } + return $text; } /** @@ -342,10 +343,11 @@ class OEmbed * Returns a formatted HTML code from given URL and sets optional title * * @param string $url URL to fetch - * @param string $title Optional title (default: what comes from OEmbed object) + * @param string $title title (default: what comes from OEmbed object) + * @param int $uriid * @return string Formatted HTML */ - public static function getHTML(string $url, string $title = ''): string + public static function getHTML(string $url, string $title, int $uriid): string { $o = self::fetchURL($url); @@ -357,7 +359,7 @@ class OEmbed $o->title = $title; } - $html = self::formatObject($o); + $html = self::formatObject($o, $uriid); return $html; } diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 23f02dd26b..877e3e2e55 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -310,7 +310,7 @@ class BBCode return trim($text); } - private static function proxyUrl(string $image, int $simplehtml = self::INTERNAL, int $uriid = 0, string $size = ''): string + public static function proxyUrl(string $image, int $simplehtml = self::INTERNAL, int $uriid = 0, string $size = ''): string { // Only send proxied pictures to API and for internal display if (!in_array($simplehtml, [self::INTERNAL, self::MASTODON_API, self::TWITTER_API])) { @@ -453,7 +453,7 @@ class BBCode $return = ''; try { if ($tryoembed && OEmbed::isAllowedURL($data['url'])) { - $return = OEmbed::getHTML($data['url'], $data['title']); + $return = OEmbed::getHTML($data['url'], $data['title'], $uriid); } else { throw new Exception('OEmbed is disabled for this attachment.'); } @@ -1358,12 +1358,12 @@ class BBCode * $match[1] = $url * $match[2] = $title or absent */ - $try_oembed_callback = function (array $match) { + $try_oembed_callback = function (array $match) use ($uriid) { $url = $match[1]; $title = $match[2] ?? ''; try { - $return = OEmbed::getHTML($url, $title); + $return = OEmbed::getHTML($url, $title, $uriid); } catch (Exception $ex) { $return = $match[0]; } @@ -1810,7 +1810,7 @@ class BBCode } // oembed tag - $text = OEmbed::BBCode2HTML($text); + $text = OEmbed::BBCode2HTML($text, $uriid); // Avoid triple linefeeds through oembed $text = str_replace("


", "

", $text); diff --git a/src/Util/Proxy.php b/src/Util/Proxy.php index 9a2d25c680..17b71ddd88 100644 --- a/src/Util/Proxy.php +++ b/src/Util/Proxy.php @@ -21,8 +21,8 @@ namespace Friendica\Util; +use Friendica\Content\Text\BBCode; use Friendica\Core\Logger; -use Friendica\Core\System; use Friendica\DI; use GuzzleHttp\Psr7\Uri; @@ -133,15 +133,24 @@ class Proxy * proxy storage directory. * * @param string $html Un-proxified HTML code + * @param int $uriid * * @return string Proxified HTML code * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function proxifyHtml(string $html): string + public static function proxifyHtml(string $html, int $uriid): string { $html = str_replace(Strings::normaliseLink(DI::baseUrl()) . '/', DI::baseUrl() . '/', $html); - return preg_replace_callback('/(]*src *= *["\'])([^"\']+)(["\'][^>]*>)/siU', [self::class, 'replaceUrl'], $html); + if (!preg_match_all('/(]*src *= *["\'])([^"\']+)(["\'][^>]*>)/siU', $html, $matches, PREG_SET_ORDER)) { + return $html; + } + + foreach ($matches as $match) { + $html = str_replace($match[0], self::replaceUrl($match, $uriid), $html); + } + + return $html; } /** @@ -193,7 +202,7 @@ class Proxy * @return string Proxified HTML image tag * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - private static function replaceUrl(array $matches): string + private static function replaceUrl(array $matches, int $uriid): string { // if the picture seems to be from another picture cache then take the original source $queryvar = self::parseQuery($matches[2]); @@ -208,7 +217,7 @@ class Proxy } // Return proxified HTML - return $matches[1] . self::proxifyUrl(htmlspecialchars_decode($matches[2])) . $matches[3]; + return $matches[1] . BBCode::proxyUrl(htmlspecialchars_decode($matches[2]), BBCode::INTERNAL, $uriid, Proxy::SIZE_MEDIUM) . $matches[3]; } public static function getPixelsFromSize(string $size): int