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