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] ?? null; 53 54// No access to this route? Go somewhere else. 55if ($controller_action === null) { 56 if ($tree instanceof Tree && $tree->getPreference('imported') === '1') { 57 $response = new RedirectResponse(route('tree-page', ['ged' => $tree->getName()])); 58 } elseif (!Auth::check()) { 59 // Probably need to log in to see this page? 60 $response = new RedirectResponse(Html::url('login.php', ['url' => $request->getRequestUri()])); 61 } elseif (Auth::isAdmin()) { 62 // No tree or tree not imported? 63 $response = new RedirectResponse(Html::url('admin_trees_manage.php', [])); 64 } 65 66 return $response->prepare($request)->send(); 67} 68 69DebugBar::stopMeasure('routing'); 70 71DebugBar::startMeasure('create controller'); 72 73// Create the controller 74list($controller_name, $action) = explode('@', $controller_action); 75$controller_class = __NAMESPACE__ . '\\Http\\Controllers\\' . $controller_name; 76$controller = new $controller_class; 77 78DebugBar::stopMeasure('create controller'); 79 80// Note that we can't stop this timer, as running the action will 81// generate the response - which includes (and stops) the timer 82DebugBar::startMeasure('controller_action', $controller_action); 83 84if ($method === 'POST' && Database::isConnected()) { 85 Database::beginTransaction(); 86 try { 87 /** @var Response $response */ 88 $response = $controller->$action($request); 89 Database::commit(); 90 } catch (Throwable $ex) { 91 DebugBar::addThrowable($ex); 92 93 Database::rollBack(); 94 95 // Yikes! Something went badly wrong. 96 throw $ex; 97 } 98} else { 99 /** @var Response $response */ 100 $response = $controller->$action($request); 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 112return $response->prepare($request)->send(); 113