xref: /webtrees/index.php (revision 89d880da80f202b08b9fe837f38a87b48c6d2a97)
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\UseFilesystem;
29use Fisharebest\Webtrees\Http\Middleware\UseLocale;
30use Fisharebest\Webtrees\Http\Middleware\UseSession;
31use Fisharebest\Webtrees\Http\Middleware\UseTheme;
32use Fisharebest\Webtrees\Http\Middleware\UseTransaction;
33use Fisharebest\Webtrees\Http\Middleware\UseTree;
34use Fisharebest\Webtrees\Services\MigrationService;
35use Fisharebest\Webtrees\Services\ModuleService;
36use Fisharebest\Webtrees\Services\TimeoutService;
37use Fisharebest\Webtrees\Webtrees;
38use Illuminate\Cache\ArrayStore;
39use Illuminate\Cache\Repository;
40use Illuminate\Support\Collection;
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    // Read the connection settings and create the database
91    Database::connect($database_config);
92
93    // Update the database schema, if necessary.
94    app(MigrationService::class)->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION);
95
96    // Middleware allows code to intercept the request before it reaches the controller, and to
97    // intercept the response afterwards.
98    //
99    //                   +----------------------------------+
100    //                   |           Middleware1            |
101    //                   | +------------------------------+ |
102    //                   | |         Middleware2          | |
103    //                   | | +--------------------------+ | |
104    //                   | | |                          | | |
105    //       Request ----|-|-|-> Controller::action() --|-|-|---> Response
106    //                   | | |                          | | |
107    //                   | | +--------------------------+ | |
108    //                   | |                              | |
109    //                   | +------------------------------+ |
110    //                   |                                  |
111    //                   +----------------------------------+
112
113    // Create the middleware, from the "inside" to the "outside".
114    /** @var Collection $middleware_stack */
115    $middleware_stack = app(ModuleService::class)
116        ->findByInterface(MiddlewareInterface::class);
117
118    // Core middleware.
119    $middleware_stack = $middleware_stack->merge([
120        CheckCsrf::class,
121        UseTransaction::class,
122        Housekeeping::class,
123        DebugBarData::class,
124        BootModules::class,
125        UseTheme::class,
126        UseLocale::class,
127        UseTree::class,
128        UseSession::class,
129        UseFilesystem::class,
130        CheckForMaintenanceMode::class,
131    ]);
132
133    // Construct the core middleware *after* loading the modules, to reduce dependencies.
134    $middleware_stack = $middleware_stack->map(function ($middleware): MiddlewareInterface {
135        return $middleware instanceof MiddlewareInterface ? $middleware : app($middleware);
136    });
137
138    // Create a pipeline, which applies the middleware as a nested function call.
139    $pipeline = $middleware_stack->reduce(function (Closure $next, MiddlewareInterface $middleware): Closure {
140        // Create a closure to apply the middleware.
141        return function (Request $request) use ($middleware, $next): Response {
142            return $middleware->handle($request, $next);
143        };
144    }, function (Request $request): Response {
145        // Load the route and routing table.
146        $route  = $request->get('route');
147        $routes = require 'routes/web.php';
148
149        // Find the controller and action for the selected route
150        $controller_action = $routes[$request->getMethod() . ':' . $route] ?? 'ErrorController@noRouteFound';
151        [$controller_name, $action] = explode('@', $controller_action);
152        $controller_class = '\\Fisharebest\\Webtrees\\Http\\Controllers\\' . $controller_name;
153
154        $controller = app($controller_class);
155
156        return app()->dispatch($controller, $action);
157    });
158
159    $response = $pipeline($request);
160} catch (Throwable $exception) {
161    $response = (new Handler())->render($request, $exception);
162}
163
164// Send response
165$response->prepare($request)->send();
166