1fd6c003fSGreg Roach<?php 2fd6c003fSGreg Roach 3fd6c003fSGreg Roach/** 4fd6c003fSGreg Roach * webtrees: online genealogy 5d11be702SGreg 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 22*52550490SGreg Roachuse Fisharebest\Webtrees\DB; 23fd6c003fSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 24fd6c003fSGreg Roachuse Fisharebest\Webtrees\I18N; 256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 26fd6c003fSGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 27b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 28f0448b68SGreg Roachuse League\Flysystem\FilesystemException; 29782714c2SGreg Roachuse League\Flysystem\FilesystemReader; 30f7cf8a15SGreg Roachuse League\Flysystem\StorageAttributes; 31fd6c003fSGreg Roachuse Psr\Http\Message\ResponseInterface; 32fd6c003fSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 33fd6c003fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 34fd6c003fSGreg Roach 35fd6c003fSGreg Roachuse function explode; 36fd6c003fSGreg Roach 37fd6c003fSGreg Roach/** 38fd6c003fSGreg Roach * Show old files that could be deleted. 39fd6c003fSGreg Roach */ 40fd6c003fSGreg Roachclass CleanDataFolder implements RequestHandlerInterface 41fd6c003fSGreg Roach{ 42fd6c003fSGreg Roach use ViewResponseTrait; 43fd6c003fSGreg Roach 44c4943cffSGreg Roach private TreeService $tree_service; 45fd6c003fSGreg Roach 46fd6c003fSGreg Roach /** 47fd6c003fSGreg Roach * @param TreeService $tree_service 48fd6c003fSGreg Roach */ 49a04bb9a2SGreg 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 { 616b9cb339SGreg Roach $data_filesystem = Registry::filesystem()->data(); 62a04bb9a2SGreg Roach 63fd6c003fSGreg Roach $this->layout = 'layouts/administration'; 64fd6c003fSGreg Roach 65fd6c003fSGreg Roach $protected = [ 66fd6c003fSGreg Roach '.htaccess', 67fd6c003fSGreg Roach '.gitignore', 68fd6c003fSGreg Roach 'index.php', 69fd6c003fSGreg Roach 'config.ini.php', 70fd6c003fSGreg Roach ]; 71fd6c003fSGreg Roach 72*52550490SGreg Roach if (Validator::attributes($request)->string('dbtype', DB::MYSQL) === DB::SQLITE) { 73b55cbc6bSGreg Roach $protected[] = Validator::attributes($request)->string('dbname') . '.sqlite'; 74fd6c003fSGreg Roach } 75fd6c003fSGreg Roach 76fd6c003fSGreg Roach // Protect the media folders 77fd6c003fSGreg Roach foreach ($this->tree_service->all() as $tree) { 78fd6c003fSGreg Roach $media_directory = $tree->getPreference('MEDIA_DIRECTORY'); 79fd6c003fSGreg Roach [$folder] = explode('/', $media_directory); 80fd6c003fSGreg Roach 81fd6c003fSGreg Roach $protected[] = $folder . '/'; 82fd6c003fSGreg Roach } 83fd6c003fSGreg Roach 84fd6c003fSGreg Roach // List the top-level contents of the data folder 85f0448b68SGreg Roach try { 86782714c2SGreg Roach $entries = $data_filesystem->listContents('', FilesystemReader::LIST_SHALLOW) 87f7cf8a15SGreg Roach ->map(static function (StorageAttributes $attributes): string { 88f7cf8a15SGreg Roach if ($attributes->isDir()) { 89f7cf8a15SGreg Roach return $attributes->path() . '/'; 90fd6c003fSGreg Roach } 91fd6c003fSGreg Roach 92f7cf8a15SGreg Roach return $attributes->path(); 93f0448b68SGreg Roach }) 94f0448b68SGreg Roach ->toArray(); 9528d026adSGreg Roach } catch (FilesystemException) { 96f0448b68SGreg Roach $entries = []; 97f0448b68SGreg Roach } 98f0448b68SGreg Roach 99fd6c003fSGreg Roach return $this->viewResponse('admin/clean-data', [ 100fd6c003fSGreg Roach 'title' => I18N::translate('Clean up data folder'), 101fd6c003fSGreg Roach 'entries' => $entries, 102fd6c003fSGreg Roach 'protected' => $protected, 103fd6c003fSGreg Roach ]); 104fd6c003fSGreg Roach } 105fd6c003fSGreg Roach} 106