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