xref: /webtrees/app/Helpers/functions.php (revision 9ed80f829cfb208ae92f7928345696619569bf4e)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17declare(strict_types=1);
18
19use Fig\Http\Message\StatusCodeInterface;
20use Fisharebest\Webtrees\Application;
21use Fisharebest\Webtrees\Html;
22use Fisharebest\Webtrees\Session;
23use Fisharebest\Webtrees\View as WebtreesView;
24use Fisharebest\Webtrees\Webtrees;
25use Psr\Http\Message\ResponseFactoryInterface;
26use Psr\Http\Message\ResponseInterface;
27use Psr\Http\Message\StreamFactoryInterface;
28
29/**
30 * Get the IoC container, or fetch something from it.
31 *
32 * @param string|null $abstract
33 *
34 * @return mixed
35 */
36function app(string $abstract = null)
37{
38    if ($abstract === null) {
39        return Application::getInstance();
40    }
41
42    return Application::getInstance()->make($abstract);
43}
44
45/**
46 * Generate a URL to an asset file in the public folder.
47 * Add a version parameter for cache-busting.
48 *
49 * @param string $path
50 *
51 * @return string
52 */
53function asset(string $path): string
54{
55    if (Webtrees::STABILITY === '') {
56        $version = Webtrees::VERSION;
57    } else {
58        $version = filemtime(Webtrees::ROOT_DIR . 'public/' . $path);
59    }
60
61    return 'public/' . $path . '?v=' . $version;
62}
63
64/**
65 * Generate a CSRF token form field.
66 *
67 * @return string
68 */
69function csrf_field()
70{
71    return '<input type="hidden" name="csrf" value="' . e(Session::getCsrfToken()) . '">';
72}
73
74/**
75 * Get the CSRF token value.
76 *
77 * @return string
78 */
79function csrf_token()
80{
81    return Session::getCsrfToken();
82}
83
84/**
85 * @param string $url
86 * @param int    $code
87 *
88 * @return ResponseInterface
89 */
90function redirect(string $url, $code = StatusCodeInterface::STATUS_FOUND): ResponseInterface
91{
92    /** @var ResponseFactoryInterface $response_factory */
93    $response_factory = app(ResponseFactoryInterface::class);
94
95    return $response_factory
96        ->createResponse($code)
97        ->withHeader('Location', $url);
98}
99
100/**
101 * Create a response.
102 *
103 * @param mixed    $content
104 * @param int      $code
105 * @param string[] $headers
106 *
107 * @return ResponseInterface
108 */
109function response($content = '', $code = StatusCodeInterface::STATUS_OK, $headers = []): ResponseInterface
110{
111    if ($headers === []) {
112        if (is_string($content)) {
113            $headers = [
114                'Content-Type'   => 'text/html; charset=utf-8',
115                'Content-Length' => strlen($content),
116            ];
117        } else {
118            $content = json_encode($content, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
119            $headers = [
120                'Content-Type'   => 'application/json',
121                'Content-Length' => strlen($content),
122            ];
123        }
124    }
125
126    /** @var ResponseFactoryInterface $response_factory */
127    $response_factory = app(ResponseFactoryInterface::class);
128
129    /** @var StreamFactoryInterface $stream_factory */
130    $stream_factory = app(StreamFactoryInterface::class);
131
132    $stream = $stream_factory->createStream($content);
133
134    $response = $response_factory
135        ->createResponse($code)
136        ->withBody($stream);
137
138    foreach ($headers as $key => $value) {
139        $response = $response->withHeader($key, $value);
140    }
141
142    return $response;
143}
144
145/**
146 * Generate a URL for a named route.
147 *
148 * @param string $route
149 * @param array  $parameters
150 * @param bool   $absolute
151 *
152 * @return string
153 */
154function route(string $route, array $parameters = [], bool $absolute = true): string
155{
156    $parameters = ['route' => $route] + $parameters;
157
158    if ($absolute) {
159        return Html::url(WT_BASE_URL . 'index.php', $parameters);
160    }
161
162    return Html::url('index.php', $parameters);
163}
164
165/**
166 * Cerate and render a view in a single operation.
167 *
168 * @param string  $name
169 * @param mixed[] $data
170 *
171 * @return string
172 */
173function view(string $name, array $data = [])
174{
175    return WebtreesView::make($name, $data);
176}
177