xref: /webtrees/app/Helpers/functions.php (revision d35568b467207589ea9059739da0bf1f7e785a0d)
11f3fb95cSGreg Roach<?php
23976b470SGreg Roach
31f3fb95cSGreg Roach/**
41f3fb95cSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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/**
3152bcc402SGreg Roach * Generate a URL to an asset file in the public folder.
3252bcc402SGreg Roach * Add a version parameter for cache-busting.
3352bcc402SGreg Roach *
3452bcc402SGreg Roach * @param string $path
3552bcc402SGreg Roach *
3652bcc402SGreg Roach * @return string
3752bcc402SGreg Roach */
3852bcc402SGreg Roachfunction asset(string $path): string
3952bcc402SGreg Roach{
405181bce3SGreg Roach    if (str_ends_with($path, '/')) {
4175e7614aSGreg Roach        $version = '';
4275e7614aSGreg Roach    } elseif (Webtrees::STABILITY === '') {
4375e7614aSGreg Roach        $version = '?v=' . Webtrees::VERSION;
4452bcc402SGreg Roach    } else {
4575e7614aSGreg Roach        $version = '?v=' . filemtime(Webtrees::ROOT_DIR . 'public/' . $path);
4652bcc402SGreg Roach    }
4752bcc402SGreg Roach
48*d35568b4SGreg Roach    $request  = Registry::container()->get(ServerRequestInterface::class);
49b55cbc6bSGreg Roach    $base_url = Validator::attributes($request)->string('base_url');
50bb1b9730SGreg Roach
51bb1b9730SGreg Roach    return $base_url . '/public/' . $path . $version;
5252bcc402SGreg Roach}
5352bcc402SGreg Roach
5452bcc402SGreg Roach/**
55f97c7170SGreg Roach * Generate a CSRF token form field.
56f97c7170SGreg Roach *
57f97c7170SGreg Roach * @return string
58f97c7170SGreg Roach */
5924f2a3afSGreg Roachfunction csrf_field(): string
60c1010edaSGreg Roach{
61e240f5e1SGreg Roach    return '<input type="hidden" name="_csrf" value="' . e(WebtreesSession::getCsrfToken()) . '">';
62f97c7170SGreg Roach}
63f97c7170SGreg Roach
64f97c7170SGreg Roach/**
658655ee66SGreg Roach * Get the CSRF token value.
668655ee66SGreg Roach *
678655ee66SGreg Roach * @return string
688655ee66SGreg Roach */
6924f2a3afSGreg Roachfunction csrf_token(): string
70c1010edaSGreg Roach{
71a6656bb5SGreg Roach    return WebtreesSession::getCsrfToken();
726ccdf4f0SGreg Roach}
736ccdf4f0SGreg Roach
746ccdf4f0SGreg Roach/**
756ccdf4f0SGreg Roach * @param string $url
766ccdf4f0SGreg Roach * @param int    $code
776ccdf4f0SGreg Roach *
786ccdf4f0SGreg Roach * @return ResponseInterface
796ccdf4f0SGreg Roach */
80785274b8SGreg Roachfunction redirect(string $url, int $code = StatusCodeInterface::STATUS_FOUND): ResponseInterface
816ccdf4f0SGreg Roach{
82*d35568b4SGreg Roach    $response_factory = Registry::container()->get(ResponseFactoryInterface::class);
8300c45d23SGreg Roach
8400c45d23SGreg Roach    return $response_factory
856ccdf4f0SGreg Roach        ->createResponse($code)
866172e7f6SGreg Roach        ->withHeader('location', $url);
876ccdf4f0SGreg Roach}
886ccdf4f0SGreg Roach
896ccdf4f0SGreg Roach/**
906ccdf4f0SGreg Roach * Create a response.
916ccdf4f0SGreg Roach *
9245ebc6c6SGreg Roach * @param array<mixed>|object|string $content
936ccdf4f0SGreg Roach * @param int                        $code
9409482a55SGreg Roach * @param array<string>              $headers
956ccdf4f0SGreg Roach *
966ccdf4f0SGreg Roach * @return ResponseInterface
976ccdf4f0SGreg Roach */
9845ebc6c6SGreg Roachfunction response(array|object|string $content = '', int $code = StatusCodeInterface::STATUS_OK, array $headers = []): ResponseInterface
996ccdf4f0SGreg Roach{
100208909d8SGreg Roach    return Registry::responseFactory()->response($content, $code, $headers);
1018655ee66SGreg Roach}
1028655ee66SGreg Roach
1038655ee66SGreg Roach/**
1041f3fb95cSGreg Roach * Generate a URL for a named route.
1051f3fb95cSGreg Roach *
106ee4364daSGreg Roach * @param string                                    $route_name
10776d39c55SGreg Roach * @param array<bool|int|string|array<string>|null> $parameters
1081f3fb95cSGreg Roach *
1091f3fb95cSGreg Roach * @return string
1101f3fb95cSGreg Roach */
111ee4364daSGreg Roachfunction route(string $route_name, array $parameters = []): string
112c1010edaSGreg Roach{
113208909d8SGreg Roach    return Registry::routeFactory()->route($route_name, $parameters);
1141f3fb95cSGreg Roach}
115b2ce94c6SRico Sonntag
1168655ee66SGreg Roach/**
117785274b8SGreg Roach * Create and render a view in a single operation.
1188655ee66SGreg Roach *
1198655ee66SGreg Roach * @param string       $name
120785274b8SGreg Roach * @param array<mixed> $data
1218655ee66SGreg Roach *
1228655ee66SGreg Roach * @return string
1238655ee66SGreg Roach */
12424f2a3afSGreg Roachfunction view(string $name, array $data = []): string
125c1010edaSGreg Roach{
1268898179dSGreg Roach    return WebtreesView::make($name, $data);
1278655ee66SGreg Roach}
128