1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20use Aura\Router\RouterContainer; 21use Fig\Http\Message\StatusCodeInterface; 22use Fisharebest\Webtrees\Html; 23use Fisharebest\Webtrees\Registry; 24use Fisharebest\Webtrees\Session as WebtreesSession; 25use Fisharebest\Webtrees\Validator; 26use Fisharebest\Webtrees\View as WebtreesView; 27use Fisharebest\Webtrees\Webtrees; 28use Psr\Http\Message\ResponseFactoryInterface; 29use Psr\Http\Message\ResponseInterface; 30use Psr\Http\Message\ServerRequestInterface; 31use Psr\Http\Message\StreamFactoryInterface; 32 33/** 34 * Get the IoC container, or fetch something from it. 35 * 36 * @param string|null $abstract 37 * 38 * @return mixed 39 */ 40function app(string $abstract = null) 41{ 42 if ($abstract === null) { 43 return Webtrees::container(); 44 } 45 46 return Webtrees::make($abstract); 47} 48 49/** 50 * Generate a URL to an asset file in the public folder. 51 * Add a version parameter for cache-busting. 52 * 53 * @param string $path 54 * 55 * @return string 56 */ 57function asset(string $path): string 58{ 59 if (substr($path, -1) === '/') { 60 $version = ''; 61 } elseif (Webtrees::STABILITY === '') { 62 $version = '?v=' . Webtrees::VERSION; 63 } else { 64 $version = '?v=' . filemtime(Webtrees::ROOT_DIR . 'public/' . $path); 65 } 66 67 $request = app(ServerRequestInterface::class); 68 assert($request instanceof ServerRequestInterface); 69 70 $base_url = Validator::attributes($request)->string('base_url'); 71 72 return $base_url . '/public/' . $path . $version; 73} 74 75/** 76 * Generate a CSRF token form field. 77 * 78 * @return string 79 */ 80function csrf_field(): string 81{ 82 return '<input type="hidden" name="_csrf" value="' . e(WebtreesSession::getCsrfToken()) . '">'; 83} 84 85/** 86 * Get the CSRF token value. 87 * 88 * @return string 89 */ 90function csrf_token(): string 91{ 92 return WebtreesSession::getCsrfToken(); 93} 94 95/** 96 * @param string $url 97 * @param int $code 98 * 99 * @return ResponseInterface 100 */ 101function redirect(string $url, int $code = StatusCodeInterface::STATUS_FOUND): ResponseInterface 102{ 103 /** @var ResponseFactoryInterface $response_factory */ 104 $response_factory = app(ResponseFactoryInterface::class); 105 106 return $response_factory 107 ->createResponse($code) 108 ->withHeader('Location', $url); 109} 110 111/** 112 * Create a response. 113 * 114 * @param string|array<mixed>|object $content 115 * @param int $code 116 * @param array<string> $headers 117 * 118 * @return ResponseInterface 119 */ 120function response($content = '', int $code = StatusCodeInterface::STATUS_OK, array $headers = []): ResponseInterface 121{ 122 return Registry::responseFactory()->response($content, $code, $headers); 123} 124 125/** 126 * Generate a URL for a named route. 127 * 128 * @param string $route_name 129 * @param array<bool|int|string|array<string>|null> $parameters 130 * 131 * @return string 132 */ 133function route(string $route_name, array $parameters = []): string 134{ 135 return Registry::routeFactory()->route($route_name, $parameters); 136} 137 138/** 139 * Create and render a view in a single operation. 140 * 141 * @param string $name 142 * @param array<mixed> $data 143 * 144 * @return string 145 */ 146function view(string $name, array $data = []): string 147{ 148 return WebtreesView::make($name, $data); 149} 150