xref: /webtrees/app/Helpers/functions.php (revision 12c79f746ea1903d66aea1fe0412eda6149a5532)
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\Html;
19use Fisharebest\Webtrees\Session;
20use Fisharebest\Webtrees\View;
21
22/**
23 * Generate a CSRF token form field.
24 *
25 * @return string
26 */
27function csrf_field()
28{
29    return '<input type="hidden" name="csrf" value="' . e(Session::getCsrfToken()) . '">';
30}
31
32/**
33 * Get the CSRF token value.
34 *
35 * @return string
36 */
37function csrf_token()
38{
39    return \Fisharebest\Webtrees\Session::getCsrfToken();
40}
41
42/**
43 * Escape a string for inclusion within HTML.
44 *
45 * @param $text
46 *
47 * @return string
48 */
49function e(string $text): string
50{
51    return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
52}
53
54/**
55 * Generate a URL for a named route.
56 *
57 * @param string $route
58 * @param array  $parameters
59 * @param bool   $absolute
60 *
61 * @return string
62 */
63function route(string $route, array $parameters = [], bool $absolute = true): string
64{
65    $parameters = ['route' => $route] + $parameters;
66
67    if ($absolute) {
68        return Html::url(WT_BASE_URL . 'index.php', $parameters);
69    } else {
70        return Html::url('index.php', $parameters);
71    }
72}
73
74/**
75 * Cerate and render a view in a single operation.
76 *
77 * @param string  $name
78 * @param mixed[] $data
79 *
80 * @return string
81 */
82function view(string $name, array $data = [])
83{
84    return View::make($name, $data);
85}
86