xref: /webtrees/app/Helpers/functions.php (revision 1062a1429914c995339f502856821457aa975a5a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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 */
16declare(strict_types=1);
17
18use Fisharebest\Webtrees\Filter;
19use Fisharebest\Webtrees\Html;
20use Fisharebest\Webtrees\View;
21
22/**
23 * Generate a CSRF token form field.
24 *
25 * @return string
26 */
27function csrf_field() {
28	return '<input type="hidden" name="csrf" value="' . e(Filter::getCsrfToken()) . '">';
29}
30
31/**
32 * Get the CSRF token value.
33 *
34 * @return string
35 */
36function csrf_token() {
37	return Filter::getCsrfToken();
38}
39
40/**
41 * Escape a string for inclusion within HTML.
42 *
43 * @param $text
44 *
45 * @return string
46 */
47function e(string $text): string {
48	return Html::escape($text);
49}
50
51/**
52 * Generate a URL for a named route.
53 *
54 * @param string $route
55 * @param array  $parameters
56 *
57 * @return string
58 */
59function route(string $route, array $parameters = []): string {
60	$parameters = ['route' => $route] + $parameters;
61
62	return Html::url('index.php', $parameters);
63}
64
65/**
66 * Cerate and render a view in a single operation.
67 *
68 * @param string  $name
69 * @param mixed[] $data
70 *
71 * @return string
72 */
73function view(string $name, array $data = []) {
74	return View::make($name, $data);
75}
76