149a243cbSGreg Roach<?php 23976b470SGreg Roach 349a243cbSGreg Roach/** 449a243cbSGreg Roach * webtrees: online genealogy 5*5bfc6897SGreg Roach * Copyright (C) 2022 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 /** 41b582d134SGreg Roach * A unique internal name for this module (based on the installation folder). 42b582d134SGreg Roach * 43b582d134SGreg Roach * @return string 44b582d134SGreg Roach */ 45b582d134SGreg Roach abstract public function name(): string; 46b582d134SGreg Roach 47b582d134SGreg Roach /** 48b582d134SGreg Roach * Where does this module store its resources 49b582d134SGreg Roach * 50b582d134SGreg Roach * @return string 51b582d134SGreg Roach */ 52b582d134SGreg Roach abstract public function resourcesFolder(): string; 53b582d134SGreg Roach 54b582d134SGreg Roach /** 5549a243cbSGreg Roach * The person or organisation who created this module. 5649a243cbSGreg Roach * 5749a243cbSGreg Roach * @return string 5849a243cbSGreg Roach */ 59cbf4b7faSGreg Roach public function customModuleAuthorName(): string 60cbf4b7faSGreg Roach { 61304fefbeSGreg Roach return ''; 6249a243cbSGreg Roach } 6349a243cbSGreg Roach 6449a243cbSGreg Roach /** 6549a243cbSGreg Roach * The version of this module. 6649a243cbSGreg Roach * 67304fefbeSGreg Roach * @return string e.g. '1.2.3' 6849a243cbSGreg Roach */ 69cbf4b7faSGreg Roach public function customModuleVersion(): string 70cbf4b7faSGreg Roach { 71304fefbeSGreg Roach return ''; 7249a243cbSGreg Roach } 7349a243cbSGreg Roach 7449a243cbSGreg Roach /** 7549a243cbSGreg Roach * A URL that will provide the latest version of this module. 7649a243cbSGreg Roach * 7749a243cbSGreg Roach * @return string 7849a243cbSGreg Roach */ 79cbf4b7faSGreg Roach public function customModuleLatestVersionUrl(): string 80cbf4b7faSGreg Roach { 81304fefbeSGreg Roach return ''; 8249a243cbSGreg Roach } 8349a243cbSGreg Roach 8449a243cbSGreg Roach /** 8564d12f7bSGreg Roach * Fetch the latest version of this module. 8664d12f7bSGreg Roach * 8764d12f7bSGreg Roach * @return string 8864d12f7bSGreg Roach */ 8964d12f7bSGreg Roach public function customModuleLatestVersion(): string 9064d12f7bSGreg Roach { 9164d12f7bSGreg Roach // No update URL provided. 9264d12f7bSGreg Roach if ($this->customModuleLatestVersionUrl() === '') { 9364d12f7bSGreg Roach return $this->customModuleVersion(); 9464d12f7bSGreg Roach } 9564d12f7bSGreg Roach 96df5ebf7cSGreg Roach return Registry::cache()->file()->remember($this->name() . '-latest-version', function (): string { 9764d12f7bSGreg Roach try { 9864d12f7bSGreg Roach $client = new Client([ 9964d12f7bSGreg Roach 'timeout' => 3, 10064d12f7bSGreg Roach ]); 10164d12f7bSGreg Roach 10264d12f7bSGreg Roach $response = $client->get($this->customModuleLatestVersionUrl()); 10364d12f7bSGreg Roach 10464d12f7bSGreg Roach if ($response->getStatusCode() === StatusCodeInterface::STATUS_OK) { 10564d12f7bSGreg Roach $version = $response->getBody()->getContents(); 10664d12f7bSGreg Roach 10764d12f7bSGreg Roach // Does the response look like a version? 10864d12f7bSGreg Roach if (preg_match('/^\d+\.\d+\.\d+/', $version)) { 10964d12f7bSGreg Roach return $version; 11064d12f7bSGreg Roach } 11164d12f7bSGreg Roach } 1124022f21aSGreg Roach } catch (GuzzleException $ex) { 11364d12f7bSGreg Roach // Can't connect to the server? 11464d12f7bSGreg Roach } 11564d12f7bSGreg Roach 11664d12f7bSGreg Roach return $this->customModuleVersion(); 11764d12f7bSGreg Roach }, 86400); 11864d12f7bSGreg Roach } 11964d12f7bSGreg Roach 12064d12f7bSGreg Roach /** 12164d12f7bSGreg Roach * Where to get support for this module. Perhaps a github repository? 12249a243cbSGreg Roach * 12349a243cbSGreg Roach * @return string 12449a243cbSGreg Roach */ 125cbf4b7faSGreg Roach public function customModuleSupportUrl(): string 126cbf4b7faSGreg Roach { 127304fefbeSGreg Roach return ''; 12849a243cbSGreg Roach } 129d37db671SGreg Roach 130d37db671SGreg Roach /** 131d37db671SGreg Roach * Additional/updated translations. 132d37db671SGreg Roach * 133d37db671SGreg Roach * @param string $language 134d37db671SGreg Roach * 135f4c767fdSGreg Roach * @return array<string,string> 136d37db671SGreg Roach */ 137d37db671SGreg Roach public function customTranslations(string $language): array 138d37db671SGreg Roach { 139d37db671SGreg Roach return []; 140d37db671SGreg Roach } 1419ef73392SGreg Roach 1429ef73392SGreg Roach /** 1439ef73392SGreg Roach * Create a URL for an asset. 1449ef73392SGreg Roach * 1459ef73392SGreg Roach * @param string $asset e.g. "css/theme.css" or "img/banner.png" 1469ef73392SGreg Roach * 1479ef73392SGreg Roach * @return string 1489ef73392SGreg Roach */ 1499ef73392SGreg Roach public function assetUrl(string $asset): string 1509ef73392SGreg Roach { 15102086832SGreg Roach $file = $this->resourcesFolder() . $asset; 1529ef73392SGreg Roach 1539ef73392SGreg Roach // Add the file's modification time to the URL, so we can set long expiry cache headers. 1549ef73392SGreg Roach $hash = filemtime($file); 1559ef73392SGreg Roach 1569ef73392SGreg Roach return route('module', [ 1579ef73392SGreg Roach 'module' => $this->name(), 158a81abcafSGreg Roach 'action' => 'Asset', 1599ef73392SGreg Roach 'asset' => $asset, 1609ef73392SGreg Roach 'hash' => $hash, 1619ef73392SGreg Roach ]); 1629ef73392SGreg Roach } 1639ef73392SGreg Roach 1649ef73392SGreg Roach /** 1659ef73392SGreg Roach * Serve a CSS/JS file. 1669ef73392SGreg Roach * 1676ccdf4f0SGreg Roach * @param ServerRequestInterface $request 1689ef73392SGreg Roach * 1696ccdf4f0SGreg Roach * @return ResponseInterface 1709ef73392SGreg Roach */ 1716ccdf4f0SGreg Roach public function getAssetAction(ServerRequestInterface $request): ResponseInterface 1729ef73392SGreg Roach { 1739ef73392SGreg Roach // The file being requested. e.g. "css/theme.css" 174e106ff43SGreg Roach $asset = $request->getQueryParams()['asset']; 1759ef73392SGreg Roach 1769ef73392SGreg Roach // Do not allow requests that try to access parent folders. 177dec352c1SGreg Roach if (str_contains($asset, '..')) { 178d501c45dSGreg Roach throw new HttpAccessDeniedException($asset); 1799ef73392SGreg Roach } 1809ef73392SGreg Roach 1819ef73392SGreg Roach // Find the file for this asset. 1829ef73392SGreg Roach // Note that we could also generate CSS files using views/templates. 183249522f1SGreg Roach // e.g. $file = view(....) 18402086832SGreg Roach $file = $this->resourcesFolder() . $asset; 1859ef73392SGreg Roach 1869ef73392SGreg Roach if (!file_exists($file)) { 187249522f1SGreg Roach throw new HttpNotFoundException(e($file)); 1889ef73392SGreg Roach } 1899ef73392SGreg Roach 1909ef73392SGreg Roach $content = file_get_contents($file); 191249522f1SGreg Roach $extension = strtolower(pathinfo($asset, PATHINFO_EXTENSION)); 192e7f16b43SGreg Roach $mime_type = Mime::TYPES[$extension] ?? Mime::DEFAULT_TYPE; 1939ef73392SGreg Roach 194b11d5678SGreg Roach return response($content, StatusCodeInterface::STATUS_OK, [ 195b11d5678SGreg Roach 'Cache-Control' => 'public,max-age=31536000', 196b11d5678SGreg Roach 'Content-Type' => $mime_type, 197b11d5678SGreg Roach ]); 1989ef73392SGreg Roach } 19949a243cbSGreg Roach} 200