xref: /webtrees/app/Exceptions/FileUploadException.php (revision 756a2ca01a3791b4718ecb07a1038fdc54f5cb4c)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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     * @param UploadedFileInterface|null $uploaded_file
44     */
45    public function __construct(?UploadedFileInterface $uploaded_file)
46    {
47        if ($uploaded_file === null) {
48            parent::__construct(I18N::translate('No file was received. Please try again.'));
49
50            return;
51        }
52
53        switch ($uploaded_file->getError()) {
54            case UPLOAD_ERR_OK:
55                $message = I18N::translate('File successfully uploaded');
56                break;
57
58            case UPLOAD_ERR_INI_SIZE:
59            case UPLOAD_ERR_FORM_SIZE:
60                // I18N: PHP internal error message - php.net/manual/en/features.file-upload.errors.php
61                $message = I18N::translate('The uploaded file exceeds the allowed size.');
62                break;
63
64            case UPLOAD_ERR_PARTIAL:
65                // I18N: PHP internal error message - php.net/manual/en/features.file-upload.errors.php
66                $message = I18N::translate('The file was only partially uploaded. Please try again.');
67                break;
68
69            case UPLOAD_ERR_NO_FILE:
70                // I18N: PHP internal error message - php.net/manual/en/features.file-upload.errors.php
71                $message = I18N::translate('No file was received. Please try again.');
72                break;
73
74            case UPLOAD_ERR_NO_TMP_DIR:
75                // I18N: PHP internal error message - php.net/manual/en/features.file-upload.errors.php
76                $message = I18N::translate('The PHP temporary folder is missing.');
77                break;
78
79            case UPLOAD_ERR_CANT_WRITE:
80                // I18N: PHP internal error message - php.net/manual/en/features.file-upload.errors.php
81                $message = I18N::translate('PHP failed to write to disk.');
82                break;
83
84            case UPLOAD_ERR_EXTENSION:
85                // I18N: PHP internal error message - php.net/manual/en/features.file-upload.errors.php
86                $message = I18N::translate('PHP blocked the file because of its extension.');
87                break;
88
89            default:
90                $message = 'Error: ' . $uploaded_file->getError();
91                break;
92        }
93
94        $filename = $uploaded_file->getClientFilename() ?? '????????.???';
95
96        $message =
97            I18N::translate('There was an error uploading your file.') .
98            '<br>' .
99            I18N::translate('%1$s: %2$s', I18N::translate('Filename'), e($filename)) .
100            '<br>' .
101            $message;
102
103        parent::__construct($message);
104    }
105}
106