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