xref: /webtrees/app/Http/RequestHandlers/UploadMediaAction.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
18ce3bd73SGreg Roach<?php
28ce3bd73SGreg Roach
38ce3bd73SGreg Roach/**
48ce3bd73SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
68ce3bd73SGreg Roach * This program is free software: you can redistribute it and/or modify
78ce3bd73SGreg Roach * it under the terms of the GNU General Public License as published by
88ce3bd73SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98ce3bd73SGreg Roach * (at your option) any later version.
108ce3bd73SGreg Roach * This program is distributed in the hope that it will be useful,
118ce3bd73SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128ce3bd73SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138ce3bd73SGreg Roach * GNU General Public License for more details.
148ce3bd73SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168ce3bd73SGreg Roach */
178ce3bd73SGreg Roach
188ce3bd73SGreg Roachdeclare(strict_types=1);
198ce3bd73SGreg Roach
208ce3bd73SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
218ce3bd73SGreg Roach
22b5c53c7fSGreg Roachuse Fisharebest\Webtrees\Exceptions\FileUploadException;
238ce3bd73SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
248ce3bd73SGreg Roachuse Fisharebest\Webtrees\Html;
258ce3bd73SGreg Roachuse Fisharebest\Webtrees\I18N;
268ce3bd73SGreg Roachuse Fisharebest\Webtrees\Log;
278ce3bd73SGreg Roachuse Fisharebest\Webtrees\Registry;
288ce3bd73SGreg Roachuse Fisharebest\Webtrees\Services\MediaFileService;
29748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
30f0448b68SGreg Roachuse League\Flysystem\FilesystemException;
31f0448b68SGreg Roachuse League\Flysystem\UnableToCheckFileExistence;
32f0448b68SGreg Roachuse League\Flysystem\UnableToWriteFile;
338ce3bd73SGreg Roachuse Psr\Http\Message\ResponseInterface;
348ce3bd73SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
358ce3bd73SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
368ce3bd73SGreg Roach
378ce3bd73SGreg Roachuse function e;
388ce3bd73SGreg Roachuse function preg_match;
398ce3bd73SGreg Roachuse function redirect;
408ce3bd73SGreg Roachuse function route;
418ce3bd73SGreg Roachuse function str_replace;
428ce3bd73SGreg Roachuse function substr;
438ce3bd73SGreg Roachuse function trim;
448ce3bd73SGreg Roach
45cdc88467SGreg Roachuse const UPLOAD_ERR_NO_FILE;
468ce3bd73SGreg Roachuse const UPLOAD_ERR_OK;
478ce3bd73SGreg Roach
488ce3bd73SGreg Roach/**
498ce3bd73SGreg Roach * Manage media from the control panel.
508ce3bd73SGreg Roach */
518ce3bd73SGreg Roachclass UploadMediaAction implements RequestHandlerInterface
528ce3bd73SGreg Roach{
53c4943cffSGreg Roach    private MediaFileService $media_file_service;
548ce3bd73SGreg Roach
558ce3bd73SGreg Roach    /**
568ce3bd73SGreg Roach     * @param MediaFileService $media_file_service
578ce3bd73SGreg Roach     */
588ce3bd73SGreg Roach    public function __construct(MediaFileService $media_file_service)
598ce3bd73SGreg Roach    {
608ce3bd73SGreg Roach        $this->media_file_service = $media_file_service;
618ce3bd73SGreg Roach    }
628ce3bd73SGreg Roach
638ce3bd73SGreg Roach    /**
648ce3bd73SGreg Roach     * @param ServerRequestInterface $request
658ce3bd73SGreg Roach     *
668ce3bd73SGreg Roach     * @return ResponseInterface
678ce3bd73SGreg Roach     */
688ce3bd73SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
698ce3bd73SGreg Roach    {
708ce3bd73SGreg Roach        $data_filesystem = Registry::filesystem()->data();
718ce3bd73SGreg Roach        $all_folders     = $this->media_file_service->allMediaFolders($data_filesystem);
728ce3bd73SGreg Roach
738ce3bd73SGreg Roach        foreach ($request->getUploadedFiles() as $key => $uploaded_file) {
74491602a7SGreg Roach            if ($uploaded_file->getError() === UPLOAD_ERR_NO_FILE) {
75cdc88467SGreg Roach                continue;
76cdc88467SGreg Roach            }
77cdc88467SGreg Roach
788ce3bd73SGreg Roach            if ($uploaded_file->getError() !== UPLOAD_ERR_OK) {
79b5c53c7fSGreg Roach                throw new FileUploadException($uploaded_file);
808ce3bd73SGreg Roach            }
81b5c53c7fSGreg Roach
828ce3bd73SGreg Roach            $key      = substr($key, 9);
83748dbe15SGreg Roach            $folder   = Validator::parsedBody($request)->string('folder' . $key);
84748dbe15SGreg Roach            $filename = Validator::parsedBody($request)->string('filename' . $key);
858ce3bd73SGreg Roach
868ce3bd73SGreg Roach            // If no filename specified, use the original filename.
878ce3bd73SGreg Roach            if ($filename === '') {
888ce3bd73SGreg Roach                $filename = $uploaded_file->getClientFilename();
898ce3bd73SGreg Roach            }
908ce3bd73SGreg Roach
918ce3bd73SGreg Roach            // Validate the folder
928ce3bd73SGreg Roach            if (!$all_folders->contains($folder)) {
938ce3bd73SGreg Roach                break;
948ce3bd73SGreg Roach            }
958ce3bd73SGreg Roach
968ce3bd73SGreg Roach            // Validate the filename.
978ce3bd73SGreg Roach            $filename = str_replace('\\', '/', $filename);
988ce3bd73SGreg Roach            $filename = trim($filename, '/');
998ce3bd73SGreg Roach
1008ce3bd73SGreg Roach            if (preg_match('/([:])/', $filename, $match)) {
1018ce3bd73SGreg Roach                // Local media files cannot contain certain special characters, especially on MS Windows
1028ce3bd73SGreg Roach                FlashMessages::addMessage(I18N::translate('Filenames are not allowed to contain the character “%s”.', $match[1]));
1038ce3bd73SGreg Roach                continue;
1048ce3bd73SGreg Roach            }
1058ce3bd73SGreg Roach
1068ce3bd73SGreg Roach            if (preg_match('/(\.(php|pl|cgi|bash|sh|bat|exe|com|htm|html|shtml))$/i', $filename, $match)) {
1078ce3bd73SGreg Roach                // Do not allow obvious script files.
1088ce3bd73SGreg Roach                FlashMessages::addMessage(I18N::translate('Filenames are not allowed to have the extension “%s”.', $match[1]));
1098ce3bd73SGreg Roach                continue;
1108ce3bd73SGreg Roach            }
1118ce3bd73SGreg Roach
1128ce3bd73SGreg Roach            $path = $folder . $filename;
1138ce3bd73SGreg Roach
114f0448b68SGreg Roach            try {
115f0448b68SGreg Roach                $file_exists = $data_filesystem->fileExists($path);
11628d026adSGreg Roach            } catch (FilesystemException | UnableToCheckFileExistence) {
117f0448b68SGreg Roach                $file_exists = false;
118f0448b68SGreg Roach            }
119f0448b68SGreg Roach
120f0448b68SGreg Roach            if ($file_exists) {
1218ce3bd73SGreg Roach                FlashMessages::addMessage(I18N::translate('The file %s already exists. Use another filename.', $path, 'error'));
1228ce3bd73SGreg Roach                continue;
1238ce3bd73SGreg Roach            }
1248ce3bd73SGreg Roach
1258ce3bd73SGreg Roach            // Now copy the file to the correct location.
1268ce3bd73SGreg Roach            try {
1278ce3bd73SGreg Roach                $data_filesystem->writeStream($path, $uploaded_file->getStream()->detach());
1288ce3bd73SGreg Roach                FlashMessages::addMessage(I18N::translate('The file %s has been uploaded.', Html::filename($path)), 'success');
1298ce3bd73SGreg Roach                Log::addMediaLog('Media file ' . $path . ' uploaded');
130f0448b68SGreg Roach            } catch (FilesystemException | UnableToWriteFile $ex) {
1318ce3bd73SGreg Roach                FlashMessages::addMessage(I18N::translate('There was an error uploading your file.') . '<br>' . e($ex->getMessage()), 'danger');
1328ce3bd73SGreg Roach            }
1338ce3bd73SGreg Roach        }
1348ce3bd73SGreg Roach
1358ce3bd73SGreg Roach        $url = route(UploadMediaPage::class);
1368ce3bd73SGreg Roach
1378ce3bd73SGreg Roach        return redirect($url);
1388ce3bd73SGreg Roach    }
1398ce3bd73SGreg Roach}
140