xref: /webtrees/app/Helpers/functions.php (revision 82e92bfae343d705cca07385d9e2dc90a3d85621)
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 Fig\Http\Message\StatusCodeInterface;
21use Fisharebest\Webtrees\Application;
22use Fisharebest\Webtrees\Html;
23use Fisharebest\Webtrees\Session;
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 Application::getInstance();
42    }
43
44    return Application::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()
76{
77    return '<input type="hidden" name="csrf" value="' . e(Session::getCsrfToken()) . '">';
78}
79
80/**
81 * Get the CSRF token value.
82 *
83 * @return string
84 */
85function csrf_token()
86{
87    return Session::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    /** @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 = '', $code = StatusCodeInterface::STATUS_OK, $headers = []): ResponseInterface
116{
117    if ($headers === []) {
118        if (is_string($content)) {
119            $headers = [
120                'Content-Type'   => 'text/html; charset=utf-8',
121                'Content-Length' => strlen($content),
122            ];
123        } else {
124            $content = json_encode($content, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
125            $headers = [
126                'Content-Type'   => 'application/json',
127                'Content-Length' => strlen($content),
128            ];
129        }
130    }
131
132    /** @var ResponseFactoryInterface $response_factory */
133    $response_factory = app(ResponseFactoryInterface::class);
134
135    /** @var StreamFactoryInterface $stream_factory */
136    $stream_factory = app(StreamFactoryInterface::class);
137
138    $stream = $stream_factory->createStream($content);
139
140    $response = $response_factory
141        ->createResponse($code)
142        ->withBody($stream);
143
144    foreach ($headers as $key => $value) {
145        $response = $response->withHeader($key, $value);
146    }
147
148    return $response;
149}
150
151/**
152 * Generate a URL for a named route.
153 *
154 * @param string $route
155 * @param array  $parameters
156 *
157 * @return string
158 */
159function route(string $route, array $parameters = []): string
160{
161    $parameters = ['route' => $route] + $parameters;
162
163    $base_url = app(ServerRequestInterface::class)->getAttribute('base_url');
164
165    return Html::url($base_url . 'index.php', $parameters);
166}
167
168/**
169 * Cerate and render a view in a single operation.
170 *
171 * @param string  $name
172 * @param mixed[] $data
173 *
174 * @return string
175 */
176function view(string $name, array $data = [])
177{
178    return WebtreesView::make($name, $data);
179}
180