xref: /webtrees/index.php (revision 6ccdf4f0fd1b65a5d54259c969912382ce49629d)
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\DebugBar;
19use Fisharebest\Webtrees\Http\Middleware\BootModules;
20use Fisharebest\Webtrees\Http\Middleware\CheckCsrf;
21use Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode;
22use Fisharebest\Webtrees\Http\Middleware\DebugBarData;
23use Fisharebest\Webtrees\Http\Middleware\ExceptionHandler;
24use Fisharebest\Webtrees\Http\Middleware\Housekeeping;
25use Fisharebest\Webtrees\Http\Middleware\ModuleMiddleware;
26use Fisharebest\Webtrees\Http\Middleware\RequestRouter;
27use Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema;
28use Fisharebest\Webtrees\Http\Middleware\UseCache;
29use Fisharebest\Webtrees\Http\Middleware\UseDatabase;
30use Fisharebest\Webtrees\Http\Middleware\UseFilesystem;
31use Fisharebest\Webtrees\Http\Middleware\UseLocale;
32use Fisharebest\Webtrees\Http\Middleware\UseSession;
33use Fisharebest\Webtrees\Http\Middleware\UseTheme;
34use Fisharebest\Webtrees\Http\Middleware\UseTransaction;
35use Fisharebest\Webtrees\Http\Middleware\UseTree;
36use Fisharebest\Webtrees\Http\Request as SymfonyRequest;
37use Fisharebest\Webtrees\Webtrees;
38use Middleland\Dispatcher;
39use Narrowspark\HttpEmitter\SapiEmitter;
40use Nyholm\Psr7\Factory\Psr17Factory;
41use Nyholm\Psr7Server\ServerRequestCreator;
42use Psr\Http\Message\ResponseFactoryInterface;
43use Psr\Http\Message\ServerRequestFactoryInterface;
44use Psr\Http\Message\StreamFactoryInterface;
45use Psr\Http\Message\UploadedFileFactoryInterface;
46use Psr\Http\Message\UriFactoryInterface;
47
48require __DIR__ . '/vendor/autoload.php';
49
50const WT_ROOT = __DIR__ . DIRECTORY_SEPARATOR;
51
52Webtrees::init();
53
54// Use nyholm/psr7 for our PSR7 messages and PSR17 factory.
55app()->bind(ResponseFactoryInterface::class, Psr17Factory::class);
56app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class);
57app()->bind(StreamFactoryInterface::class, Psr17Factory::class);
58app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class);
59app()->bind(UriFactoryInterface::class, Psr17Factory::class);
60
61// Use nyholm/psr7-server to create a request from the PHP environment.
62$server_request_creator = new ServerRequestCreator(
63    app(ServerRequestFactoryInterface::class),
64    app(UriFactoryInterface::class),
65    app(UploadedFileFactoryInterface::class),
66    app(StreamFactoryInterface::class)
67);
68
69// Create a PSR-7 request
70$request = $server_request_creator->fromGlobals();
71
72// Initialise the DebugBar for development.
73// Use `composer install --dev` on a development build to enable.
74// Note that you may need to increase the size of the fcgi buffers on nginx.
75// e.g. add these lines to your fastcgi_params file:
76// fastcgi_buffers 16 16m;
77// fastcgi_buffer_size 32m;
78DebugBar::init(class_exists('\\DebugBar\\StandardDebugBar'));
79
80// Until all the code is rewritten to use PSR-7 requests, we still need our hybrid request.
81$request = SymfonyRequest::createFromGlobals();
82
83// Calculate the base URL, so we can generate absolute URLs.
84// Remove any PHP script name and parameters.
85define('WT_BASE_URL', preg_replace('/[^\/]+\.php(\?.*)?$/', '', $request->getUri()));
86
87$middleware = [
88    ExceptionHandler::class,
89    CheckForMaintenanceMode::class,
90    UseDatabase::class,
91    DebugBarData::class,
92    UpdateDatabaseSchema::class,
93    UseCache::class,
94    UseFilesystem::class,
95    UseSession::class,
96    UseTree::class,
97    UseLocale::class,
98    UseTheme::class,
99    Housekeeping::class,
100    CheckCsrf::class,
101    UseTransaction::class,
102    BootModules::class,
103    ModuleMiddleware::class,
104    RequestRouter::class,
105];
106
107// Dispatch the middleware.
108$dispatcher = new Dispatcher($middleware, app());
109$response   = $dispatcher->dispatch($request);
110
111// Emit the response.
112$emitter = new SapiEmitter();
113$emitter->emit($response);
114