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