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