1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2026 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees\Http\RequestHandlers;
use Fisharebest\Webtrees\Exceptions\FileUploadException;
use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\Html;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Log;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Services\MediaFileService;
use Fisharebest\Webtrees\Validator;
use League\Flysystem\FilesystemException;
use League\Flysystem\UnableToCheckFileExistence;
use League\Flysystem\UnableToWriteFile;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use function e;
use function preg_match;
use function redirect;
use function route;
use function str_replace;
use function substr;
use function trim;
use const UPLOAD_ERR_NO_FILE;
use const UPLOAD_ERR_OK;
final class UploadMediaAction implements RequestHandlerInterface
{
public function __construct(
private readonly MediaFileService $media_file_service,
) {
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
$data_filesystem = Registry::filesystem()->data();
$all_folders = $this->media_file_service->allMediaFolders($data_filesystem);
foreach ($request->getUploadedFiles() as $key => $uploaded_file) {
if ($uploaded_file->getError() === UPLOAD_ERR_NO_FILE) {
continue;
}
if ($uploaded_file->getError() !== UPLOAD_ERR_OK) {
throw new FileUploadException($uploaded_file);
}
$key = substr($key, 9);
$folder = Validator::parsedBody($request)->string('folder' . $key);
$filename = Validator::parsedBody($request)->string('filename' . $key);
// If no filename specified, use the original filename.
if ($filename === '') {
$filename = $uploaded_file->getClientFilename();
}
// Validate the folder
if (!$all_folders->contains($folder)) {
break;
}
// Validate the filename.
$filename = str_replace('\\', '/', $filename);
$filename = trim($filename, '/');
if (preg_match('/([:])/', $filename, $match)) {
// Local media files cannot contain certain special characters, especially on MS Windows
FlashMessages::addMessage(I18N::translate('Filenames are not allowed to contain the character “%s”.', $match[1]));
continue;
}
if (preg_match('/(\.(php|pl|cgi|bash|sh|bat|exe|com|htm|html|shtml))$/i', $filename, $match)) {
// Do not allow obvious script files.
FlashMessages::addMessage(I18N::translate('Filenames are not allowed to have the extension “%s”.', $match[1]));
continue;
}
$path = $folder . $filename;
try {
$file_exists = $data_filesystem->fileExists($path);
} catch (FilesystemException | UnableToCheckFileExistence) {
$file_exists = false;
}
if ($file_exists) {
FlashMessages::addMessage(I18N::translate('The file %s already exists. Use another filename.', $path, 'error'));
continue;
}
// Now copy the file to the correct location.
try {
$data_filesystem->writeStream($path, $uploaded_file->getStream()->detach());
FlashMessages::addMessage(I18N::translate('The file %s has been uploaded.', Html::filename($path)), 'success');
Log::addMediaLog('Media file ' . $path . ' uploaded');
} catch (FilesystemException | UnableToWriteFile $ex) {
FlashMessages::addMessage(I18N::translate('There was an error uploading your file.') . '<br>' . e($ex->getMessage()), 'danger');
}
}
$url = route(UploadMediaPage::class);
return redirect($url);
}
}
|