Issues (2564)

app/Exceptions/FileUploadException.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Exceptions;
21
22
use Fisharebest\Webtrees\I18N;
0 ignored issues
show
The type Fisharebest\Webtrees\I18N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Psr\Http\Message\UploadedFileInterface;
24
use RuntimeException;
25
26
use function e;
27
28
use const UPLOAD_ERR_CANT_WRITE;
29
use const UPLOAD_ERR_EXTENSION;
30
use const UPLOAD_ERR_FORM_SIZE;
31
use const UPLOAD_ERR_INI_SIZE;
32
use const UPLOAD_ERR_NO_FILE;
33
use const UPLOAD_ERR_NO_TMP_DIR;
34
use const UPLOAD_ERR_OK;
35
use const UPLOAD_ERR_PARTIAL;
36
37
/**
38
 * Exception thrown when a file upload fails.
39
 */
40
class FileUploadException extends RuntimeException
41
{
42
    /**
43
     * @param UploadedFileInterface|null $uploaded_file
44
     */
45
    public function __construct(UploadedFileInterface|null $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
        if ($filename !== '') {
97
            $message = I18N::translate('%1$s: %2$s', e($filename), $message);
98
        }
99
100
        parent::__construct($message);
101
    }
102
}
103