1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\RequestHandlers; 21 22use Fisharebest\Webtrees\FlashMessages; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Registry; 25use Fisharebest\Webtrees\Validator; 26use League\Flysystem\FilesystemException; 27use League\Flysystem\UnableToDeleteDirectory; 28use League\Flysystem\UnableToDeleteFile; 29use League\Flysystem\WhitespacePathNormalizer; 30use Psr\Http\Message\ResponseInterface; 31use Psr\Http\Message\ServerRequestInterface; 32use Psr\Http\Server\RequestHandlerInterface; 33 34use function e; 35use function in_array; 36use function response; 37use function str_ends_with; 38 39/** 40 * Delete a file or folder from the data filesystem. 41 */ 42class DeletePath implements RequestHandlerInterface 43{ 44 private const PROTECTED_PATHS = [ 45 'config.ini.php', 46 'index.php', 47 '.htaccess', 48 ]; 49 50 private WhitespacePathNormalizer $whitespace_path_normalizer; 51 52 /** 53 * @param WhitespacePathNormalizer $whitespace_path_normalizer 54 */ 55 public function __construct(WhitespacePathNormalizer $whitespace_path_normalizer) 56 { 57 $this->whitespace_path_normalizer = $whitespace_path_normalizer; 58 } 59 60 /** 61 * @param ServerRequestInterface $request 62 * 63 * @return ResponseInterface 64 */ 65 public function handle(ServerRequestInterface $request): ResponseInterface 66 { 67 $data_filesystem = Registry::filesystem()->data(); 68 69 $path = Validator::queryParams($request)->string('path'); 70 71 $normalized_path = $this->whitespace_path_normalizer->normalizePath($path); 72 73 if (in_array($normalized_path, self::PROTECTED_PATHS, true)) { 74 FlashMessages::addMessage(I18N::translate('The file %s could not be deleted.', e($path)), 'danger'); 75 return response(); 76 } 77 78 // The request adds a slash to folders, so we know which delete function to use. 79 if (str_ends_with($path, '/')) { 80 try { 81 $data_filesystem->deleteDirectory($normalized_path); 82 FlashMessages::addMessage(I18N::translate('The folder %s has been deleted.', e($path)), 'success'); 83 } catch (FilesystemException | UnableToDeleteDirectory) { 84 FlashMessages::addMessage(I18N::translate('The folder %s could not be deleted.', e($path)), 'danger'); 85 } 86 } else { 87 try { 88 $data_filesystem->delete($normalized_path); 89 FlashMessages::addMessage(I18N::translate('The file %s has been deleted.', e($path)), 'success'); 90 } catch (FilesystemException | UnableToDeleteFile) { 91 FlashMessages::addMessage(I18N::translate('The file %s could not be deleted.', e($path)), 'danger'); 92 } 93 } 94 95 return response(); 96 } 97} 98