xref: /webtrees/app/Module/CheckForNewVersion.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
11898855eSGreg Roach<?php
21898855eSGreg Roach
31898855eSGreg Roach/**
41898855eSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
61898855eSGreg Roach * This program is free software: you can redistribute it and/or modify
71898855eSGreg Roach * it under the terms of the GNU General Public License as published by
81898855eSGreg Roach * the Free Software Foundation, either version 3 of the License, or
91898855eSGreg Roach * (at your option) any later version.
101898855eSGreg Roach * This program is distributed in the hope that it will be useful,
111898855eSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
121898855eSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
131898855eSGreg Roach * GNU General Public License for more details.
141898855eSGreg Roach * You should have received a copy of the GNU General Public License
151898855eSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
161898855eSGreg Roach */
171898855eSGreg Roach
181898855eSGreg Roachdeclare(strict_types=1);
191898855eSGreg Roach
201898855eSGreg Roachnamespace Fisharebest\Webtrees\Module;
211898855eSGreg Roach
223dcd4833SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
231898855eSGreg Roachuse Fisharebest\Webtrees\I18N;
241898855eSGreg Roachuse Fisharebest\Webtrees\Services\EmailService;
251898855eSGreg Roachuse Fisharebest\Webtrees\Services\UpgradeService;
261898855eSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
271898855eSGreg Roachuse Fisharebest\Webtrees\Site;
281898855eSGreg Roachuse Fisharebest\Webtrees\SiteUser;
291898855eSGreg Roachuse Psr\Http\Message\ResponseInterface;
301898855eSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
311898855eSGreg Roachuse Psr\Http\Server\MiddlewareInterface;
321898855eSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
331898855eSGreg Roach
341898855eSGreg Roachuse function view;
351898855eSGreg Roach
361898855eSGreg Roach/**
371898855eSGreg Roach * Middleware to check if a new version of webtrees is available.
381898855eSGreg Roach */
391898855eSGreg Roachclass CheckForNewVersion extends AbstractModule implements MiddlewareInterface
401898855eSGreg Roach{
411898855eSGreg Roach    private EmailService $email_service;
421898855eSGreg Roach
431898855eSGreg Roach    private UpgradeService $upgrade_service;
441898855eSGreg Roach
451898855eSGreg Roach    private UserService $user_service;
461898855eSGreg Roach
471898855eSGreg Roach    /**
481898855eSGreg Roach     * @param EmailService   $email_service
491898855eSGreg Roach     * @param UpgradeService $upgrade_service
501898855eSGreg Roach     * @param UserService    $user_service
511898855eSGreg Roach     */
521898855eSGreg Roach    public function __construct(EmailService $email_service, UpgradeService $upgrade_service, UserService $user_service)
531898855eSGreg Roach    {
541898855eSGreg Roach        $this->email_service   = $email_service;
551898855eSGreg Roach        $this->upgrade_service = $upgrade_service;
561898855eSGreg Roach        $this->user_service    = $user_service;
571898855eSGreg Roach    }
581898855eSGreg Roach
591898855eSGreg Roach    /**
601898855eSGreg Roach     * How should this module be identified in the control panel, etc.?
611898855eSGreg Roach     *
621898855eSGreg Roach     * @return string
631898855eSGreg Roach     */
641898855eSGreg Roach    public function title(): string
651898855eSGreg Roach    {
661898855eSGreg Roach        return I18N::translate('Check for new version');
671898855eSGreg Roach    }
681898855eSGreg Roach
691898855eSGreg Roach    public function description(): string
701898855eSGreg Roach    {
71f37874adSGreg Roach        return I18N::translate('Send an email to all administrators when an upgrade is available.');
721898855eSGreg Roach    }
731898855eSGreg Roach
741898855eSGreg Roach    /**
751898855eSGreg Roach     * @param ServerRequestInterface  $request
761898855eSGreg Roach     * @param RequestHandlerInterface $handler
771898855eSGreg Roach     *
781898855eSGreg Roach     * @return ResponseInterface
791898855eSGreg Roach     */
801898855eSGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
811898855eSGreg Roach    {
82f37874adSGreg Roach        if ($this->upgrade_service->isUpgradeAvailable()) {
831898855eSGreg Roach            $latest_version       = $this->upgrade_service->latestVersion();
841898855eSGreg Roach            $latest_version_email = Site::getPreference('LATEST_WT_VERSION_EMAIL');
851898855eSGreg Roach
861898855eSGreg Roach            // Have we emailed about this version before?
871898855eSGreg Roach            if ($latest_version !== $latest_version_email) {
881898855eSGreg Roach                Site::setPreference('LATEST_WT_VERSION_EMAIL', $latest_version);
891898855eSGreg Roach
903dcd4833SGreg Roach                $old_language = I18N::languageTag();
913dcd4833SGreg Roach
921898855eSGreg Roach                foreach ($this->user_service->administrators() as $administrator) {
933dcd4833SGreg Roach                    I18N::init($administrator->getPreference(UserInterface::PREF_LANGUAGE));
943dcd4833SGreg Roach
951898855eSGreg Roach                    $this->email_service->send(
961898855eSGreg Roach                        new SiteUser(),
971898855eSGreg Roach                        $administrator,
981898855eSGreg Roach                        new SiteUser(),
991898855eSGreg Roach                        I18N::translate('A new version of webtrees is available.'),
1001898855eSGreg Roach                        view('emails/new-version-text', [
1011898855eSGreg Roach                            'latest_version' => $latest_version,
1021898855eSGreg Roach                            'recipient'      => $administrator,
103*b6fe8cd7SGreg Roach                            'url'            => $request->getAttribute('base_url', ''),
1041898855eSGreg Roach                        ]),
1051898855eSGreg Roach                        view('emails/new-version-html', [
1061898855eSGreg Roach                            'latest_version' => $latest_version,
1071898855eSGreg Roach                            'recipient'      => $administrator,
108*b6fe8cd7SGreg Roach                            'url'            => $request->getAttribute('base_url', ''),
1091898855eSGreg Roach                        ])
1101898855eSGreg Roach                    );
1111898855eSGreg Roach                }
1123dcd4833SGreg Roach
1133dcd4833SGreg Roach                I18N::init($old_language);
1141898855eSGreg Roach            }
1151898855eSGreg Roach        }
1161898855eSGreg Roach
1171898855eSGreg Roach        return $handler->handle($request);
1181898855eSGreg Roach    }
1191898855eSGreg Roach}
120