xref: /webtrees/app/Webtrees.php (revision a992e8c1f5cff3fba48e8b69ec698e4e21142dc8)
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 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees;
20
21use Closure;
22use ErrorException;
23use Fisharebest\Webtrees\Http\Middleware\BootModules;
24use Fisharebest\Webtrees\Http\Middleware\CheckCsrf;
25use Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode;
26use Fisharebest\Webtrees\Http\Middleware\DoHousekeeping;
27use Fisharebest\Webtrees\Http\Middleware\EmitResponse;
28use Fisharebest\Webtrees\Http\Middleware\HandleExceptions;
29use Fisharebest\Webtrees\Http\Middleware\NoRouteFound;
30use Fisharebest\Webtrees\Http\Middleware\PhpEnvironment;
31use Fisharebest\Webtrees\Http\Middleware\ReadConfigIni;
32use Fisharebest\Webtrees\Http\Middleware\Router;
33use Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema;
34use Fisharebest\Webtrees\Http\Middleware\UseCache;
35use Fisharebest\Webtrees\Http\Middleware\UseDatabase;
36use Fisharebest\Webtrees\Http\Middleware\UseDebugbar;
37use Fisharebest\Webtrees\Http\Middleware\UseFilesystem;
38use Fisharebest\Webtrees\Http\Middleware\UseLocale;
39use Fisharebest\Webtrees\Http\Middleware\UseSession;
40use Fisharebest\Webtrees\Http\Middleware\UseTheme;
41use Fisharebest\Webtrees\Http\Middleware\UseTransaction;
42use Fisharebest\Webtrees\Http\Middleware\UseTree;
43use Fisharebest\Webtrees\Http\Middleware\WebEnvironment;
44use Nyholm\Psr7\Factory\Psr17Factory;
45use Psr\Http\Message\ResponseFactoryInterface;
46use Psr\Http\Message\ServerRequestFactoryInterface;
47use Psr\Http\Message\StreamFactoryInterface;
48use Psr\Http\Message\UploadedFileFactoryInterface;
49use Psr\Http\Message\UriFactoryInterface;
50use Throwable;
51
52use function app;
53use function dirname;
54use function error_reporting;
55use function ob_end_clean;
56use function ob_get_level;
57use function set_error_handler;
58use function set_exception_handler;
59use function str_replace;
60
61use const PHP_EOL;
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    // Location of the file containing the database connection details.
72    public const CONFIG_FILE = self::ROOT_DIR . 'data/config.ini.php';
73
74    // Location of the file that triggers maintenance mode.
75    public const OFFLINE_FILE = self::ROOT_DIR . 'data/offline.txt';
76
77    // Location of our modules.
78    public const MODULES_PATH = 'modules_v4/';
79    public const MODULES_DIR  = self::ROOT_DIR . self::MODULES_PATH;
80
81    // Enable debugging on development builds.
82    public const DEBUG = self::STABILITY !== '';
83
84    // We want to know about all PHP errors during development, and fewer in production.
85    public const ERROR_REPORTING = self::DEBUG ? E_ALL | E_STRICT | E_NOTICE | E_DEPRECATED : E_ALL;
86
87    // The name of the application.
88    public const NAME = 'webtrees';
89
90    // Required version of database tables/columns/indexes/etc.
91    public const SCHEMA_VERSION = 43;
92
93    // e.g. "dev", "alpha", "beta", etc.
94    public const STABILITY = 'beta.4';
95
96    // Version number
97    public const VERSION = '2.0.0' . (self::STABILITY === '' ? '' : '-') . self::STABILITY;
98
99    // Project website.
100    public const URL = 'https://www.webtrees.net/';
101
102    private const MIDDLEWARE = [
103        PhpEnvironment::class,
104        EmitResponse::class,
105        HandleExceptions::class,
106        ReadConfigIni::class,
107        WebEnvironment::class,
108        UseDatabase::class,
109        UseDebugbar::class,
110        UpdateDatabaseSchema::class,
111        UseCache::class,
112        UseFilesystem::class,
113        UseSession::class,
114        UseTree::class,
115        UseLocale::class,
116        CheckForMaintenanceMode::class,
117        UseTheme::class,
118        DoHousekeeping::class,
119        CheckCsrf::class,
120        UseTransaction::class,
121        BootModules::class,
122        Router::class,
123        NoRouteFound::class,
124    ];
125
126    /**
127     * Initialise the application.
128     *
129     * @return void
130     */
131    public function bootstrap(): void
132    {
133        // Show all errors and warnings in development, fewer in production.
134        error_reporting(self::ERROR_REPORTING);
135
136        set_error_handler($this->phpErrorHandler());
137        set_exception_handler($this->phpExceptionHandler());
138    }
139
140    /**
141     * An error handler that can be passed to set_error_handler().
142     *
143     * @return Closure
144     */
145    private function phpErrorHandler(): Closure
146    {
147        return static function (int $errno, string $errstr, string $errfile, int $errline): bool {
148            // Ignore errors that are silenced with '@'
149            if (error_reporting() & $errno) {
150                throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
151            }
152
153            return true;
154        };
155    }
156
157    /**
158     * An exception handler that can be passed to set_exception_handler().
159     * Display any exception that are not caught by the middleware exception handler.
160     *
161     * @return Closure
162     */
163    private function phpExceptionHandler(): Closure
164    {
165        return static function (Throwable $ex): void {
166            $base_path = dirname(__DIR__);
167            $trace     = $ex->getMessage() . ' ' . $ex->getFile() . ':' . $ex->getLine() . PHP_EOL . $ex->getTraceAsString();
168            $trace     = str_replace($base_path, '', $trace);
169
170            while (ob_get_level() > 0) {
171                ob_end_clean();
172            }
173
174            echo '<html lang="en"><head><title>Error</title><meta charset="UTF-8"></head><body><pre>' . $trace . '</pre></body></html>';
175        };
176    }
177
178    /**
179     * We can use any PSR-7 / PSR-17 compatible message factory.
180     *
181     * @return void
182     */
183    public function selectMessageFactory(): void
184    {
185        app()->bind(ResponseFactoryInterface::class, Psr17Factory::class);
186        app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class);
187        app()->bind(StreamFactoryInterface::class, Psr17Factory::class);
188        app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class);
189        app()->bind(UriFactoryInterface::class, Psr17Factory::class);
190    }
191
192    /**
193     * The webtrees application is built from middleware.
194     *
195     * @return string[]
196     */
197    public function middleware(): array
198    {
199        return self::MIDDLEWARE;
200    }
201}
202