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