| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * ExportController.php | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * Copyright (c) 2017 thegrumpydictator@gmail.com | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * This file is part of Firefly III. | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02: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 14:41:58 +01:00
										 |  |  |  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2016-05-20 12:27:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  | namespace FireflyIII\Http\Controllers; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2016-02-06 15:01:26 +01:00
										 |  |  | use ExpandedForm; | 
					
						
							| 
									
										
										
										
											2016-02-25 15:04:17 +01:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2016-12-11 18:34:18 +01:00
										 |  |  | use FireflyIII\Export\ProcessorInterface; | 
					
						
							| 
									
										
										
										
											2017-12-25 15:31:57 +01:00
										 |  |  | use FireflyIII\Http\Middleware\IsDemoUser; | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  | use FireflyIII\Http\Requests\ExportFormRequest; | 
					
						
							| 
									
										
										
										
											2016-05-20 11:02:07 +02:00
										 |  |  | use FireflyIII\Models\AccountType; | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  | use FireflyIII\Models\ExportJob; | 
					
						
							| 
									
										
										
										
											2016-10-10 07:25:27 +02:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2016-12-25 12:55:22 +01:00
										 |  |  | use FireflyIII\Repositories\ExportJob\ExportJobRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2017-01-14 19:43:33 +01:00
										 |  |  | use Illuminate\Http\Response as LaravelResponse; | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  | use Preferences; | 
					
						
							|  |  |  | use Response; | 
					
						
							|  |  |  | use View; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class ExportController. | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | class ExportController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::__construct(); | 
					
						
							| 
									
										
										
										
											2016-10-29 07:44:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->middleware( | 
					
						
							|  |  |  |             function ($request, $next) { | 
					
						
							| 
									
										
										
										
											2017-12-16 19:46:36 +01:00
										 |  |  |                 app('view')->share('mainTitleIcon', 'fa-file-archive-o'); | 
					
						
							|  |  |  |                 app('view')->share('title', trans('firefly.export_and_backup_data')); | 
					
						
							| 
									
										
										
										
											2016-10-29 07:44:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 return $next($request); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2017-12-25 15:31:57 +01:00
										 |  |  |         $this->middleware(IsDemoUser::class)->except(['index']); | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-03-05 13:21:36 +01:00
										 |  |  |      * @param ExportJobRepositoryInterface $repository | 
					
						
							|  |  |  |      * @param ExportJob                    $job | 
					
						
							| 
									
										
										
										
											2016-02-25 15:04:17 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2017-02-25 05:57:01 +01:00
										 |  |  |      * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-02-25 15:04:17 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-12-25 12:55:22 +01:00
										 |  |  |     public function download(ExportJobRepositoryInterface $repository, ExportJob $job) | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-02-25 15:04:17 +01:00
										 |  |  |         $file   = $job->key . '.zip'; | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |         $date   = date('Y-m-d \a\t H-i-s'); | 
					
						
							|  |  |  |         $name   = 'Export job on ' . $date . '.zip'; | 
					
						
							|  |  |  |         $quoted = sprintf('"%s"', addcslashes($name, '"\\')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-25 12:55:22 +01:00
										 |  |  |         if (!$repository->exists($job)) { | 
					
						
							| 
									
										
										
										
											2016-02-25 15:04:17 +01:00
										 |  |  |             throw new FireflyException('Against all expectations, zip file "' . $file . '" does not exist.'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-12-25 12:55:22 +01:00
										 |  |  |         $content = $repository->getContent($job); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-04 17:22:09 +01:00
										 |  |  |         $job->change('export_downloaded'); | 
					
						
							| 
									
										
										
										
											2017-01-14 19:43:33 +01:00
										 |  |  |         /** @var LaravelResponse $response */ | 
					
						
							|  |  |  |         $response = response($content, 200); | 
					
						
							|  |  |  |         $response | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |             ->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') | 
					
						
							| 
									
										
										
										
											2016-12-25 12:55:22 +01:00
										 |  |  |             ->header('Content-Length', strlen($content)); | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 19:43:33 +01:00
										 |  |  |         return $response; | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-02-05 15:41:40 +01:00
										 |  |  |      * @param ExportJob $job | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |      * @return \Illuminate\Http\JsonResponse | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getStatus(ExportJob $job) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Response::json(['status' => trans('firefly.' . $job->status)]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-03-05 13:21:36 +01:00
										 |  |  |      * @param AccountRepositoryInterface   $repository | 
					
						
							|  |  |  |      * @param ExportJobRepositoryInterface $jobs | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-08-26 09:30:52 +02:00
										 |  |  |      * @return View | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-03-05 13:21:36 +01:00
										 |  |  |     public function index(AccountRepositoryInterface $repository, ExportJobRepositoryInterface $jobs) | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         // create new export job.
 | 
					
						
							|  |  |  |         $job = $jobs->create(); | 
					
						
							|  |  |  |         // delete old ones.
 | 
					
						
							|  |  |  |         $jobs->cleanup(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // does the user have shared accounts?
 | 
					
						
							| 
									
										
										
										
											2016-10-10 07:49:39 +02:00
										 |  |  |         $accounts      = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); | 
					
						
							| 
									
										
										
										
											2016-02-06 15:01:26 +01:00
										 |  |  |         $accountList   = ExpandedForm::makeSelectList($accounts); | 
					
						
							|  |  |  |         $checked       = array_keys($accountList); | 
					
						
							| 
									
										
										
										
											2016-04-26 21:40:15 +02:00
										 |  |  |         $formats       = array_keys(config('firefly.export_formats')); | 
					
						
							|  |  |  |         $defaultFormat = Preferences::get('export_format', config('firefly.default_export_format'))->data; | 
					
						
							| 
									
										
										
										
											2016-02-23 07:31:01 +01:00
										 |  |  |         $first         = session('first')->format('Y-m-d'); | 
					
						
							| 
									
										
										
										
											2016-02-04 17:57:36 +01:00
										 |  |  |         $today         = Carbon::create()->format('Y-m-d'); | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-06 15:01:26 +01:00
										 |  |  |         return view('export.index', compact('job', 'checked', 'accountList', 'formats', 'defaultFormat', 'first', 'today')); | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-03-05 13:21:36 +01:00
										 |  |  |      * @param ExportFormRequest            $request | 
					
						
							|  |  |  |      * @param AccountRepositoryInterface   $repository | 
					
						
							|  |  |  |      * @param ExportJobRepositoryInterface $jobs | 
					
						
							| 
									
										
										
										
											2016-02-05 15:41:40 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2017-01-14 19:43:33 +01:00
										 |  |  |      * @return \Illuminate\Http\JsonResponse | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-03-05 13:21:36 +01:00
										 |  |  |     public function postIndex(ExportFormRequest $request, AccountRepositoryInterface $repository, ExportJobRepositoryInterface $jobs) | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $job      = $jobs->findByKey($request->get('job')); | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:42 +02:00
										 |  |  |         $accounts = $request->get('accounts') ?? []; | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |         $settings = [ | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:42 +02:00
										 |  |  |             'accounts'           => $repository->getAccountsById($accounts), | 
					
						
							| 
									
										
										
										
											2016-02-23 19:59:41 +01:00
										 |  |  |             'startDate'          => new Carbon($request->get('export_start_range')), | 
					
						
							|  |  |  |             'endDate'            => new Carbon($request->get('export_end_range')), | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |             'exportFormat'       => $request->get('exportFormat'), | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:42 +02:00
										 |  |  |             'includeAttachments' => $request->boolean('include_attachments'), | 
					
						
							|  |  |  |             'includeOldUploads'  => $request->boolean('include_old_uploads'), | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |             'job'                => $job, | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-27 15:46:52 +01:00
										 |  |  |         $jobs->changeStatus($job, 'export_status_make_exporter'); | 
					
						
							| 
									
										
										
										
											2016-12-11 18:34:18 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var ProcessorInterface $processor */ | 
					
						
							| 
									
										
										
										
											2017-08-18 15:32:11 +02:00
										 |  |  |         $processor = app(ProcessorInterface::class); | 
					
						
							| 
									
										
										
										
											2017-02-05 16:16:15 +01:00
										 |  |  |         $processor->setSettings($settings); | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         // Collect journals:
 | 
					
						
							| 
									
										
										
										
											2016-12-27 15:46:52 +01:00
										 |  |  |         $jobs->changeStatus($job, 'export_status_collecting_journals'); | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |         $processor->collectJournals(); | 
					
						
							| 
									
										
										
										
											2016-12-27 15:46:52 +01:00
										 |  |  |         $jobs->changeStatus($job, 'export_status_collected_journals'); | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         // Transform to exportable entries:
 | 
					
						
							| 
									
										
										
										
											2016-12-27 15:46:52 +01:00
										 |  |  |         $jobs->changeStatus($job, 'export_status_converting_to_export_format'); | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |         $processor->convertJournals(); | 
					
						
							| 
									
										
										
										
											2016-12-27 15:46:52 +01:00
										 |  |  |         $jobs->changeStatus($job, 'export_status_converted_to_export_format'); | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         // Transform to (temporary) file:
 | 
					
						
							| 
									
										
										
										
											2016-12-27 15:46:52 +01:00
										 |  |  |         $jobs->changeStatus($job, 'export_status_creating_journal_file'); | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |         $processor->exportJournals(); | 
					
						
							| 
									
										
										
										
											2016-12-27 15:46:52 +01:00
										 |  |  |         $jobs->changeStatus($job, 'export_status_created_journal_file'); | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         // Collect attachments, if applicable.
 | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |         if ($settings['includeAttachments']) { | 
					
						
							| 
									
										
										
										
											2016-12-27 15:46:52 +01:00
										 |  |  |             $jobs->changeStatus($job, 'export_status_collecting_attachments'); | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |             $processor->collectAttachments(); | 
					
						
							| 
									
										
										
										
											2016-12-27 15:46:52 +01:00
										 |  |  |             $jobs->changeStatus($job, 'export_status_collected_attachments'); | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         // Collect old uploads
 | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |         if ($settings['includeOldUploads']) { | 
					
						
							| 
									
										
										
										
											2016-12-27 15:46:52 +01:00
										 |  |  |             $jobs->changeStatus($job, 'export_status_collecting_old_uploads'); | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |             $processor->collectOldUploads(); | 
					
						
							| 
									
										
										
										
											2016-12-27 15:46:52 +01:00
										 |  |  |             $jobs->changeStatus($job, 'export_status_collected_old_uploads'); | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         // Create ZIP file:
 | 
					
						
							| 
									
										
										
										
											2016-12-27 15:46:52 +01:00
										 |  |  |         $jobs->changeStatus($job, 'export_status_creating_zip_file'); | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  |         $processor->createZipFile(); | 
					
						
							| 
									
										
										
										
											2016-12-27 15:46:52 +01:00
										 |  |  |         $jobs->changeStatus($job, 'export_status_created_zip_file'); | 
					
						
							|  |  |  |         $jobs->changeStatus($job, 'export_status_finished'); | 
					
						
							| 
									
										
										
										
											2016-02-04 17:13:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return Response::json('ok'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |