18d0ebef0SGreg Roach<?php 23976b470SGreg Roach 38d0ebef0SGreg Roach/** 48d0ebef0SGreg Roach * webtrees: online genealogy 590949315SGreg Roach * Copyright (C) 2021 webtrees development team 68d0ebef0SGreg Roach * This program is free software: you can redistribute it and/or modify 78d0ebef0SGreg Roach * it under the terms of the GNU General Public License as published by 88d0ebef0SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98d0ebef0SGreg Roach * (at your option) any later version. 108d0ebef0SGreg Roach * This program is distributed in the hope that it will be useful, 118d0ebef0SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128d0ebef0SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138d0ebef0SGreg Roach * GNU General Public License for more details. 148d0ebef0SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 168d0ebef0SGreg Roach */ 17fcfa147eSGreg Roach 188d0ebef0SGreg Roachdeclare(strict_types=1); 198d0ebef0SGreg Roach 208d0ebef0SGreg Roachnamespace Fisharebest\Webtrees; 218d0ebef0SGreg Roach 22f397d0fdSGreg Roachuse Closure; 23f397d0fdSGreg Roachuse ErrorException; 24785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\CacheFactory; 25f882f05dSGreg Roachuse Fisharebest\Webtrees\Factories\CalendarDateFactory; 26*d97083feSGreg Roachuse Fisharebest\Webtrees\Factories\TimestampFactory; 27785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\ElementFactory; 281c6adce8SGreg Roachuse Fisharebest\Webtrees\Factories\EncodingFactory; 29785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\FamilyFactory; 30785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\FilesystemFactory; 31785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\GedcomRecordFactory; 32785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\HeaderFactory; 33785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\ImageFactory; 34785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\IndividualFactory; 35785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\LocationFactory; 364d35caa7SGreg Roachuse Fisharebest\Webtrees\Factories\MarkdownFactory; 37785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\MediaFactory; 38785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\NoteFactory; 39785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\RepositoryFactory; 40785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SlugFactory; 41785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SourceFactory; 42785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SubmissionFactory; 43785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SubmitterFactory; 44785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\XrefFactory; 45089dadacSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BadBotBlocker; 46f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BootModules; 47f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode; 484874f72dSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ClientIp; 49179fae31SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CompressResponse; 50d1da5ba4SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ContentLength; 51f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\DoHousekeeping; 52f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\EmitResponse; 53f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\HandleExceptions; 5471378461SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\LoadRoutes; 55f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\NoRouteFound; 56e16a1bfdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ReadConfigIni; 579e5d8e6fSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\Router; 589c88b4e9SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\SecurityHeaders; 59f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema; 60f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseDatabase; 61150f35adSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseLanguage; 62f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseSession; 63f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseTheme; 64f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseTransaction; 654874f72dSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BaseUrl; 661c6adce8SGreg Roachuse Fisharebest\Webtrees\GedcomFilters\GedcomEncodingFilter; 67785274b8SGreg Roachuse Illuminate\Container\Container; 68785274b8SGreg Roachuse Middleland\Dispatcher; 69f397d0fdSGreg Roachuse Nyholm\Psr7\Factory\Psr17Factory; 70785274b8SGreg Roachuse Nyholm\Psr7Server\ServerRequestCreator; 71785274b8SGreg Roachuse Psr\Container\ContainerInterface; 7200c45d23SGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 73785274b8SGreg Roachuse Psr\Http\Message\ResponseInterface; 7400c45d23SGreg Roachuse Psr\Http\Message\ServerRequestFactoryInterface; 75785274b8SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 7600c45d23SGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 7700c45d23SGreg Roachuse Psr\Http\Message\UploadedFileFactoryInterface; 7800c45d23SGreg Roachuse Psr\Http\Message\UriFactoryInterface; 79785274b8SGreg Roachuse Psr\Http\Server\MiddlewareInterface; 803976b470SGreg Roach 81785274b8SGreg Roachuse function date_default_timezone_set; 828d0ebef0SGreg Roachuse function error_reporting; 83785274b8SGreg Roachuse function is_string; 84785274b8SGreg Roachuse function mb_internal_encoding; 858d0ebef0SGreg Roachuse function set_error_handler; 861c6adce8SGreg Roachuse function stream_filter_register; 878d0ebef0SGreg Roach 88721424feSGreg Roachuse const E_ALL; 89721424feSGreg Roachuse const E_DEPRECATED; 90054771e9SGreg Roachuse const E_USER_DEPRECATED; 91054771e9SGreg Roach 928d0ebef0SGreg Roach/** 938d0ebef0SGreg Roach * Definitions for the webtrees application. 948d0ebef0SGreg Roach */ 958d0ebef0SGreg Roachclass Webtrees 968d0ebef0SGreg Roach{ 97f397d0fdSGreg Roach // The root folder of this installation 98f397d0fdSGreg Roach public const ROOT_DIR = __DIR__ . '/../'; 99f397d0fdSGreg Roach 100baef639bSGreg Roach // This is the location of system data, such as temporary and cache files. 101baef639bSGreg Roach // The system files are always in this location. 102baef639bSGreg Roach // It is also the default location of user data, such as media and GEDCOM files. 103baef639bSGreg Roach // The user files could be anywhere supported by Flysystem. 104fd6c003fSGreg Roach public const DATA_DIR = self::ROOT_DIR . 'data/'; 105fd6c003fSGreg Roach 1068d0ebef0SGreg Roach // Location of the file containing the database connection details. 107baef639bSGreg Roach public const CONFIG_FILE = self::DATA_DIR . 'config.ini.php'; 108f397d0fdSGreg Roach 109f397d0fdSGreg Roach // Location of the file that triggers maintenance mode. 110baef639bSGreg Roach public const OFFLINE_FILE = self::DATA_DIR . 'offline.txt'; 111f397d0fdSGreg Roach 112f397d0fdSGreg Roach // Location of our modules. 113b50f90f8SGreg Roach public const MODULES_PATH = 'modules_v4/'; 114f397d0fdSGreg Roach public const MODULES_DIR = self::ROOT_DIR . self::MODULES_PATH; 1158d0ebef0SGreg Roach 1168d0ebef0SGreg Roach // Enable debugging on development builds. 11716d6367aSGreg Roach public const DEBUG = self::STABILITY !== ''; 1188d0ebef0SGreg Roach 1198d0ebef0SGreg Roach // We want to know about all PHP errors during development, and fewer in production. 120721424feSGreg Roach public const ERROR_REPORTING = self::DEBUG ? E_ALL : E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED; 1218d0ebef0SGreg Roach 1228d0ebef0SGreg Roach // The name of the application. 12316d6367aSGreg Roach public const NAME = 'webtrees'; 1248d0ebef0SGreg Roach 1258d0ebef0SGreg Roach // Required version of database tables/columns/indexes/etc. 12690949315SGreg Roach public const SCHEMA_VERSION = 45; 1278d0ebef0SGreg Roach 128721424feSGreg Roach // e.g. "-dev", "-alpha", "-beta", etc. 1295b793bd1SGreg Roach public const STABILITY = '-alpha.2'; 1308d0ebef0SGreg Roach 1318d0ebef0SGreg Roach // Version number 132f32d77e6SGreg Roach public const VERSION = '2.1.0' . self::STABILITY; 1338d0ebef0SGreg Roach 134f397d0fdSGreg Roach // Project website. 135db1aabc1SGreg Roach public const URL = 'https://webtrees.net/'; 1368d0ebef0SGreg Roach 137fceda430SGreg Roach // FAQ links 1383282ccecSGreg Roach public const URL_FAQ_EMAIL = 'https://webtrees.net/faq/email'; 1393282ccecSGreg Roach 1409d7cdf93SGreg Roach // Project website. 1410f3c4769SGreg Roach public const GEDCOM_PDF = 'https://webtrees.net/downloads/gedcom-5-5-1.pdf'; 1429d7cdf93SGreg Roach 14382e92bfaSGreg Roach private const MIDDLEWARE = [ 14482e92bfaSGreg Roach EmitResponse::class, 1459c88b4e9SGreg Roach SecurityHeaders::class, 14682e92bfaSGreg Roach ReadConfigIni::class, 1474874f72dSGreg Roach BaseUrl::class, 1485fb051e9SGreg Roach HandleExceptions::class, 1494874f72dSGreg Roach ClientIp::class, 150d1da5ba4SGreg Roach ContentLength::class, 151179fae31SGreg Roach CompressResponse::class, 152089dadacSGreg Roach BadBotBlocker::class, 15382e92bfaSGreg Roach UseDatabase::class, 15482e92bfaSGreg Roach UpdateDatabaseSchema::class, 15582e92bfaSGreg Roach UseSession::class, 156150f35adSGreg Roach UseLanguage::class, 15782e92bfaSGreg Roach CheckForMaintenanceMode::class, 15882e92bfaSGreg Roach UseTheme::class, 15982e92bfaSGreg Roach DoHousekeeping::class, 16082e92bfaSGreg Roach UseTransaction::class, 16171378461SGreg Roach LoadRoutes::class, 16282e92bfaSGreg Roach BootModules::class, 16382e92bfaSGreg Roach Router::class, 16482e92bfaSGreg Roach NoRouteFound::class, 16582e92bfaSGreg Roach ]; 16682e92bfaSGreg Roach 1678d0ebef0SGreg Roach /** 16800c45d23SGreg Roach * Initialise the application. 16900c45d23SGreg Roach * 1708d0ebef0SGreg Roach * @return void 1718d0ebef0SGreg Roach */ 17200c45d23SGreg Roach public function bootstrap(): void 1738d0ebef0SGreg Roach { 1748d0ebef0SGreg Roach // Show all errors and warnings in development, fewer in production. 175cfbf56adSGreg Roach error_reporting(self::ERROR_REPORTING); 176f397d0fdSGreg Roach set_error_handler($this->phpErrorHandler()); 177785274b8SGreg Roach 178785274b8SGreg Roach // All modern software uses UTF-8 encoding. 179785274b8SGreg Roach mb_internal_encoding('UTF-8'); 180785274b8SGreg Roach 181785274b8SGreg Roach // Use UTC internally and convert to local time when displaying datetimes. 182785274b8SGreg Roach date_default_timezone_set('UTC'); 183785274b8SGreg Roach 184785274b8SGreg Roach // Factory objects 185785274b8SGreg Roach Registry::cache(new CacheFactory()); 186f882f05dSGreg Roach Registry::calendarDateFactory(new CalendarDateFactory()); 187785274b8SGreg Roach Registry::elementFactory(new ElementFactory()); 1881c6adce8SGreg Roach Registry::encodingFactory(new EncodingFactory()); 189*d97083feSGreg Roach Registry::familyFactory(new FamilyFactory()); 190*d97083feSGreg Roach Registry::filesystem(new FilesystemFactory()); 191785274b8SGreg Roach Registry::gedcomRecordFactory(new GedcomRecordFactory()); 192785274b8SGreg Roach Registry::headerFactory(new HeaderFactory()); 193785274b8SGreg Roach Registry::imageFactory(new ImageFactory()); 194785274b8SGreg Roach Registry::individualFactory(new IndividualFactory()); 195785274b8SGreg Roach Registry::locationFactory(new LocationFactory()); 1964d35caa7SGreg Roach Registry::markdownFactory(new MarkdownFactory()); 197785274b8SGreg Roach Registry::mediaFactory(new MediaFactory()); 198785274b8SGreg Roach Registry::noteFactory(new NoteFactory()); 199785274b8SGreg Roach Registry::repositoryFactory(new RepositoryFactory()); 200785274b8SGreg Roach Registry::slugFactory(new SlugFactory()); 201785274b8SGreg Roach Registry::sourceFactory(new SourceFactory()); 202785274b8SGreg Roach Registry::submissionFactory(new SubmissionFactory()); 203785274b8SGreg Roach Registry::submitterFactory(new SubmitterFactory()); 204*d97083feSGreg Roach Registry::timestampFactory(new TimestampFactory()); 205785274b8SGreg Roach Registry::xrefFactory(new XrefFactory()); 2061c6adce8SGreg Roach 2071c6adce8SGreg Roach stream_filter_register(GedcomEncodingFilter::class, GedcomEncodingFilter::class); 208785274b8SGreg Roach } 209785274b8SGreg Roach 210785274b8SGreg Roach /** 211785274b8SGreg Roach * Respond to a CLI request. 212785274b8SGreg Roach * 213785274b8SGreg Roach * @return void 214785274b8SGreg Roach */ 215785274b8SGreg Roach public function cliRequest(): void 216785274b8SGreg Roach { 217785274b8SGreg Roach // CLI handler will go here. 218785274b8SGreg Roach } 219785274b8SGreg Roach 220785274b8SGreg Roach /** 221dbfdfe6bSGreg Roach * Respond to an HTTP request. 222785274b8SGreg Roach * 223785274b8SGreg Roach * @return ResponseInterface 224785274b8SGreg Roach */ 225785274b8SGreg Roach public function httpRequest(): ResponseInterface 226785274b8SGreg Roach { 2274348fc02SGreg Roach $psr17factory = new Psr17Factory(); 228785274b8SGreg Roach 2294348fc02SGreg Roach // PSR7 messages and PSR17 message-factories 2304348fc02SGreg Roach self::set(ResponseFactoryInterface::class, $psr17factory); 2314348fc02SGreg Roach self::set(ServerRequestFactoryInterface::class, $psr17factory); 2324348fc02SGreg Roach self::set(StreamFactoryInterface::class, $psr17factory); 2334348fc02SGreg Roach self::set(UploadedFileFactoryInterface::class, $psr17factory); 2344348fc02SGreg Roach self::set(UriFactoryInterface::class, $psr17factory); 2354348fc02SGreg Roach 2364348fc02SGreg Roach $server_request_creator = new ServerRequestCreator($psr17factory, $psr17factory, $psr17factory, $psr17factory); 2374348fc02SGreg Roach 2384348fc02SGreg Roach $request = $server_request_creator->fromGlobals(); 239785274b8SGreg Roach 240785274b8SGreg Roach return self::dispatch($request, self::MIDDLEWARE); 241785274b8SGreg Roach } 242785274b8SGreg Roach 243785274b8SGreg Roach /** 244785274b8SGreg Roach * @param ServerRequestInterface $request 245785274b8SGreg Roach * @param array<string|MiddlewareInterface> $middleware 246785274b8SGreg Roach * 247785274b8SGreg Roach * @return ResponseInterface 248785274b8SGreg Roach */ 249785274b8SGreg Roach public static function dispatch(ServerRequestInterface $request, array $middleware): ResponseInterface 250785274b8SGreg Roach { 251785274b8SGreg Roach $dispatcher = new Dispatcher($middleware, self::container()); 252785274b8SGreg Roach 253785274b8SGreg Roach return $dispatcher->dispatch($request); 254f397d0fdSGreg Roach } 2558d0ebef0SGreg Roach 256f397d0fdSGreg Roach /** 257f397d0fdSGreg Roach * An error handler that can be passed to set_error_handler(). 258f397d0fdSGreg Roach * 259f397d0fdSGreg Roach * @return Closure 260f397d0fdSGreg Roach */ 261f397d0fdSGreg Roach private function phpErrorHandler(): Closure 262f397d0fdSGreg Roach { 263f397d0fdSGreg Roach return static function (int $errno, string $errstr, string $errfile, int $errline): bool { 264f397d0fdSGreg Roach // Ignore errors that are silenced with '@' 265f397d0fdSGreg Roach if (error_reporting() & $errno) { 266f397d0fdSGreg Roach throw new ErrorException($errstr, 0, $errno, $errfile, $errline); 267f397d0fdSGreg Roach } 268f397d0fdSGreg Roach 269f397d0fdSGreg Roach return true; 270f397d0fdSGreg Roach }; 271f397d0fdSGreg Roach } 272f397d0fdSGreg Roach 273f397d0fdSGreg Roach /** 274785274b8SGreg Roach * @return ContainerInterface 275f397d0fdSGreg Roach */ 276785274b8SGreg Roach public static function container(): ContainerInterface 277f397d0fdSGreg Roach { 278785274b8SGreg Roach return Container::getInstance(); 279785274b8SGreg Roach } 280785274b8SGreg Roach 281785274b8SGreg Roach /** 282785274b8SGreg Roach * Make an object, using dependency injection. 283785274b8SGreg Roach * 284785274b8SGreg Roach * @param string $class 285785274b8SGreg Roach * 286785274b8SGreg Roach * @return mixed 287785274b8SGreg Roach */ 288785274b8SGreg Roach public static function make(string $class) 289785274b8SGreg Roach { 290785274b8SGreg Roach return Container::getInstance()->make($class); 291785274b8SGreg Roach } 292785274b8SGreg Roach 293785274b8SGreg Roach /** 294785274b8SGreg Roach * Write a value into the container. 295785274b8SGreg Roach * 296785274b8SGreg Roach * @param string $abstract 297785274b8SGreg Roach * @param string|object $concrete 298785274b8SGreg Roach */ 299785274b8SGreg Roach public static function set(string $abstract, $concrete): void 300785274b8SGreg Roach { 301785274b8SGreg Roach if (is_string($concrete)) { 302785274b8SGreg Roach Container::getInstance()->bind($abstract, $concrete); 303785274b8SGreg Roach } else { 304785274b8SGreg Roach Container::getInstance()->instance($abstract, $concrete); 305785274b8SGreg Roach } 3068d0ebef0SGreg Roach } 3078d0ebef0SGreg Roach} 308