xref: /webtrees/app/Http/RequestHandlers/CleanDataFolder.php (revision 8fa10296f379d50098f57cb159e098c429813f7f)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Http\ViewResponseTrait;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Services\TreeService;
25use League\Flysystem\FilesystemInterface;
26use Psr\Http\Message\ResponseInterface;
27use Psr\Http\Message\ServerRequestInterface;
28use Psr\Http\Server\RequestHandlerInterface;
29
30use function array_map;
31use function explode;
32
33/**
34 * Show old files that could be deleted.
35 */
36class CleanDataFolder implements RequestHandlerInterface
37{
38    use ViewResponseTrait;
39
40    /** @var FilesystemInterface */
41    private $filesystem;
42
43    /** @var TreeService */
44    private $tree_service;
45
46    /**
47     * CleanDataFolder constructor.
48     *
49     * @param FilesystemInterface $filesystem
50     * @param TreeService         $tree_service
51     */
52    public function __construct(FilesystemInterface $filesystem, TreeService $tree_service)
53    {
54        $this->filesystem   = $filesystem;
55        $this->tree_service = $tree_service;
56    }
57
58    /**
59     * @param ServerRequestInterface $request
60     *
61     * @return ResponseInterface
62     */
63    public function handle(ServerRequestInterface $request): ResponseInterface
64    {
65        $this->layout = 'layouts/administration';
66
67        $protected = [
68            '.htaccess',
69            '.gitignore',
70            'index.php',
71            'config.ini.php',
72        ];
73
74        if ($request->getAttribute('dbtype') === 'sqlite') {
75            $protected[] = $request->getAttribute('dbname') . '.sqlite';
76        }
77
78        // Protect the media folders
79        foreach ($this->tree_service->all() as $tree) {
80            $media_directory = $tree->getPreference('MEDIA_DIRECTORY');
81            [$folder] = explode('/', $media_directory);
82
83            $protected[] = $folder . '/';
84        }
85
86        // List the top-level contents of the data folder
87        $entries = array_map(static function (array $content) {
88            if ($content['type'] === 'dir') {
89                return $content['path'] . '/';
90            }
91
92            return $content['path'];
93        }, $this->filesystem->listContents());
94
95        return $this->viewResponse('admin/clean-data', [
96            'title'     => I18N::translate('Clean up data folder'),
97            'entries'   => $entries,
98            'protected' => $protected,
99        ]);
100    }
101}
102