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;
*/
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.
*
* 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 = '<div class="oembed ' . $oembed->type . '">';
'$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;
break;
case 'photo':
- $ret .= '<img width="' . $oembed->width . '" src="' . Proxy::proxifyUrl($oembed->url) . '">';
+ $ret .= '<img width="' . $oembed->width . '" src="' . BBCode::proxyUrl($oembed->url, BBCode::INTERNAL, $uriid, Proxy::SIZE_MEDIUM) . '">';
break;
case 'link':
break;
case 'rich':
- $ret .= Proxy::proxifyHtml($oembed->html);
+ $ret .= Proxy::proxifyHtml($oembed->html, $uriid);
break;
}
$ret .= '<a href="' . $oembed->embed_url . '" rel="oembed">' . $oembed->embed_url . '</a>';
}
$ret .= "</h4>";
+ if ($oembed->type == 'link') {
+ if (!empty($oembed->thumbnail_url)) {
+ $ret .= '<img width="' . $oembed->width . '" src="' . BBCode::proxyUrl($oembed->thumbnail_url, BBCode::INTERNAL, $uriid, Proxy::SIZE_MEDIUM) . '">';
+ }
+ if (!empty($oembed->description)) {
+ $ret .= '<p>' . $oembed->description . '</p>';
+ }
+ }
} elseif (!strpos($oembed->html, $oembed->embed_url)) {
// add <a> for html2bbcode conversion
$ret .= '<a href="' . $oembed->embed_url . '" rel="oembed">' . $oembed->title . '</a>';
}
$ret .= '</div>';
+$test = Proxy::proxifyHtml($ret, $uriid);
return str_replace("\n", "", $ret);
}
* 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", "<!-- oembed $1 --><i>" . DI::l10n()->t('Embedding disabled') . " : $1</i><!-- /oembed $1 -->", $text);
+ if (!preg_match_all("/\[embed\](.+?)\[\/embed\]/is", $text, $matches, PREG_SET_ORDER)) {
+ return $text;
+ }
+ foreach ($matches as $match) {
+ $data = self::fetchURL($match[1]);
+ $text = str_replace($match[0], self::formatObject($data, $uriid), $text);
}
- return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", [self::class, 'replaceCallback'], $text);
+ return $text;
}
/**
* 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);
$o->title = $title;
}
- $html = self::formatObject($o);
+ $html = self::formatObject($o, $uriid);
return $html;
}
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])) {
$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.');
}
* $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];
}
}
// oembed tag
- $text = OEmbed::BBCode2HTML($text);
+ $text = OEmbed::BBCode2HTML($text, $uriid);
// Avoid triple linefeeds through oembed
$text = str_replace("<br style='clear:left'></span><br><br>", "<br style='clear:left'></span><br>", $text);
namespace Friendica\Util;
+use Friendica\Content\Text\BBCode;
use Friendica\Core\Logger;
-use Friendica\Core\System;
use Friendica\DI;
use GuzzleHttp\Psr7\Uri;
* 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('/(<img [^>]*src *= *["\'])([^"\']+)(["\'][^>]*>)/siU', [self::class, 'replaceUrl'], $html);
+ if (!preg_match_all('/(<img [^>]*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;
}
/**
* @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]);
}
// 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