xref: /webtrees/app/Webtrees.php (revision 92e93024855adb570827da3ad347dfe222333bff)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees;
19
20use Closure;
21use ErrorException;
22use Fisharebest\Webtrees\Http\Middleware\BootModules;
23use Fisharebest\Webtrees\Http\Middleware\CheckCsrf;
24use Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode;
25use Fisharebest\Webtrees\Http\Middleware\DoHousekeeping;
26use Fisharebest\Webtrees\Http\Middleware\EmitResponse;
27use Fisharebest\Webtrees\Http\Middleware\HandleExceptions;
28use Fisharebest\Webtrees\Http\Middleware\ModuleMiddleware;
29use Fisharebest\Webtrees\Http\Middleware\NoRouteFound;
30use Fisharebest\Webtrees\Http\Middleware\PhpEnvironment;
31use Fisharebest\Webtrees\Http\Middleware\ReadConfigIni;
32use Fisharebest\Webtrees\Http\Middleware\RequestRouter;
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\Request;
44use Nyholm\Psr7\Factory\Psr17Factory;
45use Nyholm\Psr7Server\ServerRequestCreator;
46use Psr\Http\Message\ResponseFactoryInterface;
47use Psr\Http\Message\ServerRequestFactoryInterface;
48use Psr\Http\Message\ServerRequestInterface;
49use Psr\Http\Message\StreamFactoryInterface;
50use Psr\Http\Message\UploadedFileFactoryInterface;
51use Psr\Http\Message\UriFactoryInterface;
52use Throwable;
53use function app;
54use function class_exists;
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;
62use const PHP_EOL;
63
64/**
65 * Definitions for the webtrees application.
66 */
67class Webtrees
68{
69    // The root folder of this installation
70    public const ROOT_DIR = __DIR__ . '/../';
71
72    // Location of the file containing the database connection details.
73    public const CONFIG_FILE = self::ROOT_DIR . 'data/config.ini.php';
74
75    // Location of the file that triggers maintenance mode.
76    public const OFFLINE_FILE = self::ROOT_DIR . 'data/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 = 43;
93
94    // e.g. "dev", "alpha", "beta.3", etc.
95    public const STABILITY = 'beta.1';
96
97    // Version number
98    public const VERSION = '2.0.0' . (self::STABILITY === '' ? '' : '-') . self::STABILITY;
99
100    // Project website.
101    public const URL = 'https://www.webtrees.net/';
102
103    /**
104     * Initialise the application.
105     *
106     * @return void
107     */
108    public function bootstrap(): void
109    {
110        // Show all errors and warnings in development, fewer in production.
111        error_reporting(self::ERROR_REPORTING);
112
113        set_error_handler($this->phpErrorHandler());
114        set_exception_handler($this->phpExceptionHandler());
115    }
116
117    /**
118     * An error handler that can be passed to set_error_handler().
119     *
120     * @return Closure
121     */
122    private function phpErrorHandler(): Closure
123    {
124        return static function (int $errno, string $errstr, string $errfile, int $errline): bool {
125            // Ignore errors that are silenced with '@'
126            if (error_reporting() & $errno) {
127                throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
128            }
129
130            return true;
131        };
132    }
133
134    /**
135     * An exception handler that can be passed to set_exception_handler().
136     * Display any exception that are not caught by the middleware exception handler.
137     *
138     * @return Closure
139     */
140    private function phpExceptionHandler(): Closure
141    {
142        return static function (Throwable $ex): void {
143            $base_path = dirname(__DIR__);
144            $trace     = $ex->getMessage() . PHP_EOL . $ex->getTraceAsString();
145            $trace     = str_replace($base_path, '', $trace);
146
147            while (ob_get_level() > 0) {
148                ob_end_clean();
149            }
150
151            echo '<html lang="en"><head><title>Error</title><meta charset="UTF-8"></head><body><pre>' . $trace . '</pre></body></html>';
152        };
153    }
154
155    /**
156     * We can use any PSR-7 / PSR-17 compatible message factory.
157     *
158     * @return void
159     */
160    public function selectMessageFactory(): void
161    {
162        app()->bind(ResponseFactoryInterface::class, Psr17Factory::class);
163        app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class);
164        app()->bind(StreamFactoryInterface::class, Psr17Factory::class);
165        app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class);
166        app()->bind(UriFactoryInterface::class, Psr17Factory::class);
167    }
168
169    /**
170     * We can use any PSR-7 compatible requests.
171     *
172     * @return ServerRequestInterface
173     */
174    public function createServerRequest(): ServerRequestInterface
175    {
176        $server_request_creator = new ServerRequestCreator(
177            app(ServerRequestFactoryInterface::class),
178            app(UriFactoryInterface::class),
179            app(UploadedFileFactoryInterface::class),
180            app(StreamFactoryInterface::class)
181        );
182
183        // Until all the code is rewritten to use PSR-7 requests, we still need our hybrid request.
184        if (class_exists(Request::class)) {
185            return Request::createFromGlobals();
186        }
187
188        return $server_request_creator->fromGlobals();
189    }
190
191    /**
192     * The webtrees application is built from middleware.
193     *
194     * @return string[]
195     */
196    public function middleware(): array
197    {
198        return [
199            PhpEnvironment::class,
200            EmitResponse::class,
201            HandleExceptions::class,
202            ReadConfigIni::class,
203            UseDatabase::class,
204            UseDebugbar::class,
205            UpdateDatabaseSchema::class,
206            UseCache::class,
207            UseFilesystem::class,
208            UseSession::class,
209            UseTree::class,
210            UseLocale::class,
211            CheckForMaintenanceMode::class,
212            UseTheme::class,
213            DoHousekeeping::class,
214            CheckCsrf::class,
215            UseTransaction::class,
216            BootModules::class,
217            ModuleMiddleware::class,
218            RequestRouter::class,
219            NoRouteFound::class,
220        ];
221    }
222}
223