1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees; 21 22use Closure; 23use ErrorException; 24use Fisharebest\Webtrees\Factories\CacheFactory; 25use Fisharebest\Webtrees\Factories\ElementFactory; 26use Fisharebest\Webtrees\Factories\FamilyFactory; 27use Fisharebest\Webtrees\Factories\FilesystemFactory; 28use Fisharebest\Webtrees\Factories\GedcomRecordFactory; 29use Fisharebest\Webtrees\Factories\HeaderFactory; 30use Fisharebest\Webtrees\Factories\ImageFactory; 31use Fisharebest\Webtrees\Factories\IndividualFactory; 32use Fisharebest\Webtrees\Factories\LocationFactory; 33use Fisharebest\Webtrees\Factories\MarkdownFactory; 34use Fisharebest\Webtrees\Factories\MediaFactory; 35use Fisharebest\Webtrees\Factories\NoteFactory; 36use Fisharebest\Webtrees\Factories\RepositoryFactory; 37use Fisharebest\Webtrees\Factories\SlugFactory; 38use Fisharebest\Webtrees\Factories\SourceFactory; 39use Fisharebest\Webtrees\Factories\SubmissionFactory; 40use Fisharebest\Webtrees\Factories\SubmitterFactory; 41use Fisharebest\Webtrees\Factories\XrefFactory; 42use Fisharebest\Webtrees\Http\Middleware\BadBotBlocker; 43use Fisharebest\Webtrees\Http\Middleware\BootModules; 44use Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode; 45use Fisharebest\Webtrees\Http\Middleware\ClientIp; 46use Fisharebest\Webtrees\Http\Middleware\CompressResponse; 47use Fisharebest\Webtrees\Http\Middleware\ContentLength; 48use Fisharebest\Webtrees\Http\Middleware\DoHousekeeping; 49use Fisharebest\Webtrees\Http\Middleware\EmitResponse; 50use Fisharebest\Webtrees\Http\Middleware\HandleExceptions; 51use Fisharebest\Webtrees\Http\Middleware\LoadRoutes; 52use Fisharebest\Webtrees\Http\Middleware\NoRouteFound; 53use Fisharebest\Webtrees\Http\Middleware\ReadConfigIni; 54use Fisharebest\Webtrees\Http\Middleware\Router; 55use Fisharebest\Webtrees\Http\Middleware\SecurityHeaders; 56use Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema; 57use Fisharebest\Webtrees\Http\Middleware\UseDatabase; 58use Fisharebest\Webtrees\Http\Middleware\UseDebugbar; 59use Fisharebest\Webtrees\Http\Middleware\UseLanguage; 60use Fisharebest\Webtrees\Http\Middleware\UseSession; 61use Fisharebest\Webtrees\Http\Middleware\UseTheme; 62use Fisharebest\Webtrees\Http\Middleware\UseTransaction; 63use Fisharebest\Webtrees\Http\Middleware\BaseUrl; 64use Illuminate\Container\Container; 65use Middleland\Dispatcher; 66use Nyholm\Psr7\Factory\Psr17Factory; 67use Nyholm\Psr7Server\ServerRequestCreator; 68use Psr\Container\ContainerInterface; 69use Psr\Http\Message\ResponseFactoryInterface; 70use Psr\Http\Message\ResponseInterface; 71use Psr\Http\Message\ServerRequestFactoryInterface; 72use Psr\Http\Message\ServerRequestInterface; 73use Psr\Http\Message\StreamFactoryInterface; 74use Psr\Http\Message\UploadedFileFactoryInterface; 75use Psr\Http\Message\UriFactoryInterface; 76use Psr\Http\Server\MiddlewareInterface; 77 78use function date_default_timezone_set; 79use function error_reporting; 80use function is_string; 81use function mb_internal_encoding; 82use function set_error_handler; 83 84use const E_ALL; 85use const E_DEPRECATED; 86use const E_USER_DEPRECATED; 87 88/** 89 * Definitions for the webtrees application. 90 */ 91class Webtrees 92{ 93 // The root folder of this installation 94 public const ROOT_DIR = __DIR__ . '/../'; 95 96 // This is the location of system data, such as temporary and cache files. 97 // The system files are always in this location. 98 // It is also the default location of user data, such as media and GEDCOM files. 99 // The user files could be anywhere supported by Flysystem. 100 public const DATA_DIR = self::ROOT_DIR . 'data/'; 101 102 // Location of the file containing the database connection details. 103 public const CONFIG_FILE = self::DATA_DIR . 'config.ini.php'; 104 105 // Location of the file that triggers maintenance mode. 106 public const OFFLINE_FILE = self::DATA_DIR . 'offline.txt'; 107 108 // Location of our modules. 109 public const MODULES_PATH = 'modules_v4/'; 110 public const MODULES_DIR = self::ROOT_DIR . self::MODULES_PATH; 111 112 // Enable debugging on development builds. 113 public const DEBUG = self::STABILITY !== ''; 114 115 // We want to know about all PHP errors during development, and fewer in production. 116 public const ERROR_REPORTING = self::DEBUG ? E_ALL : E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED; 117 118 // The name of the application. 119 public const NAME = 'webtrees'; 120 121 // Required version of database tables/columns/indexes/etc. 122 public const SCHEMA_VERSION = 45; 123 124 // e.g. "-dev", "-alpha", "-beta", etc. 125 public const STABILITY = '-alpha.2'; 126 127 // Version number 128 public const VERSION = '2.1.0' . self::STABILITY; 129 130 // Project website. 131 public const URL = 'https://webtrees.net/'; 132 133 // FAQ links 134 public const URL_FAQ_EMAIL = 'https://webtrees.net/faq/email'; 135 136 // Project website. 137 public const GEDCOM_PDF = 'https://webtrees.net/downloads/gedcom-5-5-1.pdf'; 138 139 private const MIDDLEWARE = [ 140 EmitResponse::class, 141 SecurityHeaders::class, 142 ReadConfigIni::class, 143 BaseUrl::class, 144 HandleExceptions::class, 145 ClientIp::class, 146 ContentLength::class, 147 CompressResponse::class, 148 BadBotBlocker::class, 149 UseDatabase::class, 150 UseDebugbar::class, 151 UpdateDatabaseSchema::class, 152 UseSession::class, 153 UseLanguage::class, 154 CheckForMaintenanceMode::class, 155 UseTheme::class, 156 DoHousekeeping::class, 157 UseTransaction::class, 158 LoadRoutes::class, 159 BootModules::class, 160 Router::class, 161 NoRouteFound::class, 162 ]; 163 164 /** 165 * Initialise the application. 166 * 167 * @return void 168 */ 169 public function bootstrap(): void 170 { 171 // Show all errors and warnings in development, fewer in production. 172 error_reporting(self::ERROR_REPORTING); 173 set_error_handler($this->phpErrorHandler()); 174 175 // All modern software uses UTF-8 encoding. 176 mb_internal_encoding('UTF-8'); 177 178 // Use UTC internally and convert to local time when displaying datetimes. 179 date_default_timezone_set('UTC'); 180 181 // Factory objects 182 Registry::cache(new CacheFactory()); 183 Registry::familyFactory(new FamilyFactory()); 184 Registry::filesystem(new FilesystemFactory()); 185 Registry::elementFactory(new ElementFactory()); 186 Registry::gedcomRecordFactory(new GedcomRecordFactory()); 187 Registry::headerFactory(new HeaderFactory()); 188 Registry::imageFactory(new ImageFactory()); 189 Registry::individualFactory(new IndividualFactory()); 190 Registry::locationFactory(new LocationFactory()); 191 Registry::markdownFactory(new MarkdownFactory()); 192 Registry::mediaFactory(new MediaFactory()); 193 Registry::noteFactory(new NoteFactory()); 194 Registry::repositoryFactory(new RepositoryFactory()); 195 Registry::slugFactory(new SlugFactory()); 196 Registry::sourceFactory(new SourceFactory()); 197 Registry::submissionFactory(new SubmissionFactory()); 198 Registry::submitterFactory(new SubmitterFactory()); 199 Registry::xrefFactory(new XrefFactory()); 200 } 201 202 /** 203 * Respond to a CLI request. 204 * 205 * @return void 206 */ 207 public function cliRequest(): void 208 { 209 // CLI handler will go here. 210 } 211 212 /** 213 * Respond to an HTTP request. 214 * 215 * @return ResponseInterface 216 */ 217 public function httpRequest(): ResponseInterface 218 { 219 $psr17factory = new Psr17Factory(); 220 221 // PSR7 messages and PSR17 message-factories 222 self::set(ResponseFactoryInterface::class, $psr17factory); 223 self::set(ServerRequestFactoryInterface::class, $psr17factory); 224 self::set(StreamFactoryInterface::class, $psr17factory); 225 self::set(UploadedFileFactoryInterface::class, $psr17factory); 226 self::set(UriFactoryInterface::class, $psr17factory); 227 228 $server_request_creator = new ServerRequestCreator($psr17factory, $psr17factory, $psr17factory, $psr17factory); 229 230 $request = $server_request_creator->fromGlobals(); 231 232 return self::dispatch($request, self::MIDDLEWARE); 233 } 234 235 /** 236 * @param ServerRequestInterface $request 237 * @param array<string|MiddlewareInterface> $middleware 238 * 239 * @return ResponseInterface 240 */ 241 public static function dispatch(ServerRequestInterface $request, array $middleware): ResponseInterface 242 { 243 $dispatcher = new Dispatcher($middleware, self::container()); 244 245 return $dispatcher->dispatch($request); 246 } 247 248 /** 249 * An error handler that can be passed to set_error_handler(). 250 * 251 * @return Closure 252 */ 253 private function phpErrorHandler(): Closure 254 { 255 return static function (int $errno, string $errstr, string $errfile, int $errline): bool { 256 // Ignore errors that are silenced with '@' 257 if (error_reporting() & $errno) { 258 throw new ErrorException($errstr, 0, $errno, $errfile, $errline); 259 } 260 261 return true; 262 }; 263 } 264 265 /** 266 * @return ContainerInterface 267 */ 268 public static function container(): ContainerInterface 269 { 270 return Container::getInstance(); 271 } 272 273 /** 274 * Make an object, using dependency injection. 275 * 276 * @param string $class 277 * 278 * @return mixed 279 */ 280 public static function make(string $class) 281 { 282 return Container::getInstance()->make($class); 283 } 284 285 /** 286 * Write a value into the container. 287 * 288 * @param string $abstract 289 * @param string|object $concrete 290 */ 291 public static function set(string $abstract, $concrete): void 292 { 293 if (is_string($concrete)) { 294 Container::getInstance()->bind($abstract, $concrete); 295 } else { 296 Container::getInstance()->instance($abstract, $concrete); 297 } 298 } 299} 300