xref: /webtrees/app/Http/Middleware/CheckForNewVersion.php (revision d11be7027e34e3121be11cc025421873364403f9)
1f37874adSGreg Roach<?php
2f37874adSGreg Roach
3f37874adSGreg Roach/**
4f37874adSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6f37874adSGreg Roach * This program is free software: you can redistribute it and/or modify
7f37874adSGreg Roach * it under the terms of the GNU General Public License as published by
8f37874adSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9f37874adSGreg Roach * (at your option) any later version.
10f37874adSGreg Roach * This program is distributed in the hope that it will be useful,
11f37874adSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12f37874adSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13f37874adSGreg Roach * GNU General Public License for more details.
14f37874adSGreg Roach * You should have received a copy of the GNU General Public License
15f37874adSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16f37874adSGreg Roach */
17f37874adSGreg Roach
18f37874adSGreg Roachdeclare(strict_types=1);
19f37874adSGreg Roach
20f37874adSGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
21f37874adSGreg Roach
22f37874adSGreg Roachuse Fig\Http\Message\RequestMethodInterface;
23f37874adSGreg Roachuse Fisharebest\Webtrees\Services\UpgradeService;
24f37874adSGreg Roachuse Psr\Http\Message\ResponseInterface;
25f37874adSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
26f37874adSGreg Roachuse Psr\Http\Server\MiddlewareInterface;
27f37874adSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
28f37874adSGreg Roach
29f37874adSGreg Roach/**
30f37874adSGreg Roach * Middleware to check if a new version of webtrees is available.
31f37874adSGreg Roach */
32f37874adSGreg Roachclass CheckForNewVersion implements MiddlewareInterface
33f37874adSGreg Roach{
34f37874adSGreg Roach    private UpgradeService $upgrade_service;
35f37874adSGreg Roach
36f37874adSGreg Roach    /**
37f37874adSGreg Roach     * @param UpgradeService $upgrade_service
38f37874adSGreg Roach     */
39f37874adSGreg Roach    public function __construct(UpgradeService $upgrade_service)
40f37874adSGreg Roach    {
41f37874adSGreg Roach        $this->upgrade_service = $upgrade_service;
42f37874adSGreg Roach    }
43f37874adSGreg Roach
44f37874adSGreg Roach    /**
45f37874adSGreg Roach     * @param ServerRequestInterface  $request
46f37874adSGreg Roach     * @param RequestHandlerInterface $handler
47f37874adSGreg Roach     *
48f37874adSGreg Roach     * @return ResponseInterface
49f37874adSGreg Roach     */
50f37874adSGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
51f37874adSGreg Roach    {
52f37874adSGreg Roach        // Only run on full page requests.
53f37874adSGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_GET && $request->getHeaderLine('X-Requested-With') === '') {
54f37874adSGreg Roach            $this->upgrade_service->isUpgradeAvailable();
55f37874adSGreg Roach        }
56f37874adSGreg Roach
57f37874adSGreg Roach        return $handler->handle($request);
58f37874adSGreg Roach    }
59f37874adSGreg Roach}
60