xref: /webtrees/app/Helpers/functions.php (revision 9f2390a04226d0058d1862402c80d50fe6e79aa1)
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 * @param bool   $absolute
57 *
58 * @return string
59 */
60function route(string $route, array $parameters = [], bool $absolute = true): string {
61	$parameters = ['route' => $route] + $parameters;
62
63	if ($absolute) {
64		return Html::url(WT_BASE_URL . 'index.php', $parameters);
65	} else {
66		return Html::url('index.php', $parameters);
67	}
68}
69
70/**
71 * Cerate and render a view in a single operation.
72 *
73 * @param string  $name
74 * @param mixed[] $data
75 *
76 * @return string
77 */
78function view(string $name, array $data = []) {
79	return View::make($name, $data);
80}
81