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('routing'); 30 31// The HTTP request. 32$request = Request::createFromGlobals(); 33$method = $request->getMethod(); 34$route = $request->get('route'); 35 36// Most requests will need the current tree and user. 37$all_tree_names = array_keys(Tree::getNameList()); 38$first_tree_name = current($all_tree_names) ?? ''; 39$previous_tree_name = Session::get('GEDCOM', $first_tree_name); 40$default_tree_name = $previous_tree_name ?: Site::getPreference('DEFAULT_GEDCOM'); 41$tree_name = $request->get('ged', $default_tree_name); 42$tree = Tree::findByName($tree_name); 43Session::put('GEDCOM', $tree_name); 44 45$request->attributes->set('tree', $tree); 46$request->attributes->set('user', AUth::user()); 47 48// Load the routing table. 49$routes = require 'routes/web.php'; 50 51// Find the action for the selected route 52$controller_action = $routes[$method . ':' . $route] ?? 'ErrorController@noRouteFound'; 53 54DebugBar::stopMeasure('routing'); 55 56DebugBar::startMeasure('create controller'); 57 58// Create the controller 59list($controller_name, $action) = explode('@', $controller_action); 60$controller_class = __NAMESPACE__ . '\\Http\\Controllers\\' . $controller_name; 61$controller = new $controller_class; 62 63DebugBar::stopMeasure('create controller'); 64 65// Note that we can't stop this timer, as running the action will 66// generate the response - which includes (and stops) the timer 67DebugBar::startMeasure('controller_action', $controller_action); 68 69if ($method === 'POST' && Database::isConnected()) { 70 Database::beginTransaction(); 71 try { 72 /** @var Response $response */ 73 $response = $controller->$action($request); 74 Database::commit(); 75 } catch (Throwable $ex) { 76 DebugBar::addThrowable($ex); 77 78 Database::rollBack(); 79 80 // Yikes! Something went badly wrong. 81 throw $ex; 82 } 83} else { 84 /** @var Response $response */ 85 $response = $controller->$action($request); 86} 87 88// Send response 89if ($response instanceof RedirectResponse) { 90 // Show the debug data on the next page 91 DebugBar::stackData(); 92} elseif ($response instanceof JsonResponse) { 93 // Use HTTP headers and some jQuery to add debug to the current page. 94 DebugBar::sendDataInHeaders(); 95} 96 97return $response->prepare($request)->send(); 98