2015-07-18 02:49:29 -05:00
|
|
|
<?php
|
2016-02-05 05:08:25 -06:00
|
|
|
declare(strict_types = 1);
|
2015-07-18 02:49:29 -05:00
|
|
|
namespace FireflyIII\Helpers\Attachments;
|
|
|
|
|
|
|
|
use Auth;
|
2015-07-18 15:17:31 -05:00
|
|
|
use Config;
|
2015-07-18 02:49:29 -05:00
|
|
|
use Crypt;
|
|
|
|
use FireflyIII\Models\Attachment;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Support\MessageBag;
|
|
|
|
use Input;
|
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
2016-02-16 06:28:55 -06:00
|
|
|
use Log;
|
2015-07-18 02:49:29 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AttachmentHelper
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Helpers\Attachments
|
|
|
|
*/
|
|
|
|
class AttachmentHelper implements AttachmentHelperInterface
|
|
|
|
{
|
|
|
|
|
2015-07-18 15:17:31 -05:00
|
|
|
/** @var MessageBag */
|
2015-07-18 02:49:29 -05:00
|
|
|
public $errors;
|
2015-07-18 15:17:31 -05:00
|
|
|
/** @var MessageBag */
|
2015-07-18 02:49:29 -05:00
|
|
|
public $messages;
|
2016-01-19 06:59:54 -06:00
|
|
|
/** @var array */
|
|
|
|
protected $allowedMimes;
|
|
|
|
/** @var int */
|
|
|
|
protected $maxUploadSize;
|
2015-07-18 02:49:29 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2015-07-18 15:17:31 -05:00
|
|
|
$this->maxUploadSize = Config::get('firefly.maxUploadSize');
|
|
|
|
$this->allowedMimes = Config::get('firefly.allowedMimes');
|
|
|
|
$this->errors = new MessageBag;
|
|
|
|
$this->messages = new MessageBag;
|
2015-07-18 02:49:29 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
public function getAttachmentLocation(Attachment $attachment)
|
|
|
|
{
|
|
|
|
$path = storage_path('upload') . DIRECTORY_SEPARATOR . 'at-' . $attachment->id . '.data';
|
|
|
|
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
2016-01-19 06:59:54 -06:00
|
|
|
/**
|
|
|
|
* @return MessageBag
|
|
|
|
*/
|
|
|
|
public function getErrors()
|
|
|
|
{
|
|
|
|
return $this->errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return MessageBag
|
|
|
|
*/
|
|
|
|
public function getMessages()
|
|
|
|
{
|
|
|
|
return $this->messages;
|
|
|
|
}
|
|
|
|
|
2015-07-18 02:49:29 -05:00
|
|
|
/**
|
|
|
|
* @param Model $model
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function saveAttachmentsForModel(Model $model)
|
|
|
|
{
|
2016-02-16 06:25:03 -06:00
|
|
|
$files = null;
|
2016-02-16 06:28:55 -06:00
|
|
|
try {
|
|
|
|
if(Input::hasFile('attachments')) {
|
|
|
|
$files = Input::file('attachments');
|
|
|
|
}
|
2016-02-16 06:30:50 -06:00
|
|
|
} catch (TypeError $e) {
|
2016-02-16 06:28:55 -06:00
|
|
|
// Log it, do nothing else.
|
|
|
|
Log::error($e->getMessage());
|
|
|
|
}
|
2015-07-18 14:46:16 -05:00
|
|
|
|
2015-07-25 11:33:19 -05:00
|
|
|
if (is_array($files)) {
|
|
|
|
foreach ($files as $entry) {
|
|
|
|
if (!is_null($entry)) {
|
|
|
|
$this->processFile($entry, $model);
|
|
|
|
}
|
2015-07-18 02:49:29 -05:00
|
|
|
}
|
2015-07-25 11:33:19 -05:00
|
|
|
} else {
|
2015-12-28 00:41:44 -06:00
|
|
|
if (!is_null($files)) {
|
|
|
|
$this->processFile($files, $model);
|
|
|
|
}
|
2015-07-18 02:49:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param UploadedFile $file
|
|
|
|
* @param Model $model
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function hasFile(UploadedFile $file, Model $model)
|
|
|
|
{
|
2015-07-18 14:46:16 -05:00
|
|
|
$md5 = md5_file($file->getRealPath());
|
|
|
|
$name = $file->getClientOriginalName();
|
2015-07-18 02:49:29 -05:00
|
|
|
$class = get_class($model);
|
|
|
|
$count = Auth::user()->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count();
|
|
|
|
|
2015-07-18 14:46:16 -05:00
|
|
|
if ($count > 0) {
|
2016-01-27 11:31:44 -06:00
|
|
|
$msg = (string)trans('validation.file_already_attached', ['name' => $name]);
|
2015-07-18 15:17:31 -05:00
|
|
|
$this->errors->add('attachments', $msg);
|
2015-07-18 14:46:16 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-07-18 02:49:29 -05:00
|
|
|
}
|
|
|
|
|
2015-07-18 15:17:31 -05:00
|
|
|
/**
|
|
|
|
* @param UploadedFile $file
|
|
|
|
* @param Model $model
|
|
|
|
*
|
|
|
|
* @return bool|Attachment
|
|
|
|
*/
|
|
|
|
protected function processFile(UploadedFile $file, Model $model)
|
|
|
|
{
|
|
|
|
$validation = $this->validateUpload($file, $model);
|
|
|
|
if ($validation === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$attachment = new Attachment; // create Attachment object.
|
2015-07-18 02:49:29 -05:00
|
|
|
$attachment->user()->associate(Auth::user());
|
|
|
|
$attachment->attachable()->associate($model);
|
2015-07-18 14:46:16 -05:00
|
|
|
$attachment->md5 = md5_file($file->getRealPath());
|
2015-07-18 02:49:29 -05:00
|
|
|
$attachment->filename = $file->getClientOriginalName();
|
|
|
|
$attachment->mime = $file->getMimeType();
|
|
|
|
$attachment->size = $file->getSize();
|
|
|
|
$attachment->uploaded = 0;
|
|
|
|
$attachment->save();
|
|
|
|
|
2015-07-18 15:17:31 -05:00
|
|
|
$path = $file->getRealPath(); // encrypt and move file to storage.
|
2015-07-18 02:49:29 -05:00
|
|
|
$content = file_get_contents($path);
|
|
|
|
$encrypted = Crypt::encrypt($content);
|
|
|
|
|
|
|
|
// store it:
|
2015-07-18 15:17:31 -05:00
|
|
|
$upload = $this->getAttachmentLocation($attachment);
|
2015-07-18 02:49:29 -05:00
|
|
|
if (is_writable(dirname($upload))) {
|
|
|
|
file_put_contents($upload, $encrypted);
|
|
|
|
}
|
|
|
|
|
2015-07-18 15:17:31 -05:00
|
|
|
$attachment->uploaded = 1; // update attachment
|
2015-07-18 02:49:29 -05:00
|
|
|
$attachment->save();
|
|
|
|
|
2015-07-18 15:17:31 -05:00
|
|
|
$name = e($file->getClientOriginalName()); // add message:
|
2016-01-27 11:31:44 -06:00
|
|
|
$msg = (string)trans('validation.file_attached', ['name' => $name]);
|
2015-07-18 15:17:31 -05:00
|
|
|
$this->messages->add('attachments', $msg);
|
2015-07-18 02:49:29 -05:00
|
|
|
|
|
|
|
// return it.
|
|
|
|
return $attachment;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-18 14:46:16 -05:00
|
|
|
/**
|
|
|
|
* @param UploadedFile $file
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-07-18 02:49:29 -05:00
|
|
|
protected function validMime(UploadedFile $file)
|
|
|
|
{
|
2015-07-18 15:17:31 -05:00
|
|
|
$mime = e($file->getMimeType());
|
|
|
|
$name = e($file->getClientOriginalName());
|
2015-07-18 02:49:29 -05:00
|
|
|
|
|
|
|
if (!in_array($mime, $this->allowedMimes)) {
|
2016-01-27 11:31:44 -06:00
|
|
|
$msg = (string)trans('validation.file_invalid_mime', ['name' => $name, 'mime' => $mime]);
|
2015-07-18 15:17:31 -05:00
|
|
|
$this->errors->add('attachments', $msg);
|
2015-07-18 02:49:29 -05:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-18 14:46:16 -05:00
|
|
|
/**
|
|
|
|
* @param UploadedFile $file
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-07-18 02:49:29 -05:00
|
|
|
protected function validSize(UploadedFile $file)
|
|
|
|
{
|
|
|
|
$size = $file->getSize();
|
2015-07-18 15:17:31 -05:00
|
|
|
$name = e($file->getClientOriginalName());
|
2015-07-18 02:49:29 -05:00
|
|
|
if ($size > $this->maxUploadSize) {
|
2016-01-27 11:31:44 -06:00
|
|
|
$msg = (string)trans('validation.file_too_large', ['name' => $name]);
|
2015-07-18 15:17:31 -05:00
|
|
|
$this->errors->add('attachments', $msg);
|
2015-07-18 02:49:29 -05:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-01-19 06:59:54 -06:00
|
|
|
protected function validateUpload(UploadedFile $file, Model $model)
|
2015-07-18 02:49:29 -05:00
|
|
|
{
|
2016-01-19 06:59:54 -06:00
|
|
|
if (!$this->validMime($file)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!$this->validSize($file)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($this->hasFile($file, $model)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-18 02:49:29 -05:00
|
|
|
|
2016-01-19 06:59:54 -06:00
|
|
|
return true;
|
2015-07-18 02:49:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-19 02:38:44 -05:00
|
|
|
}
|