firefly-iii/app/Http/Controllers/AttachmentController.php

247 lines
8.0 KiB
PHP
Raw Normal View History

2015-07-18 14:32:31 -05:00
<?php
/**
* AttachmentController.php
2020-01-31 00:32:04 -06:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program 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.
2017-10-21 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05:00
* 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.
2017-10-21 01:40:00 -05:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-07-18 14:32:31 -05:00
namespace FireflyIII\Http\Controllers;
2016-02-07 00:56:58 -06:00
use FireflyIII\Exceptions\FireflyException;
2015-07-19 02:37:28 -05:00
use FireflyIII\Http\Requests\AttachmentFormRequest;
2015-07-18 14:32:31 -05:00
use FireflyIII\Models\Attachment;
2015-07-19 02:37:28 -05:00
use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
2018-07-08 05:08:53 -05:00
use Illuminate\Http\RedirectResponse;
2017-02-17 13:14:22 -06:00
use Illuminate\Http\Request;
2017-01-14 12:43:33 -06:00
use Illuminate\Http\Response as LaravelResponse;
2015-07-18 16:51:51 -05:00
2015-07-18 14:32:31 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class AttachmentController.
2015-07-18 14:32:31 -05:00
*
*/
class AttachmentController extends Controller
{
2018-07-22 01:10:16 -05:00
/** @var AttachmentRepositoryInterface Attachment repository */
private $repository;
2015-07-18 16:51:51 -05:00
/**
2018-07-22 01:10:16 -05:00
* AttachmentController constructor.
*
2019-07-21 10:15:06 -05:00
* @codeCoverageIgnore
2015-07-18 16:51:51 -05:00
*/
public function __construct()
{
parent::__construct();
2016-10-29 00:44:46 -05:00
// translations:
$this->middleware(
function ($request, $next) {
2017-12-16 12:46:36 -06:00
app('view')->share('mainTitleIcon', 'fa-paperclip');
app('view')->share('title', (string) trans('firefly.attachments'));
$this->repository = app(AttachmentRepositoryInterface::class);
2016-10-29 00:44:46 -05:00
return $next($request);
}
);
2015-07-18 16:51:51 -05:00
}
2015-07-19 02:53:58 -05:00
/**
2018-07-22 01:10:16 -05:00
* Form to delete an attachment.
*
2015-07-19 02:53:58 -05:00
* @param Attachment $attachment
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-07-19 02:53:58 -05:00
*/
2017-12-29 02:05:35 -06:00
public function delete(Attachment $attachment)
2015-07-19 02:53:58 -05:00
{
$subTitle = (string) trans('firefly.delete_attachment', ['name' => $attachment->filename]);
2015-07-19 02:53:58 -05:00
// put previous url in session
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('attachments.delete.uri');
2015-07-19 02:53:58 -05:00
return view('attachments.delete', compact('attachment', 'subTitle'));
}
/**
2018-07-22 01:10:16 -05:00
* Destroy attachment.
*
* @param Request $request
* @param Attachment $attachment
2015-07-19 02:53:58 -05:00
*
2017-02-17 13:14:22 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-07-19 02:53:58 -05:00
*/
public function destroy(Request $request, Attachment $attachment)
2015-07-19 02:53:58 -05:00
{
$name = $attachment->filename;
$this->repository->destroy($attachment);
2015-07-19 02:53:58 -05:00
$request->session()->flash('success', (string) trans('firefly.attachment_deleted', ['name' => $name]));
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
2015-07-19 02:53:58 -05:00
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('attachments.delete.uri'));
2015-07-19 02:53:58 -05:00
}
2015-07-18 14:32:31 -05:00
/**
2018-07-22 01:10:16 -05:00
* Download attachment to PC.
*
* @param Attachment $attachment
2016-01-29 00:08:17 -06:00
*
* @throws FireflyException
2017-01-05 03:06:46 -06:00
* @return mixed
2017-11-15 05:25:49 -06:00
*
2015-07-18 14:32:31 -05:00
*/
public function download(Attachment $attachment)
2015-07-18 14:32:31 -05:00
{
if ($this->repository->exists($attachment)) {
$content = $this->repository->getContent($attachment);
2016-08-26 01:21:31 -05:00
$quoted = sprintf('"%s"', addcslashes(basename($attachment->filename), '"\\'));
2016-12-25 05:23:36 -06:00
2017-01-14 12:43:33 -06:00
/** @var LaravelResponse $response */
2019-02-13 10:38:41 -06:00
$response = response($content);
2017-01-14 12:43:33 -06:00
$response
->header('Content-Description', 'File Transfer')
->header('Content-Type', 'application/octet-stream')
->header('Content-Disposition', 'attachment; filename=' . $quoted)
->header('Content-Transfer-Encoding', 'binary')
->header('Connection', 'Keep-Alive')
->header('Expires', '0')
->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
->header('Pragma', 'public')
2019-06-07 10:58:11 -05:00
->header('Content-Length', strlen($content));
2017-01-14 12:43:33 -06:00
return $response;
2015-07-18 14:32:31 -05:00
}
2016-02-07 00:56:58 -06:00
throw new FireflyException('Could not find the indicated attachment. The file is no longer there.');
2015-07-19 02:37:28 -05:00
}
2016-01-24 08:58:16 -06:00
/**
2018-07-22 01:10:16 -05:00
* Edit an attachment.
*
* @param Request $request
2016-01-24 08:58:16 -06:00
* @param Attachment $attachment
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2016-01-24 08:58:16 -06:00
*/
2017-02-17 13:14:22 -06:00
public function edit(Request $request, Attachment $attachment)
2016-01-24 08:58:16 -06:00
{
$subTitleIcon = 'fa-pencil';
$subTitle = (string) trans('firefly.edit_attachment', ['name' => $attachment->filename]);
2016-01-24 08:58:16 -06:00
// put previous url in session if not redirect from store (not "return_to_edit").
2017-11-15 05:25:49 -06:00
if (true !== session('attachments.edit.fromUpdate')) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('attachments.edit.uri');
2016-01-24 08:58:16 -06:00
}
2017-02-17 13:14:22 -06:00
$request->session()->forget('attachments.edit.fromUpdate');
2018-07-25 23:10:17 -05:00
$preFilled = [
'notes' => $this->repository->getNoteText($attachment),
];
$request->session()->flash('preFilled', $preFilled);
2016-01-24 08:58:16 -06:00
return view('attachments.edit', compact('attachment', 'subTitleIcon', 'subTitle'));
}
2018-04-27 23:34:01 -05:00
/**
2018-07-22 01:10:16 -05:00
* Index of all attachments.
*
2018-04-27 23:34:01 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index()
{
2018-04-27 23:53:37 -05:00
$set = $this->repository->get()->reverse();
2018-04-27 23:34:01 -05:00
$set = $set->each(
function (Attachment $attachment) {
$attachment->file_exists = $this->repository->exists($attachment);
return $attachment;
}
);
return view('attachments.index', compact('set'));
}
/**
2018-07-22 01:10:16 -05:00
* Update attachment.
*
* @param AttachmentFormRequest $request
* @param Attachment $attachment
2015-07-19 02:37:28 -05:00
*
2018-07-08 05:08:53 -05:00
* @return RedirectResponse
2015-07-19 02:37:28 -05:00
*/
2018-07-08 05:08:53 -05:00
public function update(AttachmentFormRequest $request, Attachment $attachment): RedirectResponse
2015-07-19 02:37:28 -05:00
{
2016-10-22 15:03:00 -05:00
$data = $request->getAttachmentData();
$this->repository->update($attachment, $data);
2015-07-19 02:37:28 -05:00
$request->session()->flash('success', (string) trans('firefly.attachment_updated', ['name' => $attachment->filename]));
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
2015-07-19 02:37:28 -05:00
2018-07-08 05:08:53 -05:00
$redirect = redirect($this->getPreviousUri('attachments.edit.uri'));
if (1 === (int) $request->get('return_to_edit')) {
2017-03-18 05:02:02 -05:00
// @codeCoverageIgnoreStart
2017-02-17 13:14:22 -06:00
$request->session()->put('attachments.edit.fromUpdate', true);
2015-07-19 02:37:28 -05:00
2018-07-08 05:08:53 -05:00
$redirect = redirect(route('attachments.edit', [$attachment->id]))->withInput(['return_to_edit' => 1]);
2017-03-18 05:02:02 -05:00
// @codeCoverageIgnoreEnd
2015-07-19 02:37:28 -05:00
}
2015-07-18 14:32:31 -05:00
2015-07-19 02:37:28 -05:00
// redirect to previous URL.
2018-07-08 05:08:53 -05:00
return $redirect;
2015-07-18 14:32:31 -05:00
}
/**
2018-07-22 01:10:16 -05:00
* View attachment in browser.
*
* @param Attachment $attachment
*
* @throws FireflyException
* @return LaravelResponse
*/
2019-07-16 12:21:58 -05:00
public function view(Request $request, Attachment $attachment): LaravelResponse
{
if ($this->repository->exists($attachment)) {
$content = $this->repository->getContent($attachment);
2019-07-16 12:21:58 -05:00
// prevent XSS by adding a new secure header.
$csp = [
"default-src 'none'",
"object-src 'none'",
"script-src 'none'",
2019-10-11 21:04:03 -05:00
"style-src 'self' 'unsafe-inline'",
2019-07-16 12:21:58 -05:00
"base-uri 'none'",
"font-src 'none'",
"connect-src 'none'",
2019-10-11 21:04:03 -05:00
"img-src 'self'",
2019-07-16 12:21:58 -05:00
"manifest-src 'none'",
];
2018-03-10 13:30:09 -06:00
return response()->make(
$content,
200,
[
'Content-Security-Policy' => implode('; ', $csp),
'Content-Type' => $attachment->mime,
'Content-Disposition' => 'inline; filename="' . $attachment->filename . '"',
]
);
}
throw new FireflyException('Could not find the indicated attachment. The file is no longer there.');
}
2015-07-19 02:38:44 -05:00
}