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 public function description(): string 70 { 71 return I18N::translate('Send an email to all administrators when an upgrade is available.'); 72 } 73 74 /** 75 * @param ServerRequestInterface $request 76 * @param RequestHandlerInterface $handler 77 * 78 * @return ResponseInterface 79 */ 80 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 81 { 82 if ($this->upgrade_service->isUpgradeAvailable()) { 83 $latest_version = $this->upgrade_service->latestVersion(); 84 $latest_version_email = Site::getPreference('LATEST_WT_VERSION_EMAIL'); 85 86 // Have we emailed about this version before? 87 if ($latest_version !== $latest_version_email) { 88 Site::setPreference('LATEST_WT_VERSION_EMAIL', $latest_version); 89 90 $old_language = I18N::languageTag(); 91 92 foreach ($this->user_service->administrators() as $administrator) { 93 I18N::init($administrator->getPreference(UserInterface::PREF_LANGUAGE)); 94 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 I18N::init($old_language); 114 } 115 } 116 117 return $handler->handle($request); 118 } 119} 120