149a243cbSGreg Roach<?php 23976b470SGreg 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 */ 17fcfa147eSGreg Roach 1849a243cbSGreg Roachdeclare(strict_types=1); 1949a243cbSGreg Roach 2049a243cbSGreg Roachnamespace Fisharebest\Webtrees\Module; 2149a243cbSGreg Roach 226ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 239ef73392SGreg Roachuse Fisharebest\Webtrees\Carbon; 249ef73392SGreg Roachuse Illuminate\Support\Str; 256ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 266ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 279ef73392SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 289ef73392SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 293976b470SGreg Roach 301abbd7e1SGreg Roachuse function strlen; 319ef73392SGreg Roach 3249a243cbSGreg Roach/** 3349a243cbSGreg Roach * Trait ModuleCustomTrait - default implementation of ModuleCustomInterface 3449a243cbSGreg Roach */ 3549a243cbSGreg Roachtrait ModuleCustomTrait 3649a243cbSGreg Roach{ 3749a243cbSGreg Roach /** 3802086832SGreg Roach * Where does this module store its resources 3902086832SGreg Roach * 4002086832SGreg Roach * @return string 4102086832SGreg Roach */ 4202086832SGreg Roach abstract public function resourcesFolder(): string; 4302086832SGreg Roach 4402086832SGreg Roach /** 4502086832SGreg Roach * A unique internal name for this module (based on the installation folder). 4602086832SGreg Roach * 4702086832SGreg Roach * @return string 4802086832SGreg Roach */ 4902086832SGreg Roach abstract public function name(): string; 5002086832SGreg Roach 5102086832SGreg Roach /** 5249a243cbSGreg Roach * The person or organisation who created this module. 5349a243cbSGreg Roach * 5449a243cbSGreg Roach * @return string 5549a243cbSGreg Roach */ 56cbf4b7faSGreg Roach public function customModuleAuthorName(): string 57cbf4b7faSGreg Roach { 58304fefbeSGreg Roach return ''; 5949a243cbSGreg Roach } 6049a243cbSGreg Roach 6149a243cbSGreg Roach /** 6249a243cbSGreg Roach * The version of this module. 6349a243cbSGreg Roach * 64304fefbeSGreg Roach * @return string e.g. '1.2.3' 6549a243cbSGreg Roach */ 66cbf4b7faSGreg Roach public function customModuleVersion(): string 67cbf4b7faSGreg Roach { 68304fefbeSGreg Roach return ''; 6949a243cbSGreg Roach } 7049a243cbSGreg Roach 7149a243cbSGreg Roach /** 7249a243cbSGreg Roach * A URL that will provide the latest version of this module. 7349a243cbSGreg Roach * 7449a243cbSGreg Roach * @return string 7549a243cbSGreg Roach */ 76cbf4b7faSGreg Roach public function customModuleLatestVersionUrl(): string 77cbf4b7faSGreg Roach { 78304fefbeSGreg Roach return ''; 7949a243cbSGreg Roach } 8049a243cbSGreg Roach 8149a243cbSGreg Roach /** 8249a243cbSGreg Roach * Where to get support for this module. Perhaps a github respository? 8349a243cbSGreg Roach * 8449a243cbSGreg Roach * @return string 8549a243cbSGreg Roach */ 86cbf4b7faSGreg Roach public function customModuleSupportUrl(): string 87cbf4b7faSGreg Roach { 88304fefbeSGreg Roach return ''; 8949a243cbSGreg Roach } 90d37db671SGreg Roach 91d37db671SGreg Roach /** 92d37db671SGreg Roach * Additional/updated translations. 93d37db671SGreg Roach * 94d37db671SGreg Roach * @param string $language 95d37db671SGreg Roach * 96d37db671SGreg Roach * @return string[] 97d37db671SGreg Roach */ 98d37db671SGreg Roach public function customTranslations(string $language): array 99d37db671SGreg Roach { 100d37db671SGreg Roach return []; 101d37db671SGreg Roach } 1029ef73392SGreg Roach 1039ef73392SGreg Roach /** 1049ef73392SGreg Roach * Create a URL for an asset. 1059ef73392SGreg Roach * 1069ef73392SGreg Roach * @param string $asset e.g. "css/theme.css" or "img/banner.png" 1079ef73392SGreg Roach * 1089ef73392SGreg Roach * @return string 1099ef73392SGreg Roach */ 1109ef73392SGreg Roach public function assetUrl(string $asset): string 1119ef73392SGreg Roach { 11202086832SGreg Roach $file = $this->resourcesFolder() . $asset; 1139ef73392SGreg Roach 1149ef73392SGreg Roach // Add the file's modification time to the URL, so we can set long expiry cache headers. 1159ef73392SGreg Roach $hash = filemtime($file); 1169ef73392SGreg Roach 1179ef73392SGreg Roach return route('module', [ 1189ef73392SGreg Roach 'module' => $this->name(), 119*a81abcafSGreg Roach 'action' => 'Asset', 1209ef73392SGreg Roach 'asset' => $asset, 1219ef73392SGreg Roach 'hash' => $hash, 1229ef73392SGreg Roach ]); 1239ef73392SGreg Roach } 1249ef73392SGreg Roach 1259ef73392SGreg Roach /** 1269ef73392SGreg Roach * Serve a CSS/JS file. 1279ef73392SGreg Roach * 1286ccdf4f0SGreg Roach * @param ServerRequestInterface $request 1299ef73392SGreg Roach * 1306ccdf4f0SGreg Roach * @return ResponseInterface 1319ef73392SGreg Roach */ 1326ccdf4f0SGreg Roach public function getAssetAction(ServerRequestInterface $request): ResponseInterface 1339ef73392SGreg Roach { 1349ef73392SGreg Roach // The file being requested. e.g. "css/theme.css" 135e106ff43SGreg Roach $asset = $request->getQueryParams()['asset']; 1369ef73392SGreg Roach 1379ef73392SGreg Roach // Do not allow requests that try to access parent folders. 1389ef73392SGreg Roach if (Str::contains($asset, '..')) { 1399ef73392SGreg Roach throw new AccessDeniedHttpException($asset); 1409ef73392SGreg Roach } 1419ef73392SGreg Roach 1429ef73392SGreg Roach // Find the file for this asset. 1439ef73392SGreg Roach // Note that we could also generate CSS files using views/templates. 1449ef73392SGreg Roach // e.g. $file = view(.... 14502086832SGreg Roach $file = $this->resourcesFolder() . $asset; 1469ef73392SGreg Roach 1479ef73392SGreg Roach if (!file_exists($file)) { 1489ef73392SGreg Roach throw new NotFoundHttpException($file); 1499ef73392SGreg Roach } 1509ef73392SGreg Roach 1519ef73392SGreg Roach $content = file_get_contents($file); 1529ef73392SGreg Roach $extension = pathinfo($asset, PATHINFO_EXTENSION); 1539ef73392SGreg Roach 1549ef73392SGreg Roach $mime_types = [ 1559ef73392SGreg Roach 'css' => 'text/css', 1569ef73392SGreg Roach 'gif' => 'image/gif', 1579ef73392SGreg Roach 'js' => 'application/javascript', 15885a166d8SGreg Roach 'jpg' => 'image/jpeg', 15985a166d8SGreg Roach 'jpeg' => 'image/jpeg', 1609ef73392SGreg Roach 'json' => 'application/json', 1619ef73392SGreg Roach 'png' => 'image/png', 1629ef73392SGreg Roach 'txt' => 'text/plain', 1639ef73392SGreg Roach ]; 1649ef73392SGreg Roach 1659ef73392SGreg Roach $mime_type = $mime_types[$extension] ?? 'application/octet-stream'; 1669ef73392SGreg Roach 1679ef73392SGreg Roach $headers = [ 1689ef73392SGreg Roach 'Content-Type' => $mime_type, 1691abbd7e1SGreg Roach 'Cache-Control' => 'max-age=31536000, public', 1701abbd7e1SGreg Roach 'Content-Length' => strlen($content), 1711abbd7e1SGreg Roach 'Expires' => Carbon::now()->addYears(10)->toRfc7231String(), 1729ef73392SGreg Roach ]; 1739ef73392SGreg Roach 1746ccdf4f0SGreg Roach return response($content, StatusCodeInterface::STATUS_OK, $headers); 1759ef73392SGreg Roach } 17649a243cbSGreg Roach} 177