xref: /webtrees/app/Http/Middleware/CheckForNewVersion.php (revision c4cbcd7b5d2995f2119e8970ddb302651026bf90)
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 Fisharebest\Webtrees\Webtrees;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\MiddlewareInterface;
33use Psr\Http\Server\RequestHandlerInterface;
34use Throwable;
35
36use function var_dump;
37use function view;
38
39/**
40 * Middleware to check if a new version of webtrees is available.
41 */
42class CheckForNewVersion implements MiddlewareInterface
43{
44    private EmailService $email_service;
45
46    private UpgradeService $upgrade_service;
47
48    private UserService $user_service;
49
50    /**
51     * @param EmailService   $email_service
52     * @param UpgradeService $upgrade_service
53     * @param UserService    $user_service
54     */
55    public function __construct(EmailService $email_service, UpgradeService $upgrade_service, UserService $user_service)
56    {
57        $this->email_service   = $email_service;
58        $this->upgrade_service = $upgrade_service;
59        $this->user_service    = $user_service;
60    }
61
62    /**
63     * @param ServerRequestInterface  $request
64     * @param RequestHandlerInterface $handler
65     *
66     * @return ResponseInterface
67     */
68    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
69    {
70        try {
71            if ($request->getMethod() === RequestMethodInterface::METHOD_GET && $this->upgrade_service->isUpgradeAvailable()) {
72                $latest_version       = $this->upgrade_service->latestVersion();
73                $latest_version_email = Site::getPreference('LATEST_WT_VERSION_EMAIL');
74
75                // Have we emailed about this version before?
76                if ($latest_version !== $latest_version_email) {
77                    Site::setPreference('LATEST_WT_VERSION_EMAIL', $latest_version);
78
79                    // Yuck.  The email service needs a setting from config.ini.php - which is in the request.
80                    Webtrees::set(ServerRequestInterface::class, $request);
81
82                    foreach ($this->user_service->administrators() as $administrator) {
83                        $this->email_service->send(
84                            new SiteUser(),
85                            $administrator,
86                            new SiteUser(),
87                            I18N::translate('A new version of webtrees is available.'),
88                            view('emails/new-version-text', [
89                                'latest_version' => $latest_version,
90                                'recipient'      => $administrator,
91                                'url'            => $request->getAttribute('base_url'),
92                            ]),
93                            view('emails/new-version-html', [
94                                'latest_version' => $latest_version,
95                                'recipient'      => $administrator,
96                                'url'            => $request->getAttribute('base_url'),
97                            ])
98                        );
99                    }
100                }
101            }
102        } catch (Throwable $ex) {
103            // We couldn't fetch the latest version or send an email? Nothing we can do...
104        }
105
106        return $handler->handle($request);
107    }
108}
109