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