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 20*f397d0fdSGreg Roachuse Closure; 21*f397d0fdSGreg Roachuse DebugBar\StandardDebugBar; 22*f397d0fdSGreg Roachuse ErrorException; 23*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BootModules; 24*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CheckCsrf; 25*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode; 26*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\DoHousekeeping; 27*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\EmitResponse; 28*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\HandleExceptions; 29*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ModuleMiddleware; 30*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\NoRouteFound; 31*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\PhpEnvironment; 32*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\RequestRouter; 33*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema; 34*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseCache; 35*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseDatabase; 36*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseDebugbar; 37*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseFilesystem; 38*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseLocale; 39*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseSession; 40*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseTheme; 41*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseTransaction; 42*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseTree; 43*f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Request; 44*f397d0fdSGreg Roachuse Nyholm\Psr7\Factory\Psr17Factory; 45*f397d0fdSGreg Roachuse Nyholm\Psr7Server\ServerRequestCreator; 46*f397d0fdSGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 47*f397d0fdSGreg Roachuse Psr\Http\Message\ServerRequestFactoryInterface; 48*f397d0fdSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 49*f397d0fdSGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 50*f397d0fdSGreg Roachuse Psr\Http\Message\UploadedFileFactoryInterface; 51*f397d0fdSGreg Roachuse Psr\Http\Message\UriFactoryInterface; 52*f397d0fdSGreg Roachuse Psr\Http\Server\MiddlewareInterface; 53*f397d0fdSGreg Roachuse Throwable; 54*f397d0fdSGreg Roachuse function app; 55*f397d0fdSGreg Roachuse function class_exists; 56*f397d0fdSGreg Roachuse function dirname; 578d0ebef0SGreg Roachuse function error_reporting; 58*f397d0fdSGreg Roachuse function ob_end_clean; 59*f397d0fdSGreg Roachuse function ob_get_level; 608d0ebef0SGreg Roachuse function set_error_handler; 616ccdf4f0SGreg Roachuse function set_exception_handler; 62*f397d0fdSGreg Roachuse function str_replace; 63*f397d0fdSGreg Roachuse const PHP_EOL; 648d0ebef0SGreg Roach 658d0ebef0SGreg Roach/** 668d0ebef0SGreg Roach * Definitions for the webtrees application. 678d0ebef0SGreg Roach */ 688d0ebef0SGreg Roachclass Webtrees 698d0ebef0SGreg Roach{ 70*f397d0fdSGreg Roach // The root folder of this installation 71*f397d0fdSGreg Roach public const ROOT_DIR = __DIR__ . '/../'; 72*f397d0fdSGreg Roach 738d0ebef0SGreg Roach // Location of the file containing the database connection details. 74*f397d0fdSGreg Roach public const CONFIG_FILE = self::ROOT_DIR . '/data/config.ini.php'; 75*f397d0fdSGreg Roach 76*f397d0fdSGreg Roach // Location of the file that triggers maintenance mode. 77*f397d0fdSGreg Roach public const OFFLINE_FILE = self::ROOT_DIR . '/data/offline.txt'; 78*f397d0fdSGreg Roach 79*f397d0fdSGreg Roach // Location of our modules. 80*f397d0fdSGreg Roach public const MODULES_PATH = 'modules_v4'; 81*f397d0fdSGreg Roach public const MODULES_DIR = self::ROOT_DIR . self::MODULES_PATH; 828d0ebef0SGreg Roach 838d0ebef0SGreg Roach // Enable debugging on development builds. 8416d6367aSGreg Roach public const DEBUG = self::STABILITY !== ''; 858d0ebef0SGreg Roach 868d0ebef0SGreg Roach // We want to know about all PHP errors during development, and fewer in production. 8716d6367aSGreg Roach public const ERROR_REPORTING = self::DEBUG ? E_ALL | E_STRICT | E_NOTICE | E_DEPRECATED : E_ALL; 888d0ebef0SGreg Roach 898d0ebef0SGreg Roach // The name of the application. 9016d6367aSGreg Roach public const NAME = 'webtrees'; 918d0ebef0SGreg Roach 928d0ebef0SGreg Roach // Required version of database tables/columns/indexes/etc. 9367992b6aSRichard Cissee public const SCHEMA_VERSION = 43; 948d0ebef0SGreg Roach 958d0ebef0SGreg Roach // e.g. "dev", "alpha", "beta.3", etc. 96963ec568SGreg Roach public const STABILITY = 'alpha.5'; 978d0ebef0SGreg Roach 988d0ebef0SGreg Roach // Version number 9916d6367aSGreg Roach public const VERSION = '2.0.0' . (self::STABILITY === '' ? '' : '-') . self::STABILITY; 1008d0ebef0SGreg Roach 101*f397d0fdSGreg Roach // Project website. 102*f397d0fdSGreg Roach public const URL = 'https://www.webtrees.net/'; 1038d0ebef0SGreg Roach 1048d0ebef0SGreg Roach /** 1058d0ebef0SGreg Roach * Initialise the application. 1068d0ebef0SGreg Roach * 1078d0ebef0SGreg Roach * @return void 1088d0ebef0SGreg Roach */ 109*f397d0fdSGreg Roach public function bootstrap(): void 1108d0ebef0SGreg Roach { 1118d0ebef0SGreg Roach // Show all errors and warnings in development, fewer in production. 112cfbf56adSGreg Roach error_reporting(self::ERROR_REPORTING); 1138d0ebef0SGreg Roach 114*f397d0fdSGreg Roach set_error_handler($this->phpErrorHandler()); 115*f397d0fdSGreg Roach set_exception_handler($this->phpExceptionHandler()); 116*f397d0fdSGreg Roach } 1178d0ebef0SGreg Roach 118*f397d0fdSGreg Roach /** 119*f397d0fdSGreg Roach * An error handler that can be passed to set_error_handler(). 120*f397d0fdSGreg Roach * 121*f397d0fdSGreg Roach * @return Closure 122*f397d0fdSGreg Roach */ 123*f397d0fdSGreg Roach private function phpErrorHandler(): Closure 124*f397d0fdSGreg Roach { 125*f397d0fdSGreg Roach return static function (int $errno, string $errstr, string $errfile, int $errline): bool { 126*f397d0fdSGreg Roach // Ignore errors that are silenced with '@' 127*f397d0fdSGreg Roach if (error_reporting() & $errno) { 128*f397d0fdSGreg Roach throw new ErrorException($errstr, 0, $errno, $errfile, $errline); 129*f397d0fdSGreg Roach } 130*f397d0fdSGreg Roach 131*f397d0fdSGreg Roach return true; 132*f397d0fdSGreg Roach }; 133*f397d0fdSGreg Roach } 134*f397d0fdSGreg Roach 135*f397d0fdSGreg Roach /** 136*f397d0fdSGreg Roach * An exception handler that can be passed to set_exception_handler(). 137*f397d0fdSGreg Roach * Display any exception that are not caught by the middleware exception handler. 138*f397d0fdSGreg Roach * 139*f397d0fdSGreg Roach * @return Closure 140*f397d0fdSGreg Roach */ 141*f397d0fdSGreg Roach private function phpExceptionHandler(): Closure 142*f397d0fdSGreg Roach { 143*f397d0fdSGreg Roach return static function (Throwable $ex): void { 144*f397d0fdSGreg Roach $base_path = dirname(__DIR__); 145*f397d0fdSGreg Roach $trace = $ex->getMessage() . PHP_EOL . $ex->getTraceAsString(); 146*f397d0fdSGreg Roach $trace = str_replace($base_path, '…', $trace); 147*f397d0fdSGreg Roach 148*f397d0fdSGreg Roach while (ob_get_level() > 0) { 149*f397d0fdSGreg Roach ob_end_clean(); 150*f397d0fdSGreg Roach } 151*f397d0fdSGreg Roach 152*f397d0fdSGreg Roach echo '<html lang="en"><head><title>Error</title><meta charset="UTF-8"></head><body><pre>' . $trace . '</pre></body></html>'; 153*f397d0fdSGreg Roach }; 154*f397d0fdSGreg Roach } 155*f397d0fdSGreg Roach 156*f397d0fdSGreg Roach /** 157*f397d0fdSGreg Roach * We can use any PSR-7 / PSR-17 compatible message factory. 158*f397d0fdSGreg Roach * 159*f397d0fdSGreg Roach * @return void 160*f397d0fdSGreg Roach */ 161*f397d0fdSGreg Roach public function selectMessageFactory(): void 162*f397d0fdSGreg Roach { 163*f397d0fdSGreg Roach app()->bind(ResponseFactoryInterface::class, Psr17Factory::class); 164*f397d0fdSGreg Roach app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class); 165*f397d0fdSGreg Roach app()->bind(StreamFactoryInterface::class, Psr17Factory::class); 166*f397d0fdSGreg Roach app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class); 167*f397d0fdSGreg Roach app()->bind(UriFactoryInterface::class, Psr17Factory::class); 168*f397d0fdSGreg Roach } 169*f397d0fdSGreg Roach 170*f397d0fdSGreg Roach /** 171*f397d0fdSGreg Roach * We can use any PSR-7 compatible requests. 172*f397d0fdSGreg Roach * 173*f397d0fdSGreg Roach * @return ServerRequestInterface 174*f397d0fdSGreg Roach */ 175*f397d0fdSGreg Roach public function createServerRequest(): ServerRequestInterface 176*f397d0fdSGreg Roach { 177*f397d0fdSGreg Roach $server_request_creator = new ServerRequestCreator( 178*f397d0fdSGreg Roach app(ServerRequestFactoryInterface::class), 179*f397d0fdSGreg Roach app(UriFactoryInterface::class), 180*f397d0fdSGreg Roach app(UploadedFileFactoryInterface::class), 181*f397d0fdSGreg Roach app(StreamFactoryInterface::class) 182*f397d0fdSGreg Roach ); 183*f397d0fdSGreg Roach 184*f397d0fdSGreg Roach // Until all the code is rewritten to use PSR-7 requests, we still need our hybrid request. 185*f397d0fdSGreg Roach if (class_exists(Request::class)) { 186*f397d0fdSGreg Roach return Request::createFromGlobals(); 187*f397d0fdSGreg Roach } 188*f397d0fdSGreg Roach 189*f397d0fdSGreg Roach return $server_request_creator->fromGlobals(); 190*f397d0fdSGreg Roach 191*f397d0fdSGreg Roach } 192*f397d0fdSGreg Roach 193*f397d0fdSGreg Roach /** 194*f397d0fdSGreg Roach * The webtrees application is built from middleware. 195*f397d0fdSGreg Roach * 196*f397d0fdSGreg Roach * @return MiddlewareInterface[] 197*f397d0fdSGreg Roach */ 198*f397d0fdSGreg Roach public function middleware(): array 199*f397d0fdSGreg Roach { 200*f397d0fdSGreg Roach return [ 201*f397d0fdSGreg Roach PhpEnvironment::class, 202*f397d0fdSGreg Roach EmitResponse::class, 203*f397d0fdSGreg Roach HandleExceptions::class, 204*f397d0fdSGreg Roach CheckForMaintenanceMode::class, 205*f397d0fdSGreg Roach UseDatabase::class, 206*f397d0fdSGreg Roach UseDebugbar::class, 207*f397d0fdSGreg Roach UpdateDatabaseSchema::class, 208*f397d0fdSGreg Roach UseCache::class, 209*f397d0fdSGreg Roach UseFilesystem::class, 210*f397d0fdSGreg Roach UseSession::class, 211*f397d0fdSGreg Roach UseTree::class, 212*f397d0fdSGreg Roach UseLocale::class, 213*f397d0fdSGreg Roach UseTheme::class, 214*f397d0fdSGreg Roach DoHousekeeping::class, 215*f397d0fdSGreg Roach CheckCsrf::class, 216*f397d0fdSGreg Roach UseTransaction::class, 217*f397d0fdSGreg Roach BootModules::class, 218*f397d0fdSGreg Roach ModuleMiddleware::class, 219*f397d0fdSGreg Roach RequestRouter::class, 220*f397d0fdSGreg Roach NoRouteFound::class, 221*f397d0fdSGreg Roach ]; 2228d0ebef0SGreg Roach } 2238d0ebef0SGreg Roach} 224