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