1cfbf56adSGreg Roach<?php 23976b470SGreg Roach 3cfbf56adSGreg Roach/** 4cfbf56adSGreg Roach * webtrees: online genealogy 5*89f7189bSGreg Roach * Copyright (C) 2021 webtrees development team 6cfbf56adSGreg Roach * This program is free software: you can redistribute it and/or modify 7cfbf56adSGreg Roach * it under the terms of the GNU General Public License as published by 8cfbf56adSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9cfbf56adSGreg Roach * (at your option) any later version. 10cfbf56adSGreg Roach * This program is distributed in the hope that it will be useful, 11cfbf56adSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12cfbf56adSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13cfbf56adSGreg Roach * GNU General Public License for more details. 14cfbf56adSGreg Roach * You should have received a copy of the GNU General Public License 15*89f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16cfbf56adSGreg Roach */ 17fcfa147eSGreg Roach 18cfbf56adSGreg Roachdeclare(strict_types=1); 19cfbf56adSGreg Roach 20cfbf56adSGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware; 21cfbf56adSGreg Roach 22cfbf56adSGreg Roachuse Fig\Http\Message\RequestMethodInterface; 236b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 24cfbf56adSGreg Roachuse Fisharebest\Webtrees\Services\HousekeepingService; 25cfbf56adSGreg Roachuse League\Flysystem\FilesystemInterface; 26cfbf56adSGreg Roachuse Psr\Http\Message\ResponseInterface; 27cfbf56adSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 28cfbf56adSGreg Roachuse Psr\Http\Server\MiddlewareInterface; 29cfbf56adSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 30cfbf56adSGreg Roach 31cfbf56adSGreg Roach/** 32cfbf56adSGreg Roach * Run the housekeeping service at irregular intervals. 33cfbf56adSGreg Roach */ 34cfbf56adSGreg Roachclass DoHousekeeping implements MiddlewareInterface 35cfbf56adSGreg Roach{ 36e1357632SGreg Roach // Delete old thumbnails after 90 days. 37e1357632SGreg Roach private const THUMBNAIL_DIR = 'thumbnail-cache'; 38cfbf56adSGreg Roach private const MAX_THUMBNAIL_AGE = 60 * 60 * 24 * 90; 39cfbf56adSGreg Roach 40cfbf56adSGreg Roach // Delete files in /data/tmp after 1 hour. 41cfbf56adSGreg Roach private const TMP_DIR = 'data/tmp'; 42cfbf56adSGreg Roach private const MAX_TMP_FILE_AGE = 60 * 60; 43cfbf56adSGreg Roach 44cfbf56adSGreg Roach // Delete error logs after 90 days. 45cfbf56adSGreg Roach private const MAX_LOG_AGE = 60 * 60 * 24 * 90; 46cfbf56adSGreg Roach 47cfbf56adSGreg Roach // Delete inactive sessions after 1 day. 48cfbf56adSGreg Roach private const MAX_SESSION_AGE = 60 * 60 * 24; 49cfbf56adSGreg Roach 50e1357632SGreg Roach // Run the cleanup every N requests. 51e1357632SGreg Roach private const PROBABILITY = 250; 52cfbf56adSGreg Roach 53cfbf56adSGreg Roach /** @var HousekeepingService */ 54cfbf56adSGreg Roach private $housekeeping_service; 55cfbf56adSGreg Roach 56cfbf56adSGreg Roach /** 57cfbf56adSGreg Roach * Housekeeping constructor. 58cfbf56adSGreg Roach * 59cfbf56adSGreg Roach * @param HousekeepingService $housekeeping_service 60cfbf56adSGreg Roach */ 61cfbf56adSGreg Roach public function __construct(HousekeepingService $housekeeping_service) 62cfbf56adSGreg Roach { 63cfbf56adSGreg Roach $this->housekeeping_service = $housekeeping_service; 64cfbf56adSGreg Roach } 65cfbf56adSGreg Roach 66cfbf56adSGreg Roach /** 67cfbf56adSGreg Roach * @param ServerRequestInterface $request 68cfbf56adSGreg Roach * @param RequestHandlerInterface $handler 69cfbf56adSGreg Roach * 70cfbf56adSGreg Roach * @return ResponseInterface 71cfbf56adSGreg Roach */ 72cfbf56adSGreg Roach public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 73cfbf56adSGreg Roach { 74cfbf56adSGreg Roach $response = $handler->handle($request); 75cfbf56adSGreg Roach 76cfbf56adSGreg Roach // Run the cleanup after random page requests. 77cfbf56adSGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_GET && random_int(1, self::PROBABILITY) === 1) { 786b9cb339SGreg Roach $this->runHousekeeping(Registry::filesystem()->data(), Registry::filesystem()->root()); 79cfbf56adSGreg Roach } 80cfbf56adSGreg Roach 81cfbf56adSGreg Roach return $response; 82cfbf56adSGreg Roach } 83cfbf56adSGreg Roach 84cfbf56adSGreg Roach /** 85cfbf56adSGreg Roach * Run the various housekeeping services. 86cfbf56adSGreg Roach * 87a04bb9a2SGreg Roach * @param FilesystemInterface $data_filesystem 88a04bb9a2SGreg Roach * @param FilesystemInterface $root_filesystem 89a04bb9a2SGreg Roach * 90cfbf56adSGreg Roach * @return void 91cfbf56adSGreg Roach */ 92a04bb9a2SGreg Roach private function runHousekeeping(FilesystemInterface $data_filesystem, FilesystemInterface $root_filesystem): void 93cfbf56adSGreg Roach { 94b3cd3bbfSGreg Roach // Clear old thumbnails 95e1357632SGreg Roach $this->housekeeping_service->deleteOldFiles($data_filesystem, self::THUMBNAIL_DIR, self::MAX_THUMBNAIL_AGE); 96cfbf56adSGreg Roach 97b3cd3bbfSGreg Roach // Clear temporary files 98cfbf56adSGreg Roach $this->housekeeping_service->deleteOldFiles($root_filesystem, self::TMP_DIR, self::MAX_TMP_FILE_AGE); 99cfbf56adSGreg Roach 100cfbf56adSGreg Roach // Clear entries in database tables 101cfbf56adSGreg Roach $this->housekeeping_service->deleteOldLogs(self::MAX_LOG_AGE); 102cfbf56adSGreg Roach 103cfbf56adSGreg Roach $this->housekeeping_service->deleteOldSessions(self::MAX_SESSION_AGE); 104cfbf56adSGreg Roach } 105cfbf56adSGreg Roach} 106