18ce3bd73SGreg Roach<?php 28ce3bd73SGreg Roach 38ce3bd73SGreg Roach/** 48ce3bd73SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 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 228ce3bd73SGreg Roachuse Fisharebest\Webtrees\FlashMessages; 238ce3bd73SGreg Roachuse Fisharebest\Webtrees\Functions\Functions; 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*f0448b68SGreg Roachuse League\Flysystem\FilesystemException; 30*f0448b68SGreg Roachuse League\Flysystem\UnableToCheckFileExistence; 31*f0448b68SGreg Roachuse League\Flysystem\UnableToWriteFile; 328ce3bd73SGreg Roachuse Psr\Http\Message\ResponseInterface; 338ce3bd73SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 348ce3bd73SGreg Roachuse Psr\Http\Message\UploadedFileInterface; 358ce3bd73SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 368ce3bd73SGreg Roachuse Throwable; 378ce3bd73SGreg Roach 388ce3bd73SGreg Roachuse function assert; 398ce3bd73SGreg Roachuse function e; 408ce3bd73SGreg Roachuse function preg_match; 418ce3bd73SGreg Roachuse function redirect; 428ce3bd73SGreg Roachuse function route; 438ce3bd73SGreg Roachuse function str_replace; 448ce3bd73SGreg Roachuse function substr; 458ce3bd73SGreg Roachuse function trim; 468ce3bd73SGreg Roach 478ce3bd73SGreg Roachuse const UPLOAD_ERR_OK; 488ce3bd73SGreg Roach 498ce3bd73SGreg Roach/** 508ce3bd73SGreg Roach * Manage media from the control panel. 518ce3bd73SGreg Roach */ 528ce3bd73SGreg Roachclass UploadMediaAction implements RequestHandlerInterface 538ce3bd73SGreg Roach{ 548ce3bd73SGreg Roach /** @var MediaFileService */ 558ce3bd73SGreg Roach private $media_file_service; 568ce3bd73SGreg Roach 578ce3bd73SGreg Roach /** 588ce3bd73SGreg Roach * MediaController constructor. 598ce3bd73SGreg Roach * 608ce3bd73SGreg Roach * @param MediaFileService $media_file_service 618ce3bd73SGreg Roach */ 628ce3bd73SGreg Roach public function __construct(MediaFileService $media_file_service) 638ce3bd73SGreg Roach { 648ce3bd73SGreg Roach $this->media_file_service = $media_file_service; 658ce3bd73SGreg Roach } 668ce3bd73SGreg Roach 678ce3bd73SGreg Roach /** 688ce3bd73SGreg Roach * @param ServerRequestInterface $request 698ce3bd73SGreg Roach * 708ce3bd73SGreg Roach * @return ResponseInterface 718ce3bd73SGreg Roach */ 728ce3bd73SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 738ce3bd73SGreg Roach { 748ce3bd73SGreg Roach $data_filesystem = Registry::filesystem()->data(); 758ce3bd73SGreg Roach 768ce3bd73SGreg Roach $params = (array) $request->getParsedBody(); 778ce3bd73SGreg Roach 788ce3bd73SGreg Roach $all_folders = $this->media_file_service->allMediaFolders($data_filesystem); 798ce3bd73SGreg Roach 808ce3bd73SGreg Roach foreach ($request->getUploadedFiles() as $key => $uploaded_file) { 818ce3bd73SGreg Roach assert($uploaded_file instanceof UploadedFileInterface); 828ce3bd73SGreg Roach if ($uploaded_file->getClientFilename() === '') { 838ce3bd73SGreg Roach continue; 848ce3bd73SGreg Roach } 858ce3bd73SGreg Roach if ($uploaded_file->getError() !== UPLOAD_ERR_OK) { 868ce3bd73SGreg Roach FlashMessages::addMessage(Functions::fileUploadErrorText($uploaded_file->getError()), 'danger'); 878ce3bd73SGreg Roach continue; 888ce3bd73SGreg Roach } 898ce3bd73SGreg Roach $key = substr($key, 9); 908ce3bd73SGreg Roach 918ce3bd73SGreg Roach $folder = $params['folder' . $key]; 928ce3bd73SGreg Roach $filename = $params['filename' . $key]; 938ce3bd73SGreg Roach 948ce3bd73SGreg Roach // If no filename specified, use the original filename. 958ce3bd73SGreg Roach if ($filename === '') { 968ce3bd73SGreg Roach $filename = $uploaded_file->getClientFilename(); 978ce3bd73SGreg Roach } 988ce3bd73SGreg Roach 998ce3bd73SGreg Roach // Validate the folder 1008ce3bd73SGreg Roach if (!$all_folders->contains($folder)) { 1018ce3bd73SGreg Roach break; 1028ce3bd73SGreg Roach } 1038ce3bd73SGreg Roach 1048ce3bd73SGreg Roach // Validate the filename. 1058ce3bd73SGreg Roach $filename = str_replace('\\', '/', $filename); 1068ce3bd73SGreg Roach $filename = trim($filename, '/'); 1078ce3bd73SGreg Roach 1088ce3bd73SGreg Roach if (preg_match('/([:])/', $filename, $match)) { 1098ce3bd73SGreg Roach // Local media files cannot contain certain special characters, especially on MS Windows 1108ce3bd73SGreg Roach FlashMessages::addMessage(I18N::translate('Filenames are not allowed to contain the character “%s”.', $match[1])); 1118ce3bd73SGreg Roach continue; 1128ce3bd73SGreg Roach } 1138ce3bd73SGreg Roach 1148ce3bd73SGreg Roach if (preg_match('/(\.(php|pl|cgi|bash|sh|bat|exe|com|htm|html|shtml))$/i', $filename, $match)) { 1158ce3bd73SGreg Roach // Do not allow obvious script files. 1168ce3bd73SGreg Roach FlashMessages::addMessage(I18N::translate('Filenames are not allowed to have the extension “%s”.', $match[1])); 1178ce3bd73SGreg Roach continue; 1188ce3bd73SGreg Roach } 1198ce3bd73SGreg Roach 1208ce3bd73SGreg Roach $path = $folder . $filename; 1218ce3bd73SGreg Roach 122*f0448b68SGreg Roach try { 123*f0448b68SGreg Roach $file_exists = $data_filesystem->fileExists($path); 124*f0448b68SGreg Roach } catch (FilesystemException | UnableToCheckFileExistence $ex) { 125*f0448b68SGreg Roach $file_exists = false; 126*f0448b68SGreg Roach } 127*f0448b68SGreg Roach 128*f0448b68SGreg Roach if ($file_exists) { 1298ce3bd73SGreg Roach FlashMessages::addMessage(I18N::translate('The file %s already exists. Use another filename.', $path, 'error')); 1308ce3bd73SGreg Roach continue; 1318ce3bd73SGreg Roach } 1328ce3bd73SGreg Roach 1338ce3bd73SGreg Roach // Now copy the file to the correct location. 1348ce3bd73SGreg Roach try { 1358ce3bd73SGreg Roach $data_filesystem->writeStream($path, $uploaded_file->getStream()->detach()); 1368ce3bd73SGreg Roach FlashMessages::addMessage(I18N::translate('The file %s has been uploaded.', Html::filename($path)), 'success'); 1378ce3bd73SGreg Roach Log::addMediaLog('Media file ' . $path . ' uploaded'); 138*f0448b68SGreg Roach } catch (FilesystemException | UnableToWriteFile $ex) { 1398ce3bd73SGreg Roach FlashMessages::addMessage(I18N::translate('There was an error uploading your file.') . '<br>' . e($ex->getMessage()), 'danger'); 1408ce3bd73SGreg Roach } 1418ce3bd73SGreg Roach } 1428ce3bd73SGreg Roach 1438ce3bd73SGreg Roach $url = route(UploadMediaPage::class); 1448ce3bd73SGreg Roach 1458ce3bd73SGreg Roach return redirect($url); 1468ce3bd73SGreg Roach } 1478ce3bd73SGreg Roach} 148