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