xref: /webtrees/app/Helpers/functions.php (revision 56afe05fa72eb037740c3a05c02828a7ba30f9a9)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 Aura\Router\RouterContainer;
21use Fig\Http\Message\StatusCodeInterface;
22use Fisharebest\Webtrees\Html;
23use Fisharebest\Webtrees\Registry;
24use Fisharebest\Webtrees\Session as WebtreesSession;
25use Fisharebest\Webtrees\View as WebtreesView;
26use Fisharebest\Webtrees\Webtrees;
27use Illuminate\Container\Container;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30
31/**
32 * Get the IoC container, or fetch something from it.
33 *
34 * @param string|null $abstract
35 *
36 * @return mixed
37 */
38function app(string $abstract = null)
39{
40    if ($abstract === null) {
41        return Container::getInstance();
42    }
43
44    return Container::getInstance()->make($abstract);
45}
46
47/**
48 * Generate a URL to an asset file in the public folder.
49 * Add a version parameter for cache-busting.
50 *
51 * @param string $path
52 *
53 * @return string
54 */
55function asset(string $path): string
56{
57    if (substr($path, -1) === '/') {
58        $version = '';
59    } elseif (Webtrees::STABILITY === '') {
60        $version = '?v=' . Webtrees::VERSION;
61    } else {
62        $version = '?v=' . filemtime(Webtrees::ROOT_DIR . 'public/' . $path);
63    }
64
65    $base_url = app(ServerRequestInterface::class)->getAttribute('base_url');
66
67    return $base_url . '/public/' . $path . $version;
68}
69
70/**
71 * Generate a CSRF token form field.
72 *
73 * @return string
74 */
75function csrf_field(): string
76{
77    return '<input type="hidden" name="_csrf" value="' . e(WebtreesSession::getCsrfToken()) . '">';
78}
79
80/**
81 * Get the CSRF token value.
82 *
83 * @return string
84 */
85function csrf_token(): string
86{
87    return WebtreesSession::getCsrfToken();
88}
89
90/**
91 * @param string $url
92 * @param int    $code
93 *
94 * @return ResponseInterface
95 */
96function redirect(string $url, $code = StatusCodeInterface::STATUS_FOUND): ResponseInterface
97{
98    return Registry::responseFactory()
99        ->createResponse($code)
100        ->withHeader('Location', $url);
101}
102
103/**
104 * Create a response.
105 *
106 * @param mixed    $content
107 * @param int      $code
108 * @param string[] $headers
109 *
110 * @return ResponseInterface
111 */
112function response($content = '', $code = StatusCodeInterface::STATUS_OK, $headers = []): ResponseInterface
113{
114    if ($content === '' && $code === StatusCodeInterface::STATUS_OK) {
115        $code = StatusCodeInterface::STATUS_NO_CONTENT;
116    }
117
118    if ($headers === []) {
119        if (is_string($content)) {
120            $headers = [
121                'Content-Type'   => 'text/html; charset=UTF-8',
122                'Content-Length' => (string) strlen($content),
123            ];
124        } else {
125            $content = json_encode($content, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
126            $headers = [
127                'Content-Type'   => 'application/json',
128                'Content-Length' => (string) strlen($content),
129            ];
130        }
131    }
132
133    $stream = Registry::streamFactory()->createStream($content);
134
135    $response = Registry::responseFactory()
136        ->createResponse($code)
137        ->withBody($stream);
138
139    foreach ($headers as $key => $value) {
140        $response = $response->withHeader($key, $value);
141    }
142
143    return $response;
144}
145
146/**
147 * Generate a URL for a named route.
148 *
149 * @param string  $route_name
150 * @param mixed[] $parameters
151 *
152 * @return string
153 */
154function route(string $route_name, array $parameters = []): string
155{
156    $request          = app(ServerRequestInterface::class);
157    $base_url         = $request->getAttribute('base_url');
158    $router_container = app(RouterContainer::class);
159    $route            = $router_container->getMap()->getRoute($route_name);
160
161    // Generate the URL.
162    $url = $router_container->getGenerator()->generate($route_name, $parameters);
163
164    // Aura ignores parameters that are not tokens.  We need to add them as query parameters.
165    $parameters = array_filter($parameters, static function (string $key) use ($route): bool {
166        return !str_contains($route->path, '{' . $key . '}') && !str_contains($route->path, '{/' . $key . '}');
167    }, ARRAY_FILTER_USE_KEY);
168
169    if ($request->getAttribute('rewrite_urls') === '1') {
170        // Make the pretty URL absolute.
171        $base_path = parse_url($base_url, PHP_URL_PATH) ?? '';
172        $url = $base_url . substr($url, strlen($base_path));
173    } else {
174        // Turn the pretty URL into an ugly one.
175        $path       = parse_url($url, PHP_URL_PATH);
176        $parameters = ['route' => $path] + $parameters;
177        $url        = $base_url . '/index.php';
178    }
179
180    return Html::url($url, $parameters);
181}
182
183/**
184 * Cerate and render a view in a single operation.
185 *
186 * @param string  $name
187 * @param mixed[] $data
188 *
189 * @return string
190 */
191function view(string $name, array $data = []): string
192{
193    return WebtreesView::make($name, $data);
194}
195