xref: /webtrees/app/Module/CheckForNewVersion.php (revision 8634febf6487fcb3ee7b59e88f828071a22cba22)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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\Module;
21
22use Fisharebest\Webtrees\Contracts\UserInterface;
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;
33
34use function view;
35
36/**
37 * Middleware to check if a new version of webtrees is available.
38 */
39class CheckForNewVersion extends AbstractModule implements MiddlewareInterface
40{
41    private EmailService $email_service;
42
43    private UpgradeService $upgrade_service;
44
45    private UserService $user_service;
46
47    /**
48     * @param EmailService   $email_service
49     * @param UpgradeService $upgrade_service
50     * @param UserService    $user_service
51     */
52    public function __construct(EmailService $email_service, UpgradeService $upgrade_service, UserService $user_service)
53    {
54        $this->email_service   = $email_service;
55        $this->upgrade_service = $upgrade_service;
56        $this->user_service    = $user_service;
57    }
58
59    /**
60     * How should this module be identified in the control panel, etc.?
61     *
62     * @return string
63     */
64    public function title(): string
65    {
66        return I18N::translate('Check for new version');
67    }
68
69    /**
70     * A sentence describing what this module does.
71     *
72     * @return string
73     */
74    public function description(): string
75    {
76        return I18N::translate('Send an email to all administrators when an upgrade is available.');
77    }
78
79    /**
80     * @param ServerRequestInterface  $request
81     * @param RequestHandlerInterface $handler
82     *
83     * @return ResponseInterface
84     */
85    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
86    {
87        if ($this->upgrade_service->isUpgradeAvailable()) {
88            $latest_version       = $this->upgrade_service->latestVersion();
89            $latest_version_email = Site::getPreference('LATEST_WT_VERSION_EMAIL');
90
91            // Have we emailed about this version before?
92            if ($latest_version !== $latest_version_email) {
93                Site::setPreference('LATEST_WT_VERSION_EMAIL', $latest_version);
94
95                $old_language = I18N::languageTag();
96
97                foreach ($this->user_service->administrators() as $administrator) {
98                    I18N::init($administrator->getPreference(UserInterface::PREF_LANGUAGE));
99
100                    $this->email_service->send(
101                        new SiteUser(),
102                        $administrator,
103                        new SiteUser(),
104                        I18N::translate('A new version of webtrees is available.'),
105                        view('emails/new-version-text', [
106                            'latest_version' => $latest_version,
107                            'recipient'      => $administrator,
108                            'url'            => $request->getAttribute('base_url'),
109                        ]),
110                        view('emails/new-version-html', [
111                            'latest_version' => $latest_version,
112                            'recipient'      => $administrator,
113                            'url'            => $request->getAttribute('base_url'),
114                        ])
115                    );
116                }
117
118                I18N::init($old_language);
119            }
120        }
121
122        return $handler->handle($request);
123    }
124}
125