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