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_trees = Tree::getAll(); 46 47 $tree = $all_trees[$request->get('ged')] ?? null; 48 49 // No tree specified/available? Choose one. 50 if ($tree === null && $method === 'GET') { 51 $tree = $all_trees[Site::getPreference('DEFAULT_GEDCOM')] ?? current($all_trees); 52 } 53 54 $request->attributes->set('tree', $tree); 55 $request->attributes->set('user', Auth::user()); 56 57 // Load the routing table. 58 $routes = require 'routes/web.php'; 59 60 // Find the action for the selected route 61 $controller_action = $routes[$method . ':' . $route] ?? 'ErrorController@noRouteFound'; 62 63 DebugBar::stopMeasure('routing'); 64 65 // Create the controller 66 DebugBar::startMeasure('create controller'); 67 68 list($controller_name, $action) = explode('@', $controller_action); 69 $controller_class = __NAMESPACE__ . '\\Http\\Controllers\\' . $controller_name; 70 $controller = new $controller_class; 71 72 DebugBar::stopMeasure('create controller'); 73 74 // Note that we can't stop this timer, as running the action will 75 // generate the response - which includes (and stops) the timer 76 DebugBar::startMeasure('controller_action', $controller_action); 77 78 $middleware_stack = []; 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, $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 (Throwable $ex) { 98 DebugBar::addThrowable($ex); 99 100 // Clear any buffered output. 101 while (ob_get_level() > 0) { 102 ob_end_clean(); 103 } 104 105 if ($ex instanceof HttpExceptionInterface) { 106 // Show a friendly page for expected exceptions. 107 if ($request->isXmlHttpRequest()) { 108 $response = new Response($ex->getMessage(), $ex->getStatusCode()); 109 } else { 110 $controller = new ErrorController; 111 $response = $controller->errorResponse($ex->getMessage()); 112 } 113 } else { 114 // Show an error page for unexpected exceptions. 115 if (getenv('DEBUG')) { 116 // Local dev environment? Show full debug. 117 $whoops = new Run; 118 $whoops->pushHandler(new PrettyPageHandler); 119 $whoops->handleException($ex); 120 } else { 121 // Running remotely? Show a friendly error page. 122 $controller = new ErrorController; 123 $response = $controller->unhandledExceptionResponse($request, $ex); 124 } 125 } 126} 127 128// Send response 129if ($response instanceof RedirectResponse) { 130 // Show the debug data on the next page 131 DebugBar::stackData(); 132} elseif ($response instanceof JsonResponse) { 133 // Use HTTP headers and some jQuery to add debug to the current page. 134 DebugBar::sendDataInHeaders(); 135} 136 137return $response->prepare($request)->send(); 138