xref: /webtrees/app/Webtrees.php (revision b9a4a6c608ac9f9f3bf6c12fbe1fbe871f30fedb)
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
22*b9a4a6c6SGreg Roachuse Fisharebest\Webtrees\Cli\Console;
23785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\CacheFactory;
24f882f05dSGreg Roachuse Fisharebest\Webtrees\Factories\CalendarDateFactory;
25785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\ElementFactory;
261c6adce8SGreg Roachuse Fisharebest\Webtrees\Factories\EncodingFactory;
27785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\FamilyFactory;
28785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\FilesystemFactory;
29785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\GedcomRecordFactory;
30785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\HeaderFactory;
31f0c88a96SGreg Roachuse Fisharebest\Webtrees\Factories\IdFactory;
32785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\ImageFactory;
33785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\IndividualFactory;
34785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\LocationFactory;
354d35caa7SGreg Roachuse Fisharebest\Webtrees\Factories\MarkdownFactory;
36785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\MediaFactory;
37785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\NoteFactory;
38785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\RepositoryFactory;
39208909d8SGreg Roachuse Fisharebest\Webtrees\Factories\ResponseFactory;
40208909d8SGreg Roachuse Fisharebest\Webtrees\Factories\RouteFactory;
41701f5d18SGreg Roachuse Fisharebest\Webtrees\Factories\SharedNoteFactory;
42785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SlugFactory;
43785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SourceFactory;
44785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SubmissionFactory;
45785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SubmitterFactory;
467e128bbfSGreg Roachuse Fisharebest\Webtrees\Factories\SurnameTraditionFactory;
47eeec557aSGreg Roachuse Fisharebest\Webtrees\Factories\TimeFactory;
48208909d8SGreg Roachuse Fisharebest\Webtrees\Factories\TimestampFactory;
49785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\XrefFactory;
50158900c2SGreg Roachuse Fisharebest\Webtrees\GedcomFilters\GedcomEncodingFilter;
51089dadacSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BadBotBlocker;
52158900c2SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BaseUrl;
53f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BootModules;
54f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode;
55f37874adSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CheckForNewVersion;
564874f72dSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ClientIp;
57179fae31SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CompressResponse;
58d1da5ba4SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ContentLength;
59f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\DoHousekeeping;
60f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\EmitResponse;
61234e0863SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ErrorHandler;
62f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\HandleExceptions;
6371378461SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\LoadRoutes;
64f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\NoRouteFound;
65d7de6d4cSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\PublicFiles;
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;
8200c45d23SGreg Roachuse Psr\Http\Message\StreamFactoryInterface;
8300c45d23SGreg Roachuse Psr\Http\Message\UploadedFileFactoryInterface;
8400c45d23SGreg Roachuse Psr\Http\Message\UriFactoryInterface;
853976b470SGreg Roach
86785274b8SGreg Roachuse function date_default_timezone_set;
878d0ebef0SGreg Roachuse function error_reporting;
88785274b8SGreg Roachuse function mb_internal_encoding;
891c6adce8SGreg Roachuse function stream_filter_register;
908d0ebef0SGreg Roach
91721424feSGreg Roachuse const E_ALL;
92721424feSGreg Roachuse const E_DEPRECATED;
93054771e9SGreg Roachuse const E_USER_DEPRECATED;
94054771e9SGreg Roach
958d0ebef0SGreg Roach/**
968d0ebef0SGreg Roach * Definitions for the webtrees application.
978d0ebef0SGreg Roach */
988d0ebef0SGreg Roachclass Webtrees
998d0ebef0SGreg Roach{
100f397d0fdSGreg Roach    // The root folder of this installation
101f397d0fdSGreg Roach    public const ROOT_DIR = __DIR__ . '/../';
102f397d0fdSGreg Roach
103baef639bSGreg Roach    // This is the location of system data, such as temporary and cache files.
104baef639bSGreg Roach    // The system files are always in this location.
105baef639bSGreg Roach    // It is also the default location of user data, such as media and GEDCOM files.
106baef639bSGreg Roach    // The user files could be anywhere supported by Flysystem.
107fd6c003fSGreg Roach    public const DATA_DIR = self::ROOT_DIR . 'data/';
108fd6c003fSGreg Roach
1098d0ebef0SGreg Roach    // Location of the file containing the database connection details.
110baef639bSGreg Roach    public const CONFIG_FILE = self::DATA_DIR . 'config.ini.php';
111f397d0fdSGreg Roach
112f397d0fdSGreg Roach    // Location of the file that triggers maintenance mode.
113baef639bSGreg Roach    public const OFFLINE_FILE = self::DATA_DIR . 'offline.txt';
114f397d0fdSGreg Roach
115f397d0fdSGreg Roach    // Location of our modules.
116b50f90f8SGreg Roach    public const MODULES_PATH = 'modules_v4/';
117f397d0fdSGreg Roach    public const MODULES_DIR  = self::ROOT_DIR . self::MODULES_PATH;
1188d0ebef0SGreg Roach
1198d0ebef0SGreg Roach    // Enable debugging on development builds.
12016d6367aSGreg Roach    public const DEBUG = self::STABILITY !== '';
1218d0ebef0SGreg Roach
1228d0ebef0SGreg Roach    // We want to know about all PHP errors during development, and fewer in production.
123721424feSGreg Roach    public const ERROR_REPORTING = self::DEBUG ? E_ALL : E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED;
1248d0ebef0SGreg Roach
125158900c2SGreg Roach    // Page layouts for various page types.
126158900c2SGreg Roach    public const LAYOUT_ADMINISTRATION = 'layouts/administration';
127158900c2SGreg Roach    public const LAYOUT_AJAX           = 'layouts/ajax';
128208909d8SGreg Roach    public const LAYOUT_DEFAULT        = 'layouts/default';
129158900c2SGreg Roach    public const LAYOUT_ERROR          = 'layouts/error';
130208909d8SGreg Roach
1318d0ebef0SGreg Roach    // The name of the application.
13216d6367aSGreg Roach    public const NAME = 'webtrees';
1338d0ebef0SGreg Roach
1348d0ebef0SGreg Roach    // Required version of database tables/columns/indexes/etc.
13590949315SGreg Roach    public const SCHEMA_VERSION = 45;
1368d0ebef0SGreg Roach
137721424feSGreg Roach    // e.g. "-dev", "-alpha", "-beta", etc.
138dc870b4fSGreg Roach    public const STABILITY = '-dev';
1398d0ebef0SGreg Roach
140c4cbcd7bSGreg Roach    // Version number.
141dc870b4fSGreg Roach    public const VERSION = '2.2.0' . self::STABILITY;
1428d0ebef0SGreg Roach
143f397d0fdSGreg Roach    // Project website.
144db1aabc1SGreg Roach    public const URL = 'https://webtrees.net/';
1458d0ebef0SGreg Roach
146c4cbcd7bSGreg Roach    // FAQ links.
1473282ccecSGreg Roach    public const URL_FAQ_EMAIL = 'https://webtrees.net/faq/email';
1483282ccecSGreg Roach
149c4cbcd7bSGreg Roach    // GEDCOM specification.
1500f3c4769SGreg Roach    public const GEDCOM_PDF = 'https://webtrees.net/downloads/gedcom-5-5-1.pdf';
1519d7cdf93SGreg Roach
15282e92bfaSGreg Roach    private const MIDDLEWARE = [
153234e0863SGreg Roach        ErrorHandler::class,
15482e92bfaSGreg Roach        EmitResponse::class,
1559c88b4e9SGreg Roach        SecurityHeaders::class,
15682e92bfaSGreg Roach        ReadConfigIni::class,
1574874f72dSGreg Roach        BaseUrl::class,
1585fb051e9SGreg Roach        HandleExceptions::class,
159d7de6d4cSGreg Roach        PublicFiles::class,
1604874f72dSGreg Roach        ClientIp::class,
161d1da5ba4SGreg Roach        ContentLength::class,
162179fae31SGreg Roach        CompressResponse::class,
163089dadacSGreg Roach        BadBotBlocker::class,
16482e92bfaSGreg Roach        UseDatabase::class,
16582e92bfaSGreg Roach        UpdateDatabaseSchema::class,
16682e92bfaSGreg Roach        UseSession::class,
167150f35adSGreg Roach        UseLanguage::class,
16882e92bfaSGreg Roach        CheckForMaintenanceMode::class,
16982e92bfaSGreg Roach        UseTheme::class,
17082e92bfaSGreg Roach        DoHousekeeping::class,
17182e92bfaSGreg Roach        UseTransaction::class,
172f37874adSGreg Roach        CheckForNewVersion::class,
17371378461SGreg Roach        LoadRoutes::class,
17400c92694SGreg Roach        RegisterGedcomTags::class,
17582e92bfaSGreg Roach        BootModules::class,
17682e92bfaSGreg Roach        Router::class,
17782e92bfaSGreg Roach        NoRouteFound::class,
17882e92bfaSGreg Roach    ];
17982e92bfaSGreg Roach
180*b9a4a6c6SGreg Roach    public static function new(): self
181*b9a4a6c6SGreg Roach    {
182*b9a4a6c6SGreg Roach        return new self();
183*b9a4a6c6SGreg Roach    }
184*b9a4a6c6SGreg Roach
1858d0ebef0SGreg Roach    /**
18600c45d23SGreg Roach     * Initialise the application.
1878d0ebef0SGreg Roach     */
188*b9a4a6c6SGreg Roach    public function bootstrap(): self
1898d0ebef0SGreg Roach    {
1908d0ebef0SGreg Roach        // Show all errors and warnings in development, fewer in production.
191cfbf56adSGreg Roach        error_reporting(self::ERROR_REPORTING);
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());
202d35568b4SGreg 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
229d35568b4SGreg Roach        // PSR7 messages and PSR17 message-factories
230d35568b4SGreg Roach        Registry::container()
231d35568b4SGreg Roach            ->set(ResponseFactoryInterface::class, new Psr17Factory())
232d35568b4SGreg Roach            ->set(ServerRequestFactoryInterface::class, new Psr17Factory())
233d35568b4SGreg Roach            ->set(StreamFactoryInterface::class, new Psr17Factory())
234d35568b4SGreg Roach            ->set(UploadedFileFactoryInterface::class, new Psr17Factory())
235d35568b4SGreg Roach            ->set(UriFactoryInterface::class, new Psr17Factory());
236d35568b4SGreg Roach
2371c6adce8SGreg Roach        stream_filter_register(GedcomEncodingFilter::class, GedcomEncodingFilter::class);
238d7de6d4cSGreg Roach
239d7de6d4cSGreg Roach        return $this;
240d7de6d4cSGreg Roach    }
241d7de6d4cSGreg Roach
242d7de6d4cSGreg Roach    /**
243d7de6d4cSGreg Roach     * Run the application.
244d7de6d4cSGreg Roach     */
245*b9a4a6c6SGreg Roach    public function run(string $php_sapi): int|ResponseInterface
246d7de6d4cSGreg Roach    {
247*b9a4a6c6SGreg Roach        if ($php_sapi === 'cli') {
248*b9a4a6c6SGreg Roach            return $this->bootstrap()->cliRequest();
249*b9a4a6c6SGreg Roach        }
250*b9a4a6c6SGreg Roach
251*b9a4a6c6SGreg Roach        return $this->bootstrap()->httpRequest();
252785274b8SGreg Roach    }
253785274b8SGreg Roach
254785274b8SGreg Roach    /**
255785274b8SGreg Roach     * Respond to a CLI request.
256785274b8SGreg Roach     */
257*b9a4a6c6SGreg Roach    public function cliRequest(): int
258785274b8SGreg Roach    {
259*b9a4a6c6SGreg Roach        $console = new Console();
260*b9a4a6c6SGreg Roach
261*b9a4a6c6SGreg Roach        return $console->loadCommands()->bootstrap()->run();
262785274b8SGreg Roach    }
263785274b8SGreg Roach
264785274b8SGreg Roach    /**
265dbfdfe6bSGreg Roach     * Respond to an HTTP request.
266785274b8SGreg Roach     */
267785274b8SGreg Roach    public function httpRequest(): ResponseInterface
268785274b8SGreg Roach    {
269d35568b4SGreg Roach        $server_request_creator = new ServerRequestCreator(
270d35568b4SGreg Roach            Registry::container()->get(ServerRequestFactoryInterface::class),
271d35568b4SGreg Roach            Registry::container()->get(UriFactoryInterface::class),
272d35568b4SGreg Roach            Registry::container()->get(UploadedFileFactoryInterface::class),
273d35568b4SGreg Roach            Registry::container()->get(StreamFactoryInterface::class)
274d35568b4SGreg Roach        );
2754348fc02SGreg Roach
2764348fc02SGreg Roach        $request = $server_request_creator->fromGlobals();
277785274b8SGreg Roach
278d7de6d4cSGreg Roach        $dispatcher = new Dispatcher(self::MIDDLEWARE, Registry::container());
279785274b8SGreg Roach
280785274b8SGreg Roach        return $dispatcher->dispatch($request);
281f397d0fdSGreg Roach    }
2828d0ebef0SGreg Roach}
283