1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2026 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees\Module;
use Fig\Http\Message\StatusCodeInterface;
use Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
use Fisharebest\Webtrees\Mime;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Validator;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use function str_contains;
use function strtoupper;
/**
* Trait ModuleCustomTrait - default implementation of ModuleCustomInterface
*/
trait ModuleCustomTrait
{
/**
* A unique internal name for this module (based on the installation folder).
*
* @return string
*/
abstract public function name(): string;
/**
* Where does this module store its resources
*
* @return string
*/
abstract public function resourcesFolder(): string;
/**
* The person or organisation who created this module.
*
* @return string
*/
public function customModuleAuthorName(): string
{
return '';
}
/**
* The version of this module.
*
* @return string e.g. '1.2.3'
*/
public function customModuleVersion(): string
{
return '';
}
/**
* A URL that will provide the latest version of this module.
*
* @return string
*/
public function customModuleLatestVersionUrl(): string
{
return '';
}
/**
* Fetch the latest version of this module.
*
* @return string
*/
public function customModuleLatestVersion(): string
{
// No update URL provided.
if ($this->customModuleLatestVersionUrl() === '') {
return $this->customModuleVersion();
}
return Registry::cache()->file()->remember($this->name() . '-latest-version', function (): string {
try {
$client = new Client([
'timeout' => 3,
]);
$response = $client->get($this->customModuleLatestVersionUrl());
if ($response->getStatusCode() === StatusCodeInterface::STATUS_OK) {
$version = $response->getBody()->getContents();
// Does the response look like a version?
if (preg_match('/^\d+\.\d+\.\d+/', $version)) {
return $version;
}
}
} catch (GuzzleException) {
// Can't connect to the server?
}
return $this->customModuleVersion();
}, 86400);
}
/**
* Where to get support for this module. Perhaps a github repository?
*
* @return string
*/
public function customModuleSupportUrl(): string
{
return '';
}
/**
* Additional/updated translations.
*
* @param string $language
*
* @return array<string,string>
*/
public function customTranslations(string $language): array
{
return [];
}
/**
* Create a URL for an asset.
*
* @param string $asset e.g. "css/theme.css" or "img/banner.png"
*
* @return string
*/
public function assetUrl(string $asset): string
{
$file = $this->resourcesFolder() . $asset;
// Add the file's modification time to the URL, so we can set long expiry cache headers.
$hash = filemtime($file);
return route('module', [
'module' => $this->name(),
'action' => 'Asset',
'asset' => $asset,
'hash' => $hash,
]);
}
/**
* Serve a CSS/JS file.
*
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function getAssetAction(ServerRequestInterface $request): ResponseInterface
{
// The file being requested. e.g. "css/theme.css"
$asset = Validator::queryParams($request)->string('asset');
// Do not allow requests that try to access parent folders.
if (str_contains($asset, '..')) {
throw new HttpAccessDeniedException($asset);
}
// Find the file for this asset.
// Note that we could also generate CSS files using views/templates.
// e.g. $file = view(....)
$file = $this->resourcesFolder() . $asset;
if (!file_exists($file)) {
throw new HttpNotFoundException(e($file));
}
$content = file_get_contents($file);
$extension = strtoupper(pathinfo($asset, PATHINFO_EXTENSION));
$mime_type = Mime::TYPES[$extension] ?? Mime::DEFAULT_TYPE;
return response($content, StatusCodeInterface::STATUS_OK, [
'cache-control' => 'public,max-age=31536000',
'content-type' => $mime_type,
]);
}
}
|