xref: /webtrees/index.php (revision bd52fa32338513b5d44dc6b1b89178c1bbf60b6a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 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 Symfony\Component\HttpFoundation\JsonResponse;
21use Symfony\Component\HttpFoundation\RedirectResponse;
22use Symfony\Component\HttpFoundation\Request;
23use Symfony\Component\HttpFoundation\Response;
24use Throwable;
25
26// Bootstrap the application
27require 'includes/session.php';
28
29DebugBar::startMeasure('request');
30
31// The HTTP request.
32$request = Request::createFromGlobals();
33$method  = $request->getMethod();
34$route   = $request->get('route');
35
36DebugBar::stopMeasure('request');
37
38DebugBar::startMeasure('find tree/user');
39
40// Most requests will need the current tree and user.
41$all_tree_names     = array_keys(Tree::getNameList());
42$first_tree_name    = current($all_tree_names) ?? '';
43$previous_tree_name = Session::get('GEDCOM', $first_tree_name);
44$default_tree_name  = $previous_tree_name ?: Site::getPreference('DEFAULT_GEDCOM');
45$tree_name          = $request->get('ged', $default_tree_name);
46$tree               = Tree::findByName($tree_name);
47Session::put('GEDCOM', $tree_name);
48
49$request->attributes->set('tree', $tree);
50$request->attributes->set('user', AUth::user());
51
52DebugBar::stopMeasure('find tree/user');
53
54DebugBar::startMeasure('routing');
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] ?? null;
61
62// No access to this route?  Do something else.
63if ($controller_action === null) {
64	if (!Auth::check()) {
65	}
66}
67
68DebugBar::stopMeasure('routing');
69
70DebugBar::startMeasure('create controller');
71
72// Create the controller
73list($controller_name, $action) = explode('@', $controller_action);
74$controller_class = __NAMESPACE__ . '\\Controller\\' . $controller_name;
75$controller = new $controller_class;
76
77DebugBar::stopMeasure('create controller');
78
79// Note that we can't stop this timer, as running the action will
80// generate the response - which includes (and stops) the timer
81DebugBar::startMeasure('controller_action', $controller_action);
82
83if ($method === 'POST' && Database::isConnected()) {
84	Database::beginTransaction();
85	try {
86		/** @var Response $response */
87		$response = $controller->$action($request);
88		Database::commit();
89	} catch (Throwable $ex) {
90		DebugBar::addThrowable($ex);
91
92		Database::rollBack();
93
94		// Yikes!  Something went badly wrong.
95		throw $ex;
96	}
97} else {
98	/** @var Response $response */
99	$response = $controller->$action($request);
100}
101
102// Send response
103if ($response instanceof RedirectResponse) {
104	// Show the debug data on the next page
105	DebugBar::stackData();
106} elseif ($response instanceof JsonResponse) {
107	// Use HTTP headers and some jQuery to add debug to the current page.
108	DebugBar::sendDataInHeaders();
109}
110
111return $response->prepare($request)->send();
112