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