xref: /webtrees/app/Http/RequestHandlers/DeletePath.php (revision dde5ecaf84c9b72478b27700459f4227ab09ff2a)
1fd6c003fSGreg Roach<?php
2fd6c003fSGreg Roach
3fd6c003fSGreg Roach/**
4fd6c003fSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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;
25f7cf8a15SGreg Roachuse League\Flysystem\FilesystemException;
26f7cf8a15SGreg Roachuse League\Flysystem\UnableToDeleteDirectory;
27f7cf8a15SGreg Roachuse League\Flysystem\UnableToDeleteFile;
28*dde5ecafSGreg Roachuse League\Flysystem\WhitespacePathNormalizer;
29fd6c003fSGreg Roachuse Psr\Http\Message\ResponseInterface;
30fd6c003fSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
31fd6c003fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
32fd6c003fSGreg Roach
33fd6c003fSGreg Roachuse function e;
34*dde5ecafSGreg Roachuse function in_array;
35fd6c003fSGreg Roachuse function response;
36f7cf8a15SGreg Roachuse function str_ends_with;
37fd6c003fSGreg Roach
38fd6c003fSGreg Roach/**
3913aa75d8SGreg Roach * Delete a file or folder from the data filesystem.
40fd6c003fSGreg Roach */
411d1f373cSGreg Roachclass DeletePath implements RequestHandlerInterface
42fd6c003fSGreg Roach{
43*dde5ecafSGreg Roach    private const PROTECTED_PATHS = [
44*dde5ecafSGreg Roach        'config.ini.php',
45*dde5ecafSGreg Roach        'index.php',
46*dde5ecafSGreg Roach        '.htaccess',
47*dde5ecafSGreg Roach    ];
48*dde5ecafSGreg Roach
49*dde5ecafSGreg Roach    private WhitespacePathNormalizer $whitespace_path_normalizer;
50*dde5ecafSGreg Roach
51*dde5ecafSGreg Roach    /**
52*dde5ecafSGreg Roach     * @param WhitespacePathNormalizer $whitespace_path_normalizer
53*dde5ecafSGreg Roach     */
54*dde5ecafSGreg Roach    public function __construct(WhitespacePathNormalizer $whitespace_path_normalizer)
55*dde5ecafSGreg Roach    {
56*dde5ecafSGreg Roach        $this->whitespace_path_normalizer = $whitespace_path_normalizer;
57*dde5ecafSGreg Roach    }
58*dde5ecafSGreg Roach
59fd6c003fSGreg Roach    /**
60fd6c003fSGreg Roach     * @param ServerRequestInterface $request
61fd6c003fSGreg Roach     *
62fd6c003fSGreg Roach     * @return ResponseInterface
63fd6c003fSGreg Roach     */
64fd6c003fSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
65fd6c003fSGreg Roach    {
666b9cb339SGreg Roach        $data_filesystem = Registry::filesystem()->data();
67a04bb9a2SGreg Roach
68*dde5ecafSGreg Roach        $path = $request->getQueryParams()['path'] ?? '';
69fd6c003fSGreg Roach
70*dde5ecafSGreg Roach        $normalized_path = $this->whitespace_path_normalizer->normalizePath($path);
71*dde5ecafSGreg Roach
72*dde5ecafSGreg Roach        if (in_array($normalized_path, self::PROTECTED_PATHS, true)) {
73*dde5ecafSGreg Roach            FlashMessages::addMessage(I18N::translate('The file %s could not be deleted.', e($path)), 'danger');
74*dde5ecafSGreg Roach            return response();
75*dde5ecafSGreg Roach        }
76*dde5ecafSGreg Roach
77*dde5ecafSGreg Roach        // The request adds a slash to folders, so we know which delete function to use.
78f7cf8a15SGreg Roach        if (str_ends_with($path, '/')) {
79f7cf8a15SGreg Roach            try {
80*dde5ecafSGreg Roach                $data_filesystem->deleteDirectory($normalized_path);
81f7cf8a15SGreg Roach                FlashMessages::addMessage(I18N::translate('The folder %s has been deleted.', e($path)), 'success');
82f7cf8a15SGreg Roach            } catch (FilesystemException | UnableToDeleteDirectory $ex) {
83f7cf8a15SGreg Roach                FlashMessages::addMessage(I18N::translate('The folder %s could not be deleted.', e($path)), 'danger');
84f7cf8a15SGreg Roach            }
85f7cf8a15SGreg Roach        } else {
86fd6c003fSGreg Roach            try {
87*dde5ecafSGreg Roach                $data_filesystem->delete($normalized_path);
88fd6c003fSGreg Roach                FlashMessages::addMessage(I18N::translate('The file %s has been deleted.', e($path)), 'success');
89f7cf8a15SGreg Roach            } catch (FilesystemException | UnableToDeleteFile $ex) {
90fd6c003fSGreg Roach                FlashMessages::addMessage(I18N::translate('The file %s could not be deleted.', e($path)), 'danger');
91fd6c003fSGreg Roach            }
92fd6c003fSGreg Roach        }
93fd6c003fSGreg Roach
941d1f373cSGreg Roach        return response();
95fd6c003fSGreg Roach    }
96fd6c003fSGreg Roach}
97