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 20*6ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 219ef73392SGreg Roachuse Fisharebest\Webtrees\Carbon; 229ef73392SGreg Roachuse Illuminate\Support\Str; 23*6ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 24*6ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 259ef73392SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 269ef73392SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 279ef73392SGreg Roach 2849a243cbSGreg Roach/** 2949a243cbSGreg Roach * Trait ModuleCustomTrait - default implementation of ModuleCustomInterface 3049a243cbSGreg Roach */ 3149a243cbSGreg Roachtrait ModuleCustomTrait 3249a243cbSGreg Roach{ 3349a243cbSGreg Roach /** 3402086832SGreg Roach * Where does this module store its resources 3502086832SGreg Roach * 3602086832SGreg Roach * @return string 3702086832SGreg Roach */ 3802086832SGreg Roach abstract public function resourcesFolder(): string; 3902086832SGreg Roach 4002086832SGreg Roach /** 4102086832SGreg Roach * A unique internal name for this module (based on the installation folder). 4202086832SGreg Roach * 4302086832SGreg Roach * @return string 4402086832SGreg Roach */ 4502086832SGreg Roach abstract public function name(): string; 4602086832SGreg Roach 4702086832SGreg Roach /** 4849a243cbSGreg Roach * The person or organisation who created this module. 4949a243cbSGreg Roach * 5049a243cbSGreg Roach * @return string 5149a243cbSGreg Roach */ 52cbf4b7faSGreg Roach public function customModuleAuthorName(): string 53cbf4b7faSGreg Roach { 54304fefbeSGreg Roach return ''; 5549a243cbSGreg Roach } 5649a243cbSGreg Roach 5749a243cbSGreg Roach /** 5849a243cbSGreg Roach * The version of this module. 5949a243cbSGreg Roach * 60304fefbeSGreg Roach * @return string e.g. '1.2.3' 6149a243cbSGreg Roach */ 62cbf4b7faSGreg Roach public function customModuleVersion(): string 63cbf4b7faSGreg Roach { 64304fefbeSGreg Roach return ''; 6549a243cbSGreg Roach } 6649a243cbSGreg Roach 6749a243cbSGreg Roach /** 6849a243cbSGreg Roach * A URL that will provide the latest version of this module. 6949a243cbSGreg Roach * 7049a243cbSGreg Roach * @return string 7149a243cbSGreg Roach */ 72cbf4b7faSGreg Roach public function customModuleLatestVersionUrl(): string 73cbf4b7faSGreg Roach { 74304fefbeSGreg Roach return ''; 7549a243cbSGreg Roach } 7649a243cbSGreg Roach 7749a243cbSGreg Roach /** 7849a243cbSGreg Roach * Where to get support for this module. Perhaps a github respository? 7949a243cbSGreg Roach * 8049a243cbSGreg Roach * @return string 8149a243cbSGreg Roach */ 82cbf4b7faSGreg Roach public function customModuleSupportUrl(): string 83cbf4b7faSGreg Roach { 84304fefbeSGreg Roach return ''; 8549a243cbSGreg Roach } 86d37db671SGreg Roach 87d37db671SGreg Roach /** 88d37db671SGreg Roach * Additional/updated translations. 89d37db671SGreg Roach * 90d37db671SGreg Roach * @param string $language 91d37db671SGreg Roach * 92d37db671SGreg Roach * @return string[] 93d37db671SGreg Roach */ 94d37db671SGreg Roach public function customTranslations(string $language): array 95d37db671SGreg Roach { 96d37db671SGreg Roach return []; 97d37db671SGreg Roach } 989ef73392SGreg Roach 999ef73392SGreg Roach /** 1009ef73392SGreg Roach * Create a URL for an asset. 1019ef73392SGreg Roach * 1029ef73392SGreg Roach * @param string $asset e.g. "css/theme.css" or "img/banner.png" 1039ef73392SGreg Roach * 1049ef73392SGreg Roach * @return string 1059ef73392SGreg Roach */ 1069ef73392SGreg Roach public function assetUrl(string $asset): string 1079ef73392SGreg Roach { 10802086832SGreg Roach $file = $this->resourcesFolder() . $asset; 1099ef73392SGreg Roach 1109ef73392SGreg Roach // Add the file's modification time to the URL, so we can set long expiry cache headers. 1119ef73392SGreg Roach $hash = filemtime($file); 1129ef73392SGreg Roach 1139ef73392SGreg Roach return route('module', [ 1149ef73392SGreg Roach 'module' => $this->name(), 1159ef73392SGreg Roach 'action' => 'asset', 1169ef73392SGreg Roach 'asset' => $asset, 1179ef73392SGreg Roach 'hash' => $hash, 1189ef73392SGreg Roach ]); 1199ef73392SGreg Roach } 1209ef73392SGreg Roach 1219ef73392SGreg Roach /** 1229ef73392SGreg Roach * Serve a CSS/JS file. 1239ef73392SGreg Roach * 124*6ccdf4f0SGreg Roach * @param ServerRequestInterface $request 1259ef73392SGreg Roach * 126*6ccdf4f0SGreg Roach * @return ResponseInterface 1279ef73392SGreg Roach */ 128*6ccdf4f0SGreg Roach public function getAssetAction(ServerRequestInterface $request): ResponseInterface 1299ef73392SGreg Roach { 1309ef73392SGreg Roach // The file being requested. e.g. "css/theme.css" 1319ef73392SGreg Roach $asset = $request->get('asset'); 1329ef73392SGreg Roach 1339ef73392SGreg Roach // Do not allow requests that try to access parent folders. 1349ef73392SGreg Roach if (Str::contains($asset, '..')) { 1359ef73392SGreg Roach throw new AccessDeniedHttpException($asset); 1369ef73392SGreg Roach } 1379ef73392SGreg Roach 1389ef73392SGreg Roach // Find the file for this asset. 1399ef73392SGreg Roach // Note that we could also generate CSS files using views/templates. 1409ef73392SGreg Roach // e.g. $file = view(.... 14102086832SGreg Roach $file = $this->resourcesFolder() . $asset; 1429ef73392SGreg Roach 1439ef73392SGreg Roach if (!file_exists($file)) { 1449ef73392SGreg Roach throw new NotFoundHttpException($file); 1459ef73392SGreg Roach } 1469ef73392SGreg Roach 1479ef73392SGreg Roach $content = file_get_contents($file); 1489ef73392SGreg Roach $expiry_date = Carbon::now()->addYears(10); 1499ef73392SGreg Roach 1509ef73392SGreg Roach $extension = pathinfo($asset, PATHINFO_EXTENSION); 1519ef73392SGreg Roach 1529ef73392SGreg Roach $mime_types = [ 1539ef73392SGreg Roach 'css' => 'text/css', 1549ef73392SGreg Roach 'gif' => 'image/gif', 1559ef73392SGreg Roach 'js' => 'application/javascript', 1569ef73392SGreg Roach 'jpg' => 'image/jpg', 1579ef73392SGreg Roach 'jpeg' => 'image/jpg', 1589ef73392SGreg Roach 'json' => 'application/json', 1599ef73392SGreg Roach 'png' => 'image/png', 1609ef73392SGreg Roach 'txt' => 'text/plain', 1619ef73392SGreg Roach ]; 1629ef73392SGreg Roach 1639ef73392SGreg Roach $mime_type = $mime_types[$extension] ?? 'application/octet-stream'; 1649ef73392SGreg Roach 1659ef73392SGreg Roach $headers = [ 1669ef73392SGreg Roach 'Content-Type' => $mime_type, 167*6ccdf4f0SGreg Roach 'Expires' => $expiry_date, 1689ef73392SGreg Roach ]; 1699ef73392SGreg Roach 170*6ccdf4f0SGreg Roach return response($content, StatusCodeInterface::STATUS_OK, $headers); 1719ef73392SGreg Roach } 17249a243cbSGreg Roach} 173