xref: /webtrees/app/Module/ModuleCustomTrait.php (revision 02086832f6c85aab8102c1a336d227314e040441)
149a243cbSGreg Roach<?php
249a243cbSGreg Roach/**
349a243cbSGreg Roach * webtrees: online genealogy
449a243cbSGreg Roach * Copyright (C) 2019 webtrees development team
549a243cbSGreg Roach * This program is free software: you can redistribute it and/or modify
649a243cbSGreg Roach * it under the terms of the GNU General Public License as published by
749a243cbSGreg Roach * the Free Software Foundation, either version 3 of the License, or
849a243cbSGreg Roach * (at your option) any later version.
949a243cbSGreg Roach * This program is distributed in the hope that it will be useful,
1049a243cbSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1149a243cbSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1249a243cbSGreg Roach * GNU General Public License for more details.
1349a243cbSGreg Roach * You should have received a copy of the GNU General Public License
1449a243cbSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1549a243cbSGreg Roach */
1649a243cbSGreg Roachdeclare(strict_types=1);
1749a243cbSGreg Roach
1849a243cbSGreg Roachnamespace Fisharebest\Webtrees\Module;
1949a243cbSGreg Roach
209ef73392SGreg Roachuse Fisharebest\Webtrees\Carbon;
219ef73392SGreg Roachuse Illuminate\Support\Str;
229ef73392SGreg Roachuse Symfony\Component\HttpFoundation\Request;
239ef73392SGreg Roachuse Symfony\Component\HttpFoundation\Response;
249ef73392SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
259ef73392SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
269ef73392SGreg Roach
2749a243cbSGreg Roach/**
2849a243cbSGreg Roach * Trait ModuleCustomTrait - default implementation of ModuleCustomInterface
2949a243cbSGreg Roach */
3049a243cbSGreg Roachtrait ModuleCustomTrait
3149a243cbSGreg Roach{
3249a243cbSGreg Roach    /**
33*02086832SGreg Roach     * Where does this module store its resources
34*02086832SGreg Roach     *
35*02086832SGreg Roach     * @return string
36*02086832SGreg Roach     */
37*02086832SGreg Roach    abstract public function resourcesFolder(): string;
38*02086832SGreg Roach
39*02086832SGreg Roach    /**
40*02086832SGreg Roach     * A unique internal name for this module (based on the installation folder).
41*02086832SGreg Roach     *
42*02086832SGreg Roach     * @return string
43*02086832SGreg Roach     */
44*02086832SGreg Roach    abstract public function name(): string;
45*02086832SGreg Roach
46*02086832SGreg Roach    /**
4749a243cbSGreg Roach     * The person or organisation who created this module.
4849a243cbSGreg Roach     *
4949a243cbSGreg Roach     * @return string
5049a243cbSGreg Roach     */
51cbf4b7faSGreg Roach    public function customModuleAuthorName(): string
52cbf4b7faSGreg Roach    {
53304fefbeSGreg Roach        return '';
5449a243cbSGreg Roach    }
5549a243cbSGreg Roach
5649a243cbSGreg Roach    /**
5749a243cbSGreg Roach     * The version of this module.
5849a243cbSGreg Roach     *
59304fefbeSGreg Roach     * @return string  e.g. '1.2.3'
6049a243cbSGreg Roach     */
61cbf4b7faSGreg Roach    public function customModuleVersion(): string
62cbf4b7faSGreg Roach    {
63304fefbeSGreg Roach        return '';
6449a243cbSGreg Roach    }
6549a243cbSGreg Roach
6649a243cbSGreg Roach    /**
6749a243cbSGreg Roach     * A URL that will provide the latest version of this module.
6849a243cbSGreg Roach     *
6949a243cbSGreg Roach     * @return string
7049a243cbSGreg Roach     */
71cbf4b7faSGreg Roach    public function customModuleLatestVersionUrl(): string
72cbf4b7faSGreg Roach    {
73304fefbeSGreg Roach        return '';
7449a243cbSGreg Roach    }
7549a243cbSGreg Roach
7649a243cbSGreg Roach    /**
7749a243cbSGreg Roach     * Where to get support for this module.  Perhaps a github respository?
7849a243cbSGreg Roach     *
7949a243cbSGreg Roach     * @return string
8049a243cbSGreg Roach     */
81cbf4b7faSGreg Roach    public function customModuleSupportUrl(): string
82cbf4b7faSGreg Roach    {
83304fefbeSGreg Roach        return '';
8449a243cbSGreg Roach    }
85d37db671SGreg Roach
86d37db671SGreg Roach    /**
87d37db671SGreg Roach     * Additional/updated translations.
88d37db671SGreg Roach     *
89d37db671SGreg Roach     * @param string $language
90d37db671SGreg Roach     *
91d37db671SGreg Roach     * @return string[]
92d37db671SGreg Roach     */
93d37db671SGreg Roach    public function customTranslations(string $language): array
94d37db671SGreg Roach    {
95d37db671SGreg Roach        return [];
96d37db671SGreg Roach    }
979ef73392SGreg Roach
989ef73392SGreg Roach    /**
999ef73392SGreg Roach     * Create a URL for an asset.
1009ef73392SGreg Roach     *
1019ef73392SGreg Roach     * @param string $asset e.g. "css/theme.css" or "img/banner.png"
1029ef73392SGreg Roach     *
1039ef73392SGreg Roach     * @return string
1049ef73392SGreg Roach     */
1059ef73392SGreg Roach    public function assetUrl(string $asset): string
1069ef73392SGreg Roach    {
107*02086832SGreg Roach        $file = $this->resourcesFolder() . $asset;
1089ef73392SGreg Roach
1099ef73392SGreg Roach        // Add the file's modification time to the URL, so we can set long expiry cache headers.
1109ef73392SGreg Roach        $hash = filemtime($file);
1119ef73392SGreg Roach
1129ef73392SGreg Roach        return route('module', [
1139ef73392SGreg Roach            'module' => $this->name(),
1149ef73392SGreg Roach            'action' => 'asset',
1159ef73392SGreg Roach            'asset'  => $asset,
1169ef73392SGreg Roach            'hash'   => $hash,
1179ef73392SGreg Roach        ]);
1189ef73392SGreg Roach    }
1199ef73392SGreg Roach
1209ef73392SGreg Roach    /**
1219ef73392SGreg Roach     * Serve a CSS/JS file.
1229ef73392SGreg Roach     *
1239ef73392SGreg Roach     * @param Request $request
1249ef73392SGreg Roach     *
1259ef73392SGreg Roach     * @return Response
1269ef73392SGreg Roach     */
1279ef73392SGreg Roach    public function getAssetAction(Request $request): Response
1289ef73392SGreg Roach    {
1299ef73392SGreg Roach        // The file being requested.  e.g. "css/theme.css"
1309ef73392SGreg Roach        $asset = $request->get('asset');
1319ef73392SGreg Roach
1329ef73392SGreg Roach        // Do not allow requests that try to access parent folders.
1339ef73392SGreg Roach        if (Str::contains($asset, '..')) {
1349ef73392SGreg Roach            throw new AccessDeniedHttpException($asset);
1359ef73392SGreg Roach        }
1369ef73392SGreg Roach
1379ef73392SGreg Roach        // Find the file for this asset.
1389ef73392SGreg Roach        // Note that we could also generate CSS files using views/templates.
1399ef73392SGreg Roach        // e.g. $file = view(....
140*02086832SGreg Roach        $file = $this->resourcesFolder() . $asset;
1419ef73392SGreg Roach
1429ef73392SGreg Roach        if (!file_exists($file)) {
1439ef73392SGreg Roach            throw new NotFoundHttpException($file);
1449ef73392SGreg Roach        }
1459ef73392SGreg Roach
1469ef73392SGreg Roach        $content     = file_get_contents($file);
1479ef73392SGreg Roach        $expiry_date = Carbon::now()->addYears(10);
1489ef73392SGreg Roach
1499ef73392SGreg Roach        $extension = pathinfo($asset, PATHINFO_EXTENSION);
1509ef73392SGreg Roach
1519ef73392SGreg Roach        $mime_types = [
1529ef73392SGreg Roach            'css'  => 'text/css',
1539ef73392SGreg Roach            'gif'  => 'image/gif',
1549ef73392SGreg Roach            'js'   => 'application/javascript',
1559ef73392SGreg Roach            'jpg'  => 'image/jpg',
1569ef73392SGreg Roach            'jpeg' => 'image/jpg',
1579ef73392SGreg Roach            'json' => 'application/json',
1589ef73392SGreg Roach            'png'  => 'image/png',
1599ef73392SGreg Roach            'txt'  => 'text/plain',
1609ef73392SGreg Roach        ];
1619ef73392SGreg Roach
1629ef73392SGreg Roach        $mime_type = $mime_types[$extension] ?? 'application/octet-stream';
1639ef73392SGreg Roach
1649ef73392SGreg Roach        $headers = [
1659ef73392SGreg Roach            'Content-Type' => $mime_type,
1669ef73392SGreg Roach        ];
1679ef73392SGreg Roach
1689ef73392SGreg Roach        $response = new Response($content, Response::HTTP_OK, $headers);
1699ef73392SGreg Roach
1709ef73392SGreg Roach        return $response
1719ef73392SGreg Roach            ->setExpires($expiry_date);
1729ef73392SGreg Roach    }
17349a243cbSGreg Roach}
174