11f3fb95cSGreg Roach<?php 23976b470SGreg Roach 31f3fb95cSGreg Roach/** 41f3fb95cSGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 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 151f3fb95cSGreg Roach * along with this program. If not, see <http://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; 228b67c11aSGreg Roachuse Fisharebest\Webtrees\Application; 236ccdf4f0SGreg Roachuse Fisharebest\Webtrees\Html; 24*a6656bb5SGreg Roachuse Fisharebest\Webtrees\Session as WebtreesSession; 258898179dSGreg Roachuse Fisharebest\Webtrees\View as WebtreesView; 2652bcc402SGreg Roachuse Fisharebest\Webtrees\Webtrees; 276ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 286ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 299b93b7c3SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 306ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 318b67c11aSGreg Roach 328b67c11aSGreg Roach/** 338b67c11aSGreg Roach * Get the IoC container, or fetch something from it. 348b67c11aSGreg Roach * 358b67c11aSGreg Roach * @param string|null $abstract 368b67c11aSGreg Roach * 37cab242e7SGreg Roach * @return mixed 388b67c11aSGreg Roach */ 398b67c11aSGreg Roachfunction app(string $abstract = null) 408b67c11aSGreg Roach{ 418b67c11aSGreg Roach if ($abstract === null) { 428b67c11aSGreg Roach return Application::getInstance(); 438b67c11aSGreg Roach } 44e364afe4SGreg Roach 45e364afe4SGreg Roach return Application::getInstance()->make($abstract); 468b67c11aSGreg Roach} 478b67c11aSGreg Roach 481f3fb95cSGreg Roach/** 4952bcc402SGreg Roach * Generate a URL to an asset file in the public folder. 5052bcc402SGreg Roach * Add a version parameter for cache-busting. 5152bcc402SGreg Roach * 5252bcc402SGreg Roach * @param string $path 5352bcc402SGreg Roach * 5452bcc402SGreg Roach * @return string 5552bcc402SGreg Roach */ 5652bcc402SGreg Roachfunction asset(string $path): string 5752bcc402SGreg Roach{ 5875e7614aSGreg Roach if (substr($path, -1) === '/') { 5975e7614aSGreg Roach $version = ''; 6075e7614aSGreg Roach } elseif (Webtrees::STABILITY === '') { 6175e7614aSGreg Roach $version = '?v=' . Webtrees::VERSION; 6252bcc402SGreg Roach } else { 6375e7614aSGreg Roach $version = '?v=' . filemtime(Webtrees::ROOT_DIR . 'public/' . $path); 6452bcc402SGreg Roach } 6552bcc402SGreg Roach 66bb1b9730SGreg Roach $base_url = app(ServerRequestInterface::class)->getAttribute('base_url'); 67bb1b9730SGreg Roach 68bb1b9730SGreg Roach return $base_url . '/public/' . $path . $version; 6952bcc402SGreg Roach} 7052bcc402SGreg Roach 7152bcc402SGreg Roach/** 72f97c7170SGreg Roach * Generate a CSRF token form field. 73f97c7170SGreg Roach * 74f97c7170SGreg Roach * @return string 75f97c7170SGreg Roach */ 76c1010edaSGreg Roachfunction csrf_field() 77c1010edaSGreg Roach{ 78*a6656bb5SGreg Roach return '<input type="hidden" name="csrf" value="' . e(WebtreesSession::getCsrfToken()) . '">'; 79f97c7170SGreg Roach} 80f97c7170SGreg Roach 81f97c7170SGreg Roach/** 828655ee66SGreg Roach * Get the CSRF token value. 838655ee66SGreg Roach * 848655ee66SGreg Roach * @return string 858655ee66SGreg Roach */ 86c1010edaSGreg Roachfunction csrf_token() 87c1010edaSGreg Roach{ 88*a6656bb5SGreg Roach return WebtreesSession::getCsrfToken(); 896ccdf4f0SGreg Roach} 906ccdf4f0SGreg Roach 916ccdf4f0SGreg Roach/** 926ccdf4f0SGreg Roach * @param string $url 936ccdf4f0SGreg Roach * @param int $code 946ccdf4f0SGreg Roach * 956ccdf4f0SGreg Roach * @return ResponseInterface 966ccdf4f0SGreg Roach */ 976ccdf4f0SGreg Roachfunction redirect(string $url, $code = StatusCodeInterface::STATUS_FOUND): ResponseInterface 986ccdf4f0SGreg Roach{ 996ccdf4f0SGreg Roach /** @var ResponseFactoryInterface $response_factory */ 1006ccdf4f0SGreg Roach $response_factory = app(ResponseFactoryInterface::class); 1016ccdf4f0SGreg Roach 1026ccdf4f0SGreg Roach return $response_factory 1036ccdf4f0SGreg Roach ->createResponse($code) 1046ccdf4f0SGreg Roach ->withHeader('Location', $url); 1056ccdf4f0SGreg Roach} 1066ccdf4f0SGreg Roach 1076ccdf4f0SGreg Roach/** 1086ccdf4f0SGreg Roach * Create a response. 1096ccdf4f0SGreg Roach * 1106ccdf4f0SGreg Roach * @param mixed $content 1116ccdf4f0SGreg Roach * @param int $code 1126ccdf4f0SGreg Roach * @param string[] $headers 1136ccdf4f0SGreg Roach * 1146ccdf4f0SGreg Roach * @return ResponseInterface 1156ccdf4f0SGreg Roach */ 1166ccdf4f0SGreg Roachfunction response($content = '', $code = StatusCodeInterface::STATUS_OK, $headers = []): ResponseInterface 1176ccdf4f0SGreg Roach{ 1181d1f373cSGreg Roach if ($content === '' && $code === StatusCodeInterface::STATUS_OK) { 1191d1f373cSGreg Roach $code = StatusCodeInterface::STATUS_NO_CONTENT; 1201d1f373cSGreg Roach } 1211d1f373cSGreg Roach 1226ccdf4f0SGreg Roach if ($headers === []) { 1236ccdf4f0SGreg Roach if (is_string($content)) { 1246ccdf4f0SGreg Roach $headers = [ 1251b3d4731SGreg Roach 'Content-Type' => 'text/html; charset=utf-8', 1261b3d4731SGreg Roach 'Content-Length' => strlen($content), 1276ccdf4f0SGreg Roach ]; 1286ccdf4f0SGreg Roach } else { 1296ccdf4f0SGreg Roach $content = json_encode($content, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT); 1306ccdf4f0SGreg Roach $headers = [ 1311b3d4731SGreg Roach 'Content-Type' => 'application/json', 1321b3d4731SGreg Roach 'Content-Length' => strlen($content), 1336ccdf4f0SGreg Roach ]; 1346ccdf4f0SGreg Roach } 1356ccdf4f0SGreg Roach } 1366ccdf4f0SGreg Roach 1376ccdf4f0SGreg Roach /** @var ResponseFactoryInterface $response_factory */ 1386ccdf4f0SGreg Roach $response_factory = app(ResponseFactoryInterface::class); 1396ccdf4f0SGreg Roach 1406ccdf4f0SGreg Roach /** @var StreamFactoryInterface $stream_factory */ 1416ccdf4f0SGreg Roach $stream_factory = app(StreamFactoryInterface::class); 1426ccdf4f0SGreg Roach 1436ccdf4f0SGreg Roach $stream = $stream_factory->createStream($content); 1446ccdf4f0SGreg Roach 1456ccdf4f0SGreg Roach $response = $response_factory 1466ccdf4f0SGreg Roach ->createResponse($code) 1476ccdf4f0SGreg Roach ->withBody($stream); 1486ccdf4f0SGreg Roach 1496ccdf4f0SGreg Roach foreach ($headers as $key => $value) { 1506ccdf4f0SGreg Roach $response = $response->withHeader($key, $value); 1516ccdf4f0SGreg Roach } 1526ccdf4f0SGreg Roach 1536ccdf4f0SGreg Roach return $response; 1548655ee66SGreg Roach} 1558655ee66SGreg Roach 1568655ee66SGreg Roach/** 1571f3fb95cSGreg Roach * Generate a URL for a named route. 1581f3fb95cSGreg Roach * 159ee4364daSGreg Roach * @param string $route_name 16059597b37SGreg Roach * @param mixed[] $parameters 1611f3fb95cSGreg Roach * 1621f3fb95cSGreg Roach * @return string 1631f3fb95cSGreg Roach */ 164ee4364daSGreg Roachfunction route(string $route_name, array $parameters = []): string 165c1010edaSGreg Roach{ 166ee4364daSGreg Roach $request = app(ServerRequestInterface::class); 16722de662bSGreg Roach $base_url = $request->getAttribute('base_url'); 168ee4364daSGreg Roach $router_container = app(RouterContainer::class); 169ee4364daSGreg Roach $route = $router_container->getMap()->getRoute($route_name); 1701f3fb95cSGreg Roach 171ee4364daSGreg Roach // Generate the URL. 1723a0b88a6SGreg Roach $url = $router_container->getGenerator()->generate($route_name, $parameters); 1739b93b7c3SGreg Roach 174ee4364daSGreg Roach // Aura ignores parameters that are not tokens. We need to add them as query parameters. 1753cfcc809SGreg Roach $parameters = array_filter($parameters, static function (string $key) use ($route): bool { 176ee4364daSGreg Roach return strpos($route->path, '{' . $key . '}') === false && strpos($route->path, '{/' . $key . '}') === false; 177ee4364daSGreg Roach }, ARRAY_FILTER_USE_KEY); 178ee4364daSGreg Roach 17922de662bSGreg Roach if ($request->getAttribute('rewrite_urls') === '1') { 18022de662bSGreg Roach // Make the pretty URL absolute. 1818435e0e5SGreg Roach $base_path = parse_url($base_url, PHP_URL_PATH) ?? ''; 1828435e0e5SGreg Roach $url = $base_url . substr($url, strlen($base_path)); 18322de662bSGreg Roach } else { 184ee4364daSGreg Roach // Turn the pretty URL into an ugly one. 185ee4364daSGreg Roach $path = parse_url($url, PHP_URL_PATH); 186ee4364daSGreg Roach $parameters = ['route' => $path] + $parameters; 18798116b24SGreg Roach $url = $base_url . '/index.php'; 188ee4364daSGreg Roach } 189ee4364daSGreg Roach 19098116b24SGreg Roach return Html::url($url, $parameters); 1911f3fb95cSGreg Roach} 192b2ce94c6SRico Sonntag 1938655ee66SGreg Roach/** 1948655ee66SGreg Roach * Cerate and render a view in a single operation. 1958655ee66SGreg Roach * 1968655ee66SGreg Roach * @param string $name 1978655ee66SGreg Roach * @param mixed[] $data 1988655ee66SGreg Roach * 1998655ee66SGreg Roach * @return string 2008655ee66SGreg Roach */ 201c1010edaSGreg Roachfunction view(string $name, array $data = []) 202c1010edaSGreg Roach{ 2038898179dSGreg Roach return WebtreesView::make($name, $data); 2048655ee66SGreg Roach} 205