xref: /webtrees/app/Helpers/functions.php (revision b2ce94c6833c25934b40a7e6bc15985d50b5f42a)
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
181f3fb95cSGreg Roachuse Fisharebest\Webtrees\Html;
19a45f9889SGreg Roachuse Fisharebest\Webtrees\Session;
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 */
27c1010edaSGreg Roachfunction csrf_field()
28c1010edaSGreg Roach{
29a45f9889SGreg Roach    return '<input type="hidden" name="csrf" value="' . e(Session::getCsrfToken()) . '">';
30f97c7170SGreg Roach}
31f97c7170SGreg Roach
32f97c7170SGreg Roach/**
338655ee66SGreg Roach * Get the CSRF token value.
348655ee66SGreg Roach *
358655ee66SGreg Roach * @return string
368655ee66SGreg Roach */
37c1010edaSGreg Roachfunction csrf_token()
38c1010edaSGreg Roach{
39a45f9889SGreg Roach    return \Fisharebest\Webtrees\Session::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 */
49c1010edaSGreg Roachfunction e(string $text): string
50c1010edaSGreg Roach{
5157887794SGreg Roach    return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
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 */
63c1010edaSGreg Roachfunction route(string $route, array $parameters = [], bool $absolute = true): string
64c1010edaSGreg Roach{
651f3fb95cSGreg Roach    $parameters = ['route' => $route] + $parameters;
661f3fb95cSGreg Roach
67571e6fcaSGreg Roach    if ($absolute) {
68571e6fcaSGreg Roach        return Html::url(WT_BASE_URL . 'index.php', $parameters);
691f3fb95cSGreg Roach    }
70*b2ce94c6SRico Sonntag
71*b2ce94c6SRico Sonntag    return Html::url('index.php', $parameters);
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 */
82c1010edaSGreg Roachfunction view(string $name, array $data = [])
83c1010edaSGreg Roach{
848655ee66SGreg Roach    return View::make($name, $data);
858655ee66SGreg Roach}
86