1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17declare(strict_types=1); 18 19namespace Fisharebest\Webtrees; 20 21use Closure; 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\ClientIp; 27use Fisharebest\Webtrees\Http\Middleware\DoHousekeeping; 28use Fisharebest\Webtrees\Http\Middleware\EmitResponse; 29use Fisharebest\Webtrees\Http\Middleware\HandleExceptions; 30use Fisharebest\Webtrees\Http\Middleware\LoadRoutes; 31use Fisharebest\Webtrees\Http\Middleware\NoRouteFound; 32use Fisharebest\Webtrees\Http\Middleware\PhpEnvironment; 33use Fisharebest\Webtrees\Http\Middleware\ReadConfigIni; 34use Fisharebest\Webtrees\Http\Middleware\Router; 35use Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema; 36use Fisharebest\Webtrees\Http\Middleware\UseCache; 37use Fisharebest\Webtrees\Http\Middleware\UseDatabase; 38use Fisharebest\Webtrees\Http\Middleware\UseDebugbar; 39use Fisharebest\Webtrees\Http\Middleware\UseFilesystem; 40use Fisharebest\Webtrees\Http\Middleware\UseLocale; 41use Fisharebest\Webtrees\Http\Middleware\UseSession; 42use Fisharebest\Webtrees\Http\Middleware\UseTheme; 43use Fisharebest\Webtrees\Http\Middleware\UseTransaction; 44use Fisharebest\Webtrees\Http\Middleware\BaseUrl; 45use Nyholm\Psr7\Factory\Psr17Factory; 46use Psr\Http\Message\ResponseFactoryInterface; 47use Psr\Http\Message\ServerRequestFactoryInterface; 48use Psr\Http\Message\StreamFactoryInterface; 49use Psr\Http\Message\UploadedFileFactoryInterface; 50use Psr\Http\Message\UriFactoryInterface; 51use Throwable; 52 53use function app; 54use function dirname; 55use function error_reporting; 56use function ob_end_clean; 57use function ob_get_level; 58use function set_error_handler; 59use function set_exception_handler; 60use function str_replace; 61 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 // Some code needs a local filesystem, e.g. for caching. 73 public const DATA_DIR = self::ROOT_DIR . 'data/'; 74 75 // Location of the file containing the database connection details. 76 public const CONFIG_FILE = self::ROOT_DIR . 'data/config.ini.php'; 77 78 // Location of the file that triggers maintenance mode. 79 public const OFFLINE_FILE = self::ROOT_DIR . 'data/offline.txt'; 80 81 // Location of our modules. 82 public const MODULES_PATH = 'modules_v4/'; 83 public const MODULES_DIR = self::ROOT_DIR . self::MODULES_PATH; 84 85 // Enable debugging on development builds. 86 public const DEBUG = self::STABILITY !== ''; 87 88 // We want to know about all PHP errors during development, and fewer in production. 89 public const ERROR_REPORTING = self::DEBUG ? E_ALL | E_STRICT | E_NOTICE | E_DEPRECATED : E_ALL; 90 91 // The name of the application. 92 public const NAME = 'webtrees'; 93 94 // Required version of database tables/columns/indexes/etc. 95 public const SCHEMA_VERSION = 43; 96 97 // e.g. "dev", "alpha", "beta", etc. 98 public const STABILITY = 'beta.4'; 99 100 // Version number 101 public const VERSION = '2.0.0' . (self::STABILITY === '' ? '' : '-') . self::STABILITY; 102 103 // Project website. 104 public const URL = 'https://www.webtrees.net/'; 105 106 private const MIDDLEWARE = [ 107 PhpEnvironment::class, 108 EmitResponse::class, 109 HandleExceptions::class, 110 ReadConfigIni::class, 111 BaseUrl::class, 112 ClientIp::class, 113 UseDatabase::class, 114 UseDebugbar::class, 115 UpdateDatabaseSchema::class, 116 UseCache::class, 117 UseFilesystem::class, 118 UseSession::class, 119 UseLocale::class, 120 CheckForMaintenanceMode::class, 121 UseTheme::class, 122 DoHousekeeping::class, 123 CheckCsrf::class, 124 UseTransaction::class, 125 LoadRoutes::class, 126 BootModules::class, 127 Router::class, 128 NoRouteFound::class, 129 ]; 130 131 /** 132 * Initialise the application. 133 * 134 * @return void 135 */ 136 public function bootstrap(): void 137 { 138 // Show all errors and warnings in development, fewer in production. 139 error_reporting(self::ERROR_REPORTING); 140 141 set_error_handler($this->phpErrorHandler()); 142 set_exception_handler($this->phpExceptionHandler()); 143 } 144 145 /** 146 * An error handler that can be passed to set_error_handler(). 147 * 148 * @return Closure 149 */ 150 private function phpErrorHandler(): Closure 151 { 152 return static function (int $errno, string $errstr, string $errfile, int $errline): bool { 153 // Ignore errors that are silenced with '@' 154 if (error_reporting() & $errno) { 155 throw new ErrorException($errstr, 0, $errno, $errfile, $errline); 156 } 157 158 return true; 159 }; 160 } 161 162 /** 163 * An exception handler that can be passed to set_exception_handler(). 164 * Display any exception that are not caught by the middleware exception handler. 165 * 166 * @return Closure 167 */ 168 private function phpExceptionHandler(): Closure 169 { 170 return static function (Throwable $ex): void { 171 $base_path = dirname(__DIR__); 172 $trace = $ex->getMessage() . ' ' . $ex->getFile() . ':' . $ex->getLine() . PHP_EOL . $ex->getTraceAsString(); 173 $trace = str_replace($base_path, '…', $trace); 174 175 while (ob_get_level() > 0) { 176 ob_end_clean(); 177 } 178 179 echo '<html lang="en"><head><title>Error</title><meta charset="UTF-8"></head><body><pre>' . $trace . '</pre></body></html>'; 180 }; 181 } 182 183 /** 184 * We can use any PSR-7 / PSR-17 compatible message factory. 185 * 186 * @return void 187 */ 188 public function selectMessageFactory(): void 189 { 190 app()->bind(ResponseFactoryInterface::class, Psr17Factory::class); 191 app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class); 192 app()->bind(StreamFactoryInterface::class, Psr17Factory::class); 193 app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class); 194 app()->bind(UriFactoryInterface::class, Psr17Factory::class); 195 } 196 197 /** 198 * The webtrees application is built from middleware. 199 * 200 * @return string[] 201 */ 202 public function middleware(): array 203 { 204 return self::MIDDLEWARE; 205 } 206} 207