| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-16 07:33:12 +02:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2016-09-21 19:16:47 +02:00
										 |  |  |  * ScanAttachments.php | 
					
						
							| 
									
										
										
										
											2023-04-16 07:33:12 +02:00
										 |  |  |  * Copyright (c) 2023 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2016-09-21 19:16:47 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * 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 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02: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/>. | 
					
						
							| 
									
										
										
										
											2016-09-21 19:16:47 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-16 07:33:12 +02:00
										 |  |  | namespace FireflyIII\Console\Commands\System; | 
					
						
							| 
									
										
										
										
											2016-09-21 19:16:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-20 07:16:56 +02:00
										 |  |  | use FireflyIII\Console\Commands\ShowsFriendlyMessages; | 
					
						
							| 
									
										
										
										
											2016-09-21 19:16:47 +02:00
										 |  |  | use FireflyIII\Models\Attachment; | 
					
						
							|  |  |  | use Illuminate\Console\Command; | 
					
						
							|  |  |  | use Illuminate\Contracts\Encryption\DecryptException; | 
					
						
							| 
									
										
										
										
											2025-02-23 12:47:04 +01:00
										 |  |  | use Illuminate\Support\Facades\Crypt; | 
					
						
							|  |  |  | use Illuminate\Support\Facades\Storage; | 
					
						
							| 
									
										
										
										
											2016-09-21 19:16:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-27 17:02:18 +02:00
										 |  |  | use function Safe\tempnam; | 
					
						
							|  |  |  | use function Safe\file_put_contents; | 
					
						
							|  |  |  | use function Safe\md5_file; | 
					
						
							|  |  |  | use function Safe\mime_content_type; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-27 09:30:41 +01:00
										 |  |  | class ScansAttachments extends Command | 
					
						
							| 
									
										
										
										
											2016-09-21 19:16:47 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-20 07:16:56 +02:00
										 |  |  |     use ShowsFriendlyMessages; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-16 07:33:12 +02:00
										 |  |  |     protected $description = 'Rescan all attachments and re-set the correct MD5 hash and mime.'; | 
					
						
							| 
									
										
										
										
											2016-09-21 19:16:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |     protected $signature   = 'firefly-iii:scan-attachments'; | 
					
						
							| 
									
										
										
										
											2016-09-21 19:16:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Execute the console command. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-07-22 10:05:06 +02:00
										 |  |  |     public function handle(): int | 
					
						
							| 
									
										
										
										
											2016-09-21 19:16:47 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         $attachments = Attachment::get(); | 
					
						
							| 
									
										
										
										
											2025-02-23 12:47:04 +01:00
										 |  |  |         $disk        = Storage::disk('upload'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-21 19:16:47 +02:00
										 |  |  |         /** @var Attachment $attachment */ | 
					
						
							|  |  |  |         foreach ($attachments as $attachment) { | 
					
						
							| 
									
										
										
										
											2022-03-19 08:10:42 +01:00
										 |  |  |             $fileName         = $attachment->fileName(); | 
					
						
							|  |  |  |             $encryptedContent = $disk->get($fileName); | 
					
						
							|  |  |  |             if (null === $encryptedContent) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |                 app('log')->error(sprintf('No content for attachment #%d under filename "%s"', $attachment->id, $fileName)); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-21 19:16:47 +02:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-21 19:16:47 +02:00
										 |  |  |             try { | 
					
						
							| 
									
										
										
										
											2025-02-23 12:47:04 +01:00
										 |  |  |                 $decryptedContent = Crypt::decrypt($encryptedContent); // verified
 | 
					
						
							| 
									
										
										
										
											2016-09-21 19:16:47 +02:00
										 |  |  |             } catch (DecryptException $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |                 app('log')->error(sprintf('Could not decrypt data of attachment #%d: %s', $attachment->id, $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2019-08-25 07:25:01 +02:00
										 |  |  |                 $decryptedContent = $encryptedContent; | 
					
						
							| 
									
										
										
										
											2016-09-21 19:16:47 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-05-27 17:02:18 +02:00
										 |  |  |             $tempFileName     = tempnam(sys_get_temp_dir(), 'FireflyIII'); | 
					
						
							| 
									
										
										
										
											2023-12-10 06:45:59 +01:00
										 |  |  |             if (false === $tempFileName) { | 
					
						
							| 
									
										
										
										
											2023-11-26 12:10:42 +01:00
										 |  |  |                 app('log')->error(sprintf('Could not create temporary file for attachment #%d', $attachment->id)); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-26 12:10:42 +01:00
										 |  |  |                 exit(1); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-05-27 17:02:18 +02:00
										 |  |  |             file_put_contents($tempFileName, $decryptedContent); | 
					
						
							|  |  |  |             $attachment->md5  = (string) md5_file($tempFileName); | 
					
						
							|  |  |  |             $attachment->mime = (string) mime_content_type($tempFileName); | 
					
						
							| 
									
										
										
										
											2016-09-21 19:16:47 +02:00
										 |  |  |             $attachment->save(); | 
					
						
							| 
									
										
										
										
											2023-06-20 07:16:56 +02:00
										 |  |  |             $this->friendlyInfo(sprintf('Fixed attachment #%d', $attachment->id)); | 
					
						
							| 
									
										
										
										
											2016-09-21 19:16:47 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-03-21 15:43:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-22 10:05:06 +02:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2016-09-21 19:16:47 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } |