Issues (2502)

app/Http/RequestHandlers/DeletePath.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\FlashMessages;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Registry;
25
use Fisharebest\Webtrees\Validator;
26
use League\Flysystem\FilesystemException;
27
use League\Flysystem\UnableToDeleteDirectory;
28
use League\Flysystem\UnableToDeleteFile;
29
use League\Flysystem\WhitespacePathNormalizer;
30
use Psr\Http\Message\ResponseInterface;
31
use Psr\Http\Message\ServerRequestInterface;
32
use Psr\Http\Server\RequestHandlerInterface;
33
34
use function e;
35
use function in_array;
36
use function response;
37
use function str_ends_with;
38
39
final class DeletePath implements RequestHandlerInterface
40
{
41
    private const array PROTECTED_PATHS = [
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 41 at column 24
Loading history...
42
        'config.ini.php',
43
        'index.php',
44
        '.htaccess',
45
    ];
46
47
    public function __construct(
48
        private readonly WhitespacePathNormalizer $whitespace_path_normalizer,
49
    ) {
50
    }
51
52
    public function handle(ServerRequestInterface $request): ResponseInterface
53
    {
54
        $data_filesystem = Registry::filesystem()->data();
55
56
        $path = Validator::queryParams($request)->string('path');
57
58
        $normalized_path = $this->whitespace_path_normalizer->normalizePath($path);
59
60
        if (in_array($normalized_path, self::PROTECTED_PATHS, true)) {
61
            FlashMessages::addMessage(I18N::translate('The file %s could not be deleted.', e($path)), 'danger');
62
            return response();
63
        }
64
65
        // The request adds a slash to folders, so we know which delete function to use.
66
        if (str_ends_with($path, '/')) {
67
            try {
68
                $data_filesystem->deleteDirectory($normalized_path);
69
                FlashMessages::addMessage(I18N::translate('The folder %s has been deleted.', e($path)), 'success');
70
            } catch (FilesystemException | UnableToDeleteDirectory) {
71
                FlashMessages::addMessage(I18N::translate('The folder %s could not be deleted.', e($path)), 'danger');
72
            }
73
        } else {
74
            try {
75
                $data_filesystem->delete($normalized_path);
76
                FlashMessages::addMessage(I18N::translate('The file %s has been deleted.', e($path)), 'success');
77
            } catch (FilesystemException | UnableToDeleteFile) {
78
                FlashMessages::addMessage(I18N::translate('The file %s could not be deleted.', e($path)), 'danger');
79
            }
80
        }
81
82
        return response();
83
    }
84
}
85