xref: /webtrees/index.php (revision e4421e8051c23b38e86550bedee966e79c728183)
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 Fisharebest\Webtrees\Http\Controllers\ErrorController;
22use Fisharebest\Webtrees\Http\Middleware\CheckCsrf;
23use Fisharebest\Webtrees\Http\Middleware\UseTransaction;
24use Symfony\Component\HttpFoundation\JsonResponse;
25use Symfony\Component\HttpFoundation\RedirectResponse;
26use Symfony\Component\HttpFoundation\Request;
27use Symfony\Component\HttpFoundation\Response;
28use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
29use Throwable;
30use Whoops\Handler\PrettyPageHandler;
31use Whoops\Run;
32
33// Bootstrap the application
34require 'includes/session.php';
35
36DebugBar::startMeasure('routing');
37
38// The HTTP request.
39$request = Request::createFromGlobals();
40$method  = $request->getMethod();
41$route   = $request->get('route');
42
43try {
44	// Most requests will need the current tree and user.
45	$all_tree_names     = array_keys(Tree::getNameList());
46	$first_tree_name    = current($all_tree_names) ?? '';
47	$previous_tree_name = Session::get('GEDCOM', $first_tree_name);
48	$default_tree_name  = $previous_tree_name ?: Site::getPreference('DEFAULT_GEDCOM');
49	$tree_name          = $request->get('ged', $default_tree_name);
50	$tree               = Tree::findByName($tree_name);
51	Session::put('GEDCOM', $tree_name);
52
53	$request->attributes->set('tree', $tree);
54	$request->attributes->set('user', AUth::user());
55
56	// Load the routing table.
57	$routes = require 'routes/web.php';
58
59	// Find the action for the selected route
60	$controller_action = $routes[$method . ':' . $route] ?? 'ErrorController@noRouteFound';
61
62	DebugBar::stopMeasure('routing');
63
64	// Create the controller
65	DebugBar::startMeasure('create controller');
66
67	list($controller_name, $action) = explode('@', $controller_action);
68	$controller_class = __NAMESPACE__ . '\\Http\\Controllers\\' . $controller_name;
69	$controller = new $controller_class;
70
71	DebugBar::stopMeasure('create controller');
72
73	// Note that we can't stop this timer, as running the action will
74	// generate the response - which includes (and stops) the timer
75	DebugBar::startMeasure('controller_action', $controller_action);
76
77	$middleware_stack = [];
78
79	if ($method === 'POST') {
80		$middleware_stack[] = new UseTransaction;
81		$middleware_stack[] = new CheckCsrf;
82	}
83
84	// Apply the middleware using the "onion" pattern.
85	$pipeline = array_reduce($middleware_stack, function (Closure $next, $middleware): Closure {
86		// Create a closure to apply the middleware.
87		return function (Request $request) use ($middleware, $next): Response {
88			return $middleware->handle($request, $next);
89		};
90	}, function (Request $request) use ($controller, $action): Response {
91		// Create a closure to generate the response.
92		return call_user_func([$controller, $action], $request);
93	});
94
95	$response = call_user_func($pipeline, $request);
96} catch (Throwable $ex) {
97	DebugBar::addThrowable($ex);
98
99	// Clear any buffered output.
100	while (ob_get_level() > 0) {
101		ob_end_clean();
102	}
103
104	if ($ex instanceof HttpExceptionInterface) {
105		// Show a friendly page for expected exceptions.
106		if ($request->isXmlHttpRequest()) {
107			$response = new Response($ex->getMessage(), $ex->getStatusCode());
108		} else {
109			$controller = new ErrorController;
110			$response   = $controller->errorResponse($ex->getMessage());
111		}
112	} else {
113		// Show an error page for unexpected exceptions.
114		if (getenv('DEBUG')) {
115			// Local dev environment?  Show full debug.
116			$whoops = new Run;
117			$whoops->pushHandler(new PrettyPageHandler);
118			$whoops->handleException($ex);
119		} else {
120			// Running remotely?  Show a friendly error page.
121			$controller = new ErrorController;
122			$response   = $controller->unhandledExceptionResponse($request, $ex);
123		}
124	}
125}
126
127// Send response
128if ($response instanceof RedirectResponse) {
129	// Show the debug data on the next page
130	DebugBar::stackData();
131} elseif ($response instanceof JsonResponse) {
132	// Use HTTP headers and some jQuery to add debug to the current page.
133	DebugBar::sendDataInHeaders();
134}
135
136return $response->prepare($request)->send();
137