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