xref: /webtrees/app/Module/ModuleCustomTrait.php (revision 3cfcc809af53e831fa6cafac7b274a2cb407db6e)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Module;
20
21use Fig\Http\Message\StatusCodeInterface;
22use Fisharebest\Webtrees\Carbon;
23use Illuminate\Support\Str;
24use Psr\Http\Message\ResponseInterface;
25use Psr\Http\Message\ServerRequestInterface;
26use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
27use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
28
29use function strlen;
30
31/**
32 * Trait ModuleCustomTrait - default implementation of ModuleCustomInterface
33 */
34trait ModuleCustomTrait
35{
36    /**
37     * Where does this module store its resources
38     *
39     * @return string
40     */
41    abstract public function resourcesFolder(): string;
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     * The person or organisation who created this module.
52     *
53     * @return string
54     */
55    public function customModuleAuthorName(): string
56    {
57        return '';
58    }
59
60    /**
61     * The version of this module.
62     *
63     * @return string  e.g. '1.2.3'
64     */
65    public function customModuleVersion(): string
66    {
67        return '';
68    }
69
70    /**
71     * A URL that will provide the latest version of this module.
72     *
73     * @return string
74     */
75    public function customModuleLatestVersionUrl(): string
76    {
77        return '';
78    }
79
80    /**
81     * Where to get support for this module.  Perhaps a github respository?
82     *
83     * @return string
84     */
85    public function customModuleSupportUrl(): string
86    {
87        return '';
88    }
89
90    /**
91     * Additional/updated translations.
92     *
93     * @param string $language
94     *
95     * @return string[]
96     */
97    public function customTranslations(string $language): array
98    {
99        return [];
100    }
101
102    /**
103     * Create a URL for an asset.
104     *
105     * @param string $asset e.g. "css/theme.css" or "img/banner.png"
106     *
107     * @return string
108     */
109    public function assetUrl(string $asset): string
110    {
111        $file = $this->resourcesFolder() . $asset;
112
113        // Add the file's modification time to the URL, so we can set long expiry cache headers.
114        $hash = filemtime($file);
115
116        return route('module', [
117            'module' => $this->name(),
118            'action' => 'asset',
119            'asset'  => $asset,
120            'hash'   => $hash,
121        ]);
122    }
123
124    /**
125     * Serve a CSS/JS file.
126     *
127     * @param ServerRequestInterface $request
128     *
129     * @return ResponseInterface
130     */
131    public function getAssetAction(ServerRequestInterface $request): ResponseInterface
132    {
133        // The file being requested.  e.g. "css/theme.css"
134        $asset = $request->getQueryParams()['asset'];
135
136        // Do not allow requests that try to access parent folders.
137        if (Str::contains($asset, '..')) {
138            throw new AccessDeniedHttpException($asset);
139        }
140
141        // Find the file for this asset.
142        // Note that we could also generate CSS files using views/templates.
143        // e.g. $file = view(....
144        $file = $this->resourcesFolder() . $asset;
145
146        if (!file_exists($file)) {
147            throw new NotFoundHttpException($file);
148        }
149
150        $content   = file_get_contents($file);
151        $extension = pathinfo($asset, PATHINFO_EXTENSION);
152
153        $mime_types = [
154            'css'  => 'text/css',
155            'gif'  => 'image/gif',
156            'js'   => 'application/javascript',
157            'jpg'  => 'image/jpeg',
158            'jpeg' => 'image/jpeg',
159            'json' => 'application/json',
160            'png'  => 'image/png',
161            'txt'  => 'text/plain',
162        ];
163
164        $mime_type = $mime_types[$extension] ?? 'application/octet-stream';
165
166        $headers = [
167            'Content-Type'   => $mime_type,
168            'Cache-Control'  => 'max-age=31536000, public',
169            'Content-Length' => strlen($content),
170            'Expires'        => Carbon::now()->addYears(10)->toRfc7231String(),
171        ];
172
173        return response($content, StatusCodeInterface::STATUS_OK, $headers);
174    }
175}
176