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