2014-05-06 22:00:30 +01:00
|
|
|
<?php
|
2021-04-19 19:51:05 +01:00
|
|
|
|
|
|
|
// {{{ License
|
2018-07-18 05:31:24 +01:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
2021-04-19 19:51:05 +01:00
|
|
|
// }}}
|
2018-07-18 05:31:24 +01:00
|
|
|
|
|
|
|
/**
|
2019-07-06 04:31:02 +01:00
|
|
|
* OEmbed and OpenGraph implementation for GNU social
|
2018-07-18 05:31:24 +01:00
|
|
|
*
|
|
|
|
* @package GNUsocial
|
2021-04-14 16:27:37 +01:00
|
|
|
*
|
2021-02-19 10:34:21 +00:00
|
|
|
* @author Mikael Nordfeldth
|
2018-07-18 05:31:24 +01:00
|
|
|
* @author Stephen Paul Weber
|
|
|
|
* @author hannes
|
|
|
|
* @author Mikael Nordfeldth
|
2021-02-19 10:34:21 +00:00
|
|
|
* @author Miguel Dantas
|
2021-04-19 19:51:05 +01:00
|
|
|
* @author Diogo Peralta Cordeiro <mail@diogo.site>
|
|
|
|
* @authir Hugo Sales <hugo@hsal.es>
|
|
|
|
*
|
2021-02-19 10:34:21 +00:00
|
|
|
* @copyright 2014-2021 Free Software Foundation, Inc http://www.fsf.org
|
2018-07-18 05:31:24 +01:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2021-04-14 16:27:37 +01:00
|
|
|
namespace Plugin\Embed;
|
2018-07-18 05:31:24 +01:00
|
|
|
|
2021-04-25 22:23:46 +01:00
|
|
|
use App\Core\Cache;
|
|
|
|
use App\Core\DB\DB;
|
|
|
|
use App\Core\Event;
|
2021-04-27 21:56:50 +01:00
|
|
|
use App\Core\GSFile;
|
2021-04-25 22:23:46 +01:00
|
|
|
use App\Core\HTTPClient;
|
|
|
|
use App\Core\Log;
|
2021-04-19 19:51:05 +01:00
|
|
|
use App\Core\Modules\Plugin;
|
2021-04-25 22:23:46 +01:00
|
|
|
use App\Core\Router\RouteLoader;
|
|
|
|
use App\Core\Router\Router;
|
|
|
|
use App\Entity\Attachment;
|
2021-04-27 21:56:50 +01:00
|
|
|
use App\Entity\AttachmentThumbnail;
|
|
|
|
use App\Util\Common;
|
2021-04-25 22:23:46 +01:00
|
|
|
use App\Util\Exception\DuplicateFoundException;
|
|
|
|
use App\Util\Exception\NotFoundException;
|
2021-04-28 16:03:17 +01:00
|
|
|
use App\Util\Formatting;
|
2021-04-27 21:56:50 +01:00
|
|
|
use App\Util\TemporaryFile;
|
|
|
|
use Embed\Embed as LibEmbed;
|
2021-05-01 22:03:54 +01:00
|
|
|
use Exception;
|
2021-04-25 22:23:46 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2019-07-07 13:26:10 +01:00
|
|
|
|
2018-07-18 05:31:24 +01:00
|
|
|
/**
|
2019-07-06 04:31:02 +01:00
|
|
|
* Base class for the Embed plugin that does most of the heavy lifting to get
|
2018-07-18 05:31:24 +01:00
|
|
|
* and display representations for remote content.
|
|
|
|
*
|
2021-02-19 10:34:21 +00:00
|
|
|
* @copyright 2014-2021 Free Software Foundation, Inc http://www.fsf.org
|
2018-07-18 05:31:24 +01:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
2021-04-19 19:51:05 +01:00
|
|
|
class Embed extends Plugin
|
2014-05-06 22:00:30 +01:00
|
|
|
{
|
2021-04-25 22:23:46 +01:00
|
|
|
/**
|
|
|
|
* Settings which can be set in social.local.yaml
|
|
|
|
* WARNING, these are _regexps_ (slashes added later). Always escape your dots and end ('$') your strings
|
|
|
|
*/
|
|
|
|
public $domain_allowlist = [
|
2019-07-06 04:31:02 +01:00
|
|
|
// hostname => service provider
|
2021-04-27 21:56:50 +01:00
|
|
|
'.*' => '', // Default to allowing any host
|
2019-07-06 04:31:02 +01:00
|
|
|
];
|
2019-07-06 16:52:30 +01:00
|
|
|
|
2018-07-18 05:31:24 +01:00
|
|
|
/**
|
|
|
|
* This code executes when GNU social creates the page routing, and we hook
|
2019-07-06 04:31:02 +01:00
|
|
|
* on this event to add our action handler for Embed.
|
2018-07-18 05:31:24 +01:00
|
|
|
*
|
|
|
|
* @param $m URLMapper the router that was initialized.
|
2021-04-14 16:27:37 +01:00
|
|
|
*
|
2021-05-01 22:45:47 +01:00
|
|
|
* @return bool true if successful, the exception object if it isn't.
|
2019-07-12 03:13:40 +01:00
|
|
|
* @throws Exception
|
2021-04-14 16:27:37 +01:00
|
|
|
*
|
2018-07-18 05:31:24 +01:00
|
|
|
*/
|
2021-05-01 22:45:47 +01:00
|
|
|
public function onAddRoute(RouteLoader $m): bool
|
2014-05-06 22:00:30 +01:00
|
|
|
{
|
2021-04-25 22:23:46 +01:00
|
|
|
$m->connect('oembed', 'main/oembed', Controller\Embed::class);
|
|
|
|
$m->connect('embed', 'main/embed', Controller\Embed::class);
|
|
|
|
return Event::next;
|
2014-05-06 22:00:30 +01:00
|
|
|
}
|
|
|
|
|
2021-04-25 22:23:46 +01:00
|
|
|
/**
|
|
|
|
* Insert oembed and opengraph tags in all HTML head elements
|
|
|
|
*/
|
2021-04-28 21:15:43 +01:00
|
|
|
public function onShowHeadElements(Request $request, array &$result)
|
2014-05-06 22:00:30 +01:00
|
|
|
{
|
2021-04-25 22:23:46 +01:00
|
|
|
$matches = [];
|
2021-04-28 21:15:43 +01:00
|
|
|
preg_match(',/?([^/]+)/?(.*),', $request->getPathInfo(), $matches);
|
2021-04-25 22:23:46 +01:00
|
|
|
switch ($matches[1]) {
|
2021-05-01 22:45:47 +01:00
|
|
|
case 'attachment':
|
|
|
|
$url = "{$matches[1]}/{$matches[2]}";
|
|
|
|
break;
|
2014-05-06 22:00:30 +01:00
|
|
|
}
|
|
|
|
|
2019-07-14 23:35:11 +01:00
|
|
|
if (isset($url)) {
|
|
|
|
foreach (['xml', 'json'] as $format) {
|
2021-04-25 22:23:46 +01:00
|
|
|
$result[] = [
|
|
|
|
'link' => [
|
2021-05-01 22:45:47 +01:00
|
|
|
'rel' => 'alternate',
|
|
|
|
'type' => "application/{$format}+oembed",
|
|
|
|
'href' => Router::url('embed', ['format' => $format, 'url' => $url]),
|
2021-04-14 16:27:37 +01:00
|
|
|
'title' => 'oEmbed',
|
2021-05-01 22:45:47 +01:00
|
|
|
],];
|
2019-07-14 23:35:11 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-25 22:23:46 +01:00
|
|
|
return Event::next;
|
2016-03-16 23:31:45 +00:00
|
|
|
}
|
|
|
|
|
2014-05-06 22:00:30 +01:00
|
|
|
/**
|
2021-04-25 22:23:46 +01:00
|
|
|
* Save embedding information for an Attachment, if applicable.
|
2014-05-06 22:00:30 +01:00
|
|
|
*
|
|
|
|
* Normally this event is called through File::saveNew()
|
|
|
|
*
|
2021-04-25 22:23:46 +01:00
|
|
|
* @param Attachment $attachment The newly inserted Attachment object.
|
2014-05-06 22:00:30 +01:00
|
|
|
*
|
2021-04-14 16:27:37 +01:00
|
|
|
* @return bool success
|
2014-05-06 22:00:30 +01:00
|
|
|
*/
|
2021-05-01 22:45:47 +01:00
|
|
|
public function onAttachmentStoreNew(Attachment $attachment): bool
|
2014-05-06 22:00:30 +01:00
|
|
|
{
|
2021-04-25 22:23:46 +01:00
|
|
|
try {
|
|
|
|
DB::findOneBy('attachment_embed', ['attachment_id' => $attachment->getId()]);
|
|
|
|
} catch (NotFoundException) {
|
2021-05-01 22:45:47 +01:00
|
|
|
if ($attachment->hasRemoteUrl() && $attachment->hasMimetype()) {
|
|
|
|
$mimetype = $attachment->getMimetype();
|
|
|
|
if (Formatting::startsWith($mimetype, 'text/html') || Formatting::startsWith($mimetype, 'application/xhtml+xml')) {
|
|
|
|
try {
|
|
|
|
$embed_data = $this->getEmbed($attachment->getRemoteUrl(), $attachment);
|
|
|
|
$embed_data['attachment_id'] = $attachment->getId();
|
|
|
|
DB::persist(Entity\AttachmentEmbed::create($embed_data));
|
|
|
|
DB::flush();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::warning($e);
|
|
|
|
}
|
2021-04-28 21:15:43 +01:00
|
|
|
}
|
2014-05-06 22:00:30 +01:00
|
|
|
}
|
2021-05-01 22:45:47 +01:00
|
|
|
} catch (DuplicateFoundException) {
|
|
|
|
Log::warning("Strangely, an attachment_embed object exists for new file {$attachment->getID()}");
|
2014-05-06 22:00:30 +01:00
|
|
|
}
|
2021-04-27 21:56:50 +01:00
|
|
|
return Event::next;
|
2014-05-06 22:00:30 +01:00
|
|
|
}
|
|
|
|
|
2021-04-25 22:23:46 +01:00
|
|
|
/**
|
|
|
|
* Replace enclosure representation of an attachment with the data from embed
|
|
|
|
*/
|
2021-04-28 16:03:17 +01:00
|
|
|
public function onAttachmentFileInfo(int $attachment_id, ?array &$enclosure)
|
2014-05-06 22:00:30 +01:00
|
|
|
{
|
2021-04-25 22:23:46 +01:00
|
|
|
try {
|
2021-04-28 16:03:17 +01:00
|
|
|
$embed = DB::findOneBy('attachment_embed', ['attachment_id' => $attachment_id]);
|
2021-04-25 22:23:46 +01:00
|
|
|
} catch (NotFoundException) {
|
|
|
|
return Event::next;
|
2014-05-06 22:00:30 +01:00
|
|
|
}
|
|
|
|
|
2021-04-28 16:03:17 +01:00
|
|
|
// We know about this attachment, so we 'own' it, but know
|
|
|
|
// that it doesn't have an image
|
|
|
|
if (!$embed->isImage()) {
|
|
|
|
$enclosure = null;
|
|
|
|
return Event::stop;
|
2014-05-06 22:00:30 +01:00
|
|
|
}
|
2021-04-28 16:03:17 +01:00
|
|
|
|
|
|
|
$enclosure = [
|
|
|
|
'filepath' => $embed->getFilepath(),
|
|
|
|
'mimetype' => $embed->getMimetype(),
|
2021-05-01 22:45:47 +01:00
|
|
|
'title' => $embed->getTitle(),
|
|
|
|
'width' => $embed->getWidth(),
|
|
|
|
'height' => $embed->getHeight(),
|
|
|
|
'url' => $embed->getMediaUrl(),
|
2021-04-28 16:03:17 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return Event::stop;
|
2014-05-06 22:00:30 +01:00
|
|
|
}
|
2016-03-16 23:31:45 +00:00
|
|
|
|
2021-04-28 16:03:17 +01:00
|
|
|
/**
|
|
|
|
* Show this attachment enhanced with the corresponing Embed data, if available
|
|
|
|
*/
|
2021-04-25 22:23:46 +01:00
|
|
|
public function onShowAttachment(Attachment $attachment, array &$res)
|
2016-03-16 23:31:45 +00:00
|
|
|
{
|
|
|
|
try {
|
2021-04-25 22:23:46 +01:00
|
|
|
$embed = Cache::get('attachment-embed-' . $attachment->getId(),
|
2021-05-01 22:45:47 +01:00
|
|
|
fn() => DB::findOneBy('attachment_embed', ['attachment_id' => $attachment->getId()]));
|
2021-04-25 22:23:46 +01:00
|
|
|
} catch (DuplicateFoundException $e) {
|
|
|
|
Log::waring($e);
|
|
|
|
return Event::next;
|
|
|
|
} catch (NotFoundException) {
|
|
|
|
return Event::next;
|
2016-03-16 23:31:45 +00:00
|
|
|
}
|
2021-04-25 22:23:46 +01:00
|
|
|
if (is_null($embed) && empty($embed->getAuthorName()) && empty($embed->getProvider())) {
|
2021-04-28 16:03:17 +01:00
|
|
|
Log::debug('Embed doesn\'t have a representation for the attachment #' . $attachment->getId());
|
2021-04-25 22:23:46 +01:00
|
|
|
return Event::next;
|
2016-03-22 13:02:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 16:03:17 +01:00
|
|
|
$attributes = $embed->getImageHTMLAttributes(['class' => 'u-photo embed']);
|
2021-04-25 22:23:46 +01:00
|
|
|
|
2021-04-30 22:29:42 +01:00
|
|
|
$res[] = Formatting::twigRenderString(<<<END
|
2021-04-25 22:23:46 +01:00
|
|
|
<article class="h-entry embed">
|
|
|
|
<header>
|
2021-04-28 16:03:17 +01:00
|
|
|
{% if attributes != false %}
|
|
|
|
<img class="u-photo embed" width="{{attributes['width']}}" height="{{attributes['height']}}" src="{{attributes['src']}}" />
|
|
|
|
{% endif %}
|
2021-04-25 22:23:46 +01:00
|
|
|
<h5 class="p-name embed">
|
2021-05-01 22:45:47 +01:00
|
|
|
<a class="u-url" href="{{attachment.getRemoteUrl()}}">{{embed.getTitle() | escape}}</a>
|
2021-04-25 22:23:46 +01:00
|
|
|
</h5>
|
|
|
|
<div class="p-author embed">
|
|
|
|
{% if embed.getAuthorName() is not null %}
|
|
|
|
<div class="fn vcard author">
|
|
|
|
{% if embed.getAuthorUrl() is null %}
|
|
|
|
<p>{{embed.getAuthorName()}}</p>
|
|
|
|
{% else %}
|
|
|
|
<a href="{{embed.getAuthorUrl()}}" class="url">{{embed.getAuthorName()}}</a>
|
|
|
|
{% endif %}
|
|
|
|
</div>
|
|
|
|
{% endif %}
|
|
|
|
{% if embed.getProvider() is not null %}
|
|
|
|
<div class="fn vcard">
|
|
|
|
{% if embed.getProviderUrl() is null %}
|
|
|
|
<p>{{embed.getProvider()}}</p>
|
|
|
|
{% else %}
|
|
|
|
<a href="{{embed.getProviderUrl()}}" class="url">{{embed.getProvider()}}</a>
|
|
|
|
{% endif %}
|
|
|
|
</div>
|
|
|
|
{% endif %}
|
|
|
|
</div>
|
|
|
|
</header>
|
|
|
|
<div class="p-summary embed">
|
|
|
|
{{ embed.getHtml() | escape }}
|
|
|
|
</div>
|
|
|
|
</article>
|
2021-05-01 22:45:47 +01:00
|
|
|
END, ['embed' => $embed, 'attributes' => $attributes, 'attachment' => $attachment]);
|
2021-04-25 22:23:46 +01:00
|
|
|
|
|
|
|
return Event::stop;
|
2014-05-06 22:00:30 +01:00
|
|
|
}
|
|
|
|
|
2018-07-18 05:31:24 +01:00
|
|
|
/**
|
2021-05-01 22:45:47 +01:00
|
|
|
* @return bool false on no check made, provider name on success
|
|
|
|
* @return string|false on no check made, provider name on success
|
|
|
|
*
|
2021-04-14 16:27:37 +01:00
|
|
|
* @throws ServerException if check is made but fails
|
|
|
|
*
|
2015-01-25 01:34:40 +00:00
|
|
|
*/
|
2021-05-01 22:45:47 +01:00
|
|
|
protected function checkAllowlist(string $url): string|bool
|
2015-01-25 01:34:40 +00:00
|
|
|
{
|
2021-04-27 21:56:50 +01:00
|
|
|
if ($this->check_allowlist ?? false) {
|
2015-01-25 01:34:40 +00:00
|
|
|
return false; // indicates "no check made"
|
|
|
|
}
|
|
|
|
|
|
|
|
$host = parse_url($url, PHP_URL_HOST);
|
2021-04-25 22:23:46 +01:00
|
|
|
foreach ($this->domain_allowlist as $regex => $provider) {
|
2021-04-14 16:27:37 +01:00
|
|
|
if (preg_match("/{$regex}/", $host)) {
|
2015-01-25 10:18:57 +00:00
|
|
|
return $provider; // we trust this source, return provider name
|
|
|
|
}
|
2015-01-25 01:34:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 22:23:46 +01:00
|
|
|
throw new ServerException(_m('Domain not in remote thumbnail source allowlist: {host}', ['host' => $host]));
|
2015-01-25 01:34:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 05:31:24 +01:00
|
|
|
/**
|
|
|
|
* Check the file size of a remote file using a HEAD request and checking
|
|
|
|
* the content-length variable returned. This isn't 100% foolproof but is
|
|
|
|
* reliable enough for our purposes.
|
|
|
|
*
|
2021-04-25 22:23:46 +01:00
|
|
|
* @param string $url
|
2021-05-01 22:45:47 +01:00
|
|
|
* @param array|null $headers - if we already made a request
|
2021-04-14 16:27:37 +01:00
|
|
|
*
|
2021-05-01 22:45:47 +01:00
|
|
|
* @return int|null the file size if it succeeds, false otherwise.
|
2018-07-18 05:31:24 +01:00
|
|
|
*/
|
2021-04-25 22:23:46 +01:00
|
|
|
private function getRemoteFileSize(string $url, ?array $headers = null): ?int
|
2015-01-25 01:34:40 +00:00
|
|
|
{
|
2018-07-18 05:31:24 +01:00
|
|
|
try {
|
2019-07-14 23:35:11 +01:00
|
|
|
if ($headers === null) {
|
2021-04-25 22:23:46 +01:00
|
|
|
if (!Common::isValidHttpUrl($url)) {
|
|
|
|
Log::error('Invalid URL in Embed::getRemoteFileSize()');
|
2018-07-18 05:31:24 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-05-01 22:45:47 +01:00
|
|
|
$head = HTTPClient::head($url);
|
2021-04-25 22:23:46 +01:00
|
|
|
$headers = $head->getHeaders();
|
2019-07-14 23:56:31 +01:00
|
|
|
$headers = array_change_key_case($headers, CASE_LOWER);
|
2018-07-18 05:31:24 +01:00
|
|
|
}
|
2021-04-27 21:56:50 +01:00
|
|
|
return $headers['content-length'][0] ?? false;
|
2021-04-25 22:23:46 +01:00
|
|
|
} catch (Exception $e) {
|
|
|
|
Loog::error($e);
|
2018-07-18 05:31:24 +01:00
|
|
|
return false;
|
2015-01-25 01:34:40 +00:00
|
|
|
}
|
2018-07-18 05:31:24 +01:00
|
|
|
}
|
2015-01-25 01:34:40 +00:00
|
|
|
|
2018-07-18 05:31:24 +01:00
|
|
|
/**
|
2021-04-25 22:23:46 +01:00
|
|
|
* A private helper function that uses a HEAD request to check the mime type
|
2018-07-18 05:31:24 +01:00
|
|
|
* of a remote URL to see it it's an image.
|
|
|
|
*
|
2021-05-01 22:45:47 +01:00
|
|
|
* @param mixed $url
|
2021-04-14 16:27:37 +01:00
|
|
|
* @param null|mixed $headers
|
|
|
|
*
|
2018-07-18 05:31:24 +01:00
|
|
|
* @return bool true if the remote URL is an image, or false otherwise.
|
|
|
|
*/
|
2021-05-01 22:45:47 +01:00
|
|
|
private function isRemoteImage(string $url, ?array $headers = null): bool
|
2018-07-18 05:31:24 +01:00
|
|
|
{
|
2021-04-25 22:23:46 +01:00
|
|
|
try {
|
|
|
|
if ($headers === null) {
|
|
|
|
if (!Common::isValidHttpUrl($url)) {
|
|
|
|
Log::error('Invalid URL in Embed::getRemoteFileSize()');
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-01 22:45:47 +01:00
|
|
|
$head = HTTPClient::head($url);
|
2021-04-25 22:23:46 +01:00
|
|
|
$headers = $head->getHeaders();
|
|
|
|
$headers = array_change_key_case($headers, CASE_LOWER);
|
2018-07-18 05:31:24 +01:00
|
|
|
}
|
2021-04-27 21:56:50 +01:00
|
|
|
return !empty($headers['content-type']) && GSFile::mimetypeMajor($headers['content-type'][0]) === 'image';
|
2021-04-25 22:23:46 +01:00
|
|
|
} catch (Exception $e) {
|
2021-05-01 22:45:47 +01:00
|
|
|
Log::error($e);
|
2021-04-25 22:23:46 +01:00
|
|
|
return false;
|
2018-07-18 05:31:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-25 22:23:46 +01:00
|
|
|
* Validate that $imgData is a valid image, place it in it's folder and resize
|
2018-07-18 05:31:24 +01:00
|
|
|
*
|
2021-04-25 22:23:46 +01:00
|
|
|
* @param $imgData - The image data to validate
|
2021-05-01 22:45:47 +01:00
|
|
|
* @param null|array $headers - The headers possible previous request to $url
|
2018-07-18 05:31:24 +01:00
|
|
|
*/
|
2021-05-01 22:45:47 +01:00
|
|
|
protected function validateAndWriteImage($imgData, ?array $headers = null): array
|
2018-07-18 05:31:24 +01:00
|
|
|
{
|
2021-04-25 22:23:46 +01:00
|
|
|
$file = new TemporaryFile();
|
|
|
|
$file->write($imgData);
|
2016-02-25 21:31:45 +00:00
|
|
|
|
2021-04-29 21:42:26 +01:00
|
|
|
Event::handle('HashFile', [$file->getRealPath(), &$hash]);
|
2021-05-01 22:45:47 +01:00
|
|
|
$filepath = Common::config('storage', 'dir') . "embed/{$hash}" . Common::config('thumbnail', 'extension');
|
|
|
|
$width = Common::config('plugin_embed', 'width');
|
|
|
|
$height = Common::config('plugin_embed', 'height');
|
2021-05-01 22:03:54 +01:00
|
|
|
$smart_crop = Common::config('plugin_embed', 'smart_crop');
|
|
|
|
|
|
|
|
Event::handle('ResizeImagePath', [$file->getRealPath(), $filepath, &$width, &$height, $smart_crop, &$mimetype]);
|
2021-04-28 16:03:17 +01:00
|
|
|
|
2021-04-25 22:23:46 +01:00
|
|
|
unset($file);
|
2019-08-19 01:40:31 +01:00
|
|
|
|
2021-05-01 22:45:47 +01:00
|
|
|
if (!is_null($headers) && array_key_exists('content-disposition', $headers) && preg_match('/^.+; filename="(.+?)"$/', $headers['content-disposition'][0], $matches) === 1) {
|
2021-04-27 21:56:50 +01:00
|
|
|
$original_name = $matches[1];
|
|
|
|
}
|
|
|
|
|
2021-04-28 16:03:17 +01:00
|
|
|
return [$filepath, $width, $height, $original_name ?? null, $mimetype];
|
2019-08-19 01:40:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-02 00:49:10 +01:00
|
|
|
* Fetch, Validate and Write a remote image from url to temporary file
|
2021-05-01 22:45:47 +01:00
|
|
|
*
|
|
|
|
* @param Attachment $attachment
|
|
|
|
* @param string $media_url URL for the actual media representation
|
|
|
|
* @return array|bool
|
2021-05-02 00:49:10 +01:00
|
|
|
* @throws Exception
|
2019-08-19 01:40:31 +01:00
|
|
|
*/
|
2021-05-02 00:49:10 +01:00
|
|
|
protected function fetchValidateWriteRemoteImage(Attachment $attachment, string $media_url): array|bool
|
2019-08-19 01:40:31 +01:00
|
|
|
{
|
2021-04-28 21:15:43 +01:00
|
|
|
if ($attachment->hasFilename() && file_exists($attachment->getPath())) {
|
2021-04-27 21:56:50 +01:00
|
|
|
throw new AlreadyFulfilledException(_m('A thumbnail seems to already exist for remote file with id=={id}', ['id' => $attachment->getId()]));
|
2019-08-19 01:40:31 +01:00
|
|
|
}
|
|
|
|
|
2021-05-01 22:45:47 +01:00
|
|
|
if (Formatting::startsWith($media_url, 'file://')) {
|
|
|
|
$filename = Formatting::removePrefix($media_url, 'file://');
|
|
|
|
$info = getimagesize($filename);
|
2019-08-19 01:40:31 +01:00
|
|
|
$filename = basename($filename);
|
2021-05-01 22:45:47 +01:00
|
|
|
$width = $info[0];
|
|
|
|
$height = $info[1];
|
2019-08-19 01:40:31 +01:00
|
|
|
} else {
|
2021-05-01 22:45:47 +01:00
|
|
|
$this->checkAllowlist($media_url);
|
|
|
|
$head = HTTPClient::head($media_url);
|
2021-04-25 22:23:46 +01:00
|
|
|
$headers = $head->getHeaders();
|
2019-08-19 01:40:31 +01:00
|
|
|
$headers = array_change_key_case($headers, CASE_LOWER);
|
|
|
|
|
|
|
|
try {
|
2021-05-01 22:45:47 +01:00
|
|
|
$is_image = $this->isRemoteImage($media_url, $headers);
|
2019-08-19 01:40:31 +01:00
|
|
|
if ($is_image == true) {
|
2021-05-01 22:45:47 +01:00
|
|
|
$file_size = $this->getRemoteFileSize($media_url, $headers);
|
|
|
|
$max_size = Common::config('attachments', 'file_quota');
|
2021-04-25 22:23:46 +01:00
|
|
|
if (($file_size != false) && ($file_size > $max_size)) {
|
2021-04-27 21:56:50 +01:00
|
|
|
throw new \Exception("Wanted to store remote thumbnail of size {$file_size} but the upload limit is {$max_size} so we aborted.");
|
2019-08-19 01:40:31 +01:00
|
|
|
}
|
2019-09-02 16:31:32 +01:00
|
|
|
} else {
|
|
|
|
return false;
|
2019-08-19 01:40:31 +01:00
|
|
|
}
|
|
|
|
} catch (Exception $err) {
|
2021-04-25 22:23:46 +01:00
|
|
|
Log::debug('Could not determine size of remote image, aborted local storage.');
|
2021-02-16 18:30:21 +00:00
|
|
|
throw $err;
|
2019-08-19 01:40:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// First we download the file to memory and test whether it's actually an image file
|
2021-05-01 22:45:47 +01:00
|
|
|
Log::debug('Downloading remote thumbnail for file id==' . $attachment->getId() . " with thumbnail URL: {$media_url}");
|
|
|
|
|
2019-09-02 16:31:32 +01:00
|
|
|
try {
|
2021-05-01 22:45:47 +01:00
|
|
|
$imgData = HTTPClient::get($media_url)->getContent();
|
2019-09-02 16:31:32 +01:00
|
|
|
if (isset($imgData)) {
|
2021-05-01 22:45:47 +01:00
|
|
|
[$filepath, $width, $height, $original_name, $mimetype] = $this->validateAndWriteImage($imgData, $headers);
|
2019-09-02 16:31:32 +01:00
|
|
|
} else {
|
2021-04-25 22:23:46 +01:00
|
|
|
throw new UnsupportedMediaException(_m('HTTPClient returned an empty result'));
|
2019-09-02 16:31:32 +01:00
|
|
|
}
|
2019-11-01 11:19:42 +00:00
|
|
|
} catch (UnsupportedMediaException $e) {
|
2019-09-02 16:31:32 +01:00
|
|
|
// Couldn't find anything that looks like an image, nothing to do
|
2021-04-25 22:23:46 +01:00
|
|
|
Log::debug($e);
|
2019-09-02 16:31:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-07-18 05:31:24 +01:00
|
|
|
}
|
|
|
|
|
2021-04-28 16:03:17 +01:00
|
|
|
return [$filepath, $width, $height, $original_name, $mimetype];
|
2021-04-27 21:56:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform an oEmbed or OpenGraph lookup for the given $url.
|
|
|
|
*
|
|
|
|
* Some known hosts are allowlisted with API endpoints where we
|
|
|
|
* know they exist but autodiscovery data isn't available.
|
|
|
|
*
|
|
|
|
* Throws exceptions on failure.
|
|
|
|
*
|
|
|
|
* @param string $url
|
2021-05-01 22:45:47 +01:00
|
|
|
* @param Attachment $attachment
|
|
|
|
* @return array
|
2021-04-27 21:56:50 +01:00
|
|
|
*/
|
|
|
|
public function getEmbed(string $url, Attachment $attachment): array
|
|
|
|
{
|
|
|
|
Log::info('Checking for remote URL metadata for ' . $url);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Log::info("Trying to find Embed data for {$url} with 'oscarotero/Embed'");
|
2021-05-01 22:45:47 +01:00
|
|
|
$embed = new LibEmbed();
|
|
|
|
$info = $embed->get($url);
|
|
|
|
$metadata['title'] = $info->title;
|
|
|
|
$metadata['html'] = $info->description;
|
|
|
|
$metadata['author_name'] = $info->authorName;
|
|
|
|
$metadata['author_url'] = $info->authorUrl;
|
2021-04-27 21:56:50 +01:00
|
|
|
$metadata['provider_name'] = $info->providerName;
|
2021-05-01 22:45:47 +01:00
|
|
|
$metadata['provider_url'] = $info->providerUrl;
|
2021-04-27 21:56:50 +01:00
|
|
|
|
|
|
|
if (!is_null($info->image)) {
|
2021-05-01 22:45:47 +01:00
|
|
|
$image_url = (string)$info->image;
|
|
|
|
|
|
|
|
if (Formatting::startsWith($image_url, 'data')) {
|
2021-04-27 21:56:50 +01:00
|
|
|
// Inline image
|
2021-05-01 22:45:47 +01:00
|
|
|
$imgData = base64_decode(substr($info->image, stripos($info->image, 'base64,') + 7));
|
2021-04-28 16:03:17 +01:00
|
|
|
[$filepath, $width, $height, $original_name, $mimetype] = $this->validateAndWriteImage($imgData);
|
2021-04-27 21:56:50 +01:00
|
|
|
} else {
|
2021-05-02 00:49:10 +01:00
|
|
|
[$filepath, $width, $height, $original_name, $mimetype] = $this->fetchValidateWriteRemoteImage($attachment, $image_url);
|
2021-04-27 21:56:50 +01:00
|
|
|
}
|
2021-05-01 22:45:47 +01:00
|
|
|
$metadata['width'] = $width;
|
|
|
|
$metadata['height'] = $height;
|
2021-04-27 21:56:50 +01:00
|
|
|
$metadata['mimetype'] = $mimetype;
|
2021-05-01 22:45:47 +01:00
|
|
|
$metadata['media_url'] = $image_url;
|
2021-04-28 16:03:17 +01:00
|
|
|
$metadata['filename'] = Formatting::removePrefix($filepath, Common::config('storage', 'dir'));
|
2021-04-27 21:56:50 +01:00
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
2021-05-02 00:49:10 +01:00
|
|
|
Log::info("Failed to find Embed data for {$url} with 'oscarotero/Embed', got exception: " . $e->getMessage());
|
2021-04-27 21:56:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$metadata = self::normalize($metadata);
|
|
|
|
$attachment->setTitle($metadata['title']);
|
|
|
|
return $metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize fetched info.
|
|
|
|
*/
|
|
|
|
public static function normalize(array $data): array
|
|
|
|
{
|
|
|
|
if (isset($metadata['url'])) {
|
|
|
|
// sometimes sites serve the path, not the full URL, for images
|
|
|
|
// let's "be liberal in what you accept from others"!
|
|
|
|
// add protocol and host if the thumbnail_url starts with /
|
|
|
|
if ($metadata['url'][0] == '/') {
|
|
|
|
$thumbnail_url_parsed = parse_url($metadata['url']);
|
2021-05-01 22:45:47 +01:00
|
|
|
$metadata['url'] = "{$thumbnail_url_parsed['scheme']}://{$thumbnail_url_parsed['host']}{$metadata['url']}";
|
2021-04-27 21:56:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Some wordpress opengraph implementations sometimes return a white blank image
|
|
|
|
// no need for us to save that!
|
|
|
|
if ($metadata['url'] == 'https://s0.wp.com/i/blank.jpg') {
|
|
|
|
$metadata['url'] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($data['width'])) {
|
2021-05-01 22:45:47 +01:00
|
|
|
$data['width'] = Common::config('plugin_embed', 'width');
|
2021-05-01 22:03:54 +01:00
|
|
|
$data['height'] = Common::config('plugin_embed', 'height');
|
2021-04-27 21:56:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
2014-05-06 22:00:30 +01:00
|
|
|
}
|
|
|
|
}
|