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; 23*64d12f7bSGreg Roachuse Fisharebest\Webtrees\Cache; 249ef73392SGreg Roachuse Fisharebest\Webtrees\Carbon; 25d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpAccessDeniedException; 26d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpNotFoundException; 27*64d12f7bSGreg Roachuse Fisharebest\Webtrees\Site; 28*64d12f7bSGreg Roachuse GuzzleHttp\Client; 29*64d12f7bSGreg Roachuse GuzzleHttp\Exception\RequestException; 309ef73392SGreg Roachuse Illuminate\Support\Str; 316ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 326ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 333976b470SGreg Roach 34*64d12f7bSGreg Roachuse function app; 35*64d12f7bSGreg Roachuse function assert; 361abbd7e1SGreg Roachuse function strlen; 379ef73392SGreg Roach 3849a243cbSGreg Roach/** 3949a243cbSGreg Roach * Trait ModuleCustomTrait - default implementation of ModuleCustomInterface 4049a243cbSGreg Roach */ 4149a243cbSGreg Roachtrait ModuleCustomTrait 4249a243cbSGreg Roach{ 4349a243cbSGreg Roach /** 4402086832SGreg Roach * Where does this module store its resources 4502086832SGreg Roach * 4602086832SGreg Roach * @return string 4702086832SGreg Roach */ 4802086832SGreg Roach abstract public function resourcesFolder(): string; 4902086832SGreg Roach 5002086832SGreg Roach /** 5102086832SGreg Roach * A unique internal name for this module (based on the installation folder). 5202086832SGreg Roach * 5302086832SGreg Roach * @return string 5402086832SGreg Roach */ 5502086832SGreg Roach abstract public function name(): string; 5602086832SGreg Roach 5702086832SGreg Roach /** 5849a243cbSGreg Roach * The person or organisation who created this module. 5949a243cbSGreg Roach * 6049a243cbSGreg Roach * @return string 6149a243cbSGreg Roach */ 62cbf4b7faSGreg Roach public function customModuleAuthorName(): string 63cbf4b7faSGreg Roach { 64304fefbeSGreg Roach return ''; 6549a243cbSGreg Roach } 6649a243cbSGreg Roach 6749a243cbSGreg Roach /** 6849a243cbSGreg Roach * The version of this module. 6949a243cbSGreg Roach * 70304fefbeSGreg Roach * @return string e.g. '1.2.3' 7149a243cbSGreg Roach */ 72cbf4b7faSGreg Roach public function customModuleVersion(): string 73cbf4b7faSGreg Roach { 74304fefbeSGreg Roach return ''; 7549a243cbSGreg Roach } 7649a243cbSGreg Roach 7749a243cbSGreg Roach /** 7849a243cbSGreg Roach * A URL that will provide the latest version of this module. 7949a243cbSGreg Roach * 8049a243cbSGreg Roach * @return string 8149a243cbSGreg Roach */ 82cbf4b7faSGreg Roach public function customModuleLatestVersionUrl(): string 83cbf4b7faSGreg Roach { 84304fefbeSGreg Roach return ''; 8549a243cbSGreg Roach } 8649a243cbSGreg Roach 8749a243cbSGreg Roach /** 88*64d12f7bSGreg Roach * Fetch the latest version of this module. 89*64d12f7bSGreg Roach * 90*64d12f7bSGreg Roach * @return string 91*64d12f7bSGreg Roach */ 92*64d12f7bSGreg Roach public function customModuleLatestVersion(): string 93*64d12f7bSGreg Roach { 94*64d12f7bSGreg Roach // No update URL provided. 95*64d12f7bSGreg Roach if ($this->customModuleLatestVersionUrl() === '') { 96*64d12f7bSGreg Roach return $this->customModuleVersion(); 97*64d12f7bSGreg Roach } 98*64d12f7bSGreg Roach 99*64d12f7bSGreg Roach $cache = app('cache.files'); 100*64d12f7bSGreg Roach assert($cache instanceof Cache); 101*64d12f7bSGreg Roach 102*64d12f7bSGreg Roach return $cache->remember($this->name() . '-latest-version', function () { 103*64d12f7bSGreg Roach try { 104*64d12f7bSGreg Roach $client = new Client([ 105*64d12f7bSGreg Roach 'timeout' => 3, 106*64d12f7bSGreg Roach ]); 107*64d12f7bSGreg Roach 108*64d12f7bSGreg Roach $response = $client->get($this->customModuleLatestVersionUrl()); 109*64d12f7bSGreg Roach 110*64d12f7bSGreg Roach if ($response->getStatusCode() === StatusCodeInterface::STATUS_OK) { 111*64d12f7bSGreg Roach $version = $response->getBody()->getContents(); 112*64d12f7bSGreg Roach 113*64d12f7bSGreg Roach // Does the response look like a version? 114*64d12f7bSGreg Roach if (preg_match('/^\d+\.\d+\.\d+/', $version)) { 115*64d12f7bSGreg Roach return $version; 116*64d12f7bSGreg Roach } 117*64d12f7bSGreg Roach } 118*64d12f7bSGreg Roach } catch (RequestException $ex) { 119*64d12f7bSGreg Roach // Can't connect to the server? 120*64d12f7bSGreg Roach } 121*64d12f7bSGreg Roach 122*64d12f7bSGreg Roach return $this->customModuleVersion(); 123*64d12f7bSGreg Roach }, 86400); 124*64d12f7bSGreg Roach } 125*64d12f7bSGreg Roach 126*64d12f7bSGreg Roach /** 127*64d12f7bSGreg Roach * Where to get support for this module. Perhaps a github repository? 12849a243cbSGreg Roach * 12949a243cbSGreg Roach * @return string 13049a243cbSGreg Roach */ 131cbf4b7faSGreg Roach public function customModuleSupportUrl(): string 132cbf4b7faSGreg Roach { 133304fefbeSGreg Roach return ''; 13449a243cbSGreg Roach } 135d37db671SGreg Roach 136d37db671SGreg Roach /** 137d37db671SGreg Roach * Additional/updated translations. 138d37db671SGreg Roach * 139d37db671SGreg Roach * @param string $language 140d37db671SGreg Roach * 141d37db671SGreg Roach * @return string[] 142d37db671SGreg Roach */ 143d37db671SGreg Roach public function customTranslations(string $language): array 144d37db671SGreg Roach { 145d37db671SGreg Roach return []; 146d37db671SGreg Roach } 1479ef73392SGreg Roach 1489ef73392SGreg Roach /** 1499ef73392SGreg Roach * Create a URL for an asset. 1509ef73392SGreg Roach * 1519ef73392SGreg Roach * @param string $asset e.g. "css/theme.css" or "img/banner.png" 1529ef73392SGreg Roach * 1539ef73392SGreg Roach * @return string 1549ef73392SGreg Roach */ 1559ef73392SGreg Roach public function assetUrl(string $asset): string 1569ef73392SGreg Roach { 15702086832SGreg Roach $file = $this->resourcesFolder() . $asset; 1589ef73392SGreg Roach 1599ef73392SGreg Roach // Add the file's modification time to the URL, so we can set long expiry cache headers. 1609ef73392SGreg Roach $hash = filemtime($file); 1619ef73392SGreg Roach 1629ef73392SGreg Roach return route('module', [ 1639ef73392SGreg Roach 'module' => $this->name(), 164a81abcafSGreg Roach 'action' => 'Asset', 1659ef73392SGreg Roach 'asset' => $asset, 1669ef73392SGreg Roach 'hash' => $hash, 1679ef73392SGreg Roach ]); 1689ef73392SGreg Roach } 1699ef73392SGreg Roach 1709ef73392SGreg Roach /** 1719ef73392SGreg Roach * Serve a CSS/JS file. 1729ef73392SGreg Roach * 1736ccdf4f0SGreg Roach * @param ServerRequestInterface $request 1749ef73392SGreg Roach * 1756ccdf4f0SGreg Roach * @return ResponseInterface 1769ef73392SGreg Roach */ 1776ccdf4f0SGreg Roach public function getAssetAction(ServerRequestInterface $request): ResponseInterface 1789ef73392SGreg Roach { 1799ef73392SGreg Roach // The file being requested. e.g. "css/theme.css" 180e106ff43SGreg Roach $asset = $request->getQueryParams()['asset']; 1819ef73392SGreg Roach 1829ef73392SGreg Roach // Do not allow requests that try to access parent folders. 1839ef73392SGreg Roach if (Str::contains($asset, '..')) { 184d501c45dSGreg Roach throw new HttpAccessDeniedException($asset); 1859ef73392SGreg Roach } 1869ef73392SGreg Roach 1879ef73392SGreg Roach // Find the file for this asset. 1889ef73392SGreg Roach // Note that we could also generate CSS files using views/templates. 1899ef73392SGreg Roach // e.g. $file = view(.... 19002086832SGreg Roach $file = $this->resourcesFolder() . $asset; 1919ef73392SGreg Roach 1929ef73392SGreg Roach if (!file_exists($file)) { 193d501c45dSGreg Roach throw new HttpNotFoundException($file); 1949ef73392SGreg Roach } 1959ef73392SGreg Roach 1969ef73392SGreg Roach $content = file_get_contents($file); 1979ef73392SGreg Roach $extension = pathinfo($asset, PATHINFO_EXTENSION); 1989ef73392SGreg Roach 1999ef73392SGreg Roach $mime_types = [ 2009ef73392SGreg Roach 'css' => 'text/css', 2019ef73392SGreg Roach 'gif' => 'image/gif', 2029ef73392SGreg Roach 'js' => 'application/javascript', 20385a166d8SGreg Roach 'jpg' => 'image/jpeg', 20485a166d8SGreg Roach 'jpeg' => 'image/jpeg', 2059ef73392SGreg Roach 'json' => 'application/json', 2069ef73392SGreg Roach 'png' => 'image/png', 2079ef73392SGreg Roach 'txt' => 'text/plain', 2089ef73392SGreg Roach ]; 2099ef73392SGreg Roach 2109ef73392SGreg Roach $mime_type = $mime_types[$extension] ?? 'application/octet-stream'; 2119ef73392SGreg Roach 2129ef73392SGreg Roach $headers = [ 2139ef73392SGreg Roach 'Content-Type' => $mime_type, 2141abbd7e1SGreg Roach 'Cache-Control' => 'max-age=31536000, public', 2151abbd7e1SGreg Roach 'Content-Length' => strlen($content), 2161abbd7e1SGreg Roach 'Expires' => Carbon::now()->addYears(10)->toRfc7231String(), 2179ef73392SGreg Roach ]; 2189ef73392SGreg Roach 2196ccdf4f0SGreg Roach return response($content, StatusCodeInterface::STATUS_OK, $headers); 2209ef73392SGreg Roach } 22149a243cbSGreg Roach} 222