xref: /webtrees/app/Helpers/functions.php (revision 4da968426af896ee6732892cfd7b463b4397abaf)
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\Session as WebtreesSession;
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;
29use Psr\Http\Message\StreamFactoryInterface;
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 Webtrees::container();
42    }
43
44    return Webtrees::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, int $code = StatusCodeInterface::STATUS_FOUND): ResponseInterface
97{
98    /** @var ResponseFactoryInterface $response_factory */
99    $response_factory = app(ResponseFactoryInterface::class);
100
101    return $response_factory
102        ->createResponse($code)
103        ->withHeader('Location', $url);
104}
105
106/**
107 * Create a response.
108 *
109 * @param mixed    $content
110 * @param int      $code
111 * @param string[] $headers
112 *
113 * @return ResponseInterface
114 */
115function response($content = '', int $code = StatusCodeInterface::STATUS_OK, array $headers = []): ResponseInterface
116{
117    if ($content === '' && $code === StatusCodeInterface::STATUS_OK) {
118        $code = StatusCodeInterface::STATUS_NO_CONTENT;
119    }
120
121    if ($headers === []) {
122        if (is_string($content)) {
123            $headers = [
124                'Content-Type'   => 'text/html; charset=UTF-8',
125                'Content-Length' => (string) strlen($content),
126            ];
127        } else {
128            $content = json_encode($content, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
129            $headers = [
130                'Content-Type'   => 'application/json',
131                'Content-Length' => (string) strlen($content),
132            ];
133        }
134    }
135
136    /** @var ResponseFactoryInterface $response_factory */
137    $response_factory = app(ResponseFactoryInterface::class);
138
139    /** @var StreamFactoryInterface $stream_factory */
140    $stream_factory = app(StreamFactoryInterface::class);
141
142    $stream = $stream_factory->createStream($content);
143
144    $response = $response_factory
145        ->createResponse($code)
146        ->withBody($stream);
147
148    foreach ($headers as $key => $value) {
149        $response = $response->withHeader($key, $value);
150    }
151
152    return $response;
153}
154
155/**
156 * Generate a URL for a named route.
157 *
158 * @param string       $route_name
159 * @param array<mixed> $parameters
160 *
161 * @return string
162 */
163function route(string $route_name, array $parameters = []): string
164{
165    $request          = app(ServerRequestInterface::class);
166    $base_url         = $request->getAttribute('base_url');
167    $router_container = app(RouterContainer::class);
168    $route            = $router_container->getMap()->getRoute($route_name);
169
170    // Generate the URL.
171    $url = $router_container->getGenerator()->generate($route_name, $parameters);
172
173    // Aura ignores parameters that are not tokens.  We need to add them as query parameters.
174    $parameters = array_filter($parameters, static function (string $key) use ($route): bool {
175        return !str_contains($route->path, '{' . $key . '}') && !str_contains($route->path, '{/' . $key . '}');
176    }, ARRAY_FILTER_USE_KEY);
177
178    if ($request->getAttribute('rewrite_urls') === '1') {
179        // Make the pretty URL absolute.
180        $base_path = parse_url($base_url, PHP_URL_PATH) ?? '';
181        $url = $base_url . substr($url, strlen($base_path));
182    } else {
183        // Turn the pretty URL into an ugly one.
184        $path       = parse_url($url, PHP_URL_PATH);
185        $parameters = ['route' => $path] + $parameters;
186        $url        = $base_url . '/index.php';
187    }
188
189    return Html::url($url, $parameters);
190}
191
192/**
193 * Create and render a view in a single operation.
194 *
195 * @param string       $name
196 * @param array<mixed> $data
197 *
198 * @return string
199 */
200function view(string $name, array $data = []): string
201{
202    return WebtreesView::make($name, $data);
203}
204