1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Exceptions; 21 22use Fisharebest\Webtrees\I18N; 23use Psr\Http\Message\UploadedFileInterface; 24use RuntimeException; 25 26use function e; 27 28use const UPLOAD_ERR_CANT_WRITE; 29use const UPLOAD_ERR_EXTENSION; 30use const UPLOAD_ERR_FORM_SIZE; 31use const UPLOAD_ERR_INI_SIZE; 32use const UPLOAD_ERR_NO_FILE; 33use const UPLOAD_ERR_NO_TMP_DIR; 34use const UPLOAD_ERR_OK; 35use const UPLOAD_ERR_PARTIAL; 36 37/** 38 * Exception thrown when a file upload fails. 39 */ 40class FileUploadException extends RuntimeException 41{ 42 /** 43 * GedcomErrorException constructor. 44 * 45 * @param UploadedFileInterface|null $uploaded_file 46 */ 47 public function __construct(?UploadedFileInterface $uploaded_file) 48 { 49 if ($uploaded_file === null) { 50 parent::__construct(I18N::translate('No file was received. Please try again.')); 51 52 return; 53 } 54 55 switch ($uploaded_file->getError()) { 56 case UPLOAD_ERR_OK: 57 $message = I18N::translate('File successfully uploaded'); 58 break; 59 60 case UPLOAD_ERR_INI_SIZE: 61 case UPLOAD_ERR_FORM_SIZE: 62 // I18N: PHP internal error message - php.net/manual/en/features.file-upload.errors.php 63 $message = I18N::translate('The uploaded file exceeds the allowed size.'); 64 break; 65 66 case UPLOAD_ERR_PARTIAL: 67 // I18N: PHP internal error message - php.net/manual/en/features.file-upload.errors.php 68 $message = I18N::translate('The file was only partially uploaded. Please try again.'); 69 break; 70 71 case UPLOAD_ERR_NO_FILE: 72 // I18N: PHP internal error message - php.net/manual/en/features.file-upload.errors.php 73 $message = I18N::translate('No file was received. Please try again.'); 74 break; 75 76 case UPLOAD_ERR_NO_TMP_DIR: 77 // I18N: PHP internal error message - php.net/manual/en/features.file-upload.errors.php 78 $message = I18N::translate('The PHP temporary folder is missing.'); 79 break; 80 81 case UPLOAD_ERR_CANT_WRITE: 82 // I18N: PHP internal error message - php.net/manual/en/features.file-upload.errors.php 83 $message = I18N::translate('PHP failed to write to disk.'); 84 break; 85 86 case UPLOAD_ERR_EXTENSION: 87 // I18N: PHP internal error message - php.net/manual/en/features.file-upload.errors.php 88 $message = I18N::translate('PHP blocked the file because of its extension.'); 89 break; 90 91 default: 92 $message = 'Error: ' . $uploaded_file->getError(); 93 break; 94 } 95 96 $filename = $uploaded_file->getClientFilename() ?? '????????.???'; 97 98 $message = 99 I18N::translate('There was an error uploading your file.') . 100 '<br>' . 101 I18N::translate('%1$s: %2$s', I18N::translate('Filename'), e($filename)) . 102 '<br>' . 103 $message; 104 105 parent::__construct($message); 106 } 107} 108