xref: /webtrees/app/Helpers/functions.php (revision c1010eda29c0909ed4d5d463f32d32bfefdd4dfe)
11f3fb95cSGreg Roach<?php
21f3fb95cSGreg Roach/**
31f3fb95cSGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
51f3fb95cSGreg Roach * This program is free software: you can redistribute it and/or modify
61f3fb95cSGreg Roach * it under the terms of the GNU General Public License as published by
71f3fb95cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
81f3fb95cSGreg Roach * (at your option) any later version.
91f3fb95cSGreg Roach * This program is distributed in the hope that it will be useful,
101f3fb95cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
111f3fb95cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
121f3fb95cSGreg Roach * GNU General Public License for more details.
131f3fb95cSGreg Roach * You should have received a copy of the GNU General Public License
141f3fb95cSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
151f3fb95cSGreg Roach */
1678f07ab5SGreg Roachdeclare(strict_types=1);
171f3fb95cSGreg Roach
18f97c7170SGreg Roachuse Fisharebest\Webtrees\Filter;
191f3fb95cSGreg Roachuse Fisharebest\Webtrees\Html;
208655ee66SGreg Roachuse Fisharebest\Webtrees\View;
211f3fb95cSGreg Roach
221f3fb95cSGreg Roach/**
23f97c7170SGreg Roach * Generate a CSRF token form field.
24f97c7170SGreg Roach *
25f97c7170SGreg Roach * @return string
26f97c7170SGreg Roach */
27*c1010edaSGreg Roachfunction csrf_field()
28*c1010edaSGreg Roach{
29f97c7170SGreg Roach    return '<input type="hidden" name="csrf" value="' . e(Filter::getCsrfToken()) . '">';
30f97c7170SGreg Roach}
31f97c7170SGreg Roach
32f97c7170SGreg Roach/**
338655ee66SGreg Roach * Get the CSRF token value.
348655ee66SGreg Roach *
358655ee66SGreg Roach * @return string
368655ee66SGreg Roach */
37*c1010edaSGreg Roachfunction csrf_token()
38*c1010edaSGreg Roach{
398655ee66SGreg Roach    return Filter::getCsrfToken();
408655ee66SGreg Roach}
418655ee66SGreg Roach
428655ee66SGreg Roach/**
4378f07ab5SGreg Roach * Escape a string for inclusion within HTML.
4478f07ab5SGreg Roach *
4578f07ab5SGreg Roach * @param $text
4678f07ab5SGreg Roach *
4778f07ab5SGreg Roach * @return string
4878f07ab5SGreg Roach */
49*c1010edaSGreg Roachfunction e(string $text): string
50*c1010edaSGreg Roach{
516a3de7e5SGreg Roach    return Html::escape($text);
5278f07ab5SGreg Roach}
5378f07ab5SGreg Roach
5478f07ab5SGreg Roach/**
551f3fb95cSGreg Roach * Generate a URL for a named route.
561f3fb95cSGreg Roach *
571f3fb95cSGreg Roach * @param string $route
581f3fb95cSGreg Roach * @param array  $parameters
59571e6fcaSGreg Roach * @param bool   $absolute
601f3fb95cSGreg Roach *
611f3fb95cSGreg Roach * @return string
621f3fb95cSGreg Roach */
63*c1010edaSGreg Roachfunction route(string $route, array $parameters = [], bool $absolute = true): string
64*c1010edaSGreg Roach{
651f3fb95cSGreg Roach    $parameters = ['route' => $route] + $parameters;
661f3fb95cSGreg Roach
67571e6fcaSGreg Roach    if ($absolute) {
68571e6fcaSGreg Roach        return Html::url(WT_BASE_URL . 'index.php', $parameters);
69571e6fcaSGreg Roach    } else {
701f3fb95cSGreg Roach        return Html::url('index.php', $parameters);
711f3fb95cSGreg Roach    }
72571e6fcaSGreg Roach}
738655ee66SGreg Roach
748655ee66SGreg Roach/**
758655ee66SGreg Roach * Cerate and render a view in a single operation.
768655ee66SGreg Roach *
778655ee66SGreg Roach * @param string  $name
788655ee66SGreg Roach * @param mixed[] $data
798655ee66SGreg Roach *
808655ee66SGreg Roach * @return string
818655ee66SGreg Roach */
82*c1010edaSGreg Roachfunction view(string $name, array $data = [])
83*c1010edaSGreg Roach{
848655ee66SGreg Roach    return View::make($name, $data);
858655ee66SGreg Roach}
86