xref: /webtrees/app/Helpers/functions.php (revision 6ccdf4f0fd1b65a5d54259c969912382ce49629d)
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
19*6ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
208b67c11aSGreg Roachuse Fisharebest\Webtrees\Application;
21*6ccdf4f0SGreg Roachuse Fisharebest\Webtrees\Html;
22*6ccdf4f0SGreg Roachuse Fisharebest\Webtrees\Session;
238898179dSGreg Roachuse Fisharebest\Webtrees\View as WebtreesView;
2452bcc402SGreg Roachuse Fisharebest\Webtrees\Webtrees;
25*6ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseFactoryInterface;
26*6ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
27*6ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface;
288b67c11aSGreg Roach
298b67c11aSGreg Roach/**
308b67c11aSGreg Roach * Get the IoC container, or fetch something from it.
318b67c11aSGreg Roach *
328b67c11aSGreg Roach * @param string|null $abstract
338b67c11aSGreg Roach *
34cab242e7SGreg Roach * @return mixed
358b67c11aSGreg Roach */
368b67c11aSGreg Roachfunction app(string $abstract = null)
378b67c11aSGreg Roach{
388b67c11aSGreg Roach    if ($abstract === null) {
398b67c11aSGreg Roach        return Application::getInstance();
408b67c11aSGreg Roach    }
41e364afe4SGreg Roach
42e364afe4SGreg Roach    return Application::getInstance()->make($abstract);
438b67c11aSGreg Roach}
448b67c11aSGreg Roach
451f3fb95cSGreg Roach/**
4652bcc402SGreg Roach * Generate a URL to an asset file in the public folder.
4752bcc402SGreg Roach * Add a version parameter for cache-busting.
4852bcc402SGreg Roach *
4952bcc402SGreg Roach * @param string $path
5052bcc402SGreg Roach *
5152bcc402SGreg Roach * @return string
5252bcc402SGreg Roach */
5352bcc402SGreg Roachfunction asset(string $path): string
5452bcc402SGreg Roach{
5552bcc402SGreg Roach    if (Webtrees::STABILITY === '') {
5652bcc402SGreg Roach        $version = Webtrees::VERSION;
5752bcc402SGreg Roach    } else {
5852bcc402SGreg Roach        $version = filemtime(WT_ROOT . 'public/' . $path);
5952bcc402SGreg Roach    }
6052bcc402SGreg Roach
6152bcc402SGreg Roach    return 'public/' . $path . '?v=' . $version;
6252bcc402SGreg Roach}
6352bcc402SGreg Roach
6452bcc402SGreg Roach/**
65f97c7170SGreg Roach * Generate a CSRF token form field.
66f97c7170SGreg Roach *
67f97c7170SGreg Roach * @return string
68f97c7170SGreg Roach */
69c1010edaSGreg Roachfunction csrf_field()
70c1010edaSGreg Roach{
71*6ccdf4f0SGreg Roach    return '<input type="hidden" name="csrf" value="' . e(Session::getCsrfToken()) . '">';
72f97c7170SGreg Roach}
73f97c7170SGreg Roach
74f97c7170SGreg Roach/**
758655ee66SGreg Roach * Get the CSRF token value.
768655ee66SGreg Roach *
778655ee66SGreg Roach * @return string
788655ee66SGreg Roach */
79c1010edaSGreg Roachfunction csrf_token()
80c1010edaSGreg Roach{
81*6ccdf4f0SGreg Roach    return Session::getCsrfToken();
82*6ccdf4f0SGreg Roach}
83*6ccdf4f0SGreg Roach
84*6ccdf4f0SGreg Roach/**
85*6ccdf4f0SGreg Roach * @param string $url
86*6ccdf4f0SGreg Roach * @param int    $code
87*6ccdf4f0SGreg Roach *
88*6ccdf4f0SGreg Roach * @return ResponseInterface
89*6ccdf4f0SGreg Roach */
90*6ccdf4f0SGreg Roachfunction redirect(string $url, $code = StatusCodeInterface::STATUS_FOUND): ResponseInterface
91*6ccdf4f0SGreg Roach{
92*6ccdf4f0SGreg Roach    /** @var ResponseFactoryInterface $response_factory */
93*6ccdf4f0SGreg Roach    $response_factory = app(ResponseFactoryInterface::class);
94*6ccdf4f0SGreg Roach
95*6ccdf4f0SGreg Roach    return $response_factory
96*6ccdf4f0SGreg Roach        ->createResponse($code)
97*6ccdf4f0SGreg Roach        ->withHeader('Location', $url);
98*6ccdf4f0SGreg Roach}
99*6ccdf4f0SGreg Roach
100*6ccdf4f0SGreg Roach/**
101*6ccdf4f0SGreg Roach * Create a response.
102*6ccdf4f0SGreg Roach *
103*6ccdf4f0SGreg Roach * @param mixed    $content
104*6ccdf4f0SGreg Roach * @param int      $code
105*6ccdf4f0SGreg Roach * @param string[] $headers
106*6ccdf4f0SGreg Roach *
107*6ccdf4f0SGreg Roach * @return ResponseInterface
108*6ccdf4f0SGreg Roach */
109*6ccdf4f0SGreg Roachfunction response($content = '', $code = StatusCodeInterface::STATUS_OK, $headers = []): ResponseInterface
110*6ccdf4f0SGreg Roach{
111*6ccdf4f0SGreg Roach    if ($headers === []) {
112*6ccdf4f0SGreg Roach        if (is_string($content)) {
113*6ccdf4f0SGreg Roach            $headers = [
114*6ccdf4f0SGreg Roach                'Content-type'   => 'text/html; charset=utf-8',
115*6ccdf4f0SGreg Roach                'Content-length' => strlen($content),
116*6ccdf4f0SGreg Roach            ];
117*6ccdf4f0SGreg Roach        } else {
118*6ccdf4f0SGreg Roach            $content = json_encode($content, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
119*6ccdf4f0SGreg Roach            $headers = [
120*6ccdf4f0SGreg Roach                'Content-type'   => 'application/json',
121*6ccdf4f0SGreg Roach                'Content-length' => strlen($content),
122*6ccdf4f0SGreg Roach            ];
123*6ccdf4f0SGreg Roach        }
124*6ccdf4f0SGreg Roach    }
125*6ccdf4f0SGreg Roach
126*6ccdf4f0SGreg Roach    /** @var ResponseFactoryInterface $response_factory */
127*6ccdf4f0SGreg Roach    $response_factory = app(ResponseFactoryInterface::class);
128*6ccdf4f0SGreg Roach
129*6ccdf4f0SGreg Roach    /** @var StreamFactoryInterface $stream_factory */
130*6ccdf4f0SGreg Roach    $stream_factory = app(StreamFactoryInterface::class);
131*6ccdf4f0SGreg Roach
132*6ccdf4f0SGreg Roach    $stream = $stream_factory->createStream($content);
133*6ccdf4f0SGreg Roach
134*6ccdf4f0SGreg Roach    $response = $response_factory
135*6ccdf4f0SGreg Roach        ->createResponse($code)
136*6ccdf4f0SGreg Roach        ->withBody($stream);
137*6ccdf4f0SGreg Roach
138*6ccdf4f0SGreg Roach    foreach ($headers as $key => $value) {
139*6ccdf4f0SGreg Roach        $response = $response->withHeader($key, $value);
140*6ccdf4f0SGreg Roach    }
141*6ccdf4f0SGreg Roach
142*6ccdf4f0SGreg Roach    return $response;
1438655ee66SGreg Roach}
1448655ee66SGreg Roach
1458655ee66SGreg Roach/**
1461f3fb95cSGreg Roach * Generate a URL for a named route.
1471f3fb95cSGreg Roach *
1481f3fb95cSGreg Roach * @param string $route
1491f3fb95cSGreg Roach * @param array  $parameters
150571e6fcaSGreg Roach * @param bool   $absolute
1511f3fb95cSGreg Roach *
1521f3fb95cSGreg Roach * @return string
1531f3fb95cSGreg Roach */
154c1010edaSGreg Roachfunction route(string $route, array $parameters = [], bool $absolute = true): string
155c1010edaSGreg Roach{
1561f3fb95cSGreg Roach    $parameters = ['route' => $route] + $parameters;
1571f3fb95cSGreg Roach
158571e6fcaSGreg Roach    if ($absolute) {
159*6ccdf4f0SGreg Roach        return Html::url(WT_BASE_URL . 'index.php', $parameters);
1601f3fb95cSGreg Roach    }
161b2ce94c6SRico Sonntag
162*6ccdf4f0SGreg Roach    return Html::url('index.php', $parameters);
163571e6fcaSGreg Roach}
1648655ee66SGreg Roach
1658655ee66SGreg Roach/**
1668655ee66SGreg Roach * Cerate and render a view in a single operation.
1678655ee66SGreg Roach *
1688655ee66SGreg Roach * @param string  $name
1698655ee66SGreg Roach * @param mixed[] $data
1708655ee66SGreg Roach *
1718655ee66SGreg Roach * @return string
1728655ee66SGreg Roach */
173c1010edaSGreg Roachfunction view(string $name, array $data = [])
174c1010edaSGreg Roach{
1758898179dSGreg Roach    return WebtreesView::make($name, $data);
1768655ee66SGreg Roach}
177