18d0ebef0SGreg Roach<?php 23976b470SGreg Roach 38d0ebef0SGreg Roach/** 48d0ebef0SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 68d0ebef0SGreg Roach * This program is free software: you can redistribute it and/or modify 78d0ebef0SGreg Roach * it under the terms of the GNU General Public License as published by 88d0ebef0SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98d0ebef0SGreg Roach * (at your option) any later version. 108d0ebef0SGreg Roach * This program is distributed in the hope that it will be useful, 118d0ebef0SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128d0ebef0SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138d0ebef0SGreg Roach * GNU General Public License for more details. 148d0ebef0SGreg Roach * You should have received a copy of the GNU General Public License 158d0ebef0SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 168d0ebef0SGreg Roach */ 178d0ebef0SGreg Roachdeclare(strict_types=1); 188d0ebef0SGreg Roach 198d0ebef0SGreg Roachnamespace Fisharebest\Webtrees; 208d0ebef0SGreg Roach 21f397d0fdSGreg Roachuse Closure; 22f397d0fdSGreg Roachuse ErrorException; 23f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BootModules; 24f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CheckCsrf; 25f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode; 264874f72dSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ClientIp; 27f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\DoHousekeeping; 28f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\EmitResponse; 29f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\HandleExceptions; 3071378461SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\LoadRoutes; 31f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\NoRouteFound; 32f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\PhpEnvironment; 33e16a1bfdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ReadConfigIni; 349e5d8e6fSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\Router; 35f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema; 36f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseCache; 37f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseDatabase; 38f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseDebugbar; 39f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseFilesystem; 40f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseLocale; 41f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseSession; 42f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseTheme; 43f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseTransaction; 444874f72dSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BaseUrl; 45f397d0fdSGreg Roachuse Nyholm\Psr7\Factory\Psr17Factory; 46f397d0fdSGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 47f397d0fdSGreg Roachuse Psr\Http\Message\ServerRequestFactoryInterface; 48f397d0fdSGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 49f397d0fdSGreg Roachuse Psr\Http\Message\UploadedFileFactoryInterface; 50f397d0fdSGreg Roachuse Psr\Http\Message\UriFactoryInterface; 51f397d0fdSGreg Roachuse Throwable; 523976b470SGreg Roach 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; 613976b470SGreg Roach 62f397d0fdSGreg Roachuse const PHP_EOL; 638d0ebef0SGreg Roach 648d0ebef0SGreg Roach/** 658d0ebef0SGreg Roach * Definitions for the webtrees application. 668d0ebef0SGreg Roach */ 678d0ebef0SGreg Roachclass Webtrees 688d0ebef0SGreg Roach{ 69f397d0fdSGreg Roach // The root folder of this installation 70f397d0fdSGreg Roach public const ROOT_DIR = __DIR__ . '/../'; 71f397d0fdSGreg Roach 72*fd6c003fSGreg Roach // Some code needs a local filesystem, e.g. for caching. 73*fd6c003fSGreg Roach public const DATA_DIR = self::ROOT_DIR . 'data/'; 74*fd6c003fSGreg Roach 758d0ebef0SGreg Roach // Location of the file containing the database connection details. 762e9a57f7SGreg Roach public const CONFIG_FILE = self::ROOT_DIR . 'data/config.ini.php'; 77f397d0fdSGreg Roach 78f397d0fdSGreg Roach // Location of the file that triggers maintenance mode. 792e9a57f7SGreg Roach public const OFFLINE_FILE = self::ROOT_DIR . 'data/offline.txt'; 80f397d0fdSGreg Roach 81f397d0fdSGreg Roach // Location of our modules. 82b50f90f8SGreg Roach public const MODULES_PATH = 'modules_v4/'; 83f397d0fdSGreg Roach public const MODULES_DIR = self::ROOT_DIR . self::MODULES_PATH; 848d0ebef0SGreg Roach 858d0ebef0SGreg Roach // Enable debugging on development builds. 8616d6367aSGreg Roach public const DEBUG = self::STABILITY !== ''; 878d0ebef0SGreg Roach 888d0ebef0SGreg Roach // We want to know about all PHP errors during development, and fewer in production. 8916d6367aSGreg Roach public const ERROR_REPORTING = self::DEBUG ? E_ALL | E_STRICT | E_NOTICE | E_DEPRECATED : E_ALL; 908d0ebef0SGreg Roach 918d0ebef0SGreg Roach // The name of the application. 9216d6367aSGreg Roach public const NAME = 'webtrees'; 938d0ebef0SGreg Roach 948d0ebef0SGreg Roach // Required version of database tables/columns/indexes/etc. 9567992b6aSRichard Cissee public const SCHEMA_VERSION = 43; 968d0ebef0SGreg Roach 979ba014a7SGreg Roach // e.g. "dev", "alpha", "beta", etc. 989ba014a7SGreg Roach public const STABILITY = 'beta.4'; 998d0ebef0SGreg Roach 1008d0ebef0SGreg Roach // Version number 10116d6367aSGreg Roach public const VERSION = '2.0.0' . (self::STABILITY === '' ? '' : '-') . self::STABILITY; 1028d0ebef0SGreg Roach 103f397d0fdSGreg Roach // Project website. 104f397d0fdSGreg Roach public const URL = 'https://www.webtrees.net/'; 1058d0ebef0SGreg Roach 10682e92bfaSGreg Roach private const MIDDLEWARE = [ 10782e92bfaSGreg Roach PhpEnvironment::class, 10882e92bfaSGreg Roach EmitResponse::class, 10982e92bfaSGreg Roach HandleExceptions::class, 11082e92bfaSGreg Roach ReadConfigIni::class, 1114874f72dSGreg Roach BaseUrl::class, 1124874f72dSGreg Roach ClientIp::class, 11382e92bfaSGreg Roach UseDatabase::class, 11482e92bfaSGreg Roach UseDebugbar::class, 11582e92bfaSGreg Roach UpdateDatabaseSchema::class, 11682e92bfaSGreg Roach UseCache::class, 11782e92bfaSGreg Roach UseFilesystem::class, 11882e92bfaSGreg Roach UseSession::class, 11982e92bfaSGreg Roach UseLocale::class, 12082e92bfaSGreg Roach CheckForMaintenanceMode::class, 12182e92bfaSGreg Roach UseTheme::class, 12282e92bfaSGreg Roach DoHousekeeping::class, 12382e92bfaSGreg Roach CheckCsrf::class, 12482e92bfaSGreg Roach UseTransaction::class, 12571378461SGreg Roach LoadRoutes::class, 12682e92bfaSGreg Roach BootModules::class, 12782e92bfaSGreg Roach Router::class, 12882e92bfaSGreg Roach NoRouteFound::class, 12982e92bfaSGreg Roach ]; 13082e92bfaSGreg Roach 1318d0ebef0SGreg Roach /** 1328d0ebef0SGreg Roach * Initialise the application. 1338d0ebef0SGreg Roach * 1348d0ebef0SGreg Roach * @return void 1358d0ebef0SGreg Roach */ 136f397d0fdSGreg Roach public function bootstrap(): void 1378d0ebef0SGreg Roach { 1388d0ebef0SGreg Roach // Show all errors and warnings in development, fewer in production. 139cfbf56adSGreg Roach error_reporting(self::ERROR_REPORTING); 1408d0ebef0SGreg Roach 141f397d0fdSGreg Roach set_error_handler($this->phpErrorHandler()); 142f397d0fdSGreg Roach set_exception_handler($this->phpExceptionHandler()); 143f397d0fdSGreg Roach } 1448d0ebef0SGreg Roach 145f397d0fdSGreg Roach /** 146f397d0fdSGreg Roach * An error handler that can be passed to set_error_handler(). 147f397d0fdSGreg Roach * 148f397d0fdSGreg Roach * @return Closure 149f397d0fdSGreg Roach */ 150f397d0fdSGreg Roach private function phpErrorHandler(): Closure 151f397d0fdSGreg Roach { 152f397d0fdSGreg Roach return static function (int $errno, string $errstr, string $errfile, int $errline): bool { 153f397d0fdSGreg Roach // Ignore errors that are silenced with '@' 154f397d0fdSGreg Roach if (error_reporting() & $errno) { 155f397d0fdSGreg Roach throw new ErrorException($errstr, 0, $errno, $errfile, $errline); 156f397d0fdSGreg Roach } 157f397d0fdSGreg Roach 158f397d0fdSGreg Roach return true; 159f397d0fdSGreg Roach }; 160f397d0fdSGreg Roach } 161f397d0fdSGreg Roach 162f397d0fdSGreg Roach /** 163f397d0fdSGreg Roach * An exception handler that can be passed to set_exception_handler(). 164f397d0fdSGreg Roach * Display any exception that are not caught by the middleware exception handler. 165f397d0fdSGreg Roach * 166f397d0fdSGreg Roach * @return Closure 167f397d0fdSGreg Roach */ 168f397d0fdSGreg Roach private function phpExceptionHandler(): Closure 169f397d0fdSGreg Roach { 170f397d0fdSGreg Roach return static function (Throwable $ex): void { 171f397d0fdSGreg Roach $base_path = dirname(__DIR__); 172c133c283SGreg Roach $trace = $ex->getMessage() . ' ' . $ex->getFile() . ':' . $ex->getLine() . PHP_EOL . $ex->getTraceAsString(); 173f397d0fdSGreg Roach $trace = str_replace($base_path, '…', $trace); 174f397d0fdSGreg Roach 175f397d0fdSGreg Roach while (ob_get_level() > 0) { 176f397d0fdSGreg Roach ob_end_clean(); 177f397d0fdSGreg Roach } 178f397d0fdSGreg Roach 179f397d0fdSGreg Roach echo '<html lang="en"><head><title>Error</title><meta charset="UTF-8"></head><body><pre>' . $trace . '</pre></body></html>'; 180f397d0fdSGreg Roach }; 181f397d0fdSGreg Roach } 182f397d0fdSGreg Roach 183f397d0fdSGreg Roach /** 184f397d0fdSGreg Roach * We can use any PSR-7 / PSR-17 compatible message factory. 185f397d0fdSGreg Roach * 186f397d0fdSGreg Roach * @return void 187f397d0fdSGreg Roach */ 188f397d0fdSGreg Roach public function selectMessageFactory(): void 189f397d0fdSGreg Roach { 190f397d0fdSGreg Roach app()->bind(ResponseFactoryInterface::class, Psr17Factory::class); 191f397d0fdSGreg Roach app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class); 192f397d0fdSGreg Roach app()->bind(StreamFactoryInterface::class, Psr17Factory::class); 193f397d0fdSGreg Roach app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class); 194f397d0fdSGreg Roach app()->bind(UriFactoryInterface::class, Psr17Factory::class); 195f397d0fdSGreg Roach } 196f397d0fdSGreg Roach 197f397d0fdSGreg Roach /** 198f397d0fdSGreg Roach * The webtrees application is built from middleware. 199f397d0fdSGreg Roach * 2007770f26cSGreg Roach * @return string[] 201f397d0fdSGreg Roach */ 202f397d0fdSGreg Roach public function middleware(): array 203f397d0fdSGreg Roach { 20482e92bfaSGreg Roach return self::MIDDLEWARE; 2058d0ebef0SGreg Roach } 2068d0ebef0SGreg Roach} 207