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