xref: /webtrees/app/Helpers/functions.php (revision b55cbc6b43247e8b2ad14af6f6d24dc6747195ff)
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;
23a6656bb5SGreg Roachuse Fisharebest\Webtrees\Session as WebtreesSession;
24*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
258898179dSGreg Roachuse Fisharebest\Webtrees\View as WebtreesView;
2652bcc402SGreg Roachuse Fisharebest\Webtrees\Webtrees;
2700c45d23SGreg Roachuse Psr\Http\Message\ResponseFactoryInterface;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
299b93b7c3SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3000c45d23SGreg 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) {
42785274b8SGreg Roach        return Webtrees::container();
438b67c11aSGreg Roach    }
44e364afe4SGreg Roach
45785274b8SGreg Roach    return Webtrees::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
66*b55cbc6bSGreg Roach    $request = app(ServerRequestInterface::class);
67*b55cbc6bSGreg Roach    assert($request instanceof ServerRequestInterface);
68*b55cbc6bSGreg Roach
69*b55cbc6bSGreg Roach    $base_url = Validator::attributes($request)->string('base_url');
70bb1b9730SGreg Roach
71bb1b9730SGreg Roach    return $base_url . '/public/' . $path . $version;
7252bcc402SGreg Roach}
7352bcc402SGreg Roach
7452bcc402SGreg Roach/**
75f97c7170SGreg Roach * Generate a CSRF token form field.
76f97c7170SGreg Roach *
77f97c7170SGreg Roach * @return string
78f97c7170SGreg Roach */
7924f2a3afSGreg Roachfunction csrf_field(): string
80c1010edaSGreg Roach{
81e240f5e1SGreg Roach    return '<input type="hidden" name="_csrf" value="' . e(WebtreesSession::getCsrfToken()) . '">';
82f97c7170SGreg Roach}
83f97c7170SGreg Roach
84f97c7170SGreg Roach/**
858655ee66SGreg Roach * Get the CSRF token value.
868655ee66SGreg Roach *
878655ee66SGreg Roach * @return string
888655ee66SGreg Roach */
8924f2a3afSGreg Roachfunction csrf_token(): string
90c1010edaSGreg Roach{
91a6656bb5SGreg Roach    return WebtreesSession::getCsrfToken();
926ccdf4f0SGreg Roach}
936ccdf4f0SGreg Roach
946ccdf4f0SGreg Roach/**
956ccdf4f0SGreg Roach * @param string $url
966ccdf4f0SGreg Roach * @param int    $code
976ccdf4f0SGreg Roach *
986ccdf4f0SGreg Roach * @return ResponseInterface
996ccdf4f0SGreg Roach */
100785274b8SGreg Roachfunction redirect(string $url, int $code = StatusCodeInterface::STATUS_FOUND): ResponseInterface
1016ccdf4f0SGreg Roach{
10200c45d23SGreg Roach    /** @var ResponseFactoryInterface $response_factory */
10300c45d23SGreg Roach    $response_factory = app(ResponseFactoryInterface::class);
10400c45d23SGreg Roach
10500c45d23SGreg Roach    return $response_factory
1066ccdf4f0SGreg Roach        ->createResponse($code)
1076ccdf4f0SGreg Roach        ->withHeader('Location', $url);
1086ccdf4f0SGreg Roach}
1096ccdf4f0SGreg Roach
1106ccdf4f0SGreg Roach/**
1116ccdf4f0SGreg Roach * Create a response.
1126ccdf4f0SGreg Roach *
1136ccdf4f0SGreg Roach * @param mixed         $content
1146ccdf4f0SGreg Roach * @param int           $code
11509482a55SGreg Roach * @param array<string> $headers
1166ccdf4f0SGreg Roach *
1176ccdf4f0SGreg Roach * @return ResponseInterface
1186ccdf4f0SGreg Roach */
119785274b8SGreg Roachfunction response($content = '', int $code = StatusCodeInterface::STATUS_OK, array $headers = []): ResponseInterface
1206ccdf4f0SGreg Roach{
1211d1f373cSGreg Roach    if ($content === '' && $code === StatusCodeInterface::STATUS_OK) {
1221d1f373cSGreg Roach        $code = StatusCodeInterface::STATUS_NO_CONTENT;
1231d1f373cSGreg Roach    }
1241d1f373cSGreg Roach
1256ccdf4f0SGreg Roach    if ($headers === []) {
1266ccdf4f0SGreg Roach        if (is_string($content)) {
1276ccdf4f0SGreg Roach            $headers = [
128a0e7c429SGreg Roach                'Content-Type' => 'text/html; charset=UTF-8',
1296ccdf4f0SGreg Roach            ];
1306ccdf4f0SGreg Roach        } else {
13108b5db2aSGreg Roach            $content = json_encode($content, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
1326ccdf4f0SGreg Roach            $headers = [
1331b3d4731SGreg Roach                'Content-Type' => 'application/json',
1346ccdf4f0SGreg Roach            ];
1356ccdf4f0SGreg Roach        }
1366ccdf4f0SGreg Roach    }
1376ccdf4f0SGreg Roach
13800c45d23SGreg Roach    /** @var ResponseFactoryInterface $response_factory */
13900c45d23SGreg Roach    $response_factory = app(ResponseFactoryInterface::class);
1406ccdf4f0SGreg Roach
14100c45d23SGreg Roach    /** @var StreamFactoryInterface $stream_factory */
14200c45d23SGreg Roach    $stream_factory = app(StreamFactoryInterface::class);
14300c45d23SGreg Roach
14400c45d23SGreg Roach    $stream = $stream_factory->createStream($content);
14500c45d23SGreg Roach
14600c45d23SGreg Roach    $response = $response_factory
1476ccdf4f0SGreg Roach        ->createResponse($code)
1486ccdf4f0SGreg Roach        ->withBody($stream);
1496ccdf4f0SGreg Roach
1506ccdf4f0SGreg Roach    foreach ($headers as $key => $value) {
1516ccdf4f0SGreg Roach        $response = $response->withHeader($key, $value);
1526ccdf4f0SGreg Roach    }
1536ccdf4f0SGreg Roach
1546ccdf4f0SGreg Roach    return $response;
1558655ee66SGreg Roach}
1568655ee66SGreg Roach
1578655ee66SGreg Roach/**
1581f3fb95cSGreg Roach * Generate a URL for a named route.
1591f3fb95cSGreg Roach *
160ee4364daSGreg Roach * @param string                                    $route_name
16176d39c55SGreg Roach * @param array<bool|int|string|array<string>|null> $parameters
1621f3fb95cSGreg Roach *
1631f3fb95cSGreg Roach * @return string
1641f3fb95cSGreg Roach */
165ee4364daSGreg Roachfunction route(string $route_name, array $parameters = []): string
166c1010edaSGreg Roach{
167ee4364daSGreg Roach    $request = app(ServerRequestInterface::class);
168*b55cbc6bSGreg Roach    assert($request instanceof ServerRequestInterface);
169*b55cbc6bSGreg Roach
170*b55cbc6bSGreg Roach    $base_url         = Validator::attributes($request)->string('base_url');
171ee4364daSGreg Roach    $router_container = app(RouterContainer::class);
172ee4364daSGreg Roach    $route            = $router_container->getMap()->getRoute($route_name);
1731f3fb95cSGreg Roach
174ee4364daSGreg Roach    // Generate the URL.
1753a0b88a6SGreg Roach    $url = $router_container->getGenerator()->generate($route_name, $parameters);
1769b93b7c3SGreg Roach
177ee4364daSGreg Roach    // Aura ignores parameters that are not tokens.  We need to add them as query parameters.
1783cfcc809SGreg Roach    $parameters = array_filter($parameters, static function (string $key) use ($route): bool {
179dec352c1SGreg Roach        return !str_contains($route->path, '{' . $key . '}') && !str_contains($route->path, '{/' . $key . '}');
180ee4364daSGreg Roach    }, ARRAY_FILTER_USE_KEY);
181ee4364daSGreg Roach
182*b55cbc6bSGreg Roach    if (Validator::attributes($request)->boolean('rewrite_urls', false)) {
18322de662bSGreg Roach        // Make the pretty URL absolute.
1848435e0e5SGreg Roach        $base_path = parse_url($base_url, PHP_URL_PATH) ?? '';
1858435e0e5SGreg Roach        $url = $base_url . substr($url, strlen($base_path));
18622de662bSGreg Roach    } else {
187ee4364daSGreg Roach        // Turn the pretty URL into an ugly one.
188ee4364daSGreg Roach        $path       = parse_url($url, PHP_URL_PATH);
189ee4364daSGreg Roach        $parameters = ['route' => $path] + $parameters;
19098116b24SGreg Roach        $url        = $base_url . '/index.php';
191ee4364daSGreg Roach    }
192ee4364daSGreg Roach
19398116b24SGreg Roach    return Html::url($url, $parameters);
1941f3fb95cSGreg Roach}
195b2ce94c6SRico Sonntag
1968655ee66SGreg Roach/**
197785274b8SGreg Roach * Create and render a view in a single operation.
1988655ee66SGreg Roach *
1998655ee66SGreg Roach * @param string       $name
200785274b8SGreg Roach * @param array<mixed> $data
2018655ee66SGreg Roach *
2028655ee66SGreg Roach * @return string
2038655ee66SGreg Roach */
20424f2a3afSGreg Roachfunction view(string $name, array $data = []): string
205c1010edaSGreg Roach{
2068898179dSGreg Roach    return WebtreesView::make($name, $data);
2078655ee66SGreg Roach}
208