xref: /webtrees/app/Webtrees.php (revision 5fb61fc65b99b2d532a8e4bc9f5ba7068636203f)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\Http\Middleware\BadBotBlocker;
25use Fisharebest\Webtrees\Http\Middleware\BootModules;
26use Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode;
27use Fisharebest\Webtrees\Http\Middleware\ClientIp;
28use Fisharebest\Webtrees\Http\Middleware\CompressResponse;
29use Fisharebest\Webtrees\Http\Middleware\DoHousekeeping;
30use Fisharebest\Webtrees\Http\Middleware\EmitResponse;
31use Fisharebest\Webtrees\Http\Middleware\HandleExceptions;
32use Fisharebest\Webtrees\Http\Middleware\LoadRoutes;
33use Fisharebest\Webtrees\Http\Middleware\NoRouteFound;
34use Fisharebest\Webtrees\Http\Middleware\PhpEnvironment;
35use Fisharebest\Webtrees\Http\Middleware\ReadConfigIni;
36use Fisharebest\Webtrees\Http\Middleware\RegisterFactories;
37use Fisharebest\Webtrees\Http\Middleware\Router;
38use Fisharebest\Webtrees\Http\Middleware\SecurityHeaders;
39use Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema;
40use Fisharebest\Webtrees\Http\Middleware\UseDatabase;
41use Fisharebest\Webtrees\Http\Middleware\UseDebugbar;
42use Fisharebest\Webtrees\Http\Middleware\UseLanguage;
43use Fisharebest\Webtrees\Http\Middleware\UseSession;
44use Fisharebest\Webtrees\Http\Middleware\UseTheme;
45use Fisharebest\Webtrees\Http\Middleware\UseTransaction;
46use Fisharebest\Webtrees\Http\Middleware\BaseUrl;
47use Nyholm\Psr7\Factory\Psr17Factory;
48use Psr\Http\Message\ResponseFactoryInterface;
49use Psr\Http\Message\ServerRequestFactoryInterface;
50use Psr\Http\Message\StreamFactoryInterface;
51use Psr\Http\Message\UploadedFileFactoryInterface;
52use Psr\Http\Message\UriFactoryInterface;
53
54use function app;
55use function error_reporting;
56use function set_error_handler;
57
58use const E_ALL;
59use const E_DEPRECATED;
60use const E_USER_DEPRECATED;
61
62/**
63 * Definitions for the webtrees application.
64 */
65class Webtrees
66{
67    // The root folder of this installation
68    public const ROOT_DIR = __DIR__ . '/../';
69
70    // This is the location of system data, such as temporary and cache files.
71    // The system files are always in this location.
72    // It is also the default location of user data, such as media and GEDCOM files.
73    // The user files could be anywhere supported by Flysystem.
74    public const DATA_DIR  = self::ROOT_DIR . 'data/';
75
76    // Location of the file containing the database connection details.
77    public const CONFIG_FILE = self::DATA_DIR . 'config.ini.php';
78
79    // Location of the file that triggers maintenance mode.
80    public const OFFLINE_FILE = self::DATA_DIR . 'offline.txt';
81
82    // Location of our modules.
83    public const MODULES_PATH = 'modules_v4/';
84    public const MODULES_DIR  = self::ROOT_DIR . self::MODULES_PATH;
85
86    // Enable debugging on development builds.
87    public const DEBUG = self::STABILITY !== '';
88
89    // We want to know about all PHP errors during development, and fewer in production.
90    public const ERROR_REPORTING = self::DEBUG ? E_ALL : E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED;
91
92    // The name of the application.
93    public const NAME = 'webtrees';
94
95    // Required version of database tables/columns/indexes/etc.
96    public const SCHEMA_VERSION = 45;
97
98    // e.g. "-dev", "-alpha", "-beta", etc.
99    public const STABILITY = '-dev';
100
101    // Version number
102    public const VERSION = '2.1.0' . self::STABILITY;
103
104    // Project website.
105    public const URL = 'https://webtrees.net/';
106
107    // FAQ links
108    public const URL_FAQ_EMAIL = 'https://webtrees.net/faq/email';
109
110    // Project website.
111    public const GEDCOM_PDF = 'https://webtrees.net/downloads/gedcom-551.pdf';
112
113    private const MIDDLEWARE = [
114        PhpEnvironment::class,
115        EmitResponse::class,
116        SecurityHeaders::class,
117        ReadConfigIni::class,
118        BaseUrl::class,
119        HandleExceptions::class,
120        ClientIp::class,
121        RegisterFactories::class,
122        CompressResponse::class,
123        BadBotBlocker::class,
124        UseDatabase::class,
125        UseDebugbar::class,
126        UpdateDatabaseSchema::class,
127        UseSession::class,
128        UseLanguage::class,
129        CheckForMaintenanceMode::class,
130        UseTheme::class,
131        DoHousekeeping::class,
132        UseTransaction::class,
133        LoadRoutes::class,
134        BootModules::class,
135        Router::class,
136        NoRouteFound::class,
137    ];
138
139    /**
140     * Initialise the application.
141     *
142     * @return void
143     */
144    public function bootstrap(): void
145    {
146        // Show all errors and warnings in development, fewer in production.
147        error_reporting(self::ERROR_REPORTING);
148
149        set_error_handler($this->phpErrorHandler());
150    }
151
152    /**
153     * An error handler that can be passed to set_error_handler().
154     *
155     * @return Closure
156     */
157    private function phpErrorHandler(): Closure
158    {
159        return static function (int $errno, string $errstr, string $errfile, int $errline): bool {
160            // Ignore errors that are silenced with '@'
161            if (error_reporting() & $errno) {
162                throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
163            }
164
165            return true;
166        };
167    }
168
169    /**
170     * We can use any PSR-7 / PSR-17 compatible message factory.
171     *
172     * @return void
173     */
174    public function selectMessageFactory(): void
175    {
176        app()->bind(ResponseFactoryInterface::class, Psr17Factory::class);
177        app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class);
178        app()->bind(StreamFactoryInterface::class, Psr17Factory::class);
179        app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class);
180        app()->bind(UriFactoryInterface::class, Psr17Factory::class);
181    }
182
183    /**
184     * The webtrees application is built from middleware.
185     *
186     * @return array<string>
187     */
188    public function middleware(): array
189    {
190        return self::MIDDLEWARE;
191    }
192}
193