xref: /webtrees/app/Http/Middleware/CheckForNewVersion.php (revision dda05045b4c2f9063e99083d2dbff48269db4ac2)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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 <https://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\I18N;
24use Fisharebest\Webtrees\Services\EmailService;
25use Fisharebest\Webtrees\Services\UpgradeService;
26use Fisharebest\Webtrees\Services\UserService;
27use Fisharebest\Webtrees\Site;
28use Fisharebest\Webtrees\SiteUser;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\MiddlewareInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33use Throwable;
34
35use function view;
36
37/**
38 * Middleware to check if a new version of webtrees is available.
39 */
40class CheckForNewVersion implements MiddlewareInterface
41{
42    private UpgradeService $upgrade_service;
43
44    /**
45     * @param UpgradeService $upgrade_service
46     */
47    public function __construct(UpgradeService $upgrade_service)
48    {
49        $this->upgrade_service = $upgrade_service;
50    }
51
52    /**
53     * @param ServerRequestInterface  $request
54     * @param RequestHandlerInterface $handler
55     *
56     * @return ResponseInterface
57     */
58    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
59    {
60        // Only run on full page requests.
61        if ($request->getMethod() === RequestMethodInterface::METHOD_GET && $request->getHeaderLine('X-Requested-With') === '') {
62            $this->upgrade_service->isUpgradeAvailable();
63        }
64
65        return $handler->handle($request);
66    }
67}
68