xref: /webtrees/app/Http/Middleware/DoHousekeeping.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
1cfbf56adSGreg Roach<?php
23976b470SGreg Roach
3cfbf56adSGreg Roach/**
4cfbf56adSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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
1589f7189bSGreg 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;
25f7cf8a15SGreg Roachuse League\Flysystem\FilesystemOperator;
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
53c4943cffSGreg Roach    private HousekeepingService $housekeeping_service;
54cfbf56adSGreg Roach
55cfbf56adSGreg Roach    /**
56cfbf56adSGreg Roach     * @param HousekeepingService $housekeeping_service
57cfbf56adSGreg Roach     */
58cfbf56adSGreg Roach    public function __construct(HousekeepingService $housekeeping_service)
59cfbf56adSGreg Roach    {
60cfbf56adSGreg Roach        $this->housekeeping_service = $housekeeping_service;
61cfbf56adSGreg Roach    }
62cfbf56adSGreg Roach
63cfbf56adSGreg Roach    /**
64cfbf56adSGreg Roach     * @param ServerRequestInterface  $request
65cfbf56adSGreg Roach     * @param RequestHandlerInterface $handler
66cfbf56adSGreg Roach     *
67cfbf56adSGreg Roach     * @return ResponseInterface
68cfbf56adSGreg Roach     */
69cfbf56adSGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
70cfbf56adSGreg Roach    {
71cfbf56adSGreg Roach        $response = $handler->handle($request);
72cfbf56adSGreg Roach
73cfbf56adSGreg Roach        // Run the cleanup after random page requests.
74cfbf56adSGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_GET && random_int(1, self::PROBABILITY) === 1) {
756b9cb339SGreg Roach            $this->runHousekeeping(Registry::filesystem()->data(), Registry::filesystem()->root());
76cfbf56adSGreg Roach        }
77cfbf56adSGreg Roach
78cfbf56adSGreg Roach        return $response;
79cfbf56adSGreg Roach    }
80cfbf56adSGreg Roach
81cfbf56adSGreg Roach    /**
82cfbf56adSGreg Roach     * Run the various housekeeping services.
83cfbf56adSGreg Roach     *
84f7cf8a15SGreg Roach     * @param FilesystemOperator $data_filesystem
85f7cf8a15SGreg Roach     * @param FilesystemOperator $root_filesystem
86a04bb9a2SGreg Roach     *
87cfbf56adSGreg Roach     * @return void
88cfbf56adSGreg Roach     */
89f7cf8a15SGreg Roach    private function runHousekeeping(FilesystemOperator $data_filesystem, FilesystemOperator $root_filesystem): void
90cfbf56adSGreg Roach    {
91b3cd3bbfSGreg Roach        // Clear old thumbnails
92e1357632SGreg Roach        $this->housekeeping_service->deleteOldFiles($data_filesystem, self::THUMBNAIL_DIR, self::MAX_THUMBNAIL_AGE);
93cfbf56adSGreg Roach
94b3cd3bbfSGreg Roach        // Clear temporary files
95cfbf56adSGreg Roach        $this->housekeeping_service->deleteOldFiles($root_filesystem, self::TMP_DIR, self::MAX_TMP_FILE_AGE);
96cfbf56adSGreg Roach
97cfbf56adSGreg Roach        // Clear entries in database tables
98cfbf56adSGreg Roach        $this->housekeeping_service->deleteOldLogs(self::MAX_LOG_AGE);
99cfbf56adSGreg Roach
100cfbf56adSGreg Roach        $this->housekeeping_service->deleteOldSessions(self::MAX_SESSION_AGE);
101cfbf56adSGreg Roach    }
102cfbf56adSGreg Roach}
103