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