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 Fig\Http\Message\RequestMethodInterface; 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; 33use Throwable; 34 35use function view; 36 37/** 38 * Middleware to check if a new version of webtrees is available. 39 */ 40class CheckForNewVersion extends AbstractModule implements MiddlewareInterface 41{ 42 private EmailService $email_service; 43 44 private UpgradeService $upgrade_service; 45 46 private UserService $user_service; 47 48 /** 49 * @param EmailService $email_service 50 * @param UpgradeService $upgrade_service 51 * @param UserService $user_service 52 */ 53 public function __construct(EmailService $email_service, UpgradeService $upgrade_service, UserService $user_service) 54 { 55 $this->email_service = $email_service; 56 $this->upgrade_service = $upgrade_service; 57 $this->user_service = $user_service; 58 } 59 60 /** 61 * How should this module be identified in the control panel, etc.? 62 * 63 * @return string 64 */ 65 public function title(): string 66 { 67 return I18N::translate('Check for new version'); 68 } 69 70 /** 71 * A sentence describing what this module does. 72 * 73 * @return string 74 */ 75 public function description(): string 76 { 77 return I18N::translate('Send an email to all administrators when a new version of webtrees is available.'); 78 } 79 80 /** 81 * @param ServerRequestInterface $request 82 * @param RequestHandlerInterface $handler 83 * 84 * @return ResponseInterface 85 */ 86 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 87 { 88 try { 89 // Only run on full page requests. 90 if ( 91 $request->getMethod() === RequestMethodInterface::METHOD_GET && 92 $request->getHeaderLine('X-Requested-With') === '' && 93 $this->upgrade_service->isUpgradeAvailable() 94 ) { 95 $latest_version = $this->upgrade_service->latestVersion(); 96 $latest_version_email = Site::getPreference('LATEST_WT_VERSION_EMAIL'); 97 98 // Have we emailed about this version before? 99 if ($latest_version !== $latest_version_email) { 100 Site::setPreference('LATEST_WT_VERSION_EMAIL', $latest_version); 101 102 foreach ($this->user_service->administrators() as $administrator) { 103 $this->email_service->send( 104 new SiteUser(), 105 $administrator, 106 new SiteUser(), 107 I18N::translate('A new version of webtrees is available.'), 108 view('emails/new-version-text', [ 109 'latest_version' => $latest_version, 110 'recipient' => $administrator, 111 'url' => $request->getAttribute('base_url'), 112 ]), 113 view('emails/new-version-html', [ 114 'latest_version' => $latest_version, 115 'recipient' => $administrator, 116 'url' => $request->getAttribute('base_url'), 117 ]) 118 ); 119 } 120 } 121 } 122 } catch (Throwable $ex) { 123 throw $ex; 124 // We couldn't fetch the latest version or send an email? Nothing we can do... 125 } 126 127 return $handler->handle($request); 128 } 129} 130