11f3fb95cSGreg Roach<?php 23976b470SGreg Roach 31f3fb95cSGreg Roach/** 41f3fb95cSGreg Roach * webtrees: online genealogy 55bfc6897SGreg Roach * Copyright (C) 2022 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 206ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 21208909d8SGreg Roachuse Fisharebest\Webtrees\Registry; 22a6656bb5SGreg Roachuse Fisharebest\Webtrees\Session as WebtreesSession; 23b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 248898179dSGreg Roachuse Fisharebest\Webtrees\View as WebtreesView; 2552bcc402SGreg Roachuse Fisharebest\Webtrees\Webtrees; 2600c45d23SGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 276ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 289b93b7c3SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 298b67c11aSGreg Roach 308b67c11aSGreg Roach/** 318b67c11aSGreg Roach * Get the IoC container, or fetch something from it. 328b67c11aSGreg Roach * 338b67c11aSGreg Roach * @param string|null $abstract 348b67c11aSGreg Roach * 35cab242e7SGreg Roach * @return mixed 368b67c11aSGreg Roach */ 378b67c11aSGreg Roachfunction app(string $abstract = null) 388b67c11aSGreg Roach{ 398b67c11aSGreg Roach if ($abstract === null) { 40785274b8SGreg Roach return Webtrees::container(); 418b67c11aSGreg Roach } 42e364afe4SGreg Roach 43785274b8SGreg Roach return Webtrees::make($abstract); 448b67c11aSGreg Roach} 458b67c11aSGreg Roach 461f3fb95cSGreg Roach/** 4752bcc402SGreg Roach * Generate a URL to an asset file in the public folder. 4852bcc402SGreg Roach * Add a version parameter for cache-busting. 4952bcc402SGreg Roach * 5052bcc402SGreg Roach * @param string $path 5152bcc402SGreg Roach * 5252bcc402SGreg Roach * @return string 5352bcc402SGreg Roach */ 5452bcc402SGreg Roachfunction asset(string $path): string 5552bcc402SGreg Roach{ 565181bce3SGreg Roach if (str_ends_with($path, '/')) { 5775e7614aSGreg Roach $version = ''; 5875e7614aSGreg Roach } elseif (Webtrees::STABILITY === '') { 5975e7614aSGreg Roach $version = '?v=' . Webtrees::VERSION; 6052bcc402SGreg Roach } else { 6175e7614aSGreg Roach $version = '?v=' . filemtime(Webtrees::ROOT_DIR . 'public/' . $path); 6252bcc402SGreg Roach } 6352bcc402SGreg Roach 64b55cbc6bSGreg Roach $request = app(ServerRequestInterface::class); 65b55cbc6bSGreg Roach assert($request instanceof ServerRequestInterface); 66b55cbc6bSGreg Roach 67b55cbc6bSGreg Roach $base_url = Validator::attributes($request)->string('base_url'); 68bb1b9730SGreg Roach 69bb1b9730SGreg Roach return $base_url . '/public/' . $path . $version; 7052bcc402SGreg Roach} 7152bcc402SGreg Roach 7252bcc402SGreg Roach/** 73f97c7170SGreg Roach * Generate a CSRF token form field. 74f97c7170SGreg Roach * 75f97c7170SGreg Roach * @return string 76f97c7170SGreg Roach */ 7724f2a3afSGreg Roachfunction csrf_field(): string 78c1010edaSGreg Roach{ 79e240f5e1SGreg Roach return '<input type="hidden" name="_csrf" value="' . e(WebtreesSession::getCsrfToken()) . '">'; 80f97c7170SGreg Roach} 81f97c7170SGreg Roach 82f97c7170SGreg Roach/** 838655ee66SGreg Roach * Get the CSRF token value. 848655ee66SGreg Roach * 858655ee66SGreg Roach * @return string 868655ee66SGreg Roach */ 8724f2a3afSGreg Roachfunction csrf_token(): string 88c1010edaSGreg Roach{ 89a6656bb5SGreg Roach return WebtreesSession::getCsrfToken(); 906ccdf4f0SGreg Roach} 916ccdf4f0SGreg Roach 926ccdf4f0SGreg Roach/** 936ccdf4f0SGreg Roach * @param string $url 946ccdf4f0SGreg Roach * @param int $code 956ccdf4f0SGreg Roach * 966ccdf4f0SGreg Roach * @return ResponseInterface 976ccdf4f0SGreg Roach */ 98785274b8SGreg Roachfunction redirect(string $url, int $code = StatusCodeInterface::STATUS_FOUND): ResponseInterface 996ccdf4f0SGreg Roach{ 10000c45d23SGreg Roach /** @var ResponseFactoryInterface $response_factory */ 10100c45d23SGreg Roach $response_factory = app(ResponseFactoryInterface::class); 10200c45d23SGreg Roach 10300c45d23SGreg Roach return $response_factory 1046ccdf4f0SGreg Roach ->createResponse($code) 105*6172e7f6SGreg Roach ->withHeader('location', $url); 1066ccdf4f0SGreg Roach} 1076ccdf4f0SGreg Roach 1086ccdf4f0SGreg Roach/** 1096ccdf4f0SGreg Roach * Create a response. 1106ccdf4f0SGreg Roach * 111208909d8SGreg Roach * @param string|array<mixed>|object $content 1126ccdf4f0SGreg Roach * @param int $code 11309482a55SGreg Roach * @param array<string> $headers 1146ccdf4f0SGreg Roach * 1156ccdf4f0SGreg Roach * @return ResponseInterface 1166ccdf4f0SGreg Roach */ 117785274b8SGreg Roachfunction response($content = '', int $code = StatusCodeInterface::STATUS_OK, array $headers = []): ResponseInterface 1186ccdf4f0SGreg Roach{ 119208909d8SGreg Roach return Registry::responseFactory()->response($content, $code, $headers); 1208655ee66SGreg Roach} 1218655ee66SGreg Roach 1228655ee66SGreg Roach/** 1231f3fb95cSGreg Roach * Generate a URL for a named route. 1241f3fb95cSGreg Roach * 125ee4364daSGreg Roach * @param string $route_name 12676d39c55SGreg Roach * @param array<bool|int|string|array<string>|null> $parameters 1271f3fb95cSGreg Roach * 1281f3fb95cSGreg Roach * @return string 1291f3fb95cSGreg Roach */ 130ee4364daSGreg Roachfunction route(string $route_name, array $parameters = []): string 131c1010edaSGreg Roach{ 132208909d8SGreg Roach return Registry::routeFactory()->route($route_name, $parameters); 1331f3fb95cSGreg Roach} 134b2ce94c6SRico Sonntag 1358655ee66SGreg Roach/** 136785274b8SGreg Roach * Create and render a view in a single operation. 1378655ee66SGreg Roach * 1388655ee66SGreg Roach * @param string $name 139785274b8SGreg Roach * @param array<mixed> $data 1408655ee66SGreg Roach * 1418655ee66SGreg Roach * @return string 1428655ee66SGreg Roach */ 14324f2a3afSGreg Roachfunction view(string $name, array $data = []): string 144c1010edaSGreg Roach{ 1458898179dSGreg Roach return WebtreesView::make($name, $data); 1468655ee66SGreg Roach} 147