2021-04-18 02:17:57 +01:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2021-04-18 02:17:57 +01:00
|
|
|
// {{{ License
|
|
|
|
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
namespace App\Core;
|
|
|
|
|
2022-03-27 15:19:09 +01:00
|
|
|
use App\Core\DB;
|
2021-07-22 20:56:29 +01:00
|
|
|
use function App\Core\I18n\_m;
|
2021-04-18 02:17:57 +01:00
|
|
|
use App\Util\Common;
|
2021-07-20 21:17:53 +01:00
|
|
|
use App\Util\Exception\DuplicateFoundException;
|
2021-10-28 17:29:57 +01:00
|
|
|
use App\Util\Exception\FileNotAllowedException;
|
2021-04-18 02:17:57 +01:00
|
|
|
use App\Util\Exception\NoSuchFileException;
|
2021-05-02 16:46:12 +01:00
|
|
|
use App\Util\Exception\NotFoundException;
|
2021-08-16 17:10:33 +01:00
|
|
|
use App\Util\Exception\NotStoredLocallyException;
|
2021-05-01 14:02:14 +01:00
|
|
|
use App\Util\Exception\ServerException;
|
2022-03-07 17:08:55 +00:00
|
|
|
use App\Util\Exception\TemporaryFileException;
|
2021-09-06 23:47:28 +01:00
|
|
|
use App\Util\TemporaryFile;
|
2021-12-03 01:16:57 +00:00
|
|
|
use Component\Attachment\Entity\Attachment;
|
2021-04-18 02:17:57 +01:00
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
2021-09-06 23:47:28 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
|
2021-04-18 02:17:57 +01:00
|
|
|
use Symfony\Component\HttpFoundation\HeaderUtils;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2021-07-22 20:56:29 +01:00
|
|
|
use Symfony\Component\Mime\MimeTypes;
|
2021-04-18 02:17:57 +01:00
|
|
|
|
2021-07-20 21:17:53 +01:00
|
|
|
/**
|
|
|
|
* GNU social's File Abstraction
|
|
|
|
*
|
|
|
|
* @category Files
|
|
|
|
* @package GNUsocial
|
|
|
|
*
|
|
|
|
* @author Hugo Sales <hugo@hsal.es>
|
|
|
|
* @author Diogo Peralta Cordeiro <mail@diogo.site>
|
|
|
|
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
2021-04-18 02:17:57 +01:00
|
|
|
class GSFile
|
|
|
|
{
|
|
|
|
/**
|
2022-01-17 20:56:14 +00:00
|
|
|
* Perform file validation (checks and normalization), store the given file if needed
|
2022-03-07 17:08:55 +00:00
|
|
|
* IMPORTANT: A new attachment is stored with 1 live, a known attachment has its lives incremented
|
2021-07-20 21:17:53 +01:00
|
|
|
*
|
|
|
|
* @throws DuplicateFoundException
|
2022-03-07 17:08:55 +00:00
|
|
|
* @throws TemporaryFileException
|
2021-04-18 02:17:57 +01:00
|
|
|
*/
|
2021-10-28 17:29:57 +01:00
|
|
|
public static function storeFileAsAttachment(TemporaryFile|SymfonyFile $file, bool $check_is_supported_mimetype = true): Attachment
|
2021-04-18 02:17:57 +01:00
|
|
|
{
|
2021-07-20 21:17:53 +01:00
|
|
|
$hash = null;
|
|
|
|
Event::handle('HashFile', [$file->getPathname(), &$hash]);
|
2021-05-02 16:46:12 +01:00
|
|
|
try {
|
2021-08-14 16:47:45 +01:00
|
|
|
$attachment = DB::findOneBy('attachment', ['filehash' => $hash]);
|
2021-08-12 04:41:00 +01:00
|
|
|
// Attachment Exists
|
2021-10-10 09:26:18 +01:00
|
|
|
if (\is_null($attachment->getFilename())) {
|
2022-03-07 17:08:55 +00:00
|
|
|
// We had this attachment, but not the file, thus no filename, update meta
|
2021-12-03 01:16:57 +00:00
|
|
|
$mimetype = $attachment->getMimetype() ?? $file->getMimeType();
|
2021-09-22 15:04:45 +01:00
|
|
|
$width = $attachment->getWidth();
|
|
|
|
$height = $attachment->getHeight();
|
|
|
|
$event_map[$mimetype] = [];
|
|
|
|
$major_mime = self::mimetypeMajor($mimetype);
|
|
|
|
$event_map[$major_mime] = [];
|
2021-08-18 22:11:44 +01:00
|
|
|
if (Common::config('attachments', 'sanitize')) {
|
|
|
|
Event::handle('FileSanitizerAvailable', [&$event_map, $mimetype]);
|
2021-09-22 15:04:45 +01:00
|
|
|
} else {
|
|
|
|
Event::handle('FileMetaAvailable', [&$event_map, $mimetype]);
|
|
|
|
}
|
|
|
|
// Always prefer specific encoders
|
2021-09-23 14:52:33 +01:00
|
|
|
/** @var array<callable(TemporaryFile|SymfonyFile &$file, string &$mimetype, int &$width, int &$height): bool> $encoders */
|
2021-09-22 15:04:45 +01:00
|
|
|
$encoders = array_merge($event_map[$mimetype], $event_map[$major_mime]);
|
|
|
|
foreach ($encoders as $encoder) {
|
|
|
|
// These are all I/O params
|
|
|
|
if ($encoder($file, $mimetype, $width, $height)) {
|
|
|
|
break; // One successful File type handler plugin is enough
|
2021-08-18 22:11:44 +01:00
|
|
|
}
|
|
|
|
}
|
2021-08-12 04:41:00 +01:00
|
|
|
$attachment->setFilename($hash);
|
|
|
|
$attachment->setMimetype($mimetype);
|
|
|
|
$attachment->setWidth($width);
|
|
|
|
$attachment->setHeight($height);
|
|
|
|
$attachment->setSize($file->getSize());
|
2021-10-28 17:29:57 +01:00
|
|
|
if (!$check_is_supported_mimetype || self::isMimetypeAllowed($mimetype)) {
|
2021-10-24 15:16:37 +01:00
|
|
|
$file->move(Common::config('attachments', 'dir'), $hash);
|
|
|
|
DB::persist($attachment);
|
|
|
|
} else {
|
2021-10-28 17:29:57 +01:00
|
|
|
throw new FileNotAllowedException($mimetype);
|
2021-10-24 15:16:37 +01:00
|
|
|
}
|
2021-08-12 04:41:00 +01:00
|
|
|
}
|
2022-03-07 17:08:55 +00:00
|
|
|
// Increment lives
|
|
|
|
$attachment->livesIncrementAndGet();
|
2021-05-02 16:46:12 +01:00
|
|
|
} catch (NotFoundException) {
|
2021-08-12 04:41:00 +01:00
|
|
|
// Create an Attachment
|
2021-05-02 16:46:12 +01:00
|
|
|
// The following properly gets the mimetype with `file` or other
|
|
|
|
// available methods, so should be safe
|
2021-09-22 15:04:45 +01:00
|
|
|
$mimetype = mb_substr($file->getMimeType(), 0, 64);
|
2021-10-28 17:29:57 +01:00
|
|
|
$width = $height = null;
|
2021-09-22 15:04:45 +01:00
|
|
|
$event_map[$mimetype] = [];
|
|
|
|
$major_mime = self::mimetypeMajor($mimetype);
|
|
|
|
$event_map[$major_mime] = [];
|
2021-08-18 22:11:44 +01:00
|
|
|
if (Common::config('attachments', 'sanitize')) {
|
|
|
|
Event::handle('FileSanitizerAvailable', [&$event_map, $mimetype]);
|
2021-09-22 15:04:45 +01:00
|
|
|
} else {
|
|
|
|
Event::handle('FileMetaAvailable', [&$event_map, $mimetype]);
|
|
|
|
}
|
|
|
|
// Always prefer specific encoders
|
|
|
|
$encoders = array_merge($event_map[$mimetype], $event_map[$major_mime]);
|
|
|
|
foreach ($encoders as $encoder) {
|
|
|
|
if ($encoder($file, $mimetype, $width, $height)) {
|
|
|
|
break; // One successful sanitizer is enough
|
2021-08-18 22:11:44 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-02 16:46:12 +01:00
|
|
|
$attachment = Attachment::create([
|
2021-08-14 16:47:45 +01:00
|
|
|
'filehash' => $hash,
|
|
|
|
'mimetype' => $mimetype,
|
2021-08-10 20:24:11 +01:00
|
|
|
'filename' => $hash,
|
2021-08-14 16:47:45 +01:00
|
|
|
'size' => $file->getSize(),
|
|
|
|
'width' => $width,
|
|
|
|
'height' => $height,
|
2022-03-07 17:08:55 +00:00
|
|
|
'lives' => 1,
|
2021-05-02 16:46:12 +01:00
|
|
|
]);
|
2021-10-28 17:29:57 +01:00
|
|
|
if (!$check_is_supported_mimetype || self::isMimetypeAllowed($mimetype)) {
|
2021-10-24 15:16:37 +01:00
|
|
|
$file->move(Common::config('attachments', 'dir'), $hash);
|
|
|
|
DB::persist($attachment);
|
|
|
|
} else {
|
|
|
|
$attachment->setFilename(null);
|
|
|
|
$attachment->setMimetype(null);
|
|
|
|
$attachment->setSize(null);
|
|
|
|
$attachment->setWidth(null);
|
|
|
|
$attachment->setHeight(null);
|
|
|
|
DB::persist($attachment);
|
|
|
|
throw new FileNotAllowedException($mimetype);
|
|
|
|
}
|
2021-05-02 16:46:12 +01:00
|
|
|
Event::handle('AttachmentStoreNew', [&$attachment]);
|
2021-04-25 22:14:35 +01:00
|
|
|
}
|
2021-08-14 16:47:45 +01:00
|
|
|
return $attachment;
|
2021-04-18 02:17:57 +01:00
|
|
|
}
|
|
|
|
|
2021-10-24 15:16:37 +01:00
|
|
|
/**
|
|
|
|
* Tests against common config attachment `supported` mimetypes and `ext_blacklist`.
|
|
|
|
*
|
|
|
|
* @return bool true if allowed, false otherwise
|
|
|
|
*/
|
2021-10-28 17:29:57 +01:00
|
|
|
public static function isMimetypeAllowed(string $mimetype): bool
|
|
|
|
{
|
|
|
|
$passed_whitelist = \in_array($mimetype, array_keys(Common::config('attachments', 'supported')));
|
|
|
|
$mime = new MimeTypes();
|
|
|
|
$passed_blacklist = \count(array_intersect($mime->getExtensions($mimetype), Common::config('attachments', 'ext_blacklist'))) === 0;
|
2021-10-24 15:16:37 +01:00
|
|
|
unset($mime);
|
|
|
|
return $passed_whitelist && $passed_blacklist;
|
|
|
|
}
|
|
|
|
|
2021-04-18 02:17:57 +01:00
|
|
|
/**
|
|
|
|
* Include $filepath in the response, for viewing or downloading.
|
|
|
|
*
|
|
|
|
* @throws ServerException
|
|
|
|
*/
|
|
|
|
public static function sendFile(string $filepath, string $mimetype, ?string $output_filename, string $disposition = 'inline'): Response
|
|
|
|
{
|
2021-05-01 14:02:14 +01:00
|
|
|
if (is_file($filepath)) {
|
|
|
|
$response = new BinaryFileResponse(
|
|
|
|
$filepath,
|
|
|
|
Response::HTTP_OK,
|
|
|
|
[
|
|
|
|
'Content-Description' => 'File Transfer',
|
2021-07-22 20:56:29 +01:00
|
|
|
'Content-Type' => $mimetype,
|
|
|
|
'Content-Disposition' => HeaderUtils::makeDisposition($disposition, $output_filename ?? _m('Untitled attachment') . '.' . MimeTypes::getDefault()->getExtensions($mimetype)[0]),
|
|
|
|
'Cache-Control' => 'public',
|
2021-05-01 14:02:14 +01:00
|
|
|
],
|
2021-07-22 20:56:29 +01:00
|
|
|
public: true,
|
|
|
|
// contentDisposition: $disposition,
|
|
|
|
autoEtag: true,
|
2021-10-10 09:26:18 +01:00
|
|
|
autoLastModified: true,
|
2021-05-01 14:02:14 +01:00
|
|
|
);
|
|
|
|
if (Common::config('site', 'x_static_delivery')) {
|
2021-08-08 01:37:02 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2021-05-01 14:02:14 +01:00
|
|
|
$response->trustXSendfileTypeHeader();
|
2021-08-08 01:37:02 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2021-05-01 14:02:14 +01:00
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
} else {
|
2021-08-16 17:06:02 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
throw new NotStoredLocallyException;
|
|
|
|
// @codeCoverageIgnoreEnd
|
2021-04-18 02:17:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throw a client exception if the cache key $id doesn't contain
|
|
|
|
* exactly one entry
|
|
|
|
*/
|
2021-11-27 04:11:35 +00:00
|
|
|
public static function error($exception, $id, array $res)
|
2021-04-18 02:17:57 +01:00
|
|
|
{
|
2021-10-10 09:26:18 +01:00
|
|
|
switch (\count($res)) {
|
2021-04-18 02:17:57 +01:00
|
|
|
case 0:
|
2021-11-27 04:11:35 +00:00
|
|
|
throw new $exception();
|
2021-04-18 02:17:57 +01:00
|
|
|
case 1:
|
|
|
|
return $res[0];
|
|
|
|
default:
|
2021-08-08 01:37:02 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2021-04-18 02:17:57 +01:00
|
|
|
Log::error('Media query returned more than one result for identifier: \"' . $id . '\"');
|
2021-08-08 01:37:02 +01:00
|
|
|
throw new ServerException(_m('Internal server error'));
|
|
|
|
// @codeCoverageIgnoreEnd
|
2021-04-18 02:17:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the file info by id
|
|
|
|
*
|
|
|
|
* Returns the file's hash, mimetype and title
|
|
|
|
*/
|
|
|
|
public static function getFileInfo(int $id)
|
|
|
|
{
|
2021-10-10 09:26:18 +01:00
|
|
|
return self::error(
|
|
|
|
NoSuchFileException::class,
|
2021-04-18 02:17:57 +01:00
|
|
|
$id,
|
2021-10-10 09:26:18 +01:00
|
|
|
Cache::get(
|
|
|
|
"file-info-{$id}",
|
2021-04-18 02:17:57 +01:00
|
|
|
function () use ($id) {
|
2021-10-10 09:26:18 +01:00
|
|
|
return DB::dql(
|
|
|
|
'select at.filename, at.mimetype '
|
2021-12-02 15:12:31 +00:00
|
|
|
. 'from Component\Attachment\Entity\Attachment at '
|
2021-10-10 09:26:18 +01:00
|
|
|
. 'where at.id = :id',
|
|
|
|
['id' => $id],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2021-04-18 02:17:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----- Attachment ------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the attachment file info by id
|
|
|
|
*
|
|
|
|
* Returns the attachment file's hash, mimetype, title and path
|
|
|
|
*/
|
|
|
|
public static function getAttachmentFileInfo(int $id): array
|
|
|
|
{
|
2021-08-12 04:58:34 +01:00
|
|
|
$res = self::getFileInfo($id);
|
2021-10-10 09:26:18 +01:00
|
|
|
if (!\is_null($res['filename'])) {
|
2021-08-12 04:58:34 +01:00
|
|
|
$res['filepath'] = Common::config('attachments', 'dir') . $res['filename'];
|
|
|
|
}
|
2021-04-18 02:17:57 +01:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the minor part of a mimetype. image/webp -> image
|
|
|
|
*/
|
2021-07-28 22:10:32 +01:00
|
|
|
public static function mimetypeMajor(string $mime): string
|
2021-04-18 02:17:57 +01:00
|
|
|
{
|
2021-04-25 22:14:35 +01:00
|
|
|
return explode('/', self::mimetypeBare($mime))[0];
|
2021-04-18 02:17:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the minor part of a mimetype. image/webp -> webp
|
|
|
|
*/
|
2021-07-28 22:10:32 +01:00
|
|
|
public static function mimetypeMinor(string $mime): string
|
2021-04-18 02:17:57 +01:00
|
|
|
{
|
2021-04-25 22:14:35 +01:00
|
|
|
return explode('/', self::mimetypeBare($mime))[1];
|
2021-04-18 02:17:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get only the mimetype and not additional info (separated from bare mime with semi-colon)
|
|
|
|
*/
|
2021-07-28 22:10:32 +01:00
|
|
|
public static function mimetypeBare(string $mimetype): string
|
2021-04-18 02:17:57 +01:00
|
|
|
{
|
|
|
|
$mimetype = mb_strtolower($mimetype);
|
|
|
|
if (($semicolon = mb_strpos($mimetype, ';')) !== false) {
|
|
|
|
$mimetype = mb_substr($mimetype, 0, $semicolon);
|
|
|
|
}
|
|
|
|
return trim($mimetype);
|
|
|
|
}
|
2021-07-22 20:56:29 +01:00
|
|
|
|
|
|
|
/**
|
2021-08-14 16:47:45 +01:00
|
|
|
* Given an attachment filename and mimetype allows to generate the most appropriate filename.
|
2021-07-22 20:56:29 +01:00
|
|
|
*
|
2021-08-14 16:47:45 +01:00
|
|
|
* @param string $title Original filename with or without extension
|
|
|
|
* @param string $mimetype Original mimetype of the file
|
|
|
|
* @param null|string $ext Extension we believe to be best
|
|
|
|
* @param bool $force Should we force the extension we believe to be best? Defaults to false
|
2021-07-22 20:56:29 +01:00
|
|
|
*
|
2021-12-27 02:47:04 +00:00
|
|
|
* @return null|string the most appropriate filename or null if we deem it impossible
|
2021-07-22 20:56:29 +01:00
|
|
|
*/
|
2021-10-10 09:26:18 +01:00
|
|
|
public static function ensureFilenameWithProperExtension(string $title, string $mimetype, ?string $ext = null, bool $force = false): string|null
|
2021-07-22 20:56:29 +01:00
|
|
|
{
|
|
|
|
$valid_extensions = MimeTypes::getDefault()->getExtensions($mimetype);
|
|
|
|
|
|
|
|
// If title seems to be a filename with an extension
|
2021-08-16 17:10:33 +01:00
|
|
|
$pathinfo = pathinfo($title);
|
|
|
|
if ($pathinfo['extension'] ?? '' != '') {
|
|
|
|
$title_without_extension = $pathinfo['filename'];
|
|
|
|
$original_extension = $pathinfo['extension'];
|
2021-10-10 09:26:18 +01:00
|
|
|
if (empty(MimeTypes::getDefault()->getMimeTypes($original_extension)) || !\in_array($original_extension, $valid_extensions)) {
|
2021-07-22 20:56:29 +01:00
|
|
|
unset($title_without_extension, $original_extension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-16 17:10:33 +01:00
|
|
|
$fallback = function ($title) use ($ext) {
|
2021-10-10 09:26:18 +01:00
|
|
|
if (!\is_null($ext)) {
|
2021-08-16 17:10:33 +01:00
|
|
|
return ($title) . ".{$ext}";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-22 20:56:29 +01:00
|
|
|
if ($force) {
|
2021-08-16 17:10:33 +01:00
|
|
|
return $fallback($title_without_extension ?? $title);
|
2021-07-22 20:56:29 +01:00
|
|
|
} else {
|
|
|
|
if (isset($original_extension)) {
|
|
|
|
return $title;
|
|
|
|
} else {
|
|
|
|
if (!empty($valid_extensions)) {
|
|
|
|
return "{$title}.{$valid_extensions[0]}";
|
|
|
|
} else {
|
2021-08-16 17:10:33 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
return $fallback($title_without_extension ?? $title);
|
|
|
|
// @codeCoverageIgnoreEnd
|
2021-07-22 20:56:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-18 02:17:57 +01:00
|
|
|
}
|