xref: /webtrees/app/Helpers/functions.php (revision d11be7027e34e3121be11cc025421873364403f9)
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 * Get the IoC container, or fetch something from it.
32 *
33 * @param string|null $abstract
34 *
35 * @return mixed
36 */
37function app(string $abstract = null)
38{
39    if ($abstract === null) {
40        return Webtrees::container();
41    }
42
43    return Webtrees::make($abstract);
44}
45
46/**
47 * Generate a URL to an asset file in the public folder.
48 * Add a version parameter for cache-busting.
49 *
50 * @param string $path
51 *
52 * @return string
53 */
54function asset(string $path): string
55{
56    if (str_ends_with($path, '/')) {
57        $version = '';
58    } elseif (Webtrees::STABILITY === '') {
59        $version = '?v=' . Webtrees::VERSION;
60    } else {
61        $version = '?v=' . filemtime(Webtrees::ROOT_DIR . 'public/' . $path);
62    }
63
64    $request = app(ServerRequestInterface::class);
65    assert($request instanceof ServerRequestInterface);
66
67    $base_url = Validator::attributes($request)->string('base_url');
68
69    return $base_url . '/public/' . $path . $version;
70}
71
72/**
73 * Generate a CSRF token form field.
74 *
75 * @return string
76 */
77function csrf_field(): string
78{
79    return '<input type="hidden" name="_csrf" value="' . e(WebtreesSession::getCsrfToken()) . '">';
80}
81
82/**
83 * Get the CSRF token value.
84 *
85 * @return string
86 */
87function csrf_token(): string
88{
89    return WebtreesSession::getCsrfToken();
90}
91
92/**
93 * @param string $url
94 * @param int    $code
95 *
96 * @return ResponseInterface
97 */
98function redirect(string $url, int $code = StatusCodeInterface::STATUS_FOUND): ResponseInterface
99{
100    /** @var ResponseFactoryInterface $response_factory */
101    $response_factory = app(ResponseFactoryInterface::class);
102
103    return $response_factory
104        ->createResponse($code)
105        ->withHeader('location', $url);
106}
107
108/**
109 * Create a response.
110 *
111 * @param array<mixed>|object|string $content
112 * @param int                        $code
113 * @param array<string>              $headers
114 *
115 * @return ResponseInterface
116 */
117function response(array|object|string $content = '', int $code = StatusCodeInterface::STATUS_OK, array $headers = []): ResponseInterface
118{
119    return Registry::responseFactory()->response($content, $code, $headers);
120}
121
122/**
123 * Generate a URL for a named route.
124 *
125 * @param string                                    $route_name
126 * @param array<bool|int|string|array<string>|null> $parameters
127 *
128 * @return string
129 */
130function route(string $route_name, array $parameters = []): string
131{
132    return Registry::routeFactory()->route($route_name, $parameters);
133}
134
135/**
136 * Create and render a view in a single operation.
137 *
138 * @param string       $name
139 * @param array<mixed> $data
140 *
141 * @return string
142 */
143function view(string $name, array $data = []): string
144{
145    return WebtreesView::make($name, $data);
146}
147