11f3fb95cSGreg Roach<?php 21f3fb95cSGreg Roach/** 31f3fb95cSGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 51f3fb95cSGreg Roach * This program is free software: you can redistribute it and/or modify 61f3fb95cSGreg Roach * it under the terms of the GNU General Public License as published by 71f3fb95cSGreg Roach * the Free Software Foundation, either version 3 of the License, or 81f3fb95cSGreg Roach * (at your option) any later version. 91f3fb95cSGreg Roach * This program is distributed in the hope that it will be useful, 101f3fb95cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 111f3fb95cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 121f3fb95cSGreg Roach * GNU General Public License for more details. 131f3fb95cSGreg Roach * You should have received a copy of the GNU General Public License 141f3fb95cSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 151f3fb95cSGreg Roach */ 168b67c11aSGreg Roach 1778f07ab5SGreg Roachdeclare(strict_types=1); 181f3fb95cSGreg Roach 196ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 208b67c11aSGreg Roachuse Fisharebest\Webtrees\Application; 216ccdf4f0SGreg Roachuse Fisharebest\Webtrees\Html; 226ccdf4f0SGreg Roachuse Fisharebest\Webtrees\Session; 238898179dSGreg Roachuse Fisharebest\Webtrees\View as WebtreesView; 2452bcc402SGreg Roachuse Fisharebest\Webtrees\Webtrees; 256ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 266ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 27*9b93b7c3SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 286ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 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) { 408b67c11aSGreg Roach return Application::getInstance(); 418b67c11aSGreg Roach } 42e364afe4SGreg Roach 43e364afe4SGreg Roach return Application::getInstance()->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{ 5652bcc402SGreg Roach if (Webtrees::STABILITY === '') { 5752bcc402SGreg Roach $version = Webtrees::VERSION; 5852bcc402SGreg Roach } else { 59f397d0fdSGreg Roach $version = filemtime(Webtrees::ROOT_DIR . 'public/' . $path); 6052bcc402SGreg Roach } 6152bcc402SGreg Roach 6252bcc402SGreg Roach return 'public/' . $path . '?v=' . $version; 6352bcc402SGreg Roach} 6452bcc402SGreg Roach 6552bcc402SGreg Roach/** 66f97c7170SGreg Roach * Generate a CSRF token form field. 67f97c7170SGreg Roach * 68f97c7170SGreg Roach * @return string 69f97c7170SGreg Roach */ 70c1010edaSGreg Roachfunction csrf_field() 71c1010edaSGreg Roach{ 726ccdf4f0SGreg Roach return '<input type="hidden" name="csrf" value="' . e(Session::getCsrfToken()) . '">'; 73f97c7170SGreg Roach} 74f97c7170SGreg Roach 75f97c7170SGreg Roach/** 768655ee66SGreg Roach * Get the CSRF token value. 778655ee66SGreg Roach * 788655ee66SGreg Roach * @return string 798655ee66SGreg Roach */ 80c1010edaSGreg Roachfunction csrf_token() 81c1010edaSGreg Roach{ 826ccdf4f0SGreg Roach return Session::getCsrfToken(); 836ccdf4f0SGreg Roach} 846ccdf4f0SGreg Roach 856ccdf4f0SGreg Roach/** 866ccdf4f0SGreg Roach * @param string $url 876ccdf4f0SGreg Roach * @param int $code 886ccdf4f0SGreg Roach * 896ccdf4f0SGreg Roach * @return ResponseInterface 906ccdf4f0SGreg Roach */ 916ccdf4f0SGreg Roachfunction redirect(string $url, $code = StatusCodeInterface::STATUS_FOUND): ResponseInterface 926ccdf4f0SGreg Roach{ 936ccdf4f0SGreg Roach /** @var ResponseFactoryInterface $response_factory */ 946ccdf4f0SGreg Roach $response_factory = app(ResponseFactoryInterface::class); 956ccdf4f0SGreg Roach 966ccdf4f0SGreg Roach return $response_factory 976ccdf4f0SGreg Roach ->createResponse($code) 986ccdf4f0SGreg Roach ->withHeader('Location', $url); 996ccdf4f0SGreg Roach} 1006ccdf4f0SGreg Roach 1016ccdf4f0SGreg Roach/** 1026ccdf4f0SGreg Roach * Create a response. 1036ccdf4f0SGreg Roach * 1046ccdf4f0SGreg Roach * @param mixed $content 1056ccdf4f0SGreg Roach * @param int $code 1066ccdf4f0SGreg Roach * @param string[] $headers 1076ccdf4f0SGreg Roach * 1086ccdf4f0SGreg Roach * @return ResponseInterface 1096ccdf4f0SGreg Roach */ 1106ccdf4f0SGreg Roachfunction response($content = '', $code = StatusCodeInterface::STATUS_OK, $headers = []): ResponseInterface 1116ccdf4f0SGreg Roach{ 1126ccdf4f0SGreg Roach if ($headers === []) { 1136ccdf4f0SGreg Roach if (is_string($content)) { 1146ccdf4f0SGreg Roach $headers = [ 1151b3d4731SGreg Roach 'Content-Type' => 'text/html; charset=utf-8', 1161b3d4731SGreg Roach 'Content-Length' => strlen($content), 1176ccdf4f0SGreg Roach ]; 1186ccdf4f0SGreg Roach } else { 1196ccdf4f0SGreg Roach $content = json_encode($content, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT); 1206ccdf4f0SGreg Roach $headers = [ 1211b3d4731SGreg Roach 'Content-Type' => 'application/json', 1221b3d4731SGreg Roach 'Content-Length' => strlen($content), 1236ccdf4f0SGreg Roach ]; 1246ccdf4f0SGreg Roach } 1256ccdf4f0SGreg Roach } 1266ccdf4f0SGreg Roach 1276ccdf4f0SGreg Roach /** @var ResponseFactoryInterface $response_factory */ 1286ccdf4f0SGreg Roach $response_factory = app(ResponseFactoryInterface::class); 1296ccdf4f0SGreg Roach 1306ccdf4f0SGreg Roach /** @var StreamFactoryInterface $stream_factory */ 1316ccdf4f0SGreg Roach $stream_factory = app(StreamFactoryInterface::class); 1326ccdf4f0SGreg Roach 1336ccdf4f0SGreg Roach $stream = $stream_factory->createStream($content); 1346ccdf4f0SGreg Roach 1356ccdf4f0SGreg Roach $response = $response_factory 1366ccdf4f0SGreg Roach ->createResponse($code) 1376ccdf4f0SGreg Roach ->withBody($stream); 1386ccdf4f0SGreg Roach 1396ccdf4f0SGreg Roach foreach ($headers as $key => $value) { 1406ccdf4f0SGreg Roach $response = $response->withHeader($key, $value); 1416ccdf4f0SGreg Roach } 1426ccdf4f0SGreg Roach 1436ccdf4f0SGreg Roach return $response; 1448655ee66SGreg Roach} 1458655ee66SGreg Roach 1468655ee66SGreg Roach/** 1471f3fb95cSGreg Roach * Generate a URL for a named route. 1481f3fb95cSGreg Roach * 1491f3fb95cSGreg Roach * @param string $route 1501f3fb95cSGreg Roach * @param array $parameters 151571e6fcaSGreg Roach * @param bool $absolute 1521f3fb95cSGreg Roach * 1531f3fb95cSGreg Roach * @return string 1541f3fb95cSGreg Roach */ 155c1010edaSGreg Roachfunction route(string $route, array $parameters = [], bool $absolute = true): string 156c1010edaSGreg Roach{ 1571f3fb95cSGreg Roach $parameters = ['route' => $route] + $parameters; 1581f3fb95cSGreg Roach 159571e6fcaSGreg Roach if ($absolute) { 160*9b93b7c3SGreg Roach $base_url = app(ServerRequestInterface::class)->getAttribute('base_url'); 161*9b93b7c3SGreg Roach 162*9b93b7c3SGreg Roach return Html::url($base_url . 'index.php', $parameters); 1631f3fb95cSGreg Roach } 164b2ce94c6SRico Sonntag 1656ccdf4f0SGreg Roach return Html::url('index.php', $parameters); 166571e6fcaSGreg Roach} 1678655ee66SGreg Roach 1688655ee66SGreg Roach/** 1698655ee66SGreg Roach * Cerate and render a view in a single operation. 1708655ee66SGreg Roach * 1718655ee66SGreg Roach * @param string $name 1728655ee66SGreg Roach * @param mixed[] $data 1738655ee66SGreg Roach * 1748655ee66SGreg Roach * @return string 1758655ee66SGreg Roach */ 176c1010edaSGreg Roachfunction view(string $name, array $data = []) 177c1010edaSGreg Roach{ 1788898179dSGreg Roach return WebtreesView::make($name, $data); 1798655ee66SGreg Roach} 180