Passed
Pull Request — main (#4945)
by
unknown
06:14
created

ModuleCustomTrait::customTranslations()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 6
eloc 20
nc 6
nop 1
dl 0
loc 27
rs 8.9777
c 2
b 0
f 1
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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Module;
21
22
use Fig\Http\Message\StatusCodeInterface;
23
use Fisharebest\Localization\Translation;
24
use Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
25
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
26
use Fisharebest\Webtrees\Mime;
27
use Fisharebest\Webtrees\Registry;
28
use Fisharebest\Webtrees\Validator;
29
use GuzzleHttp\Client;
30
use GuzzleHttp\Exception\GuzzleException;
31
use Psr\Http\Message\ResponseInterface;
32
use Psr\Http\Message\ServerRequestInterface;
33
34
use function str_contains;
35
use function strtoupper;
36
use function substr_replace;
37
38
/**
39
 * Trait ModuleCustomTrait - default implementation of ModuleCustomInterface
40
 */
41
trait ModuleCustomTrait
42
{
43
    /**
44
     * A unique internal name for this module (based on the installation folder).
45
     *
46
     * @return string
47
     */
48
    abstract public function name(): string;
49
50
    /**
51
     * Where does this module store its resources
52
     *
53
     * @return string
54
     */
55
    abstract public function resourcesFolder(): string;
56
57
    /**
58
     * The person or organisation who created this module.
59
     *
60
     * @return string
61
     */
62
    public function customModuleAuthorName(): string
63
    {
64
        return '';
65
    }
66
67
    /**
68
     * The version of this module.
69
     *
70
     * @return string  e.g. '1.2.3'
71
     */
72
    public function customModuleVersion(): string
73
    {
74
        return '';
75
    }
76
77
    /**
78
     * A URL that will provide the latest version of this module.
79
     *
80
     * @return string
81
     */
82
    public function customModuleLatestVersionUrl(): string
83
    {
84
        return '';
85
    }
86
87
    /**
88
     * Fetch the latest version of this module.
89
     *
90
     * @return string
91
     */
92
    public function customModuleLatestVersion(): string
93
    {
94
        // No update URL provided.
95
        if ($this->customModuleLatestVersionUrl() === '') {
96
            return $this->customModuleVersion();
97
        }
98
99
        return Registry::cache()->file()->remember($this->name() . '-latest-version', function (): string {
100
            try {
101
                $client = new Client([
102
                    'timeout' => 3,
103
                ]);
104
105
                $response = $client->get($this->customModuleLatestVersionUrl());
106
107
                if ($response->getStatusCode() === StatusCodeInterface::STATUS_OK) {
108
                    $version = $response->getBody()->getContents();
109
110
                    // Does the response look like a version?
111
                    if (preg_match('/^\d+\.\d+\.\d+/', $version)) {
112
                        return $version;
113
                    }
114
                }
115
            } catch (GuzzleException $ex) {
116
                // Can't connect to the server?
117
            }
118
119
            return $this->customModuleVersion();
120
        }, 86400);
121
    }
122
123
    /**
124
     * Where to get support for this module.  Perhaps a github repository?
125
     *
126
     * @return string
127
     */
128
    public function customModuleSupportUrl(): string
129
    {
130
        return '';
131
    }
132
133
    /**
134
     * Additional/updated translations.
135
     *
136
     * @param string $language
137
     *
138
     * @return array<string,string>
139
     */
140
    public function customTranslations(string $language): array
141
    {
142
        $php_file     = $this->resourcesFolder() . 'lang/' . $language . '/messages.php';
143
        $po_file      = $this->resourcesFolder() . 'lang/' . $language . '/messages.po';
144
        $mo_file      = $this->resourcesFolder() . 'lang/' . $language . '/messages.mo';
145
        if (file_exists($php_file)) {
146
            $translation  = new Translation($php_file);
147
            $translations = $translation->asArray();
148
            return $translations;
149
        }
150
        if (file_exists($mo_file)) {
151
            $translation  = new Translation($mo_file);
152
            $translations = $translation->asArray();
153
            if (is_writeable($this->resourcesFolder() . 'lang/' . $language)) {
154
                file_put_contents($php_file, "<?php\n\nreturn " . var_export($translations, true) . ";\n");
155
            }
156
            return $translations;
157
        }
158
        if (!file_exists($po_file)) {
159
            return [];
160
        }
161
        $translation  = new Translation($po_file);
162
        $translations = $translation->asArray();
163
        if (is_writeable($this->resourcesFolder() . 'lang/' . $language)) {
164
            file_put_contents($php_file, "<?php\n\nreturn " . var_export($translations, true) . ";\n");
165
        }
166
        return $translations;
167
    }
168
169
    /**
170
     * Create a URL for an asset.
171
     *
172
     * @param string $asset e.g. "css/theme.css" or "img/banner.png"
173
     *
174
     * @return string
175
     */
176
    public function assetUrl(string $asset): string
177
    {
178
        $file = $this->resourcesFolder() . $asset;
179
180
        // Add the file's modification time to the URL, so we can set long expiry cache headers.
181
        $hash = filemtime($file);
182
183
        return route('module', [
184
            'module' => $this->name(),
185
            'action' => 'Asset',
186
            'asset'  => $asset,
187
            'hash'   => $hash,
188
        ]);
189
    }
190
191
    /**
192
     * Serve a CSS/JS file.
193
     *
194
     * @param ServerRequestInterface $request
195
     *
196
     * @return ResponseInterface
197
     */
198
    public function getAssetAction(ServerRequestInterface $request): ResponseInterface
199
    {
200
        // The file being requested.  e.g. "css/theme.css"
201
        $asset = Validator::queryParams($request)->string('asset');
202
203
        // Do not allow requests that try to access parent folders.
204
        if (str_contains($asset, '..')) {
205
            throw new HttpAccessDeniedException($asset);
206
        }
207
208
        // Find the file for this asset.
209
        // Note that we could also generate CSS files using views/templates.
210
        // e.g. $file = view(....)
211
        $file = $this->resourcesFolder() . $asset;
212
213
        if (!file_exists($file)) {
214
            throw new HttpNotFoundException(e($file));
215
        }
216
217
        $content   = file_get_contents($file);
218
        $extension = strtoupper(pathinfo($asset, PATHINFO_EXTENSION));
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

218
        $extension = strtoupper(/** @scrutinizer ignore-type */ pathinfo($asset, PATHINFO_EXTENSION));
Loading history...
219
        $mime_type = Mime::TYPES[$extension] ?? Mime::DEFAULT_TYPE;
220
221
        return response($content, StatusCodeInterface::STATUS_OK, [
222
            'cache-control'  => 'public,max-age=31536000',
223
            'content-type'   => $mime_type,
224
        ]);
225
    }
226
}
227