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 = '
'; +$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("