xref: /webtrees/app/Module/CheckForNewVersion.php (revision f37874adb4bbe17ac1299f5fe92d2bcce003c198)
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
221898855eSGreg Roachuse Fisharebest\Webtrees\I18N;
231898855eSGreg Roachuse Fisharebest\Webtrees\Services\EmailService;
241898855eSGreg Roachuse Fisharebest\Webtrees\Services\UpgradeService;
251898855eSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
261898855eSGreg Roachuse Fisharebest\Webtrees\Site;
271898855eSGreg Roachuse Fisharebest\Webtrees\SiteUser;
281898855eSGreg Roachuse Psr\Http\Message\ResponseInterface;
291898855eSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
301898855eSGreg Roachuse Psr\Http\Server\MiddlewareInterface;
311898855eSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
321898855eSGreg Roach
331898855eSGreg Roachuse function view;
341898855eSGreg Roach
351898855eSGreg Roach/**
361898855eSGreg Roach * Middleware to check if a new version of webtrees is available.
371898855eSGreg Roach */
381898855eSGreg Roachclass CheckForNewVersion extends AbstractModule implements MiddlewareInterface
391898855eSGreg Roach{
401898855eSGreg Roach    private EmailService $email_service;
411898855eSGreg Roach
421898855eSGreg Roach    private UpgradeService $upgrade_service;
431898855eSGreg Roach
441898855eSGreg Roach    private UserService $user_service;
451898855eSGreg Roach
461898855eSGreg Roach    /**
471898855eSGreg Roach     * @param EmailService   $email_service
481898855eSGreg Roach     * @param UpgradeService $upgrade_service
491898855eSGreg Roach     * @param UserService    $user_service
501898855eSGreg Roach     */
511898855eSGreg Roach    public function __construct(EmailService $email_service, UpgradeService $upgrade_service, UserService $user_service)
521898855eSGreg Roach    {
531898855eSGreg Roach        $this->email_service   = $email_service;
541898855eSGreg Roach        $this->upgrade_service = $upgrade_service;
551898855eSGreg Roach        $this->user_service    = $user_service;
561898855eSGreg Roach    }
571898855eSGreg Roach
581898855eSGreg Roach    /**
591898855eSGreg Roach     * How should this module be identified in the control panel, etc.?
601898855eSGreg Roach     *
611898855eSGreg Roach     * @return string
621898855eSGreg Roach     */
631898855eSGreg Roach    public function title(): string
641898855eSGreg Roach    {
651898855eSGreg Roach        return I18N::translate('Check for new version');
661898855eSGreg Roach    }
671898855eSGreg Roach
681898855eSGreg Roach    /**
691898855eSGreg Roach     * A sentence describing what this module does.
701898855eSGreg Roach     *
711898855eSGreg Roach     * @return string
721898855eSGreg Roach     */
731898855eSGreg Roach    public function description(): string
741898855eSGreg Roach    {
75*f37874adSGreg Roach        return I18N::translate('Send an email to all administrators when an upgrade is available.');
761898855eSGreg Roach    }
771898855eSGreg Roach
781898855eSGreg Roach    /**
791898855eSGreg Roach     * @param ServerRequestInterface  $request
801898855eSGreg Roach     * @param RequestHandlerInterface $handler
811898855eSGreg Roach     *
821898855eSGreg Roach     * @return ResponseInterface
831898855eSGreg Roach     */
841898855eSGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
851898855eSGreg Roach    {
86*f37874adSGreg Roach        if ($this->upgrade_service->isUpgradeAvailable()) {
871898855eSGreg Roach            $latest_version       = $this->upgrade_service->latestVersion();
881898855eSGreg Roach            $latest_version_email = Site::getPreference('LATEST_WT_VERSION_EMAIL');
891898855eSGreg Roach
901898855eSGreg Roach            // Have we emailed about this version before?
911898855eSGreg Roach            if ($latest_version !== $latest_version_email) {
921898855eSGreg Roach                Site::setPreference('LATEST_WT_VERSION_EMAIL', $latest_version);
931898855eSGreg Roach
941898855eSGreg Roach                foreach ($this->user_service->administrators() as $administrator) {
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,
1031898855eSGreg 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,
1081898855eSGreg Roach                            'url'            => $request->getAttribute('base_url'),
1091898855eSGreg Roach                        ])
1101898855eSGreg Roach                    );
1111898855eSGreg Roach                }
1121898855eSGreg Roach            }
1131898855eSGreg Roach        }
1141898855eSGreg Roach
1151898855eSGreg Roach        return $handler->handle($request);
1161898855eSGreg Roach    }
1171898855eSGreg Roach}
118