xref: /webtrees/app/Helpers/functions.php (revision 8898179db9ee9ab76e7912468223d7b1fdcddfbc)
11f3fb95cSGreg Roach<?php
21f3fb95cSGreg Roach/**
31f3fb95cSGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 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 */
168b67c11aSGreg Roach
1778f07ab5SGreg Roachdeclare(strict_types=1);
181f3fb95cSGreg Roach
198b67c11aSGreg Roachuse Fisharebest\Webtrees\Application;
20*8898179dSGreg Roachuse Fisharebest\Webtrees\View as WebtreesView;
2152bcc402SGreg Roachuse Fisharebest\Webtrees\Webtrees;
228b67c11aSGreg Roach
238b67c11aSGreg Roach/**
248b67c11aSGreg Roach * Get the IoC container, or fetch something from it.
258b67c11aSGreg Roach *
268b67c11aSGreg Roach * @param string|null $abstract
278b67c11aSGreg Roach *
28cab242e7SGreg Roach * @return mixed
298b67c11aSGreg Roach */
308b67c11aSGreg Roachfunction app(string $abstract = null)
318b67c11aSGreg Roach{
328b67c11aSGreg Roach    if ($abstract === null) {
338b67c11aSGreg Roach        return Application::getInstance();
348b67c11aSGreg Roach    } else {
358b67c11aSGreg Roach        return Application::getInstance()->make($abstract);
368b67c11aSGreg Roach    }
378b67c11aSGreg Roach}
388b67c11aSGreg Roach
391f3fb95cSGreg Roach/**
4052bcc402SGreg Roach * Generate a URL to an asset file in the public folder.
4152bcc402SGreg Roach * Add a version parameter for cache-busting.
4252bcc402SGreg Roach *
4352bcc402SGreg Roach * @param string $path
4452bcc402SGreg Roach *
4552bcc402SGreg Roach * @return string
4652bcc402SGreg Roach */
4752bcc402SGreg Roachfunction asset(string $path): string
4852bcc402SGreg Roach{
4952bcc402SGreg Roach    if (Webtrees::STABILITY === '') {
5052bcc402SGreg Roach        $version = Webtrees::VERSION;
5152bcc402SGreg Roach    } else {
5252bcc402SGreg Roach        $version = filemtime(WT_ROOT . 'public/' . $path);
5352bcc402SGreg Roach    }
5452bcc402SGreg Roach
5552bcc402SGreg Roach    return 'public/' . $path . '?v=' . $version;
5652bcc402SGreg Roach}
5752bcc402SGreg Roach
5852bcc402SGreg Roach/**
59f97c7170SGreg Roach * Generate a CSRF token form field.
60f97c7170SGreg Roach *
61f97c7170SGreg Roach * @return string
62f97c7170SGreg Roach */
63c1010edaSGreg Roachfunction csrf_field()
64c1010edaSGreg Roach{
6530ea777aSGreg Roach    return '<input type="hidden" name="csrf" value="' . e(\Fisharebest\Webtrees\Session::getCsrfToken()) . '">';
66f97c7170SGreg Roach}
67f97c7170SGreg Roach
68f97c7170SGreg Roach/**
698655ee66SGreg Roach * Get the CSRF token value.
708655ee66SGreg Roach *
718655ee66SGreg Roach * @return string
728655ee66SGreg Roach */
73c1010edaSGreg Roachfunction csrf_token()
74c1010edaSGreg Roach{
75a45f9889SGreg Roach    return \Fisharebest\Webtrees\Session::getCsrfToken();
768655ee66SGreg Roach}
778655ee66SGreg Roach
788655ee66SGreg Roach/**
791f3fb95cSGreg Roach * Generate a URL for a named route.
801f3fb95cSGreg Roach *
811f3fb95cSGreg Roach * @param string $route
821f3fb95cSGreg Roach * @param array  $parameters
83571e6fcaSGreg Roach * @param bool   $absolute
841f3fb95cSGreg Roach *
851f3fb95cSGreg Roach * @return string
861f3fb95cSGreg Roach */
87c1010edaSGreg Roachfunction route(string $route, array $parameters = [], bool $absolute = true): string
88c1010edaSGreg Roach{
891f3fb95cSGreg Roach    $parameters = ['route' => $route] + $parameters;
901f3fb95cSGreg Roach
91571e6fcaSGreg Roach    if ($absolute) {
9230ea777aSGreg Roach        return \Fisharebest\Webtrees\Html::url(WT_BASE_URL . 'index.php', $parameters);
931f3fb95cSGreg Roach    }
94b2ce94c6SRico Sonntag
9530ea777aSGreg Roach    return \Fisharebest\Webtrees\Html::url('index.php', $parameters);
96571e6fcaSGreg Roach}
978655ee66SGreg Roach
988655ee66SGreg Roach/**
998655ee66SGreg Roach * Cerate and render a view in a single operation.
1008655ee66SGreg Roach *
1018655ee66SGreg Roach * @param string  $name
1028655ee66SGreg Roach * @param mixed[] $data
1038655ee66SGreg Roach *
1048655ee66SGreg Roach * @return string
1058655ee66SGreg Roach */
106c1010edaSGreg Roachfunction view(string $name, array $data = [])
107c1010edaSGreg Roach{
108*8898179dSGreg Roach    return WebtreesView::make($name, $data);
1098655ee66SGreg Roach}
110