xref: /webtrees/app/Webtrees.php (revision 1f1ffa65b3b51df2b95b5c68894525436855964a)
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 DebugBar\StandardDebugBar;
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\ModuleMiddleware;
30use Fisharebest\Webtrees\Http\Middleware\NoRouteFound;
31use Fisharebest\Webtrees\Http\Middleware\PhpEnvironment;
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 Psr\Http\Server\MiddlewareInterface;
53use Throwable;
54use function app;
55use function class_exists;
56use function dirname;
57use function error_reporting;
58use function ob_end_clean;
59use function ob_get_level;
60use function set_error_handler;
61use function set_exception_handler;
62use function str_replace;
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.3", etc.
96    public const STABILITY = 'alpha.5';
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    /**
105     * Initialise the application.
106     *
107     * @return void
108     */
109    public function bootstrap(): void
110    {
111        // Show all errors and warnings in development, fewer in production.
112        error_reporting(self::ERROR_REPORTING);
113
114        set_error_handler($this->phpErrorHandler());
115        set_exception_handler($this->phpExceptionHandler());
116    }
117
118    /**
119     * An error handler that can be passed to set_error_handler().
120     *
121     * @return Closure
122     */
123    private function phpErrorHandler(): Closure
124    {
125        return static function (int $errno, string $errstr, string $errfile, int $errline): bool {
126            // Ignore errors that are silenced with '@'
127            if (error_reporting() & $errno) {
128                throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
129            }
130
131            return true;
132        };
133    }
134
135    /**
136     * An exception handler that can be passed to set_exception_handler().
137     * Display any exception that are not caught by the middleware exception handler.
138     *
139     * @return Closure
140     */
141    private function phpExceptionHandler(): Closure
142    {
143        return static function (Throwable $ex): void {
144            $base_path = dirname(__DIR__);
145            $trace     = $ex->getMessage() . PHP_EOL . $ex->getTraceAsString();
146            $trace     = str_replace($base_path, '', $trace);
147
148            while (ob_get_level() > 0) {
149                ob_end_clean();
150            }
151
152            echo '<html lang="en"><head><title>Error</title><meta charset="UTF-8"></head><body><pre>' . $trace . '</pre></body></html>';
153        };
154    }
155
156    /**
157     * We can use any PSR-7 / PSR-17 compatible message factory.
158     *
159     * @return void
160     */
161    public function selectMessageFactory(): void
162    {
163        app()->bind(ResponseFactoryInterface::class, Psr17Factory::class);
164        app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class);
165        app()->bind(StreamFactoryInterface::class, Psr17Factory::class);
166        app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class);
167        app()->bind(UriFactoryInterface::class, Psr17Factory::class);
168    }
169
170    /**
171     * We can use any PSR-7 compatible requests.
172     *
173     * @return ServerRequestInterface
174     */
175    public function createServerRequest(): ServerRequestInterface
176    {
177        $server_request_creator = new ServerRequestCreator(
178            app(ServerRequestFactoryInterface::class),
179            app(UriFactoryInterface::class),
180            app(UploadedFileFactoryInterface::class),
181            app(StreamFactoryInterface::class)
182        );
183
184        // Until all the code is rewritten to use PSR-7 requests, we still need our hybrid request.
185        if (class_exists(Request::class)) {
186            return Request::createFromGlobals();
187        }
188
189        return $server_request_creator->fromGlobals();
190    }
191
192    /**
193     * The webtrees application is built from middleware.
194     *
195     * @return MiddlewareInterface[]
196     */
197    public function middleware(): array
198    {
199        return [
200            PhpEnvironment::class,
201            EmitResponse::class,
202            HandleExceptions::class,
203            CheckForMaintenanceMode::class,
204            UseDatabase::class,
205            UseDebugbar::class,
206            UpdateDatabaseSchema::class,
207            UseCache::class,
208            UseFilesystem::class,
209            UseSession::class,
210            UseTree::class,
211            UseLocale::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