xref: /webtrees/app/Module/CheckForNewVersion.php (revision 3dcd4833fd9173a198cd29d16244f64873c773fb)
11898855eSGreg Roach<?php
21898855eSGreg Roach
31898855eSGreg Roach/**
41898855eSGreg Roach * webtrees: online genealogy
51898855eSGreg Roach * Copyright (C) 2022 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
22*3dcd4833SGreg 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    /**
701898855eSGreg Roach     * A sentence describing what this module does.
711898855eSGreg Roach     *
721898855eSGreg Roach     * @return string
731898855eSGreg Roach     */
741898855eSGreg Roach    public function description(): string
751898855eSGreg Roach    {
76f37874adSGreg Roach        return I18N::translate('Send an email to all administrators when an upgrade is available.');
771898855eSGreg Roach    }
781898855eSGreg Roach
791898855eSGreg Roach    /**
801898855eSGreg Roach     * @param ServerRequestInterface  $request
811898855eSGreg Roach     * @param RequestHandlerInterface $handler
821898855eSGreg Roach     *
831898855eSGreg Roach     * @return ResponseInterface
841898855eSGreg Roach     */
851898855eSGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
861898855eSGreg Roach    {
87f37874adSGreg Roach        if ($this->upgrade_service->isUpgradeAvailable()) {
881898855eSGreg Roach            $latest_version       = $this->upgrade_service->latestVersion();
891898855eSGreg Roach            $latest_version_email = Site::getPreference('LATEST_WT_VERSION_EMAIL');
901898855eSGreg Roach
911898855eSGreg Roach            // Have we emailed about this version before?
921898855eSGreg Roach            if ($latest_version !== $latest_version_email) {
931898855eSGreg Roach                Site::setPreference('LATEST_WT_VERSION_EMAIL', $latest_version);
941898855eSGreg Roach
95*3dcd4833SGreg Roach                $old_language = I18N::languageTag();
96*3dcd4833SGreg Roach
971898855eSGreg Roach                foreach ($this->user_service->administrators() as $administrator) {
98*3dcd4833SGreg Roach                    I18N::init($administrator->getPreference(UserInterface::PREF_LANGUAGE));
99*3dcd4833SGreg Roach
1001898855eSGreg Roach                    $this->email_service->send(
1011898855eSGreg Roach                        new SiteUser(),
1021898855eSGreg Roach                        $administrator,
1031898855eSGreg Roach                        new SiteUser(),
1041898855eSGreg Roach                        I18N::translate('A new version of webtrees is available.'),
1051898855eSGreg Roach                        view('emails/new-version-text', [
1061898855eSGreg Roach                            'latest_version' => $latest_version,
1071898855eSGreg Roach                            'recipient'      => $administrator,
1081898855eSGreg Roach                            'url'            => $request->getAttribute('base_url'),
1091898855eSGreg Roach                        ]),
1101898855eSGreg Roach                        view('emails/new-version-html', [
1111898855eSGreg Roach                            'latest_version' => $latest_version,
1121898855eSGreg Roach                            'recipient'      => $administrator,
1131898855eSGreg Roach                            'url'            => $request->getAttribute('base_url'),
1141898855eSGreg Roach                        ])
1151898855eSGreg Roach                    );
1161898855eSGreg Roach                }
117*3dcd4833SGreg Roach
118*3dcd4833SGreg Roach                I18N::init($old_language);
1191898855eSGreg Roach            }
1201898855eSGreg Roach        }
1211898855eSGreg Roach
1221898855eSGreg Roach        return $handler->handle($request);
1231898855eSGreg Roach    }
1241898855eSGreg Roach}
125