1cfbf56adSGreg Roach<?php 2cfbf56adSGreg Roach/** 3cfbf56adSGreg Roach * webtrees: online genealogy 4cfbf56adSGreg Roach * Copyright (C) 2019 webtrees development team 5cfbf56adSGreg Roach * This program is free software: you can redistribute it and/or modify 6cfbf56adSGreg Roach * it under the terms of the GNU General Public License as published by 7cfbf56adSGreg Roach * the Free Software Foundation, either version 3 of the License, or 8cfbf56adSGreg Roach * (at your option) any later version. 9cfbf56adSGreg Roach * This program is distributed in the hope that it will be useful, 10cfbf56adSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 11cfbf56adSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12cfbf56adSGreg Roach * GNU General Public License for more details. 13cfbf56adSGreg Roach * You should have received a copy of the GNU General Public License 14cfbf56adSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 15cfbf56adSGreg Roach */ 16cfbf56adSGreg Roachdeclare(strict_types=1); 17cfbf56adSGreg Roach 18cfbf56adSGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware; 19cfbf56adSGreg Roach 20cfbf56adSGreg Roachuse Fig\Http\Message\RequestMethodInterface; 21cfbf56adSGreg Roachuse Fisharebest\Webtrees\Services\HousekeepingService; 22*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Webtrees; 23cfbf56adSGreg Roachuse League\Flysystem\Adapter\Local; 24cfbf56adSGreg Roachuse League\Flysystem\Filesystem; 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{ 36cfbf56adSGreg Roach // Delete cache files after 1 hour. 37cfbf56adSGreg Roach private const MAX_CACHE_AGE = 60 * 60; 38cfbf56adSGreg Roach 39cfbf56adSGreg Roach // Delete thumnnails after 90 days. 40cfbf56adSGreg Roach private const MAX_THUMBNAIL_AGE = 60 * 60 * 24 * 90; 41cfbf56adSGreg Roach 42cfbf56adSGreg Roach // Delete files in /data/tmp after 1 hour. 43cfbf56adSGreg Roach private const TMP_DIR = 'data/tmp'; 44cfbf56adSGreg Roach private const MAX_TMP_FILE_AGE = 60 * 60; 45cfbf56adSGreg Roach 46cfbf56adSGreg Roach // Delete error logs after 90 days. 47cfbf56adSGreg Roach private const MAX_LOG_AGE = 60 * 60 * 24 * 90; 48cfbf56adSGreg Roach 49cfbf56adSGreg Roach // Delete inactive sessions after 1 day. 50cfbf56adSGreg Roach private const MAX_SESSION_AGE = 60 * 60 * 24; 51cfbf56adSGreg Roach 52cfbf56adSGreg Roach // Run the cleanup every 100 requests. 53cfbf56adSGreg Roach private const PROBABILITY = 100; 54cfbf56adSGreg Roach 55cfbf56adSGreg Roach /** @var HousekeepingService */ 56cfbf56adSGreg Roach private $housekeeping_service; 57cfbf56adSGreg Roach 58cfbf56adSGreg Roach /** 59cfbf56adSGreg Roach * Housekeeping constructor. 60cfbf56adSGreg Roach * 61cfbf56adSGreg Roach * @param HousekeepingService $housekeeping_service 62cfbf56adSGreg Roach */ 63cfbf56adSGreg Roach public function __construct(HousekeepingService $housekeeping_service) 64cfbf56adSGreg Roach { 65cfbf56adSGreg Roach $this->housekeeping_service = $housekeeping_service; 66cfbf56adSGreg Roach } 67cfbf56adSGreg Roach 68cfbf56adSGreg Roach /** 69cfbf56adSGreg Roach * @param ServerRequestInterface $request 70cfbf56adSGreg Roach * @param RequestHandlerInterface $handler 71cfbf56adSGreg Roach * 72cfbf56adSGreg Roach * @return ResponseInterface 73cfbf56adSGreg Roach */ 74cfbf56adSGreg Roach public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 75cfbf56adSGreg Roach { 76cfbf56adSGreg Roach $response = $handler->handle($request); 77cfbf56adSGreg Roach 78cfbf56adSGreg Roach // Run the cleanup after random page requests. 79cfbf56adSGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_GET && random_int(1, self::PROBABILITY) === 1) { 80cfbf56adSGreg Roach $this->runHousekeeping(); 81cfbf56adSGreg Roach } 82cfbf56adSGreg Roach 83cfbf56adSGreg Roach return $response; 84cfbf56adSGreg Roach } 85cfbf56adSGreg Roach 86cfbf56adSGreg Roach /** 87cfbf56adSGreg Roach * Run the various housekeeping services. 88cfbf56adSGreg Roach * 89cfbf56adSGreg Roach * @return void 90cfbf56adSGreg Roach */ 91cfbf56adSGreg Roach private function runHousekeeping(): void 92cfbf56adSGreg Roach { 93cfbf56adSGreg Roach $data_filesystem = app(FilesystemInterface::class); 94*f397d0fdSGreg Roach $root_filesystem = new Filesystem(new Local(Webtrees::ROOT_DIR)); 95cfbf56adSGreg Roach 96cfbf56adSGreg Roach // Clear files in the (user-specified) data folder - which might not be local files 97cfbf56adSGreg Roach $this->housekeeping_service->deleteOldFiles($data_filesystem, 'cache', self::MAX_CACHE_AGE); 98cfbf56adSGreg Roach 99cfbf56adSGreg Roach $this->housekeeping_service->deleteOldFiles($data_filesystem, 'thumbnail-cache', self::MAX_THUMBNAIL_AGE); 100cfbf56adSGreg Roach 101cfbf56adSGreg Roach // Clear files in /data - which need to be local files 102cfbf56adSGreg Roach $this->housekeeping_service->deleteOldFiles($root_filesystem, self::TMP_DIR, self::MAX_TMP_FILE_AGE); 103cfbf56adSGreg Roach 104cfbf56adSGreg Roach // Clear entries in database tables 105cfbf56adSGreg Roach $this->housekeeping_service->deleteOldLogs(self::MAX_LOG_AGE); 106cfbf56adSGreg Roach 107cfbf56adSGreg Roach $this->housekeeping_service->deleteOldSessions(self::MAX_SESSION_AGE); 108cfbf56adSGreg Roach } 109cfbf56adSGreg Roach} 110