xref: /webtrees/app/Webtrees.php (revision 9ba014a766385c38470d363a384f5a2c9642a7b8)
18d0ebef0SGreg Roach<?php
28d0ebef0SGreg Roach/**
38d0ebef0SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58d0ebef0SGreg Roach * This program is free software: you can redistribute it and/or modify
68d0ebef0SGreg Roach * it under the terms of the GNU General Public License as published by
78d0ebef0SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88d0ebef0SGreg Roach * (at your option) any later version.
98d0ebef0SGreg Roach * This program is distributed in the hope that it will be useful,
108d0ebef0SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118d0ebef0SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128d0ebef0SGreg Roach * GNU General Public License for more details.
138d0ebef0SGreg Roach * You should have received a copy of the GNU General Public License
148d0ebef0SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158d0ebef0SGreg Roach */
168d0ebef0SGreg Roachdeclare(strict_types=1);
178d0ebef0SGreg Roach
188d0ebef0SGreg Roachnamespace Fisharebest\Webtrees;
198d0ebef0SGreg Roach
20f397d0fdSGreg Roachuse Closure;
21f397d0fdSGreg Roachuse ErrorException;
22f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BootModules;
23f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CheckCsrf;
24f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode;
25f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\DoHousekeeping;
26f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\EmitResponse;
27f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\HandleExceptions;
28f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ModuleMiddleware;
29f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\NoRouteFound;
30f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\PhpEnvironment;
31e16a1bfdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ReadConfigIni;
32f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\RequestRouter;
33f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema;
34f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseCache;
35f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseDatabase;
36f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseDebugbar;
37f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseFilesystem;
38f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseLocale;
39f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseSession;
40f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseTheme;
41f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseTransaction;
42f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseTree;
43add3fa41SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\WebEnvironment;
44f397d0fdSGreg Roachuse Nyholm\Psr7\Factory\Psr17Factory;
45f397d0fdSGreg Roachuse Nyholm\Psr7Server\ServerRequestCreator;
46f397d0fdSGreg Roachuse Psr\Http\Message\ResponseFactoryInterface;
47f397d0fdSGreg Roachuse Psr\Http\Message\ServerRequestFactoryInterface;
48f397d0fdSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
49f397d0fdSGreg Roachuse Psr\Http\Message\StreamFactoryInterface;
50f397d0fdSGreg Roachuse Psr\Http\Message\UploadedFileFactoryInterface;
51f397d0fdSGreg Roachuse Psr\Http\Message\UriFactoryInterface;
52f397d0fdSGreg Roachuse Throwable;
53f397d0fdSGreg Roachuse function app;
54f397d0fdSGreg Roachuse function dirname;
558d0ebef0SGreg Roachuse function error_reporting;
56f397d0fdSGreg Roachuse function ob_end_clean;
57f397d0fdSGreg Roachuse function ob_get_level;
588d0ebef0SGreg Roachuse function set_error_handler;
596ccdf4f0SGreg Roachuse function set_exception_handler;
60f397d0fdSGreg Roachuse function str_replace;
61f397d0fdSGreg Roachuse const PHP_EOL;
628d0ebef0SGreg Roach
638d0ebef0SGreg Roach/**
648d0ebef0SGreg Roach * Definitions for the webtrees application.
658d0ebef0SGreg Roach */
668d0ebef0SGreg Roachclass Webtrees
678d0ebef0SGreg Roach{
68f397d0fdSGreg Roach    // The root folder of this installation
69f397d0fdSGreg Roach    public const ROOT_DIR = __DIR__ . '/../';
70f397d0fdSGreg Roach
718d0ebef0SGreg Roach    // Location of the file containing the database connection details.
722e9a57f7SGreg Roach    public const CONFIG_FILE = self::ROOT_DIR . 'data/config.ini.php';
73f397d0fdSGreg Roach
74f397d0fdSGreg Roach    // Location of the file that triggers maintenance mode.
752e9a57f7SGreg Roach    public const OFFLINE_FILE = self::ROOT_DIR . 'data/offline.txt';
76f397d0fdSGreg Roach
77f397d0fdSGreg Roach    // Location of our modules.
78b50f90f8SGreg Roach    public const MODULES_PATH = 'modules_v4/';
79f397d0fdSGreg Roach    public const MODULES_DIR = self::ROOT_DIR . self::MODULES_PATH;
808d0ebef0SGreg Roach
818d0ebef0SGreg Roach    // Enable debugging on development builds.
8216d6367aSGreg Roach    public const DEBUG = self::STABILITY !== '';
838d0ebef0SGreg Roach
848d0ebef0SGreg Roach    // We want to know about all PHP errors during development, and fewer in production.
8516d6367aSGreg Roach    public const ERROR_REPORTING = self::DEBUG ? E_ALL | E_STRICT | E_NOTICE | E_DEPRECATED : E_ALL;
868d0ebef0SGreg Roach
878d0ebef0SGreg Roach    // The name of the application.
8816d6367aSGreg Roach    public const NAME = 'webtrees';
898d0ebef0SGreg Roach
908d0ebef0SGreg Roach    // Required version of database tables/columns/indexes/etc.
9167992b6aSRichard Cissee    public const SCHEMA_VERSION = 43;
928d0ebef0SGreg Roach
93*9ba014a7SGreg Roach    // e.g. "dev", "alpha", "beta", etc.
94*9ba014a7SGreg Roach    public const STABILITY = 'beta.4';
958d0ebef0SGreg Roach
968d0ebef0SGreg Roach    // Version number
9716d6367aSGreg Roach    public const VERSION = '2.0.0' . (self::STABILITY === '' ? '' : '-') . self::STABILITY;
988d0ebef0SGreg Roach
99f397d0fdSGreg Roach    // Project website.
100f397d0fdSGreg Roach    public const URL = 'https://www.webtrees.net/';
1018d0ebef0SGreg Roach
1028d0ebef0SGreg Roach    /**
1038d0ebef0SGreg Roach     * Initialise the application.
1048d0ebef0SGreg Roach     *
1058d0ebef0SGreg Roach     * @return void
1068d0ebef0SGreg Roach     */
107f397d0fdSGreg Roach    public function bootstrap(): void
1088d0ebef0SGreg Roach    {
1098d0ebef0SGreg Roach        // Show all errors and warnings in development, fewer in production.
110cfbf56adSGreg Roach        error_reporting(self::ERROR_REPORTING);
1118d0ebef0SGreg Roach
112f397d0fdSGreg Roach        set_error_handler($this->phpErrorHandler());
113f397d0fdSGreg Roach        set_exception_handler($this->phpExceptionHandler());
114f397d0fdSGreg Roach    }
1158d0ebef0SGreg Roach
116f397d0fdSGreg Roach    /**
117f397d0fdSGreg Roach     * An error handler that can be passed to set_error_handler().
118f397d0fdSGreg Roach     *
119f397d0fdSGreg Roach     * @return Closure
120f397d0fdSGreg Roach     */
121f397d0fdSGreg Roach    private function phpErrorHandler(): Closure
122f397d0fdSGreg Roach    {
123f397d0fdSGreg Roach        return static function (int $errno, string $errstr, string $errfile, int $errline): bool {
124f397d0fdSGreg Roach            // Ignore errors that are silenced with '@'
125f397d0fdSGreg Roach            if (error_reporting() & $errno) {
126f397d0fdSGreg Roach                throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
127f397d0fdSGreg Roach            }
128f397d0fdSGreg Roach
129f397d0fdSGreg Roach            return true;
130f397d0fdSGreg Roach        };
131f397d0fdSGreg Roach    }
132f397d0fdSGreg Roach
133f397d0fdSGreg Roach    /**
134f397d0fdSGreg Roach     * An exception handler that can be passed to set_exception_handler().
135f397d0fdSGreg Roach     * Display any exception that are not caught by the middleware exception handler.
136f397d0fdSGreg Roach     *
137f397d0fdSGreg Roach     * @return Closure
138f397d0fdSGreg Roach     */
139f397d0fdSGreg Roach    private function phpExceptionHandler(): Closure
140f397d0fdSGreg Roach    {
141f397d0fdSGreg Roach        return static function (Throwable $ex): void {
142f397d0fdSGreg Roach            $base_path = dirname(__DIR__);
143c133c283SGreg Roach            $trace     = $ex->getMessage() . ' ' . $ex->getFile() . ':' . $ex->getLine() . PHP_EOL . $ex->getTraceAsString();
144f397d0fdSGreg Roach            $trace     = str_replace($base_path, '', $trace);
145f397d0fdSGreg Roach
146f397d0fdSGreg Roach            while (ob_get_level() > 0) {
147f397d0fdSGreg Roach                ob_end_clean();
148f397d0fdSGreg Roach            }
149f397d0fdSGreg Roach
150f397d0fdSGreg Roach            echo '<html lang="en"><head><title>Error</title><meta charset="UTF-8"></head><body><pre>' . $trace . '</pre></body></html>';
151f397d0fdSGreg Roach        };
152f397d0fdSGreg Roach    }
153f397d0fdSGreg Roach
154f397d0fdSGreg Roach    /**
155f397d0fdSGreg Roach     * We can use any PSR-7 / PSR-17 compatible message factory.
156f397d0fdSGreg Roach     *
157f397d0fdSGreg Roach     * @return void
158f397d0fdSGreg Roach     */
159f397d0fdSGreg Roach    public function selectMessageFactory(): void
160f397d0fdSGreg Roach    {
161f397d0fdSGreg Roach        app()->bind(ResponseFactoryInterface::class, Psr17Factory::class);
162f397d0fdSGreg Roach        app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class);
163f397d0fdSGreg Roach        app()->bind(StreamFactoryInterface::class, Psr17Factory::class);
164f397d0fdSGreg Roach        app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class);
165f397d0fdSGreg Roach        app()->bind(UriFactoryInterface::class, Psr17Factory::class);
166f397d0fdSGreg Roach    }
167f397d0fdSGreg Roach
168f397d0fdSGreg Roach    /**
169f397d0fdSGreg Roach     * We can use any PSR-7 compatible requests.
170f397d0fdSGreg Roach     *
171f397d0fdSGreg Roach     * @return ServerRequestInterface
172f397d0fdSGreg Roach     */
173f397d0fdSGreg Roach    public function createServerRequest(): ServerRequestInterface
174f397d0fdSGreg Roach    {
175f397d0fdSGreg Roach        $server_request_creator = new ServerRequestCreator(
176f397d0fdSGreg Roach            app(ServerRequestFactoryInterface::class),
177f397d0fdSGreg Roach            app(UriFactoryInterface::class),
178f397d0fdSGreg Roach            app(UploadedFileFactoryInterface::class),
179f397d0fdSGreg Roach            app(StreamFactoryInterface::class)
180f397d0fdSGreg Roach        );
181f397d0fdSGreg Roach
182f397d0fdSGreg Roach        return $server_request_creator->fromGlobals();
183f397d0fdSGreg Roach    }
184f397d0fdSGreg Roach
185f397d0fdSGreg Roach    /**
186f397d0fdSGreg Roach     * The webtrees application is built from middleware.
187f397d0fdSGreg Roach     *
1887770f26cSGreg Roach     * @return string[]
189f397d0fdSGreg Roach     */
190f397d0fdSGreg Roach    public function middleware(): array
191f397d0fdSGreg Roach    {
192f397d0fdSGreg Roach        return [
193f397d0fdSGreg Roach            PhpEnvironment::class,
194f397d0fdSGreg Roach            EmitResponse::class,
195f397d0fdSGreg Roach            HandleExceptions::class,
196e16a1bfdSGreg Roach            ReadConfigIni::class,
197add3fa41SGreg Roach            WebEnvironment::class,
198f397d0fdSGreg Roach            UseDatabase::class,
199f397d0fdSGreg Roach            UseDebugbar::class,
200f397d0fdSGreg Roach            UpdateDatabaseSchema::class,
201f397d0fdSGreg Roach            UseCache::class,
202f397d0fdSGreg Roach            UseFilesystem::class,
203f397d0fdSGreg Roach            UseSession::class,
204f397d0fdSGreg Roach            UseTree::class,
205f397d0fdSGreg Roach            UseLocale::class,
2067b1c002dSGreg Roach            CheckForMaintenanceMode::class,
207f397d0fdSGreg Roach            UseTheme::class,
208f397d0fdSGreg Roach            DoHousekeeping::class,
209f397d0fdSGreg Roach            CheckCsrf::class,
210f397d0fdSGreg Roach            UseTransaction::class,
211f397d0fdSGreg Roach            BootModules::class,
212f397d0fdSGreg Roach            ModuleMiddleware::class,
213f397d0fdSGreg Roach            RequestRouter::class,
214f397d0fdSGreg Roach            NoRouteFound::class,
215f397d0fdSGreg Roach        ];
2168d0ebef0SGreg Roach    }
2178d0ebef0SGreg Roach}
218