11f3fb95cSGreg Roach<?php 23976b470SGreg Roach 31f3fb95cSGreg Roach/** 41f3fb95cSGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team 61f3fb95cSGreg Roach * This program is free software: you can redistribute it and/or modify 71f3fb95cSGreg Roach * it under the terms of the GNU General Public License as published by 81f3fb95cSGreg Roach * the Free Software Foundation, either version 3 of the License, or 91f3fb95cSGreg Roach * (at your option) any later version. 101f3fb95cSGreg Roach * This program is distributed in the hope that it will be useful, 111f3fb95cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 121f3fb95cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 131f3fb95cSGreg Roach * GNU General Public License for more details. 141f3fb95cSGreg 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/>. 161f3fb95cSGreg Roach */ 178b67c11aSGreg Roach 1878f07ab5SGreg Roachdeclare(strict_types=1); 191f3fb95cSGreg Roach 20ee4364daSGreg Roachuse Aura\Router\RouterContainer; 216ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 226ccdf4f0SGreg Roachuse Fisharebest\Webtrees\Html; 23*208909d8SGreg Roachuse Fisharebest\Webtrees\Registry; 24a6656bb5SGreg Roachuse Fisharebest\Webtrees\Session as WebtreesSession; 25b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 268898179dSGreg Roachuse Fisharebest\Webtrees\View as WebtreesView; 2752bcc402SGreg Roachuse Fisharebest\Webtrees\Webtrees; 2800c45d23SGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 296ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 309b93b7c3SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 3100c45d23SGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 328b67c11aSGreg Roach 338b67c11aSGreg Roach/** 348b67c11aSGreg Roach * Get the IoC container, or fetch something from it. 358b67c11aSGreg Roach * 368b67c11aSGreg Roach * @param string|null $abstract 378b67c11aSGreg Roach * 38cab242e7SGreg Roach * @return mixed 398b67c11aSGreg Roach */ 408b67c11aSGreg Roachfunction app(string $abstract = null) 418b67c11aSGreg Roach{ 428b67c11aSGreg Roach if ($abstract === null) { 43785274b8SGreg Roach return Webtrees::container(); 448b67c11aSGreg Roach } 45e364afe4SGreg Roach 46785274b8SGreg Roach return Webtrees::make($abstract); 478b67c11aSGreg Roach} 488b67c11aSGreg Roach 491f3fb95cSGreg Roach/** 5052bcc402SGreg Roach * Generate a URL to an asset file in the public folder. 5152bcc402SGreg Roach * Add a version parameter for cache-busting. 5252bcc402SGreg Roach * 5352bcc402SGreg Roach * @param string $path 5452bcc402SGreg Roach * 5552bcc402SGreg Roach * @return string 5652bcc402SGreg Roach */ 5752bcc402SGreg Roachfunction asset(string $path): string 5852bcc402SGreg Roach{ 5975e7614aSGreg Roach if (substr($path, -1) === '/') { 6075e7614aSGreg Roach $version = ''; 6175e7614aSGreg Roach } elseif (Webtrees::STABILITY === '') { 6275e7614aSGreg Roach $version = '?v=' . Webtrees::VERSION; 6352bcc402SGreg Roach } else { 6475e7614aSGreg Roach $version = '?v=' . filemtime(Webtrees::ROOT_DIR . 'public/' . $path); 6552bcc402SGreg Roach } 6652bcc402SGreg Roach 67b55cbc6bSGreg Roach $request = app(ServerRequestInterface::class); 68b55cbc6bSGreg Roach assert($request instanceof ServerRequestInterface); 69b55cbc6bSGreg Roach 70b55cbc6bSGreg Roach $base_url = Validator::attributes($request)->string('base_url'); 71bb1b9730SGreg Roach 72bb1b9730SGreg Roach return $base_url . '/public/' . $path . $version; 7352bcc402SGreg Roach} 7452bcc402SGreg Roach 7552bcc402SGreg Roach/** 76f97c7170SGreg Roach * Generate a CSRF token form field. 77f97c7170SGreg Roach * 78f97c7170SGreg Roach * @return string 79f97c7170SGreg Roach */ 8024f2a3afSGreg Roachfunction csrf_field(): string 81c1010edaSGreg Roach{ 82e240f5e1SGreg Roach return '<input type="hidden" name="_csrf" value="' . e(WebtreesSession::getCsrfToken()) . '">'; 83f97c7170SGreg Roach} 84f97c7170SGreg Roach 85f97c7170SGreg Roach/** 868655ee66SGreg Roach * Get the CSRF token value. 878655ee66SGreg Roach * 888655ee66SGreg Roach * @return string 898655ee66SGreg Roach */ 9024f2a3afSGreg Roachfunction csrf_token(): string 91c1010edaSGreg Roach{ 92a6656bb5SGreg Roach return WebtreesSession::getCsrfToken(); 936ccdf4f0SGreg Roach} 946ccdf4f0SGreg Roach 956ccdf4f0SGreg Roach/** 966ccdf4f0SGreg Roach * @param string $url 976ccdf4f0SGreg Roach * @param int $code 986ccdf4f0SGreg Roach * 996ccdf4f0SGreg Roach * @return ResponseInterface 1006ccdf4f0SGreg Roach */ 101785274b8SGreg Roachfunction redirect(string $url, int $code = StatusCodeInterface::STATUS_FOUND): ResponseInterface 1026ccdf4f0SGreg Roach{ 10300c45d23SGreg Roach /** @var ResponseFactoryInterface $response_factory */ 10400c45d23SGreg Roach $response_factory = app(ResponseFactoryInterface::class); 10500c45d23SGreg Roach 10600c45d23SGreg Roach return $response_factory 1076ccdf4f0SGreg Roach ->createResponse($code) 1086ccdf4f0SGreg Roach ->withHeader('Location', $url); 1096ccdf4f0SGreg Roach} 1106ccdf4f0SGreg Roach 1116ccdf4f0SGreg Roach/** 1126ccdf4f0SGreg Roach * Create a response. 1136ccdf4f0SGreg Roach * 114*208909d8SGreg Roach * @param string|array<mixed>|object $content 1156ccdf4f0SGreg Roach * @param int $code 11609482a55SGreg Roach * @param array<string> $headers 1176ccdf4f0SGreg Roach * 1186ccdf4f0SGreg Roach * @return ResponseInterface 1196ccdf4f0SGreg Roach */ 120785274b8SGreg Roachfunction response($content = '', int $code = StatusCodeInterface::STATUS_OK, array $headers = []): ResponseInterface 1216ccdf4f0SGreg Roach{ 122*208909d8SGreg Roach return Registry::responseFactory()->response($content, $code, $headers); 1238655ee66SGreg Roach} 1248655ee66SGreg Roach 1258655ee66SGreg Roach/** 1261f3fb95cSGreg Roach * Generate a URL for a named route. 1271f3fb95cSGreg Roach * 128ee4364daSGreg Roach * @param string $route_name 12976d39c55SGreg Roach * @param array<bool|int|string|array<string>|null> $parameters 1301f3fb95cSGreg Roach * 1311f3fb95cSGreg Roach * @return string 1321f3fb95cSGreg Roach */ 133ee4364daSGreg Roachfunction route(string $route_name, array $parameters = []): string 134c1010edaSGreg Roach{ 135*208909d8SGreg Roach return Registry::routeFactory()->route($route_name, $parameters); 1361f3fb95cSGreg Roach} 137b2ce94c6SRico Sonntag 1388655ee66SGreg Roach/** 139785274b8SGreg Roach * Create and render a view in a single operation. 1408655ee66SGreg Roach * 1418655ee66SGreg Roach * @param string $name 142785274b8SGreg Roach * @param array<mixed> $data 1438655ee66SGreg Roach * 1448655ee66SGreg Roach * @return string 1458655ee66SGreg Roach */ 14624f2a3afSGreg Roachfunction view(string $name, array $data = []): string 147c1010edaSGreg Roach{ 1488898179dSGreg Roach return WebtreesView::make($name, $data); 1498655ee66SGreg Roach} 150