xref: /webtrees/app/Http/RequestHandlers/UploadMediaAction.php (revision 748dbe155a6d19d66918ad136947fa23ee8f8469)
18ce3bd73SGreg Roach<?php
28ce3bd73SGreg Roach
38ce3bd73SGreg Roach/**
48ce3bd73SGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 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;
29*748dbe15SGreg 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     * MediaController constructor.
578ce3bd73SGreg Roach     *
588ce3bd73SGreg Roach     * @param MediaFileService $media_file_service
598ce3bd73SGreg Roach     */
608ce3bd73SGreg Roach    public function __construct(MediaFileService $media_file_service)
618ce3bd73SGreg Roach    {
628ce3bd73SGreg Roach        $this->media_file_service = $media_file_service;
638ce3bd73SGreg Roach    }
648ce3bd73SGreg Roach
658ce3bd73SGreg Roach    /**
668ce3bd73SGreg Roach     * @param ServerRequestInterface $request
678ce3bd73SGreg Roach     *
688ce3bd73SGreg Roach     * @return ResponseInterface
698ce3bd73SGreg Roach     */
708ce3bd73SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
718ce3bd73SGreg Roach    {
728ce3bd73SGreg Roach        $data_filesystem = Registry::filesystem()->data();
738ce3bd73SGreg Roach        $all_folders     = $this->media_file_service->allMediaFolders($data_filesystem);
748ce3bd73SGreg Roach
758ce3bd73SGreg Roach        foreach ($request->getUploadedFiles() as $key => $uploaded_file) {
76491602a7SGreg Roach            if ($uploaded_file->getError() === UPLOAD_ERR_NO_FILE) {
77cdc88467SGreg Roach                continue;
78cdc88467SGreg Roach            }
79cdc88467SGreg Roach
808ce3bd73SGreg Roach            if ($uploaded_file->getError() !== UPLOAD_ERR_OK) {
81b5c53c7fSGreg Roach                throw new FileUploadException($uploaded_file);
828ce3bd73SGreg Roach            }
83b5c53c7fSGreg Roach
848ce3bd73SGreg Roach            $key      = substr($key, 9);
85*748dbe15SGreg Roach            $folder   = Validator::parsedBody($request)->string('folder' . $key);
86*748dbe15SGreg Roach            $filename = Validator::parsedBody($request)->string('filename' . $key);
878ce3bd73SGreg Roach
888ce3bd73SGreg Roach            // If no filename specified, use the original filename.
898ce3bd73SGreg Roach            if ($filename === '') {
908ce3bd73SGreg Roach                $filename = $uploaded_file->getClientFilename();
918ce3bd73SGreg Roach            }
928ce3bd73SGreg Roach
938ce3bd73SGreg Roach            // Validate the folder
948ce3bd73SGreg Roach            if (!$all_folders->contains($folder)) {
958ce3bd73SGreg Roach                break;
968ce3bd73SGreg Roach            }
978ce3bd73SGreg Roach
988ce3bd73SGreg Roach            // Validate the filename.
998ce3bd73SGreg Roach            $filename = str_replace('\\', '/', $filename);
1008ce3bd73SGreg Roach            $filename = trim($filename, '/');
1018ce3bd73SGreg Roach
1028ce3bd73SGreg Roach            if (preg_match('/([:])/', $filename, $match)) {
1038ce3bd73SGreg Roach                // Local media files cannot contain certain special characters, especially on MS Windows
1048ce3bd73SGreg Roach                FlashMessages::addMessage(I18N::translate('Filenames are not allowed to contain the character “%s”.', $match[1]));
1058ce3bd73SGreg Roach                continue;
1068ce3bd73SGreg Roach            }
1078ce3bd73SGreg Roach
1088ce3bd73SGreg Roach            if (preg_match('/(\.(php|pl|cgi|bash|sh|bat|exe|com|htm|html|shtml))$/i', $filename, $match)) {
1098ce3bd73SGreg Roach                // Do not allow obvious script files.
1108ce3bd73SGreg Roach                FlashMessages::addMessage(I18N::translate('Filenames are not allowed to have the extension “%s”.', $match[1]));
1118ce3bd73SGreg Roach                continue;
1128ce3bd73SGreg Roach            }
1138ce3bd73SGreg Roach
1148ce3bd73SGreg Roach            $path = $folder . $filename;
1158ce3bd73SGreg Roach
116f0448b68SGreg Roach            try {
117f0448b68SGreg Roach                $file_exists = $data_filesystem->fileExists($path);
118f0448b68SGreg Roach            } catch (FilesystemException | UnableToCheckFileExistence $ex) {
119f0448b68SGreg Roach                $file_exists = false;
120f0448b68SGreg Roach            }
121f0448b68SGreg Roach
122f0448b68SGreg Roach            if ($file_exists) {
1238ce3bd73SGreg Roach                FlashMessages::addMessage(I18N::translate('The file %s already exists. Use another filename.', $path, 'error'));
1248ce3bd73SGreg Roach                continue;
1258ce3bd73SGreg Roach            }
1268ce3bd73SGreg Roach
1278ce3bd73SGreg Roach            // Now copy the file to the correct location.
1288ce3bd73SGreg Roach            try {
1298ce3bd73SGreg Roach                $data_filesystem->writeStream($path, $uploaded_file->getStream()->detach());
1308ce3bd73SGreg Roach                FlashMessages::addMessage(I18N::translate('The file %s has been uploaded.', Html::filename($path)), 'success');
1318ce3bd73SGreg Roach                Log::addMediaLog('Media file ' . $path . ' uploaded');
132f0448b68SGreg Roach            } catch (FilesystemException | UnableToWriteFile $ex) {
1338ce3bd73SGreg Roach                FlashMessages::addMessage(I18N::translate('There was an error uploading your file.') . '<br>' . e($ex->getMessage()), 'danger');
1348ce3bd73SGreg Roach            }
1358ce3bd73SGreg Roach        }
1368ce3bd73SGreg Roach
1378ce3bd73SGreg Roach        $url = route(UploadMediaPage::class);
1388ce3bd73SGreg Roach
1398ce3bd73SGreg Roach        return redirect($url);
1408ce3bd73SGreg Roach    }
1418ce3bd73SGreg Roach}
142