1 | <?php |
||||
2 | |||||
3 | /** |
||||
4 | * webtrees: online genealogy |
||||
5 | * Copyright (C) 2025 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\StatusCodeInterface; |
||||
23 | use Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException; |
||||
24 | use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; |
||||
25 | use Fisharebest\Webtrees\Mime; |
||||
0 ignored issues
–
show
|
|||||
26 | use Fisharebest\Webtrees\Registry; |
||||
27 | use Fisharebest\Webtrees\Validator; |
||||
28 | use GuzzleHttp\Client; |
||||
29 | use GuzzleHttp\Exception\GuzzleException; |
||||
30 | use Psr\Http\Message\ResponseInterface; |
||||
31 | use Psr\Http\Message\ServerRequestInterface; |
||||
32 | |||||
33 | use function str_contains; |
||||
34 | use function strtoupper; |
||||
35 | |||||
36 | /** |
||||
37 | * Trait ModuleCustomTrait - default implementation of ModuleCustomInterface |
||||
38 | */ |
||||
39 | trait ModuleCustomTrait |
||||
40 | { |
||||
41 | /** |
||||
42 | * A unique internal name for this module (based on the installation folder). |
||||
43 | * |
||||
44 | * @return string |
||||
45 | */ |
||||
46 | abstract public function name(): string; |
||||
47 | |||||
48 | /** |
||||
49 | * Where does this module store its resources |
||||
50 | * |
||||
51 | * @return string |
||||
52 | */ |
||||
53 | abstract public function resourcesFolder(): string; |
||||
54 | |||||
55 | /** |
||||
56 | * The person or organisation who created this module. |
||||
57 | * |
||||
58 | * @return string |
||||
59 | */ |
||||
60 | public function customModuleAuthorName(): string |
||||
61 | { |
||||
62 | return ''; |
||||
63 | } |
||||
64 | |||||
65 | /** |
||||
66 | * The version of this module. |
||||
67 | * |
||||
68 | * @return string e.g. '1.2.3' |
||||
69 | */ |
||||
70 | public function customModuleVersion(): string |
||||
71 | { |
||||
72 | return ''; |
||||
73 | } |
||||
74 | |||||
75 | /** |
||||
76 | * A URL that will provide the latest version of this module. |
||||
77 | * |
||||
78 | * @return string |
||||
79 | */ |
||||
80 | public function customModuleLatestVersionUrl(): string |
||||
81 | { |
||||
82 | return ''; |
||||
83 | } |
||||
84 | |||||
85 | /** |
||||
86 | * Fetch the latest version of this module. |
||||
87 | * |
||||
88 | * @return string |
||||
89 | */ |
||||
90 | public function customModuleLatestVersion(): string |
||||
91 | { |
||||
92 | // No update URL provided. |
||||
93 | if ($this->customModuleLatestVersionUrl() === '') { |
||||
94 | return $this->customModuleVersion(); |
||||
95 | } |
||||
96 | |||||
97 | return Registry::cache()->file()->remember($this->name() . '-latest-version', function (): string { |
||||
98 | try { |
||||
99 | $client = new Client([ |
||||
100 | 'timeout' => 3, |
||||
101 | ]); |
||||
102 | |||||
103 | $response = $client->get($this->customModuleLatestVersionUrl()); |
||||
104 | |||||
105 | if ($response->getStatusCode() === StatusCodeInterface::STATUS_OK) { |
||||
106 | $version = $response->getBody()->getContents(); |
||||
107 | |||||
108 | // Does the response look like a version? |
||||
109 | if (preg_match('/^\d+\.\d+\.\d+/', $version)) { |
||||
110 | return $version; |
||||
111 | } |
||||
112 | } |
||||
113 | } catch (GuzzleException) { |
||||
114 | // Can't connect to the server? |
||||
115 | } |
||||
116 | |||||
117 | return $this->customModuleVersion(); |
||||
118 | }, 86400); |
||||
119 | } |
||||
120 | |||||
121 | /** |
||||
122 | * Where to get support for this module. Perhaps a github repository? |
||||
123 | * |
||||
124 | * @return string |
||||
125 | */ |
||||
126 | public function customModuleSupportUrl(): string |
||||
127 | { |
||||
128 | return ''; |
||||
129 | } |
||||
130 | |||||
131 | /** |
||||
132 | * Additional/updated translations. |
||||
133 | * |
||||
134 | * @param string $language |
||||
135 | * |
||||
136 | * @return array<string,string> |
||||
137 | */ |
||||
138 | public function customTranslations(string $language): array |
||||
139 | { |
||||
140 | return []; |
||||
141 | } |
||||
142 | |||||
143 | /** |
||||
144 | * Create a URL for an asset. |
||||
145 | * |
||||
146 | * @param string $asset e.g. "css/theme.css" or "img/banner.png" |
||||
147 | * |
||||
148 | * @return string |
||||
149 | */ |
||||
150 | public function assetUrl(string $asset): string |
||||
151 | { |
||||
152 | $file = $this->resourcesFolder() . $asset; |
||||
153 | |||||
154 | // Add the file's modification time to the URL, so we can set long expiry cache headers. |
||||
155 | $hash = filemtime($file); |
||||
156 | |||||
157 | return route('module', [ |
||||
158 | 'module' => $this->name(), |
||||
159 | 'action' => 'Asset', |
||||
160 | 'asset' => $asset, |
||||
161 | 'hash' => $hash, |
||||
162 | ]); |
||||
163 | } |
||||
164 | |||||
165 | /** |
||||
166 | * Serve a CSS/JS file. |
||||
167 | * |
||||
168 | * @param ServerRequestInterface $request |
||||
169 | * |
||||
170 | * @return ResponseInterface |
||||
171 | */ |
||||
172 | public function getAssetAction(ServerRequestInterface $request): ResponseInterface |
||||
173 | { |
||||
174 | // The file being requested. e.g. "css/theme.css" |
||||
175 | $asset = Validator::queryParams($request)->string('asset'); |
||||
176 | |||||
177 | // Do not allow requests that try to access parent folders. |
||||
178 | if (str_contains($asset, '..')) { |
||||
179 | throw new HttpAccessDeniedException($asset); |
||||
180 | } |
||||
181 | |||||
182 | // Find the file for this asset. |
||||
183 | // Note that we could also generate CSS files using views/templates. |
||||
184 | // e.g. $file = view(....) |
||||
185 | $file = $this->resourcesFolder() . $asset; |
||||
186 | |||||
187 | if (!file_exists($file)) { |
||||
188 | throw new HttpNotFoundException(e($file)); |
||||
189 | } |
||||
190 | |||||
191 | $content = file_get_contents($file); |
||||
192 | $extension = strtoupper(pathinfo($asset, PATHINFO_EXTENSION)); |
||||
0 ignored issues
–
show
It seems like
pathinfo($asset, Fishare...ule\PATHINFO_EXTENSION) can also be of type array ; however, parameter $string of strtoupper() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
193 | $mime_type = Mime::TYPES[$extension] ?? Mime::DEFAULT_TYPE; |
||||
194 | |||||
195 | return response($content, StatusCodeInterface::STATUS_OK, [ |
||||
196 | 'cache-control' => 'public,max-age=31536000', |
||||
197 | 'content-type' => $mime_type, |
||||
198 | ]); |
||||
199 | } |
||||
200 | } |
||||
201 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths