1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 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 18use Fisharebest\Webtrees\Database; 19use Fisharebest\Webtrees\DebugBar; 20use Fisharebest\Webtrees\Exceptions\Handler; 21use Fisharebest\Webtrees\Http\Controllers\SetupController; 22use Fisharebest\Webtrees\Http\Middleware\BootModules; 23use Fisharebest\Webtrees\Http\Middleware\CheckCsrf; 24use Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode; 25use Fisharebest\Webtrees\Http\Middleware\DebugBarData; 26use Fisharebest\Webtrees\Http\Middleware\Housekeeping; 27use Fisharebest\Webtrees\Http\Middleware\MiddlewareInterface; 28use Fisharebest\Webtrees\Http\Middleware\SetPhpLimits; 29use Fisharebest\Webtrees\Http\Middleware\UseFilesystem; 30use Fisharebest\Webtrees\Http\Middleware\UseLocale; 31use Fisharebest\Webtrees\Http\Middleware\UseSession; 32use Fisharebest\Webtrees\Http\Middleware\UseTheme; 33use Fisharebest\Webtrees\Http\Middleware\UseTransaction; 34use Fisharebest\Webtrees\Http\Middleware\UseTree; 35use Fisharebest\Webtrees\Services\MigrationService; 36use Fisharebest\Webtrees\Services\ModuleService; 37use Fisharebest\Webtrees\Services\TimeoutService; 38use Fisharebest\Webtrees\Webtrees; 39use Illuminate\Cache\ArrayStore; 40use Illuminate\Cache\Repository; 41use Symfony\Component\HttpFoundation\Request; 42use Symfony\Component\HttpFoundation\Response; 43 44require __DIR__ . '/vendor/autoload.php'; 45 46const WT_ROOT = __DIR__ . DIRECTORY_SEPARATOR; 47 48Webtrees::init(); 49 50// Initialise the DebugBar for development. 51// Use `composer install --dev` on a development build to enable. 52// Note that you may need to increase the size of the fcgi buffers on nginx. 53// e.g. add these lines to your fastcgi_params file: 54// fastcgi_buffers 16 16m; 55// fastcgi_buffer_size 32m; 56DebugBar::init(class_exists('\\DebugBar\\StandardDebugBar')); 57 58// Use an array cache for database calls, etc. 59app()->instance('cache.array', new Repository(new ArrayStore())); 60 61// Start the timer. 62app()->instance(TimeoutService::class, new TimeoutService(microtime(true))); 63 64// Extract the request parameters. 65$request = Request::createFromGlobals(); 66app()->instance(Request::class, $request); 67 68// Calculate the base URL, so we can generate absolute URLs. 69$request_uri = $request->getSchemeAndHttpHost() . $request->getRequestUri(); 70 71// Remove any PHP script name and parameters. 72$base_uri = preg_replace('/[^\/]+\.php(\?.*)?$/', '', $request_uri); 73define('WT_BASE_URL', $base_uri); 74 75try { 76 // No config file? Run the setup wizard 77 if (!file_exists(Webtrees::CONFIG_FILE)) { 78 define('WT_DATA_DIR', 'data/'); 79 80 /** @var SetupController $controller */ 81 $controller = app(SetupController::class); 82 $response = $controller->setup($request); 83 $response->prepare($request)->send(); 84 85 return; 86 } 87 88 $database_config = parse_ini_file(Webtrees::CONFIG_FILE); 89 90 if ($database_config === false) { 91 throw new Exception('Invalid config file: ' . Webtrees::CONFIG_FILE); 92 } 93 94 // Read the connection settings and create the database 95 Database::connect($database_config); 96 97 // Update the database schema, if necessary. 98 app(MigrationService::class)->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 99 100 $middleware_stack = [ 101 CheckForMaintenanceMode::class, 102 SetPhpLimits::class, 103 UseFilesystem::class, 104 UseSession::class, 105 UseTree::class, 106 UseLocale::class, 107 UseTheme::class, 108 BootModules::class, 109 ]; 110 111 if (class_exists(DebugBar::class)) { 112 $middleware_stack[] = DebugBarData::class; 113 } 114 115 if ($request->getMethod() === Request::METHOD_GET) { 116 $middleware_stack[] = Housekeeping::class; 117 } 118 119 if ($request->getMethod() === Request::METHOD_POST) { 120 $middleware_stack[] = UseTransaction::class; 121 $middleware_stack[] = CheckCsrf::class; 122 } 123 124 // Allow modules to provide middleware. 125 foreach (app(ModuleService::class)->findByInterface(MiddlewareInterface::class) as $middleware) { 126 $middleware_stack[] = $middleware; 127 } 128 129 // We build the "onion" from the inside outwards, and some middlewares are dependant on others. 130 $middleware_stack = array_reverse($middleware_stack); 131 132 // Create the middleware *after* loading the modules, to give modules the opportunity to replace middleware. 133 $middleware_stack = array_map(function ($middleware): MiddlewareInterface { 134 return $middleware instanceof MiddlewareInterface ? $middleware : app($middleware); 135 }, $middleware_stack); 136 137 // Apply the middleware using the "onion" pattern. 138 $pipeline = array_reduce($middleware_stack, function (Closure $next, MiddlewareInterface $middleware): Closure { 139 // Create a closure to apply the middleware. 140 return function (Request $request) use ($middleware, $next): Response { 141 return $middleware->handle($request, $next); 142 }; 143 }, function (Request $request): Response { 144 // Load the route and routing table. 145 $route = $request->get('route'); 146 $routes = require 'routes/web.php'; 147 148 // Find the controller and action for the selected route 149 $controller_action = $routes[$request->getMethod() . ':' . $route] ?? 'ErrorController@noRouteFound'; 150 [$controller_name, $action] = explode('@', $controller_action); 151 $controller_class = '\\Fisharebest\\Webtrees\\Http\\Controllers\\' . $controller_name; 152 153 $controller = app($controller_class); 154 155 return app()->dispatch($controller, $action); 156 }); 157 158 $response = call_user_func($pipeline, $request); 159} catch (\Throwable $exception) { 160 $response = (new Handler())->render($request, $exception); 161} 162 163// Send response 164$response->prepare($request)->send(); 165