149a243cbSGreg Roach<?php 2*3976b470SGreg Roach 349a243cbSGreg Roach/** 449a243cbSGreg Roach * webtrees: online genealogy 549a243cbSGreg Roach * Copyright (C) 2019 webtrees development team 649a243cbSGreg Roach * This program is free software: you can redistribute it and/or modify 749a243cbSGreg Roach * it under the terms of the GNU General Public License as published by 849a243cbSGreg Roach * the Free Software Foundation, either version 3 of the License, or 949a243cbSGreg Roach * (at your option) any later version. 1049a243cbSGreg Roach * This program is distributed in the hope that it will be useful, 1149a243cbSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1249a243cbSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1349a243cbSGreg Roach * GNU General Public License for more details. 1449a243cbSGreg Roach * You should have received a copy of the GNU General Public License 1549a243cbSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 1649a243cbSGreg Roach */ 1749a243cbSGreg Roachdeclare(strict_types=1); 1849a243cbSGreg Roach 1949a243cbSGreg Roachnamespace Fisharebest\Webtrees\Module; 2049a243cbSGreg Roach 216ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 229ef73392SGreg Roachuse Fisharebest\Webtrees\Carbon; 239ef73392SGreg Roachuse Illuminate\Support\Str; 246ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 256ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 269ef73392SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 279ef73392SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 28*3976b470SGreg Roach 291abbd7e1SGreg Roachuse function strlen; 309ef73392SGreg Roach 3149a243cbSGreg Roach/** 3249a243cbSGreg Roach * Trait ModuleCustomTrait - default implementation of ModuleCustomInterface 3349a243cbSGreg Roach */ 3449a243cbSGreg Roachtrait ModuleCustomTrait 3549a243cbSGreg Roach{ 3649a243cbSGreg Roach /** 3702086832SGreg Roach * Where does this module store its resources 3802086832SGreg Roach * 3902086832SGreg Roach * @return string 4002086832SGreg Roach */ 4102086832SGreg Roach abstract public function resourcesFolder(): string; 4202086832SGreg Roach 4302086832SGreg Roach /** 4402086832SGreg Roach * A unique internal name for this module (based on the installation folder). 4502086832SGreg Roach * 4602086832SGreg Roach * @return string 4702086832SGreg Roach */ 4802086832SGreg Roach abstract public function name(): string; 4902086832SGreg Roach 5002086832SGreg Roach /** 5149a243cbSGreg Roach * The person or organisation who created this module. 5249a243cbSGreg Roach * 5349a243cbSGreg Roach * @return string 5449a243cbSGreg Roach */ 55cbf4b7faSGreg Roach public function customModuleAuthorName(): string 56cbf4b7faSGreg Roach { 57304fefbeSGreg Roach return ''; 5849a243cbSGreg Roach } 5949a243cbSGreg Roach 6049a243cbSGreg Roach /** 6149a243cbSGreg Roach * The version of this module. 6249a243cbSGreg Roach * 63304fefbeSGreg Roach * @return string e.g. '1.2.3' 6449a243cbSGreg Roach */ 65cbf4b7faSGreg Roach public function customModuleVersion(): string 66cbf4b7faSGreg Roach { 67304fefbeSGreg Roach return ''; 6849a243cbSGreg Roach } 6949a243cbSGreg Roach 7049a243cbSGreg Roach /** 7149a243cbSGreg Roach * A URL that will provide the latest version of this module. 7249a243cbSGreg Roach * 7349a243cbSGreg Roach * @return string 7449a243cbSGreg Roach */ 75cbf4b7faSGreg Roach public function customModuleLatestVersionUrl(): string 76cbf4b7faSGreg Roach { 77304fefbeSGreg Roach return ''; 7849a243cbSGreg Roach } 7949a243cbSGreg Roach 8049a243cbSGreg Roach /** 8149a243cbSGreg Roach * Where to get support for this module. Perhaps a github respository? 8249a243cbSGreg Roach * 8349a243cbSGreg Roach * @return string 8449a243cbSGreg Roach */ 85cbf4b7faSGreg Roach public function customModuleSupportUrl(): string 86cbf4b7faSGreg Roach { 87304fefbeSGreg Roach return ''; 8849a243cbSGreg Roach } 89d37db671SGreg Roach 90d37db671SGreg Roach /** 91d37db671SGreg Roach * Additional/updated translations. 92d37db671SGreg Roach * 93d37db671SGreg Roach * @param string $language 94d37db671SGreg Roach * 95d37db671SGreg Roach * @return string[] 96d37db671SGreg Roach */ 97d37db671SGreg Roach public function customTranslations(string $language): array 98d37db671SGreg Roach { 99d37db671SGreg Roach return []; 100d37db671SGreg Roach } 1019ef73392SGreg Roach 1029ef73392SGreg Roach /** 1039ef73392SGreg Roach * Create a URL for an asset. 1049ef73392SGreg Roach * 1059ef73392SGreg Roach * @param string $asset e.g. "css/theme.css" or "img/banner.png" 1069ef73392SGreg Roach * 1079ef73392SGreg Roach * @return string 1089ef73392SGreg Roach */ 1099ef73392SGreg Roach public function assetUrl(string $asset): string 1109ef73392SGreg Roach { 11102086832SGreg Roach $file = $this->resourcesFolder() . $asset; 1129ef73392SGreg Roach 1139ef73392SGreg Roach // Add the file's modification time to the URL, so we can set long expiry cache headers. 1149ef73392SGreg Roach $hash = filemtime($file); 1159ef73392SGreg Roach 1169ef73392SGreg Roach return route('module', [ 1179ef73392SGreg Roach 'module' => $this->name(), 1189ef73392SGreg Roach 'action' => 'asset', 1199ef73392SGreg Roach 'asset' => $asset, 1209ef73392SGreg Roach 'hash' => $hash, 1219ef73392SGreg Roach ]); 1229ef73392SGreg Roach } 1239ef73392SGreg Roach 1249ef73392SGreg Roach /** 1259ef73392SGreg Roach * Serve a CSS/JS file. 1269ef73392SGreg Roach * 1276ccdf4f0SGreg Roach * @param ServerRequestInterface $request 1289ef73392SGreg Roach * 1296ccdf4f0SGreg Roach * @return ResponseInterface 1309ef73392SGreg Roach */ 1316ccdf4f0SGreg Roach public function getAssetAction(ServerRequestInterface $request): ResponseInterface 1329ef73392SGreg Roach { 1339ef73392SGreg Roach // The file being requested. e.g. "css/theme.css" 134e106ff43SGreg Roach $asset = $request->getQueryParams()['asset']; 1359ef73392SGreg Roach 1369ef73392SGreg Roach // Do not allow requests that try to access parent folders. 1379ef73392SGreg Roach if (Str::contains($asset, '..')) { 1389ef73392SGreg Roach throw new AccessDeniedHttpException($asset); 1399ef73392SGreg Roach } 1409ef73392SGreg Roach 1419ef73392SGreg Roach // Find the file for this asset. 1429ef73392SGreg Roach // Note that we could also generate CSS files using views/templates. 1439ef73392SGreg Roach // e.g. $file = view(.... 14402086832SGreg Roach $file = $this->resourcesFolder() . $asset; 1459ef73392SGreg Roach 1469ef73392SGreg Roach if (!file_exists($file)) { 1479ef73392SGreg Roach throw new NotFoundHttpException($file); 1489ef73392SGreg Roach } 1499ef73392SGreg Roach 1509ef73392SGreg Roach $content = file_get_contents($file); 1519ef73392SGreg Roach $extension = pathinfo($asset, PATHINFO_EXTENSION); 1529ef73392SGreg Roach 1539ef73392SGreg Roach $mime_types = [ 1549ef73392SGreg Roach 'css' => 'text/css', 1559ef73392SGreg Roach 'gif' => 'image/gif', 1569ef73392SGreg Roach 'js' => 'application/javascript', 15785a166d8SGreg Roach 'jpg' => 'image/jpeg', 15885a166d8SGreg Roach 'jpeg' => 'image/jpeg', 1599ef73392SGreg Roach 'json' => 'application/json', 1609ef73392SGreg Roach 'png' => 'image/png', 1619ef73392SGreg Roach 'txt' => 'text/plain', 1629ef73392SGreg Roach ]; 1639ef73392SGreg Roach 1649ef73392SGreg Roach $mime_type = $mime_types[$extension] ?? 'application/octet-stream'; 1659ef73392SGreg Roach 1669ef73392SGreg Roach $headers = [ 1679ef73392SGreg Roach 'Content-Type' => $mime_type, 1681abbd7e1SGreg Roach 'Cache-Control' => 'max-age=31536000, public', 1691abbd7e1SGreg Roach 'Content-Length' => strlen($content), 1701abbd7e1SGreg Roach 'Expires' => Carbon::now()->addYears(10)->toRfc7231String(), 1719ef73392SGreg Roach ]; 1729ef73392SGreg Roach 1736ccdf4f0SGreg Roach return response($content, StatusCodeInterface::STATUS_OK, $headers); 1749ef73392SGreg Roach } 17549a243cbSGreg Roach} 176