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