149a243cbSGreg Roach<?php 23976b470SGreg Roach 349a243cbSGreg Roach/** 449a243cbSGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 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 1589f7189bSGreg Roach * along with this program. If not, see <https://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; 2381b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException; 2481b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; 25e7f16b43SGreg Roachuse Fisharebest\Webtrees\Mime; 26b11d5678SGreg Roachuse Fisharebest\Webtrees\Registry; 2764d12f7bSGreg Roachuse GuzzleHttp\Client; 284022f21aSGreg Roachuse GuzzleHttp\Exception\GuzzleException; 296ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 313976b470SGreg Roach 32dec352c1SGreg Roachuse function str_contains; 33249522f1SGreg Roachuse function strtolower; 349ef73392SGreg Roach 3549a243cbSGreg Roach/** 3649a243cbSGreg Roach * Trait ModuleCustomTrait - default implementation of ModuleCustomInterface 3749a243cbSGreg Roach */ 3849a243cbSGreg Roachtrait ModuleCustomTrait 3949a243cbSGreg Roach{ 4049a243cbSGreg Roach /** 4149a243cbSGreg Roach * The person or organisation who created this module. 4249a243cbSGreg Roach * 4349a243cbSGreg Roach * @return string 4449a243cbSGreg Roach */ 45cbf4b7faSGreg Roach public function customModuleAuthorName(): string 46cbf4b7faSGreg Roach { 47304fefbeSGreg Roach return ''; 4849a243cbSGreg Roach } 4949a243cbSGreg Roach 5049a243cbSGreg Roach /** 5149a243cbSGreg Roach * The version of this module. 5249a243cbSGreg Roach * 53304fefbeSGreg Roach * @return string e.g. '1.2.3' 5449a243cbSGreg Roach */ 55cbf4b7faSGreg Roach public function customModuleVersion(): string 56cbf4b7faSGreg Roach { 57304fefbeSGreg Roach return ''; 5849a243cbSGreg Roach } 5949a243cbSGreg Roach 6049a243cbSGreg Roach /** 6149a243cbSGreg Roach * A URL that will provide the latest version of this module. 6249a243cbSGreg Roach * 6349a243cbSGreg Roach * @return string 6449a243cbSGreg Roach */ 65cbf4b7faSGreg Roach public function customModuleLatestVersionUrl(): string 66cbf4b7faSGreg Roach { 67304fefbeSGreg Roach return ''; 6849a243cbSGreg Roach } 6949a243cbSGreg Roach 7049a243cbSGreg Roach /** 7164d12f7bSGreg Roach * Fetch the latest version of this module. 7264d12f7bSGreg Roach * 7364d12f7bSGreg Roach * @return string 7464d12f7bSGreg Roach */ 7564d12f7bSGreg Roach public function customModuleLatestVersion(): string 7664d12f7bSGreg Roach { 7764d12f7bSGreg Roach // No update URL provided. 7864d12f7bSGreg Roach if ($this->customModuleLatestVersionUrl() === '') { 7964d12f7bSGreg Roach return $this->customModuleVersion(); 8064d12f7bSGreg Roach } 8164d12f7bSGreg Roach 82*df5ebf7cSGreg Roach return Registry::cache()->file()->remember($this->name() . '-latest-version', function (): string { 8364d12f7bSGreg Roach try { 8464d12f7bSGreg Roach $client = new Client([ 8564d12f7bSGreg Roach 'timeout' => 3, 8664d12f7bSGreg Roach ]); 8764d12f7bSGreg Roach 8864d12f7bSGreg Roach $response = $client->get($this->customModuleLatestVersionUrl()); 8964d12f7bSGreg Roach 9064d12f7bSGreg Roach if ($response->getStatusCode() === StatusCodeInterface::STATUS_OK) { 9164d12f7bSGreg Roach $version = $response->getBody()->getContents(); 9264d12f7bSGreg Roach 9364d12f7bSGreg Roach // Does the response look like a version? 9464d12f7bSGreg Roach if (preg_match('/^\d+\.\d+\.\d+/', $version)) { 9564d12f7bSGreg Roach return $version; 9664d12f7bSGreg Roach } 9764d12f7bSGreg Roach } 984022f21aSGreg Roach } catch (GuzzleException $ex) { 9964d12f7bSGreg Roach // Can't connect to the server? 10064d12f7bSGreg Roach } 10164d12f7bSGreg Roach 10264d12f7bSGreg Roach return $this->customModuleVersion(); 10364d12f7bSGreg Roach }, 86400); 10464d12f7bSGreg Roach } 10564d12f7bSGreg Roach 10664d12f7bSGreg Roach /** 10764d12f7bSGreg Roach * Where to get support for this module. Perhaps a github repository? 10849a243cbSGreg Roach * 10949a243cbSGreg Roach * @return string 11049a243cbSGreg Roach */ 111cbf4b7faSGreg Roach public function customModuleSupportUrl(): string 112cbf4b7faSGreg Roach { 113304fefbeSGreg Roach return ''; 11449a243cbSGreg Roach } 115d37db671SGreg Roach 116d37db671SGreg Roach /** 117d37db671SGreg Roach * Additional/updated translations. 118d37db671SGreg Roach * 119d37db671SGreg Roach * @param string $language 120d37db671SGreg Roach * 121f4c767fdSGreg Roach * @return array<string,string> 122d37db671SGreg Roach */ 123d37db671SGreg Roach public function customTranslations(string $language): array 124d37db671SGreg Roach { 125d37db671SGreg Roach return []; 126d37db671SGreg Roach } 1279ef73392SGreg Roach 1289ef73392SGreg Roach /** 1299ef73392SGreg Roach * Create a URL for an asset. 1309ef73392SGreg Roach * 1319ef73392SGreg Roach * @param string $asset e.g. "css/theme.css" or "img/banner.png" 1329ef73392SGreg Roach * 1339ef73392SGreg Roach * @return string 1349ef73392SGreg Roach */ 1359ef73392SGreg Roach public function assetUrl(string $asset): string 1369ef73392SGreg Roach { 13702086832SGreg Roach $file = $this->resourcesFolder() . $asset; 1389ef73392SGreg Roach 1399ef73392SGreg Roach // Add the file's modification time to the URL, so we can set long expiry cache headers. 1409ef73392SGreg Roach $hash = filemtime($file); 1419ef73392SGreg Roach 1429ef73392SGreg Roach return route('module', [ 1439ef73392SGreg Roach 'module' => $this->name(), 144a81abcafSGreg Roach 'action' => 'Asset', 1459ef73392SGreg Roach 'asset' => $asset, 1469ef73392SGreg Roach 'hash' => $hash, 1479ef73392SGreg Roach ]); 1489ef73392SGreg Roach } 1499ef73392SGreg Roach 1509ef73392SGreg Roach /** 1519ef73392SGreg Roach * Serve a CSS/JS file. 1529ef73392SGreg Roach * 1536ccdf4f0SGreg Roach * @param ServerRequestInterface $request 1549ef73392SGreg Roach * 1556ccdf4f0SGreg Roach * @return ResponseInterface 1569ef73392SGreg Roach */ 1576ccdf4f0SGreg Roach public function getAssetAction(ServerRequestInterface $request): ResponseInterface 1589ef73392SGreg Roach { 1599ef73392SGreg Roach // The file being requested. e.g. "css/theme.css" 160e106ff43SGreg Roach $asset = $request->getQueryParams()['asset']; 1619ef73392SGreg Roach 1629ef73392SGreg Roach // Do not allow requests that try to access parent folders. 163dec352c1SGreg Roach if (str_contains($asset, '..')) { 164d501c45dSGreg Roach throw new HttpAccessDeniedException($asset); 1659ef73392SGreg Roach } 1669ef73392SGreg Roach 1679ef73392SGreg Roach // Find the file for this asset. 1689ef73392SGreg Roach // Note that we could also generate CSS files using views/templates. 169249522f1SGreg Roach // e.g. $file = view(....) 17002086832SGreg Roach $file = $this->resourcesFolder() . $asset; 1719ef73392SGreg Roach 1729ef73392SGreg Roach if (!file_exists($file)) { 173249522f1SGreg Roach throw new HttpNotFoundException(e($file)); 1749ef73392SGreg Roach } 1759ef73392SGreg Roach 1769ef73392SGreg Roach $content = file_get_contents($file); 177249522f1SGreg Roach $extension = strtolower(pathinfo($asset, PATHINFO_EXTENSION)); 178e7f16b43SGreg Roach $mime_type = Mime::TYPES[$extension] ?? Mime::DEFAULT_TYPE; 1799ef73392SGreg Roach 180b11d5678SGreg Roach return response($content, StatusCodeInterface::STATUS_OK, [ 181b11d5678SGreg Roach 'Cache-Control' => 'public,max-age=31536000', 182b11d5678SGreg Roach 'Content-Type' => $mime_type, 183b11d5678SGreg Roach ]); 1849ef73392SGreg Roach } 18549a243cbSGreg Roach} 186