Use media link instead of proxy for pictures
This commit is contained in:
parent
5800a973cb
commit
b572b8989f
|
@ -24,6 +24,7 @@ namespace Friendica\Content;
|
||||||
use DOMDocument;
|
use DOMDocument;
|
||||||
use DOMXPath;
|
use DOMXPath;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Friendica\Content\Text\BBCode;
|
||||||
use Friendica\Core\Cache\Enum\Duration;
|
use Friendica\Core\Cache\Enum\Duration;
|
||||||
use Friendica\Core\Hook;
|
use Friendica\Core\Hook;
|
||||||
use Friendica\Core\Renderer;
|
use Friendica\Core\Renderer;
|
||||||
|
@ -47,21 +48,6 @@ use Friendica\Util\Strings;
|
||||||
*/
|
*/
|
||||||
class OEmbed
|
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.
|
* Get data from an URL to embed its content.
|
||||||
*
|
*
|
||||||
|
@ -213,9 +199,10 @@ class OEmbed
|
||||||
* Returns a formatted string from OEmbed object
|
* Returns a formatted string from OEmbed object
|
||||||
*
|
*
|
||||||
* @param \Friendica\Object\OEmbed $oembed
|
* @param \Friendica\Object\OEmbed $oembed
|
||||||
|
* @param int $uriid
|
||||||
* @return string
|
* @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 . '">';
|
$ret = '<div class="oembed ' . $oembed->type . '">';
|
||||||
|
|
||||||
|
@ -235,7 +222,7 @@ class OEmbed
|
||||||
'$escapedhtml' => base64_encode($oembed->html),
|
'$escapedhtml' => base64_encode($oembed->html),
|
||||||
'$tw' => $tw,
|
'$tw' => $tw,
|
||||||
'$th' => $th,
|
'$th' => $th,
|
||||||
'$turl' => $oembed->thumbnail_url,
|
'$turl' => BBCode::proxyUrl($oembed->thumbnail_url, BBCode::INTERNAL, $uriid, Proxy::SIZE_SMALL),
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
$ret = $oembed->html;
|
$ret = $oembed->html;
|
||||||
|
@ -243,14 +230,14 @@ class OEmbed
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'photo':
|
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;
|
break;
|
||||||
|
|
||||||
case 'link':
|
case 'link':
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'rich':
|
case 'rich':
|
||||||
$ret .= Proxy::proxifyHtml($oembed->html);
|
$ret .= Proxy::proxifyHtml($oembed->html, $uriid);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -288,12 +275,21 @@ class OEmbed
|
||||||
$ret .= '<a href="' . $oembed->embed_url . '" rel="oembed">' . $oembed->embed_url . '</a>';
|
$ret .= '<a href="' . $oembed->embed_url . '" rel="oembed">' . $oembed->embed_url . '</a>';
|
||||||
}
|
}
|
||||||
$ret .= "</h4>";
|
$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)) {
|
} elseif (!strpos($oembed->html, $oembed->embed_url)) {
|
||||||
// add <a> for html2bbcode conversion
|
// add <a> for html2bbcode conversion
|
||||||
$ret .= '<a href="' . $oembed->embed_url . '" rel="oembed">' . $oembed->title . '</a>';
|
$ret .= '<a href="' . $oembed->embed_url . '" rel="oembed">' . $oembed->title . '</a>';
|
||||||
}
|
}
|
||||||
|
|
||||||
$ret .= '</div>';
|
$ret .= '</div>';
|
||||||
|
$test = Proxy::proxifyHtml($ret, $uriid);
|
||||||
|
|
||||||
return str_replace("\n", "", $ret);
|
return str_replace("\n", "", $ret);
|
||||||
}
|
}
|
||||||
|
@ -302,14 +298,19 @@ class OEmbed
|
||||||
* Converts BBCode to HTML code
|
* Converts BBCode to HTML code
|
||||||
*
|
*
|
||||||
* @param string $text
|
* @param string $text
|
||||||
|
* @param int $uriid
|
||||||
* @return string
|
* @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')) {
|
if (!preg_match_all("/\[embed\](.+?)\[\/embed\]/is", $text, $matches, PREG_SET_ORDER)) {
|
||||||
return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>" . DI::l10n()->t('Embedding disabled') . " : $1</i><!-- /oembed $1 -->", $text);
|
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
|
* Returns a formatted HTML code from given URL and sets optional title
|
||||||
*
|
*
|
||||||
* @param string $url URL to fetch
|
* @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
|
* @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 = self::fetchURL($url);
|
||||||
|
|
||||||
|
@ -357,7 +359,7 @@ class OEmbed
|
||||||
$o->title = $title;
|
$o->title = $title;
|
||||||
}
|
}
|
||||||
|
|
||||||
$html = self::formatObject($o);
|
$html = self::formatObject($o, $uriid);
|
||||||
|
|
||||||
return $html;
|
return $html;
|
||||||
}
|
}
|
||||||
|
|
|
@ -310,7 +310,7 @@ class BBCode
|
||||||
return trim($text);
|
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
|
// Only send proxied pictures to API and for internal display
|
||||||
if (!in_array($simplehtml, [self::INTERNAL, self::MASTODON_API, self::TWITTER_API])) {
|
if (!in_array($simplehtml, [self::INTERNAL, self::MASTODON_API, self::TWITTER_API])) {
|
||||||
|
@ -453,7 +453,7 @@ class BBCode
|
||||||
$return = '';
|
$return = '';
|
||||||
try {
|
try {
|
||||||
if ($tryoembed && OEmbed::isAllowedURL($data['url'])) {
|
if ($tryoembed && OEmbed::isAllowedURL($data['url'])) {
|
||||||
$return = OEmbed::getHTML($data['url'], $data['title']);
|
$return = OEmbed::getHTML($data['url'], $data['title'], $uriid);
|
||||||
} else {
|
} else {
|
||||||
throw new Exception('OEmbed is disabled for this attachment.');
|
throw new Exception('OEmbed is disabled for this attachment.');
|
||||||
}
|
}
|
||||||
|
@ -1358,12 +1358,12 @@ class BBCode
|
||||||
* $match[1] = $url
|
* $match[1] = $url
|
||||||
* $match[2] = $title or absent
|
* $match[2] = $title or absent
|
||||||
*/
|
*/
|
||||||
$try_oembed_callback = function (array $match) {
|
$try_oembed_callback = function (array $match) use ($uriid) {
|
||||||
$url = $match[1];
|
$url = $match[1];
|
||||||
$title = $match[2] ?? '';
|
$title = $match[2] ?? '';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$return = OEmbed::getHTML($url, $title);
|
$return = OEmbed::getHTML($url, $title, $uriid);
|
||||||
} catch (Exception $ex) {
|
} catch (Exception $ex) {
|
||||||
$return = $match[0];
|
$return = $match[0];
|
||||||
}
|
}
|
||||||
|
@ -1810,7 +1810,7 @@ class BBCode
|
||||||
}
|
}
|
||||||
|
|
||||||
// oembed tag
|
// oembed tag
|
||||||
$text = OEmbed::BBCode2HTML($text);
|
$text = OEmbed::BBCode2HTML($text, $uriid);
|
||||||
|
|
||||||
// Avoid triple linefeeds through oembed
|
// Avoid triple linefeeds through oembed
|
||||||
$text = str_replace("<br style='clear:left'></span><br><br>", "<br style='clear:left'></span><br>", $text);
|
$text = str_replace("<br style='clear:left'></span><br><br>", "<br style='clear:left'></span><br>", $text);
|
||||||
|
|
|
@ -21,8 +21,8 @@
|
||||||
|
|
||||||
namespace Friendica\Util;
|
namespace Friendica\Util;
|
||||||
|
|
||||||
|
use Friendica\Content\Text\BBCode;
|
||||||
use Friendica\Core\Logger;
|
use Friendica\Core\Logger;
|
||||||
use Friendica\Core\System;
|
|
||||||
use Friendica\DI;
|
use Friendica\DI;
|
||||||
use GuzzleHttp\Psr7\Uri;
|
use GuzzleHttp\Psr7\Uri;
|
||||||
|
|
||||||
|
@ -133,15 +133,24 @@ class Proxy
|
||||||
* proxy storage directory.
|
* proxy storage directory.
|
||||||
*
|
*
|
||||||
* @param string $html Un-proxified HTML code
|
* @param string $html Un-proxified HTML code
|
||||||
|
* @param int $uriid
|
||||||
*
|
*
|
||||||
* @return string Proxified HTML code
|
* @return string Proxified HTML code
|
||||||
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
* @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);
|
$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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -193,7 +202,7 @@ class Proxy
|
||||||
* @return string Proxified HTML image tag
|
* @return string Proxified HTML image tag
|
||||||
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
* @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
|
// if the picture seems to be from another picture cache then take the original source
|
||||||
$queryvar = self::parseQuery($matches[2]);
|
$queryvar = self::parseQuery($matches[2]);
|
||||||
|
@ -208,7 +217,7 @@ class Proxy
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return proxified HTML
|
// 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
|
public static function getPixelsFromSize(string $size): int
|
||||||
|
|
Loading…
Reference in New Issue
Block a user