149a243cbSGreg Roach<?php 23976b470SGreg Roach 349a243cbSGreg Roach/** 449a243cbSGreg Roach * webtrees: online genealogy 5e7f16b43SGreg Roach * Copyright (C) 2020 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; 2364d12f7bSGreg Roachuse Fisharebest\Webtrees\Cache; 24d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpAccessDeniedException; 25d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpNotFoundException; 26e7f16b43SGreg Roachuse Fisharebest\Webtrees\Mime; 2764d12f7bSGreg Roachuse GuzzleHttp\Client; 2864d12f7bSGreg Roachuse GuzzleHttp\Exception\RequestException; 296ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 313976b470SGreg Roach 3264d12f7bSGreg Roachuse function app; 3364d12f7bSGreg Roachuse function assert; 34*dec352c1SGreg Roachuse function str_contains; 351abbd7e1SGreg Roachuse function strlen; 36249522f1SGreg Roachuse function strtolower; 379ef73392SGreg Roach 3849a243cbSGreg Roach/** 3949a243cbSGreg Roach * Trait ModuleCustomTrait - default implementation of ModuleCustomInterface 4049a243cbSGreg Roach */ 4149a243cbSGreg Roachtrait ModuleCustomTrait 4249a243cbSGreg Roach{ 4349a243cbSGreg Roach /** 4449a243cbSGreg Roach * The person or organisation who created this module. 4549a243cbSGreg Roach * 4649a243cbSGreg Roach * @return string 4749a243cbSGreg Roach */ 48cbf4b7faSGreg Roach public function customModuleAuthorName(): string 49cbf4b7faSGreg Roach { 50304fefbeSGreg Roach return ''; 5149a243cbSGreg Roach } 5249a243cbSGreg Roach 5349a243cbSGreg Roach /** 5449a243cbSGreg Roach * The version of this module. 5549a243cbSGreg Roach * 56304fefbeSGreg Roach * @return string e.g. '1.2.3' 5749a243cbSGreg Roach */ 58cbf4b7faSGreg Roach public function customModuleVersion(): string 59cbf4b7faSGreg Roach { 60304fefbeSGreg Roach return ''; 6149a243cbSGreg Roach } 6249a243cbSGreg Roach 6349a243cbSGreg Roach /** 6449a243cbSGreg Roach * A URL that will provide the latest version of this module. 6549a243cbSGreg Roach * 6649a243cbSGreg Roach * @return string 6749a243cbSGreg Roach */ 68cbf4b7faSGreg Roach public function customModuleLatestVersionUrl(): string 69cbf4b7faSGreg Roach { 70304fefbeSGreg Roach return ''; 7149a243cbSGreg Roach } 7249a243cbSGreg Roach 7349a243cbSGreg Roach /** 7464d12f7bSGreg Roach * Fetch the latest version of this module. 7564d12f7bSGreg Roach * 7664d12f7bSGreg Roach * @return string 7764d12f7bSGreg Roach */ 7864d12f7bSGreg Roach public function customModuleLatestVersion(): string 7964d12f7bSGreg Roach { 8064d12f7bSGreg Roach // No update URL provided. 8164d12f7bSGreg Roach if ($this->customModuleLatestVersionUrl() === '') { 8264d12f7bSGreg Roach return $this->customModuleVersion(); 8364d12f7bSGreg Roach } 8464d12f7bSGreg Roach 8564d12f7bSGreg Roach $cache = app('cache.files'); 8664d12f7bSGreg Roach assert($cache instanceof Cache); 8764d12f7bSGreg Roach 8864d12f7bSGreg Roach return $cache->remember($this->name() . '-latest-version', function () { 8964d12f7bSGreg Roach try { 9064d12f7bSGreg Roach $client = new Client([ 9164d12f7bSGreg Roach 'timeout' => 3, 9264d12f7bSGreg Roach ]); 9364d12f7bSGreg Roach 9464d12f7bSGreg Roach $response = $client->get($this->customModuleLatestVersionUrl()); 9564d12f7bSGreg Roach 9664d12f7bSGreg Roach if ($response->getStatusCode() === StatusCodeInterface::STATUS_OK) { 9764d12f7bSGreg Roach $version = $response->getBody()->getContents(); 9864d12f7bSGreg Roach 9964d12f7bSGreg Roach // Does the response look like a version? 10064d12f7bSGreg Roach if (preg_match('/^\d+\.\d+\.\d+/', $version)) { 10164d12f7bSGreg Roach return $version; 10264d12f7bSGreg Roach } 10364d12f7bSGreg Roach } 10464d12f7bSGreg Roach } catch (RequestException $ex) { 10564d12f7bSGreg Roach // Can't connect to the server? 10664d12f7bSGreg Roach } 10764d12f7bSGreg Roach 10864d12f7bSGreg Roach return $this->customModuleVersion(); 10964d12f7bSGreg Roach }, 86400); 11064d12f7bSGreg Roach } 11164d12f7bSGreg Roach 11264d12f7bSGreg Roach /** 11364d12f7bSGreg Roach * Where to get support for this module. Perhaps a github repository? 11449a243cbSGreg Roach * 11549a243cbSGreg Roach * @return string 11649a243cbSGreg Roach */ 117cbf4b7faSGreg Roach public function customModuleSupportUrl(): string 118cbf4b7faSGreg Roach { 119304fefbeSGreg Roach return ''; 12049a243cbSGreg Roach } 121d37db671SGreg Roach 122d37db671SGreg Roach /** 123d37db671SGreg Roach * Additional/updated translations. 124d37db671SGreg Roach * 125d37db671SGreg Roach * @param string $language 126d37db671SGreg Roach * 127f4c767fdSGreg Roach * @return array<string,string> 128d37db671SGreg Roach */ 129d37db671SGreg Roach public function customTranslations(string $language): array 130d37db671SGreg Roach { 131d37db671SGreg Roach return []; 132d37db671SGreg Roach } 1339ef73392SGreg Roach 1349ef73392SGreg Roach /** 1359ef73392SGreg Roach * Create a URL for an asset. 1369ef73392SGreg Roach * 1379ef73392SGreg Roach * @param string $asset e.g. "css/theme.css" or "img/banner.png" 1389ef73392SGreg Roach * 1399ef73392SGreg Roach * @return string 1409ef73392SGreg Roach */ 1419ef73392SGreg Roach public function assetUrl(string $asset): string 1429ef73392SGreg Roach { 14302086832SGreg Roach $file = $this->resourcesFolder() . $asset; 1449ef73392SGreg Roach 1459ef73392SGreg Roach // Add the file's modification time to the URL, so we can set long expiry cache headers. 1469ef73392SGreg Roach $hash = filemtime($file); 1479ef73392SGreg Roach 1489ef73392SGreg Roach return route('module', [ 1499ef73392SGreg Roach 'module' => $this->name(), 150a81abcafSGreg Roach 'action' => 'Asset', 1519ef73392SGreg Roach 'asset' => $asset, 1529ef73392SGreg Roach 'hash' => $hash, 1539ef73392SGreg Roach ]); 1549ef73392SGreg Roach } 1559ef73392SGreg Roach 1569ef73392SGreg Roach /** 1579ef73392SGreg Roach * Serve a CSS/JS file. 1589ef73392SGreg Roach * 1596ccdf4f0SGreg Roach * @param ServerRequestInterface $request 1609ef73392SGreg Roach * 1616ccdf4f0SGreg Roach * @return ResponseInterface 1629ef73392SGreg Roach */ 1636ccdf4f0SGreg Roach public function getAssetAction(ServerRequestInterface $request): ResponseInterface 1649ef73392SGreg Roach { 1659ef73392SGreg Roach // The file being requested. e.g. "css/theme.css" 166e106ff43SGreg Roach $asset = $request->getQueryParams()['asset']; 1679ef73392SGreg Roach 1689ef73392SGreg Roach // Do not allow requests that try to access parent folders. 169*dec352c1SGreg Roach if (str_contains($asset, '..')) { 170d501c45dSGreg Roach throw new HttpAccessDeniedException($asset); 1719ef73392SGreg Roach } 1729ef73392SGreg Roach 1739ef73392SGreg Roach // Find the file for this asset. 1749ef73392SGreg Roach // Note that we could also generate CSS files using views/templates. 175249522f1SGreg Roach // e.g. $file = view(....) 17602086832SGreg Roach $file = $this->resourcesFolder() . $asset; 1779ef73392SGreg Roach 1789ef73392SGreg Roach if (!file_exists($file)) { 179249522f1SGreg Roach throw new HttpNotFoundException(e($file)); 1809ef73392SGreg Roach } 1819ef73392SGreg Roach 1829ef73392SGreg Roach $content = file_get_contents($file); 183249522f1SGreg Roach $extension = strtolower(pathinfo($asset, PATHINFO_EXTENSION)); 184e7f16b43SGreg Roach $mime_type = Mime::TYPES[$extension] ?? Mime::DEFAULT_TYPE; 1859ef73392SGreg Roach 186249522f1SGreg Roach return response($content, StatusCodeInterface::STATUS_OK) 187dfd7362bSGreg Roach ->withHeader('Cache-Control', 'public,max-age=31536000') 1885ede9153SGreg Roach ->withHeader('Content-Length', (string) strlen($content)) 189249522f1SGreg Roach ->withHeader('Content-Type', $mime_type); 1909ef73392SGreg Roach } 19149a243cbSGreg Roach} 192