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