18d0ebef0SGreg Roach<?php 23976b470SGreg Roach 38d0ebef0SGreg Roach/** 48d0ebef0SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 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; 24*d35568b4SGreg Roachuse Fisharebest\Webtrees\Contracts\ContainerInterface; 25785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\CacheFactory; 26f882f05dSGreg Roachuse Fisharebest\Webtrees\Factories\CalendarDateFactory; 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; 33f0c88a96SGreg Roachuse Fisharebest\Webtrees\Factories\IdFactory; 34785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\ImageFactory; 35785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\IndividualFactory; 36785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\LocationFactory; 374d35caa7SGreg Roachuse Fisharebest\Webtrees\Factories\MarkdownFactory; 38785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\MediaFactory; 39785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\NoteFactory; 40785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\RepositoryFactory; 41208909d8SGreg Roachuse Fisharebest\Webtrees\Factories\ResponseFactory; 42208909d8SGreg Roachuse Fisharebest\Webtrees\Factories\RouteFactory; 43701f5d18SGreg Roachuse Fisharebest\Webtrees\Factories\SharedNoteFactory; 44785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SlugFactory; 45785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SourceFactory; 46785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SubmissionFactory; 47785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SubmitterFactory; 487e128bbfSGreg Roachuse Fisharebest\Webtrees\Factories\SurnameTraditionFactory; 49eeec557aSGreg Roachuse Fisharebest\Webtrees\Factories\TimeFactory; 50208909d8SGreg Roachuse Fisharebest\Webtrees\Factories\TimestampFactory; 51785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\XrefFactory; 52158900c2SGreg Roachuse Fisharebest\Webtrees\GedcomFilters\GedcomEncodingFilter; 53089dadacSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BadBotBlocker; 54158900c2SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BaseUrl; 55f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BootModules; 56f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode; 57f37874adSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CheckForNewVersion; 584874f72dSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ClientIp; 59179fae31SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CompressResponse; 60d1da5ba4SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ContentLength; 61f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\DoHousekeeping; 62f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\EmitResponse; 63f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\HandleExceptions; 6471378461SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\LoadRoutes; 65f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\NoRouteFound; 66e16a1bfdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ReadConfigIni; 6700c92694SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\RegisterGedcomTags; 689e5d8e6fSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\Router; 699c88b4e9SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\SecurityHeaders; 70f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema; 71f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseDatabase; 72150f35adSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseLanguage; 73f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseSession; 74f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseTheme; 75f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseTransaction; 76785274b8SGreg Roachuse Middleland\Dispatcher; 77f397d0fdSGreg Roachuse Nyholm\Psr7\Factory\Psr17Factory; 78785274b8SGreg Roachuse Nyholm\Psr7Server\ServerRequestCreator; 7900c45d23SGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 80785274b8SGreg Roachuse Psr\Http\Message\ResponseInterface; 8100c45d23SGreg Roachuse Psr\Http\Message\ServerRequestFactoryInterface; 82785274b8SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 8300c45d23SGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 8400c45d23SGreg Roachuse Psr\Http\Message\UploadedFileFactoryInterface; 8500c45d23SGreg Roachuse Psr\Http\Message\UriFactoryInterface; 86785274b8SGreg Roachuse Psr\Http\Server\MiddlewareInterface; 873976b470SGreg Roach 88785274b8SGreg Roachuse function date_default_timezone_set; 898d0ebef0SGreg Roachuse function error_reporting; 90785274b8SGreg Roachuse function is_string; 91785274b8SGreg Roachuse function mb_internal_encoding; 928d0ebef0SGreg Roachuse function set_error_handler; 931c6adce8SGreg Roachuse function stream_filter_register; 948d0ebef0SGreg Roach 95721424feSGreg Roachuse const E_ALL; 96721424feSGreg Roachuse const E_DEPRECATED; 97054771e9SGreg Roachuse const E_USER_DEPRECATED; 98054771e9SGreg Roach 998d0ebef0SGreg Roach/** 1008d0ebef0SGreg Roach * Definitions for the webtrees application. 1018d0ebef0SGreg Roach */ 1028d0ebef0SGreg Roachclass Webtrees 1038d0ebef0SGreg Roach{ 104f397d0fdSGreg Roach // The root folder of this installation 105f397d0fdSGreg Roach public const ROOT_DIR = __DIR__ . '/../'; 106f397d0fdSGreg Roach 107baef639bSGreg Roach // This is the location of system data, such as temporary and cache files. 108baef639bSGreg Roach // The system files are always in this location. 109baef639bSGreg Roach // It is also the default location of user data, such as media and GEDCOM files. 110baef639bSGreg Roach // The user files could be anywhere supported by Flysystem. 111fd6c003fSGreg Roach public const DATA_DIR = self::ROOT_DIR . 'data/'; 112fd6c003fSGreg Roach 1138d0ebef0SGreg Roach // Location of the file containing the database connection details. 114baef639bSGreg Roach public const CONFIG_FILE = self::DATA_DIR . 'config.ini.php'; 115f397d0fdSGreg Roach 116f397d0fdSGreg Roach // Location of the file that triggers maintenance mode. 117baef639bSGreg Roach public const OFFLINE_FILE = self::DATA_DIR . 'offline.txt'; 118f397d0fdSGreg Roach 119f397d0fdSGreg Roach // Location of our modules. 120b50f90f8SGreg Roach public const MODULES_PATH = 'modules_v4/'; 121f397d0fdSGreg Roach public const MODULES_DIR = self::ROOT_DIR . self::MODULES_PATH; 1228d0ebef0SGreg Roach 1238d0ebef0SGreg Roach // Enable debugging on development builds. 12416d6367aSGreg Roach public const DEBUG = self::STABILITY !== ''; 1258d0ebef0SGreg Roach 1268d0ebef0SGreg Roach // We want to know about all PHP errors during development, and fewer in production. 127721424feSGreg Roach public const ERROR_REPORTING = self::DEBUG ? E_ALL : E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED; 1288d0ebef0SGreg Roach 129158900c2SGreg Roach // Page layouts for various page types. 130158900c2SGreg Roach public const LAYOUT_ADMINISTRATION = 'layouts/administration'; 131158900c2SGreg Roach public const LAYOUT_AJAX = 'layouts/ajax'; 132208909d8SGreg Roach public const LAYOUT_DEFAULT = 'layouts/default'; 133158900c2SGreg Roach public const LAYOUT_ERROR = 'layouts/error'; 134208909d8SGreg Roach 1358d0ebef0SGreg Roach // The name of the application. 13616d6367aSGreg Roach public const NAME = 'webtrees'; 1378d0ebef0SGreg Roach 1388d0ebef0SGreg Roach // Required version of database tables/columns/indexes/etc. 13990949315SGreg Roach public const SCHEMA_VERSION = 45; 1408d0ebef0SGreg Roach 141721424feSGreg Roach // e.g. "-dev", "-alpha", "-beta", etc. 142dc870b4fSGreg Roach public const STABILITY = '-dev'; 1438d0ebef0SGreg Roach 144c4cbcd7bSGreg Roach // Version number. 145dc870b4fSGreg Roach public const VERSION = '2.2.0' . self::STABILITY; 1468d0ebef0SGreg Roach 147f397d0fdSGreg Roach // Project website. 148db1aabc1SGreg Roach public const URL = 'https://webtrees.net/'; 1498d0ebef0SGreg Roach 150c4cbcd7bSGreg Roach // FAQ links. 1513282ccecSGreg Roach public const URL_FAQ_EMAIL = 'https://webtrees.net/faq/email'; 1523282ccecSGreg Roach 153c4cbcd7bSGreg Roach // GEDCOM specification. 1540f3c4769SGreg Roach public const GEDCOM_PDF = 'https://webtrees.net/downloads/gedcom-5-5-1.pdf'; 1559d7cdf93SGreg Roach 15682e92bfaSGreg Roach private const MIDDLEWARE = [ 15782e92bfaSGreg Roach EmitResponse::class, 1589c88b4e9SGreg Roach SecurityHeaders::class, 15982e92bfaSGreg Roach ReadConfigIni::class, 1604874f72dSGreg Roach BaseUrl::class, 1615fb051e9SGreg Roach HandleExceptions::class, 1624874f72dSGreg Roach ClientIp::class, 163d1da5ba4SGreg Roach ContentLength::class, 164179fae31SGreg Roach CompressResponse::class, 165089dadacSGreg Roach BadBotBlocker::class, 16682e92bfaSGreg Roach UseDatabase::class, 16782e92bfaSGreg Roach UpdateDatabaseSchema::class, 16882e92bfaSGreg Roach UseSession::class, 169150f35adSGreg Roach UseLanguage::class, 17082e92bfaSGreg Roach CheckForMaintenanceMode::class, 17182e92bfaSGreg Roach UseTheme::class, 17282e92bfaSGreg Roach DoHousekeeping::class, 17382e92bfaSGreg Roach UseTransaction::class, 174f37874adSGreg Roach CheckForNewVersion::class, 17571378461SGreg Roach LoadRoutes::class, 17600c92694SGreg Roach RegisterGedcomTags::class, 17782e92bfaSGreg Roach BootModules::class, 17882e92bfaSGreg Roach Router::class, 17982e92bfaSGreg Roach NoRouteFound::class, 18082e92bfaSGreg Roach ]; 18182e92bfaSGreg Roach 1828d0ebef0SGreg Roach /** 18300c45d23SGreg Roach * Initialise the application. 18400c45d23SGreg Roach * 185*d35568b4SGreg Roach * @return static 1868d0ebef0SGreg Roach */ 18700c45d23SGreg Roach public function bootstrap(): void 1888d0ebef0SGreg Roach { 1898d0ebef0SGreg Roach // Show all errors and warnings in development, fewer in production. 190cfbf56adSGreg Roach error_reporting(self::ERROR_REPORTING); 191f397d0fdSGreg Roach set_error_handler($this->phpErrorHandler()); 192785274b8SGreg Roach 193785274b8SGreg Roach // All modern software uses UTF-8 encoding. 194785274b8SGreg Roach mb_internal_encoding('UTF-8'); 195785274b8SGreg Roach 196785274b8SGreg Roach // Use UTC internally and convert to local time when displaying datetimes. 197785274b8SGreg Roach date_default_timezone_set('UTC'); 198785274b8SGreg Roach 199785274b8SGreg Roach // Factory objects 200785274b8SGreg Roach Registry::cache(new CacheFactory()); 201f882f05dSGreg Roach Registry::calendarDateFactory(new CalendarDateFactory()); 202*d35568b4SGreg Roach Registry::container(new Container()); 203785274b8SGreg Roach Registry::elementFactory(new ElementFactory()); 2041c6adce8SGreg Roach Registry::encodingFactory(new EncodingFactory()); 205d97083feSGreg Roach Registry::familyFactory(new FamilyFactory()); 206d97083feSGreg Roach Registry::filesystem(new FilesystemFactory()); 207785274b8SGreg Roach Registry::gedcomRecordFactory(new GedcomRecordFactory()); 208785274b8SGreg Roach Registry::headerFactory(new HeaderFactory()); 2092e464181SGreg Roach Registry::idFactory(new IdFactory()); 210785274b8SGreg Roach Registry::imageFactory(new ImageFactory()); 211785274b8SGreg Roach Registry::individualFactory(new IndividualFactory()); 212785274b8SGreg Roach Registry::locationFactory(new LocationFactory()); 2134d35caa7SGreg Roach Registry::markdownFactory(new MarkdownFactory()); 214785274b8SGreg Roach Registry::mediaFactory(new MediaFactory()); 215785274b8SGreg Roach Registry::noteFactory(new NoteFactory()); 216785274b8SGreg Roach Registry::repositoryFactory(new RepositoryFactory()); 217208909d8SGreg Roach Registry::responseFactory(new ResponseFactory(new Psr17Factory(), new Psr17Factory())); 218208909d8SGreg Roach Registry::routeFactory(new RouteFactory()); 219701f5d18SGreg Roach Registry::sharedNoteFactory(new SharedNoteFactory()); 220785274b8SGreg Roach Registry::slugFactory(new SlugFactory()); 221785274b8SGreg Roach Registry::sourceFactory(new SourceFactory()); 222785274b8SGreg Roach Registry::submissionFactory(new SubmissionFactory()); 223785274b8SGreg Roach Registry::submitterFactory(new SubmitterFactory()); 2247e128bbfSGreg Roach Registry::surnameTraditionFactory(new SurnameTraditionFactory()); 225eeec557aSGreg Roach Registry::timeFactory(new TimeFactory()); 226d97083feSGreg Roach Registry::timestampFactory(new TimestampFactory()); 227785274b8SGreg Roach Registry::xrefFactory(new XrefFactory()); 2281c6adce8SGreg Roach 229*d35568b4SGreg Roach // PSR7 messages and PSR17 message-factories 230*d35568b4SGreg Roach Registry::container() 231*d35568b4SGreg Roach ->set(ResponseFactoryInterface::class, new Psr17Factory()) 232*d35568b4SGreg Roach ->set(ServerRequestFactoryInterface::class, new Psr17Factory()) 233*d35568b4SGreg Roach ->set(StreamFactoryInterface::class, new Psr17Factory()) 234*d35568b4SGreg Roach ->set(UploadedFileFactoryInterface::class, new Psr17Factory()) 235*d35568b4SGreg Roach ->set(UriFactoryInterface::class, new Psr17Factory()); 236*d35568b4SGreg Roach 2371c6adce8SGreg Roach stream_filter_register(GedcomEncodingFilter::class, GedcomEncodingFilter::class); 238785274b8SGreg Roach } 239785274b8SGreg Roach 240785274b8SGreg Roach /** 241785274b8SGreg Roach * Respond to a CLI request. 242785274b8SGreg Roach * 243785274b8SGreg Roach * @return void 244785274b8SGreg Roach */ 245785274b8SGreg Roach public function cliRequest(): void 246785274b8SGreg Roach { 247785274b8SGreg Roach // CLI handler will go here. 248785274b8SGreg Roach } 249785274b8SGreg Roach 250785274b8SGreg Roach /** 251dbfdfe6bSGreg Roach * Respond to an HTTP request. 252785274b8SGreg Roach * 253785274b8SGreg Roach * @return ResponseInterface 254785274b8SGreg Roach */ 255785274b8SGreg Roach public function httpRequest(): ResponseInterface 256785274b8SGreg Roach { 257*d35568b4SGreg Roach $server_request_creator = new ServerRequestCreator( 258*d35568b4SGreg Roach Registry::container()->get(ServerRequestFactoryInterface::class), 259*d35568b4SGreg Roach Registry::container()->get(UriFactoryInterface::class), 260*d35568b4SGreg Roach Registry::container()->get(UploadedFileFactoryInterface::class), 261*d35568b4SGreg Roach Registry::container()->get(StreamFactoryInterface::class) 262*d35568b4SGreg Roach ); 2634348fc02SGreg Roach 2644348fc02SGreg Roach $request = $server_request_creator->fromGlobals(); 265785274b8SGreg Roach 266785274b8SGreg Roach return self::dispatch($request, self::MIDDLEWARE); 267785274b8SGreg Roach } 268785274b8SGreg Roach 269785274b8SGreg Roach /** 270785274b8SGreg Roach * @param ServerRequestInterface $request 271785274b8SGreg Roach * @param array<string|MiddlewareInterface> $middleware 272785274b8SGreg Roach * 273785274b8SGreg Roach * @return ResponseInterface 274785274b8SGreg Roach */ 275785274b8SGreg Roach public static function dispatch(ServerRequestInterface $request, array $middleware): ResponseInterface 276785274b8SGreg Roach { 277*d35568b4SGreg Roach $dispatcher = new Dispatcher($middleware, Registry::container()); 278785274b8SGreg Roach 279785274b8SGreg Roach return $dispatcher->dispatch($request); 280f397d0fdSGreg Roach } 2818d0ebef0SGreg Roach 282f397d0fdSGreg Roach /** 283f397d0fdSGreg Roach * An error handler that can be passed to set_error_handler(). 284f397d0fdSGreg Roach * 285c6921a17SGreg Roach * @return Closure(int,string,string,int):bool 286f397d0fdSGreg Roach */ 287f397d0fdSGreg Roach private function phpErrorHandler(): Closure 288f397d0fdSGreg Roach { 289f397d0fdSGreg Roach return static function (int $errno, string $errstr, string $errfile, int $errline): bool { 290f397d0fdSGreg Roach // Ignore errors that are silenced with '@' 291f397d0fdSGreg Roach if (error_reporting() & $errno) { 292f397d0fdSGreg Roach throw new ErrorException($errstr, 0, $errno, $errfile, $errline); 293f397d0fdSGreg Roach } 294f397d0fdSGreg Roach 295f397d0fdSGreg Roach return true; 296f397d0fdSGreg Roach }; 297f397d0fdSGreg Roach } 2988d0ebef0SGreg Roach} 299