xref: /webtrees/app/Webtrees.php (revision 234e0863a607ff8e5f8e78a7fefe5366f39f8ebf)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use Closure;
23use ErrorException;
24use Fisharebest\Webtrees\Contracts\ContainerInterface;
25use Fisharebest\Webtrees\Factories\CacheFactory;
26use Fisharebest\Webtrees\Factories\CalendarDateFactory;
27use Fisharebest\Webtrees\Factories\ElementFactory;
28use Fisharebest\Webtrees\Factories\EncodingFactory;
29use Fisharebest\Webtrees\Factories\FamilyFactory;
30use Fisharebest\Webtrees\Factories\FilesystemFactory;
31use Fisharebest\Webtrees\Factories\GedcomRecordFactory;
32use Fisharebest\Webtrees\Factories\HeaderFactory;
33use Fisharebest\Webtrees\Factories\IdFactory;
34use Fisharebest\Webtrees\Factories\ImageFactory;
35use Fisharebest\Webtrees\Factories\IndividualFactory;
36use Fisharebest\Webtrees\Factories\LocationFactory;
37use Fisharebest\Webtrees\Factories\MarkdownFactory;
38use Fisharebest\Webtrees\Factories\MediaFactory;
39use Fisharebest\Webtrees\Factories\NoteFactory;
40use Fisharebest\Webtrees\Factories\RepositoryFactory;
41use Fisharebest\Webtrees\Factories\ResponseFactory;
42use Fisharebest\Webtrees\Factories\RouteFactory;
43use Fisharebest\Webtrees\Factories\SharedNoteFactory;
44use Fisharebest\Webtrees\Factories\SlugFactory;
45use Fisharebest\Webtrees\Factories\SourceFactory;
46use Fisharebest\Webtrees\Factories\SubmissionFactory;
47use Fisharebest\Webtrees\Factories\SubmitterFactory;
48use Fisharebest\Webtrees\Factories\SurnameTraditionFactory;
49use Fisharebest\Webtrees\Factories\TimeFactory;
50use Fisharebest\Webtrees\Factories\TimestampFactory;
51use Fisharebest\Webtrees\Factories\XrefFactory;
52use Fisharebest\Webtrees\GedcomFilters\GedcomEncodingFilter;
53use Fisharebest\Webtrees\Http\Middleware\BadBotBlocker;
54use Fisharebest\Webtrees\Http\Middleware\BaseUrl;
55use Fisharebest\Webtrees\Http\Middleware\BootModules;
56use Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode;
57use Fisharebest\Webtrees\Http\Middleware\CheckForNewVersion;
58use Fisharebest\Webtrees\Http\Middleware\ClientIp;
59use Fisharebest\Webtrees\Http\Middleware\CompressResponse;
60use Fisharebest\Webtrees\Http\Middleware\ContentLength;
61use Fisharebest\Webtrees\Http\Middleware\DoHousekeeping;
62use Fisharebest\Webtrees\Http\Middleware\EmitResponse;
63use Fisharebest\Webtrees\Http\Middleware\ErrorHandler;
64use Fisharebest\Webtrees\Http\Middleware\HandleExceptions;
65use Fisharebest\Webtrees\Http\Middleware\LoadRoutes;
66use Fisharebest\Webtrees\Http\Middleware\NoRouteFound;
67use Fisharebest\Webtrees\Http\Middleware\PublicFiles;
68use Fisharebest\Webtrees\Http\Middleware\ReadConfigIni;
69use Fisharebest\Webtrees\Http\Middleware\RegisterGedcomTags;
70use Fisharebest\Webtrees\Http\Middleware\Router;
71use Fisharebest\Webtrees\Http\Middleware\SecurityHeaders;
72use Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema;
73use Fisharebest\Webtrees\Http\Middleware\UseDatabase;
74use Fisharebest\Webtrees\Http\Middleware\UseLanguage;
75use Fisharebest\Webtrees\Http\Middleware\UseSession;
76use Fisharebest\Webtrees\Http\Middleware\UseTheme;
77use Fisharebest\Webtrees\Http\Middleware\UseTransaction;
78use Middleland\Dispatcher;
79use Nyholm\Psr7\Factory\Psr17Factory;
80use Nyholm\Psr7Server\ServerRequestCreator;
81use Psr\Http\Message\ResponseFactoryInterface;
82use Psr\Http\Message\ResponseInterface;
83use Psr\Http\Message\ServerRequestFactoryInterface;
84use Psr\Http\Message\StreamFactoryInterface;
85use Psr\Http\Message\UploadedFileFactoryInterface;
86use Psr\Http\Message\UriFactoryInterface;
87
88use function date_default_timezone_set;
89use function error_reporting;
90use function mb_internal_encoding;
91use function stream_filter_register;
92
93use const E_ALL;
94use const E_DEPRECATED;
95use const E_USER_DEPRECATED;
96use const PHP_SAPI;
97
98/**
99 * Definitions for the webtrees application.
100 */
101class Webtrees
102{
103    // The root folder of this installation
104    public const ROOT_DIR = __DIR__ . '/../';
105
106    // This is the location of system data, such as temporary and cache files.
107    // The system files are always in this location.
108    // It is also the default location of user data, such as media and GEDCOM files.
109    // The user files could be anywhere supported by Flysystem.
110    public const DATA_DIR = self::ROOT_DIR . 'data/';
111
112    // Location of the file containing the database connection details.
113    public const CONFIG_FILE = self::DATA_DIR . 'config.ini.php';
114
115    // Location of the file that triggers maintenance mode.
116    public const OFFLINE_FILE = self::DATA_DIR . 'offline.txt';
117
118    // Location of our modules.
119    public const MODULES_PATH = 'modules_v4/';
120    public const MODULES_DIR  = self::ROOT_DIR . self::MODULES_PATH;
121
122    // Enable debugging on development builds.
123    public const DEBUG = self::STABILITY !== '';
124
125    // We want to know about all PHP errors during development, and fewer in production.
126    public const ERROR_REPORTING = self::DEBUG ? E_ALL : E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED;
127
128    // Page layouts for various page types.
129    public const LAYOUT_ADMINISTRATION = 'layouts/administration';
130    public const LAYOUT_AJAX           = 'layouts/ajax';
131    public const LAYOUT_DEFAULT        = 'layouts/default';
132    public const LAYOUT_ERROR          = 'layouts/error';
133
134    // The name of the application.
135    public const NAME = 'webtrees';
136
137    // Required version of database tables/columns/indexes/etc.
138    public const SCHEMA_VERSION = 45;
139
140    // e.g. "-dev", "-alpha", "-beta", etc.
141    public const STABILITY = '-dev';
142
143    // Version number.
144    public const VERSION = '2.2.0' . self::STABILITY;
145
146    // Project website.
147    public const URL = 'https://webtrees.net/';
148
149    // FAQ links.
150    public const URL_FAQ_EMAIL = 'https://webtrees.net/faq/email';
151
152    // GEDCOM specification.
153    public const GEDCOM_PDF = 'https://webtrees.net/downloads/gedcom-5-5-1.pdf';
154
155    private const MIDDLEWARE = [
156        ErrorHandler::class,
157        EmitResponse::class,
158        SecurityHeaders::class,
159        ReadConfigIni::class,
160        BaseUrl::class,
161        HandleExceptions::class,
162        PublicFiles::class,
163        ClientIp::class,
164        ContentLength::class,
165        CompressResponse::class,
166        BadBotBlocker::class,
167        UseDatabase::class,
168        UpdateDatabaseSchema::class,
169        UseSession::class,
170        UseLanguage::class,
171        CheckForMaintenanceMode::class,
172        UseTheme::class,
173        DoHousekeeping::class,
174        UseTransaction::class,
175        CheckForNewVersion::class,
176        LoadRoutes::class,
177        RegisterGedcomTags::class,
178        BootModules::class,
179        Router::class,
180        NoRouteFound::class,
181    ];
182
183    /**
184     * Initialise the application.
185     *
186     * @return static
187     */
188    public function bootstrap(): static
189    {
190        // Show all errors and warnings in development, fewer in production.
191        error_reporting(self::ERROR_REPORTING);
192
193        // All modern software uses UTF-8 encoding.
194        mb_internal_encoding('UTF-8');
195
196        // Use UTC internally and convert to local time when displaying datetimes.
197        date_default_timezone_set('UTC');
198
199        // Factory objects
200        Registry::cache(new CacheFactory());
201        Registry::calendarDateFactory(new CalendarDateFactory());
202        Registry::container(new Container());
203        Registry::elementFactory(new ElementFactory());
204        Registry::encodingFactory(new EncodingFactory());
205        Registry::familyFactory(new FamilyFactory());
206        Registry::filesystem(new FilesystemFactory());
207        Registry::gedcomRecordFactory(new GedcomRecordFactory());
208        Registry::headerFactory(new HeaderFactory());
209        Registry::idFactory(new IdFactory());
210        Registry::imageFactory(new ImageFactory());
211        Registry::individualFactory(new IndividualFactory());
212        Registry::locationFactory(new LocationFactory());
213        Registry::markdownFactory(new MarkdownFactory());
214        Registry::mediaFactory(new MediaFactory());
215        Registry::noteFactory(new NoteFactory());
216        Registry::repositoryFactory(new RepositoryFactory());
217        Registry::responseFactory(new ResponseFactory(new Psr17Factory(), new Psr17Factory()));
218        Registry::routeFactory(new RouteFactory());
219        Registry::sharedNoteFactory(new SharedNoteFactory());
220        Registry::slugFactory(new SlugFactory());
221        Registry::sourceFactory(new SourceFactory());
222        Registry::submissionFactory(new SubmissionFactory());
223        Registry::submitterFactory(new SubmitterFactory());
224        Registry::surnameTraditionFactory(new SurnameTraditionFactory());
225        Registry::timeFactory(new TimeFactory());
226        Registry::timestampFactory(new TimestampFactory());
227        Registry::xrefFactory(new XrefFactory());
228
229        // PSR7 messages and PSR17 message-factories
230        Registry::container()
231            ->set(ResponseFactoryInterface::class, new Psr17Factory())
232            ->set(ServerRequestFactoryInterface::class, new Psr17Factory())
233            ->set(StreamFactoryInterface::class, new Psr17Factory())
234            ->set(UploadedFileFactoryInterface::class, new Psr17Factory())
235            ->set(UriFactoryInterface::class, new Psr17Factory());
236
237        stream_filter_register(GedcomEncodingFilter::class, GedcomEncodingFilter::class);
238
239        return $this;
240    }
241
242    /**
243     * Run the application.
244     *
245     * @return void
246     */
247    public function run(): void
248    {
249        if (PHP_SAPI === 'cli') {
250            $this->cliRequest();
251        } else {
252            $this->httpRequest();
253        };
254    }
255
256    /**
257     * Respond to a CLI request.
258     *
259     * @return void
260     */
261    public function cliRequest(): void
262    {
263        // CLI handler will go here.
264    }
265
266    /**
267     * Respond to an HTTP request.
268     *
269     * @return ResponseInterface
270     */
271    public function httpRequest(): ResponseInterface
272    {
273        $server_request_creator = new ServerRequestCreator(
274            Registry::container()->get(ServerRequestFactoryInterface::class),
275            Registry::container()->get(UriFactoryInterface::class),
276            Registry::container()->get(UploadedFileFactoryInterface::class),
277            Registry::container()->get(StreamFactoryInterface::class)
278        );
279
280        $request = $server_request_creator->fromGlobals();
281
282        $dispatcher = new Dispatcher(self::MIDDLEWARE, Registry::container());
283
284        return $dispatcher->dispatch($request);
285    }
286}
287