xref: /webtrees/index.php (revision cfbf56ad574e64d6e9caaa3d0e76e9a8c102b676)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19use Fisharebest\Webtrees\DebugBar;
20use Fisharebest\Webtrees\Http\Middleware\BootModules;
21use Fisharebest\Webtrees\Http\Middleware\CheckCsrf;
22use Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode;
23use Fisharebest\Webtrees\Http\Middleware\UseDebugbar;
24use Fisharebest\Webtrees\Http\Middleware\EmitResponse;
25use Fisharebest\Webtrees\Http\Middleware\HandleExceptions;
26use Fisharebest\Webtrees\Http\Middleware\DoHousekeeping;
27use Fisharebest\Webtrees\Http\Middleware\ModuleMiddleware;
28use Fisharebest\Webtrees\Http\Middleware\RequestRouter;
29use Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema;
30use Fisharebest\Webtrees\Http\Middleware\UseCache;
31use Fisharebest\Webtrees\Http\Middleware\UseDatabase;
32use Fisharebest\Webtrees\Http\Middleware\UseFilesystem;
33use Fisharebest\Webtrees\Http\Middleware\UseLocale;
34use Fisharebest\Webtrees\Http\Middleware\UseSession;
35use Fisharebest\Webtrees\Http\Middleware\UseTheme;
36use Fisharebest\Webtrees\Http\Middleware\UseTransaction;
37use Fisharebest\Webtrees\Http\Middleware\UseTree;
38use Fisharebest\Webtrees\Http\Request as SymfonyRequest;
39use Fisharebest\Webtrees\Webtrees;
40use Middleland\Dispatcher;
41use Nyholm\Psr7\Factory\Psr17Factory;
42use Nyholm\Psr7Server\ServerRequestCreator;
43use Psr\Http\Message\ResponseFactoryInterface;
44use Psr\Http\Message\ServerRequestFactoryInterface;
45use Psr\Http\Message\StreamFactoryInterface;
46use Psr\Http\Message\UploadedFileFactoryInterface;
47use Psr\Http\Message\UriFactoryInterface;
48use DebugBar\StandardDebugBar;
49
50require __DIR__ . '/vendor/autoload.php';
51
52const WT_ROOT = __DIR__ . DIRECTORY_SEPARATOR;
53
54Webtrees::init();
55
56// Use nyholm/psr7 for our PSR7 messages and PSR17 factory.
57app()->bind(ResponseFactoryInterface::class, Psr17Factory::class);
58app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class);
59app()->bind(StreamFactoryInterface::class, Psr17Factory::class);
60app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class);
61app()->bind(UriFactoryInterface::class, Psr17Factory::class);
62
63// Use nyholm/psr7-server to create a request from the PHP environment.
64$server_request_creator = new ServerRequestCreator(
65    app(ServerRequestFactoryInterface::class),
66    app(UriFactoryInterface::class),
67    app(UploadedFileFactoryInterface::class),
68    app(StreamFactoryInterface::class)
69);
70
71// Create a PSR-7 request
72$request = $server_request_creator->fromGlobals();
73
74// Initialise the DebugBar for development.
75// Use `composer install --dev` on a development build to enable.
76// Note that you may need to increase the size of the fcgi buffers on nginx.
77// e.g. add these lines to your fastcgi_params file:
78// fastcgi_buffers 16 16m;
79// fastcgi_buffer_size 32m;
80DebugBar::init(class_exists(StandardDebugBar::class));
81
82// Until all the code is rewritten to use PSR-7 requests, we still need our hybrid request.
83$request = SymfonyRequest::createFromGlobals();
84
85// Calculate the base URL, so we can generate absolute URLs.
86// Remove any PHP script name and parameters.
87define('WT_BASE_URL', preg_replace('/[^\/]+\.php(\?.*)?$/', '', $request->getUri()));
88
89$middleware = [
90    EmitResponse::class,
91    HandleExceptions::class,
92    CheckForMaintenanceMode::class,
93    UseDatabase::class,
94    UseDebugbar::class,
95    UpdateDatabaseSchema::class,
96    UseCache::class,
97    UseFilesystem::class,
98    UseSession::class,
99    UseTree::class,
100    UseLocale::class,
101    UseTheme::class,
102    DoHousekeeping::class,
103    CheckCsrf::class,
104    UseTransaction::class,
105    BootModules::class,
106    ModuleMiddleware::class,
107    RequestRouter::class,
108];
109
110$dispatcher = new Dispatcher($middleware, app());
111
112$dispatcher->dispatch($request);
113