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