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