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