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