xref: /webtrees/app/Helpers/functions.php (revision 24931b29a0237a5f5f1b8620af661ea530451af0)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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 Fig\Http\Message\StatusCodeInterface;
21use Fisharebest\Webtrees\Registry;
22use Fisharebest\Webtrees\Session as WebtreesSession;
23use Fisharebest\Webtrees\Validator;
24use Fisharebest\Webtrees\View as WebtreesView;
25use Fisharebest\Webtrees\Webtrees;
26use Psr\Http\Message\ResponseFactoryInterface;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29
30/**
31 * Generate a URL to an asset file in the public folder.
32 * Add a version parameter for cache-busting.
33 *
34 * @param string $path
35 *
36 * @return string
37 */
38function asset(string $path): string
39{
40    if (str_ends_with($path, '/')) {
41        $version = '';
42    } elseif (Webtrees::STABILITY === '') {
43        $version = '?v=' . Webtrees::VERSION;
44    } else {
45        $version = '?v=' . filemtime(Webtrees::ROOT_DIR . 'public/' . $path);
46    }
47
48    $request  = Registry::container()->get(ServerRequestInterface::class);
49    $base_url = Validator::attributes($request)->string('base_url');
50
51    return $base_url . '/public/' . $path . $version;
52}
53
54/**
55 * Generate a CSRF token form field.
56 *
57 * @return string
58 */
59function csrf_field(): string
60{
61    return '<input type="hidden" name="_csrf" value="' . e(WebtreesSession::getCsrfToken()) . '">';
62}
63
64/**
65 * Get the CSRF token value.
66 *
67 * @return string
68 */
69function csrf_token(): string
70{
71    return WebtreesSession::getCsrfToken();
72}
73
74/**
75 * @param string $url
76 * @param int    $code
77 *
78 * @return ResponseInterface
79 */
80function redirect(string $url, int $code = StatusCodeInterface::STATUS_FOUND): ResponseInterface
81{
82    $response_factory = Registry::container()->get(ResponseFactoryInterface::class);
83
84    return $response_factory
85        ->createResponse($code)
86        ->withHeader('location', $url);
87}
88
89/**
90 * Create a response.
91 *
92 * @param array<mixed>|object|string $content
93 * @param int                        $code
94 * @param array<string>              $headers
95 *
96 * @return ResponseInterface
97 */
98function response(array|object|string $content = '', int $code = StatusCodeInterface::STATUS_OK, array $headers = []): ResponseInterface
99{
100    return Registry::responseFactory()->response($content, $code, $headers);
101}
102
103/**
104 * Generate a URL for a named route.
105 *
106 * @param string                                    $route_name
107 * @param array<bool|int|string|array<string>|null> $parameters
108 *
109 * @return string
110 */
111function route(string $route_name, array $parameters = []): string
112{
113    return Registry::routeFactory()->route($route_name, $parameters);
114}
115
116/**
117 * Create and render a view in a single operation.
118 *
119 * @param string       $name
120 * @param array<mixed> $data
121 *
122 * @return string
123 */
124function view(string $name, array $data = []): string
125{
126    return WebtreesView::make($name, $data);
127}
128