firefly-iii/app/Helpers/Attachments/AttachmentHelper.php

368 lines
11 KiB
PHP
Raw Normal View History

2015-07-18 02:49:29 -05:00
<?php
/**
* AttachmentHelper.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-07-18 02:49:29 -05:00
namespace FireflyIII\Helpers\Attachments;
use Crypt;
use FireflyIII\Models\Attachment;
use Illuminate\Contracts\Encryption\DecryptException;
2015-07-18 02:49:29 -05:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
2015-07-18 02:49:29 -05:00
use Illuminate\Support\MessageBag;
2017-06-12 12:58:32 -05:00
use Log;
use Storage;
2016-02-16 11:39:03 -06:00
use Symfony\Component\HttpFoundation\File\UploadedFile;
2017-06-05 04:12:50 -05:00
2015-07-18 02:49:29 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class AttachmentHelper.
2015-07-18 02:49:29 -05:00
*/
class AttachmentHelper implements AttachmentHelperInterface
{
2018-07-07 14:17:46 -05:00
/** @var Collection All attachments */
public $attachments;
2018-07-07 14:17:46 -05:00
/** @var MessageBag All errors */
2015-07-18 02:49:29 -05:00
public $errors;
2018-07-07 14:17:46 -05:00
/** @var MessageBag All messages */
2015-07-18 02:49:29 -05:00
public $messages;
2018-07-07 14:17:46 -05:00
/** @var array Allowed mimes */
2017-01-14 12:43:33 -06:00
protected $allowedMimes = [];
2018-07-07 14:17:46 -05:00
/** @var int Max upload size. */
2017-01-14 12:43:33 -06:00
protected $maxUploadSize = 0;
2015-07-18 02:49:29 -05:00
2018-07-07 14:17:46 -05:00
/** @var \Illuminate\Contracts\Filesystem\Filesystem The disk where attachments are stored. */
protected $uploadDisk;
2018-02-28 14:32:59 -06:00
2015-07-18 02:49:29 -05:00
/**
2018-02-28 14:32:59 -06:00
* AttachmentHelper constructor.
2015-07-18 02:49:29 -05:00
*/
public function __construct()
{
2018-04-02 07:42:07 -05:00
$this->maxUploadSize = (int)config('firefly.maxUploadSize');
2017-02-16 23:42:36 -06:00
$this->allowedMimes = (array)config('firefly.allowedMimes');
2015-07-18 15:17:31 -05:00
$this->errors = new MessageBag;
$this->messages = new MessageBag;
$this->attachments = new Collection;
$this->uploadDisk = Storage::disk('upload');
if ('testing' === env('APP_ENV')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
}
2015-07-18 02:49:29 -05:00
}
/**
2018-07-07 14:17:46 -05:00
* Returns the content of an attachment.
*
2018-06-01 15:04:52 -05:00
* @codeCoverageIgnore
*
* @param Attachment $attachment
*
* @return string
*/
public function getAttachmentContent(Attachment $attachment): string
{
2018-07-26 21:46:21 -05:00
try {
$content = Crypt::decrypt($this->uploadDisk->get(sprintf('at-%d.data', $attachment->id)));
2018-08-06 12:14:30 -05:00
} catch (DecryptException $e) {
2018-07-07 14:17:46 -05:00
Log::error(sprintf('Could not decrypt data of attachment #%d: %s', $attachment->id, $e->getMessage()));
2018-07-26 21:46:21 -05:00
$content = '';
}
return $content;
}
2015-07-18 14:32:31 -05:00
/**
2018-07-07 14:17:46 -05:00
* Returns the file location for an attachment,
*
2015-07-18 14:32:31 -05:00
* @param Attachment $attachment
*
2015-07-18 15:17:31 -05:00
* @return string
2015-07-18 14:32:31 -05:00
*/
2016-02-18 00:21:48 -06:00
public function getAttachmentLocation(Attachment $attachment): string
2015-07-18 14:32:31 -05:00
{
2018-04-02 07:42:07 -05:00
$path = sprintf('%s%sat-%d.data', storage_path('upload'), DIRECTORY_SEPARATOR, (int)$attachment->id);
2015-07-18 14:32:31 -05:00
return $path;
}
/**
2018-07-07 14:17:46 -05:00
* Get all attachments.
*
* @return Collection
*/
public function getAttachments(): Collection
{
return $this->attachments;
}
2016-01-19 06:59:54 -06:00
/**
2018-07-07 14:17:46 -05:00
* Get all errors.
*
2016-01-19 06:59:54 -06:00
* @return MessageBag
*/
2016-02-18 00:21:48 -06:00
public function getErrors(): MessageBag
2016-01-19 06:59:54 -06:00
{
return $this->errors;
}
/**
2018-07-07 14:17:46 -05:00
* Get all messages.
*
2016-01-19 06:59:54 -06:00
* @return MessageBag
*/
2016-02-18 00:21:48 -06:00
public function getMessages(): MessageBag
2016-01-19 06:59:54 -06:00
{
return $this->messages;
}
2018-07-07 14:17:46 -05:00
/** @noinspection MultipleReturnStatementsInspection */
2018-06-23 23:51:22 -05:00
/**
* Uploads a file as a string.
*
* @param Attachment $attachment
* @param string $content
*
* @return bool
*/
public function saveAttachmentFromApi(Attachment $attachment, string $content): bool
{
$resource = tmpfile();
if (false === $resource) {
2018-08-25 13:45:42 -05:00
// @codeCoverageIgnoreStart
2018-06-23 23:51:22 -05:00
Log::error('Cannot create temp-file for file upload.');
return false;
2018-08-25 13:45:42 -05:00
// @codeCoverageIgnoreEnd
2018-06-23 23:51:22 -05:00
}
$path = stream_get_meta_data($resource)['uri'];
fwrite($resource, $content);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $path);
$allowedMime = config('firefly.allowedMimes');
if (!\in_array($mime, $allowedMime, true)) {
Log::error(sprintf('Mime type %s is not allowed for API file upload.', $mime));
return false;
}
// is allowed? Save the file!
$encrypted = Crypt::encrypt($content);
$this->uploadDisk->put($attachment->fileName(), $encrypted);
// update attachment.
$attachment->md5 = md5_file($path);
$attachment->mime = $mime;
$attachment->size = \strlen($content);
$attachment->uploaded = true;
2018-06-23 23:51:22 -05:00
$attachment->save();
return true;
}
2015-07-18 02:49:29 -05:00
/**
2018-07-07 14:17:46 -05:00
* Save attachments that get uploaded with models, through the app.
*
2018-09-22 23:57:37 -05:00
* @param object $model
2016-12-28 06:02:56 -06:00
* @param array|null $files
2015-07-18 02:49:29 -05:00
*
* @return bool
2018-09-22 23:57:37 -05:00
* @throws \Illuminate\Contracts\Encryption\EncryptException
2015-07-18 02:49:29 -05:00
*/
2018-09-22 23:57:37 -05:00
public function saveAttachmentsForModel(object $model, ?array $files): bool
2015-07-18 02:49:29 -05:00
{
2018-09-22 23:57:37 -05:00
if(!($model instanceof Model)) {
return false;
}
2018-04-27 23:23:13 -05:00
Log::debug(sprintf('Now in saveAttachmentsForModel for model %s', \get_class($model)));
if (\is_array($files)) {
2017-11-26 02:54:09 -06:00
Log::debug('$files is an array.');
2017-10-27 05:59:43 -05:00
/** @var UploadedFile $entry */
2016-12-28 06:02:56 -06:00
foreach ($files as $entry) {
2017-11-15 05:25:49 -06:00
if (null !== $entry) {
2016-12-28 06:02:56 -06:00
$this->processFile($entry, $model);
}
}
2017-10-27 05:59:43 -05:00
Log::debug('Done processing uploads.');
2015-07-18 02:49:29 -05:00
}
2018-07-07 14:17:46 -05:00
if (!\is_array($files) || (\is_array($files) && 0 === \count($files))) {
Log::debug('Array of files is not an array. Probably nothing uploaded. Will not store attachments.');
}
2015-07-18 02:49:29 -05:00
return true;
}
/**
2018-07-07 14:17:46 -05:00
* Check if a model already has this file attached.
*
2015-07-18 02:49:29 -05:00
* @param UploadedFile $file
* @param Model $model
*
* @return bool
*/
2016-02-18 00:21:48 -06:00
protected function hasFile(UploadedFile $file, Model $model): bool
2015-07-18 02:49:29 -05:00
{
$md5 = md5_file($file->getRealPath());
$name = $file->getClientOriginalName();
2018-04-27 23:23:13 -05:00
$class = \get_class($model);
2018-07-07 14:17:46 -05:00
/** @noinspection PhpUndefinedFieldInspection */
$count = $model->user->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count();
$result = false;
if ($count > 0) {
$msg = (string)trans('validation.file_already_attached', ['name' => $name]);
2015-07-18 15:17:31 -05:00
$this->errors->add('attachments', $msg);
2017-10-27 05:59:43 -05:00
Log::error($msg);
2018-07-07 14:17:46 -05:00
$result = true;
}
2018-07-07 14:17:46 -05:00
return $result;
2015-07-18 02:49:29 -05:00
}
2015-07-18 15:17:31 -05:00
/**
2018-07-07 14:17:46 -05:00
* Process the upload of a file.
*
2015-07-18 15:17:31 -05:00
* @param UploadedFile $file
* @param Model $model
*
2018-07-07 14:17:46 -05:00
* @return Attachment|null
* @throws \Illuminate\Contracts\Encryption\EncryptException
2015-07-18 15:17:31 -05:00
*/
2018-07-07 14:17:46 -05:00
protected function processFile(UploadedFile $file, Model $model): ?Attachment
2015-07-18 15:17:31 -05:00
{
2017-11-26 02:54:09 -06:00
Log::debug('Now in processFile()');
2015-07-18 15:17:31 -05:00
$validation = $this->validateUpload($file, $model);
2018-07-07 14:17:46 -05:00
$attachment = null;
if (false !== $validation) {
$attachment = new Attachment; // create Attachment object.
/** @noinspection PhpUndefinedFieldInspection */
$attachment->user()->associate($model->user);
$attachment->attachable()->associate($model);
$attachment->md5 = md5_file($file->getRealPath());
$attachment->filename = $file->getClientOriginalName();
$attachment->mime = $file->getMimeType();
$attachment->size = $file->getSize();
$attachment->uploaded = false;
2018-07-07 14:17:46 -05:00
$attachment->save();
Log::debug('Created attachment:', $attachment->toArray());
$fileObject = $file->openFile('r');
$fileObject->rewind();
$content = $fileObject->fread($file->getSize());
$encrypted = Crypt::encrypt($content);
Log::debug(sprintf('Full file length is %d and upload size is %d.', \strlen($content), $file->getSize()));
Log::debug(sprintf('Encrypted content is %d', \strlen($encrypted)));
// store it:
$this->uploadDisk->put($attachment->fileName(), $encrypted);
2018-07-24 23:45:25 -05:00
$attachment->uploaded = true; // update attachment
2018-07-07 14:17:46 -05:00
$attachment->save();
$this->attachments->push($attachment);
$name = e($file->getClientOriginalName()); // add message:
$msg = (string)trans('validation.file_attached', ['name' => $name]);
$this->messages->add('attachments', $msg);
2015-07-18 15:17:31 -05:00
}
2015-07-18 02:49:29 -05:00
return $attachment;
}
/**
2018-07-07 14:17:46 -05:00
* Verify if the mime of a file is valid.
*
* @param UploadedFile $file
*
* @return bool
*/
2016-02-18 00:21:48 -06:00
protected function validMime(UploadedFile $file): bool
2015-07-18 02:49:29 -05:00
{
2017-11-26 02:54:09 -06:00
Log::debug('Now in validMime()');
2015-07-18 15:17:31 -05:00
$mime = e($file->getMimeType());
$name = e($file->getClientOriginalName());
Log::debug(sprintf('Name is %s, and mime is %s', $name, $mime));
2017-11-26 02:54:09 -06:00
Log::debug('Valid mimes are', $this->allowedMimes);
2018-07-07 14:17:46 -05:00
$result = true;
2015-07-18 02:49:29 -05:00
2018-06-23 23:51:22 -05:00
if (!\in_array($mime, $this->allowedMimes, true)) {
$msg = (string)trans('validation.file_invalid_mime', ['name' => $name, 'mime' => $mime]);
2015-07-18 15:17:31 -05:00
$this->errors->add('attachments', $msg);
2017-10-27 05:59:43 -05:00
Log::error($msg);
2015-07-18 02:49:29 -05:00
2018-07-07 14:17:46 -05:00
$result = false;
2015-07-18 02:49:29 -05:00
}
2018-07-07 14:17:46 -05:00
return $result;
2015-07-18 02:49:29 -05:00
}
/**
2018-07-07 14:17:46 -05:00
* Verify if the size of a file is valid.
*
* @codeCoverageIgnore
2017-06-05 04:12:50 -05:00
*
* @param UploadedFile $file
*
* @return bool
*/
2016-02-18 00:21:48 -06:00
protected function validSize(UploadedFile $file): bool
2015-07-18 02:49:29 -05:00
{
2018-07-07 14:17:46 -05:00
$size = $file->getSize();
$name = e($file->getClientOriginalName());
$result = true;
2015-07-18 02:49:29 -05:00
if ($size > $this->maxUploadSize) {
$msg = (string)trans('validation.file_too_large', ['name' => $name]);
2015-07-18 15:17:31 -05:00
$this->errors->add('attachments', $msg);
2017-10-27 05:59:43 -05:00
Log::error($msg);
2015-07-18 02:49:29 -05:00
2018-07-07 14:17:46 -05:00
$result = false;
2015-07-18 02:49:29 -05:00
}
2018-07-07 14:17:46 -05:00
return $result;
2015-07-18 02:49:29 -05:00
}
/**
2018-07-07 14:17:46 -05:00
* Verify if the file was uploaded correctly.
*
2016-01-19 06:59:54 -06:00
* @param UploadedFile $file
* @param Model $model
*
* @return bool
2015-07-18 02:49:29 -05:00
*/
2016-02-18 00:21:48 -06:00
protected function validateUpload(UploadedFile $file, Model $model): bool
2015-07-18 02:49:29 -05:00
{
2017-11-26 02:54:09 -06:00
Log::debug('Now in validateUpload()');
2018-07-07 14:17:46 -05:00
$result = true;
2016-01-19 06:59:54 -06:00
if (!$this->validMime($file)) {
2018-07-07 14:17:46 -05:00
$result = false;
2016-01-19 06:59:54 -06:00
}
2018-08-25 13:45:42 -05:00
// @codeCoverageIgnoreStart
// can't seem to reach this point.
2018-07-07 14:17:46 -05:00
if (true === $result && !$this->validSize($file)) {
$result = false;
2016-01-19 06:59:54 -06:00
}
2018-08-25 13:45:42 -05:00
// @codeCoverageIgnoreEnd
2018-07-07 14:17:46 -05:00
if (true === $result && $this->hasFile($file, $model)) {
$result = false;
2016-01-19 06:59:54 -06:00
}
2015-07-18 02:49:29 -05:00
2018-07-07 14:17:46 -05:00
return $result;
2015-07-18 02:49:29 -05:00
}
2015-07-19 02:38:44 -05:00
}