xref: /webtrees/index.php (revision 319149d67195b9a7eba14cc17d3c8064b4dea7e7)
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
18namespace Fisharebest\Webtrees;
19
20use Closure;
21use Exception;
22use Fisharebest\Webtrees\Exceptions\Handler;
23use Fisharebest\Webtrees\Http\Middleware\CheckCsrf;
24use Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode;
25use Fisharebest\Webtrees\Http\Middleware\MiddlewareInterface;
26use Fisharebest\Webtrees\Http\Middleware\UseTransaction;
27use Symfony\Component\HttpFoundation\JsonResponse;
28use Symfony\Component\HttpFoundation\RedirectResponse;
29use Symfony\Component\HttpFoundation\Request;
30use Symfony\Component\HttpFoundation\Response;
31
32// Bootstrap the application
33require 'includes/session.php';
34
35DebugBar::startMeasure('routing');
36
37// The HTTP request.
38$request = Request::createFromGlobals();
39$method  = $request->getMethod();
40$route   = $request->get('route');
41
42try {
43	// Most requests will need the current tree and user.
44	$all_trees = Tree::getAll();
45
46	$tree = $all_trees[$request->get('ged')] ?? null;
47
48	// No tree specified/available?  Choose one.
49	if ($tree === null && $method === 'GET') {
50		$tree = $all_trees[Site::getPreference('DEFAULT_GEDCOM')] ?? array_values($all_trees)[0] ?? null;
51	}
52
53	$request->attributes->set('tree', $tree);
54	$request->attributes->set('user', Auth::user());
55
56	// Most layouts will require a tree for the page header/footer
57	View::share('tree', $tree);
58
59	// Load the routing table.
60	$routes = require 'routes/web.php';
61
62	// Find the action for the selected route
63	$controller_action = $routes[$method . ':' . $route] ?? 'ErrorController@noRouteFound';
64
65	// Create the controller
66	list($controller_name, $action) = explode('@', $controller_action);
67	$controller_class = __NAMESPACE__ . '\\Http\\Controllers\\' . $controller_name;
68	$controller       = new $controller_class;
69
70	DebugBar::stopMeasure('routing');
71
72	// Note that we can't stop this timer, as running the action will
73	// generate the response - which includes (and stops) the timer
74	DebugBar::startMeasure('controller_action', $controller_action);
75
76	$middleware_stack = [
77		new CheckForMaintenanceMode,
78	];
79
80	if ($method === 'POST') {
81		$middleware_stack[] = new UseTransaction;
82		$middleware_stack[] = new CheckCsrf;
83	}
84
85	// Apply the middleware using the "onion" pattern.
86	$pipeline = array_reduce($middleware_stack, function (Closure $next, MiddlewareInterface $middleware): Closure {
87		// Create a closure to apply the middleware.
88		return function (Request $request) use ($middleware, $next): Response {
89			return $middleware->handle($request, $next);
90		};
91	}, function (Request $request) use ($controller, $action): Response {
92		// Create a closure to generate the response.
93		return call_user_func([$controller, $action], $request);
94	});
95
96	$response = call_user_func($pipeline, $request);
97} catch (Exception $exception) {
98	DebugBar::addThrowable($exception);
99
100	$response = (new Handler)->render($request, $exception);
101}
102
103// Send response
104if ($response instanceof RedirectResponse) {
105	// Show the debug data on the next page
106	DebugBar::stackData();
107} elseif ($response instanceof JsonResponse) {
108	// Use HTTP headers and some jQuery to add debug to the current page.
109	DebugBar::sendDataInHeaders();
110}
111
112$response->prepare($request)->send();
113