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 Fisharebest\Webtrees\Webtrees; 23use League\Flysystem\Adapter\Local; 24use League\Flysystem\Filesystem; 25use League\Flysystem\FilesystemInterface; 26use Psr\Http\Message\ResponseInterface; 27use Psr\Http\Message\ServerRequestInterface; 28use Psr\Http\Server\MiddlewareInterface; 29use Psr\Http\Server\RequestHandlerInterface; 30 31/** 32 * Run the housekeeping service at irregular intervals. 33 */ 34class DoHousekeeping implements MiddlewareInterface 35{ 36 // Delete cache files after 1 hour. 37 private const MAX_CACHE_AGE = 60 * 60; 38 39 // Delete thumnnails after 90 days. 40 private const MAX_THUMBNAIL_AGE = 60 * 60 * 24 * 90; 41 42 // Delete files in /data/tmp after 1 hour. 43 private const TMP_DIR = 'data/tmp'; 44 private const MAX_TMP_FILE_AGE = 60 * 60; 45 46 // Delete error logs after 90 days. 47 private const MAX_LOG_AGE = 60 * 60 * 24 * 90; 48 49 // Delete inactive sessions after 1 day. 50 private const MAX_SESSION_AGE = 60 * 60 * 24; 51 52 // Run the cleanup every 100 requests. 53 private const PROBABILITY = 100; 54 55 /** @var HousekeepingService */ 56 private $housekeeping_service; 57 58 /** 59 * Housekeeping constructor. 60 * 61 * @param HousekeepingService $housekeeping_service 62 */ 63 public function __construct(HousekeepingService $housekeeping_service) 64 { 65 $this->housekeeping_service = $housekeeping_service; 66 } 67 68 /** 69 * @param ServerRequestInterface $request 70 * @param RequestHandlerInterface $handler 71 * 72 * @return ResponseInterface 73 */ 74 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 75 { 76 $response = $handler->handle($request); 77 78 // Run the cleanup after random page requests. 79 if ($request->getMethod() === RequestMethodInterface::METHOD_GET && random_int(1, self::PROBABILITY) === 1) { 80 $this->runHousekeeping(); 81 } 82 83 return $response; 84 } 85 86 /** 87 * Run the various housekeeping services. 88 * 89 * @return void 90 */ 91 private function runHousekeeping(): void 92 { 93 $data_filesystem = app(FilesystemInterface::class); 94 $root_filesystem = new Filesystem(new Local(Webtrees::ROOT_DIR)); 95 96 // Clear files in the (user-specified) data folder - which might not be local files 97 $this->housekeeping_service->deleteOldFiles($data_filesystem, 'cache', self::MAX_CACHE_AGE); 98 99 $this->housekeeping_service->deleteOldFiles($data_filesystem, 'thumbnail-cache', self::MAX_THUMBNAIL_AGE); 100 101 // Clear files in /data - which need to be local files 102 $this->housekeeping_service->deleteOldFiles($root_filesystem, self::TMP_DIR, self::MAX_TMP_FILE_AGE); 103 104 // Clear entries in database tables 105 $this->housekeeping_service->deleteOldLogs(self::MAX_LOG_AGE); 106 107 $this->housekeeping_service->deleteOldSessions(self::MAX_SESSION_AGE); 108 } 109} 110