xref: /webtrees/app/Webtrees.php (revision c4cbcd7b5d2995f2119e8970ddb302651026bf90)
18d0ebef0SGreg Roach<?php
23976b470SGreg Roach
38d0ebef0SGreg Roach/**
48d0ebef0SGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 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;
26785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\ElementFactory;
271c6adce8SGreg Roachuse Fisharebest\Webtrees\Factories\EncodingFactory;
28785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\FamilyFactory;
29785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\FilesystemFactory;
30785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\GedcomRecordFactory;
31785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\HeaderFactory;
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;
41785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SlugFactory;
42785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SourceFactory;
43785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SubmissionFactory;
44785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SubmitterFactory;
457e128bbfSGreg Roachuse Fisharebest\Webtrees\Factories\SurnameTraditionFactory;
46208909d8SGreg Roachuse Fisharebest\Webtrees\Factories\TimestampFactory;
47785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\XrefFactory;
48158900c2SGreg Roachuse Fisharebest\Webtrees\GedcomFilters\GedcomEncodingFilter;
49089dadacSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BadBotBlocker;
50158900c2SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BaseUrl;
51f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BootModules;
52f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode;
53*c4cbcd7bSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CheckForNewVersion;
544874f72dSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ClientIp;
55179fae31SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CompressResponse;
56d1da5ba4SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ContentLength;
57f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\DoHousekeeping;
58f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\EmitResponse;
59f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\HandleExceptions;
6071378461SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\LoadRoutes;
61f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\NoRouteFound;
62e16a1bfdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ReadConfigIni;
6300c92694SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\RegisterGedcomTags;
649e5d8e6fSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\Router;
659c88b4e9SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\SecurityHeaders;
66f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema;
67f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseDatabase;
68150f35adSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseLanguage;
69f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseSession;
70f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseTheme;
71f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseTransaction;
72785274b8SGreg Roachuse Illuminate\Container\Container;
73785274b8SGreg Roachuse Middleland\Dispatcher;
74f397d0fdSGreg Roachuse Nyholm\Psr7\Factory\Psr17Factory;
75785274b8SGreg Roachuse Nyholm\Psr7Server\ServerRequestCreator;
76785274b8SGreg Roachuse Psr\Container\ContainerInterface;
7700c45d23SGreg Roachuse Psr\Http\Message\ResponseFactoryInterface;
78785274b8SGreg Roachuse Psr\Http\Message\ResponseInterface;
7900c45d23SGreg Roachuse Psr\Http\Message\ServerRequestFactoryInterface;
80785274b8SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
8100c45d23SGreg Roachuse Psr\Http\Message\StreamFactoryInterface;
8200c45d23SGreg Roachuse Psr\Http\Message\UploadedFileFactoryInterface;
8300c45d23SGreg Roachuse Psr\Http\Message\UriFactoryInterface;
84785274b8SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
853976b470SGreg Roach
86785274b8SGreg Roachuse function date_default_timezone_set;
878d0ebef0SGreg Roachuse function error_reporting;
88785274b8SGreg Roachuse function is_string;
89785274b8SGreg Roachuse function mb_internal_encoding;
908d0ebef0SGreg Roachuse function set_error_handler;
911c6adce8SGreg Roachuse function stream_filter_register;
928d0ebef0SGreg Roach
93721424feSGreg Roachuse const E_ALL;
94721424feSGreg Roachuse const E_DEPRECATED;
95054771e9SGreg Roachuse const E_USER_DEPRECATED;
96054771e9SGreg Roach
978d0ebef0SGreg Roach/**
988d0ebef0SGreg Roach * Definitions for the webtrees application.
998d0ebef0SGreg Roach */
1008d0ebef0SGreg Roachclass Webtrees
1018d0ebef0SGreg Roach{
102f397d0fdSGreg Roach    // The root folder of this installation
103f397d0fdSGreg Roach    public const ROOT_DIR = __DIR__ . '/../';
104f397d0fdSGreg Roach
105baef639bSGreg Roach    // This is the location of system data, such as temporary and cache files.
106baef639bSGreg Roach    // The system files are always in this location.
107baef639bSGreg Roach    // It is also the default location of user data, such as media and GEDCOM files.
108baef639bSGreg Roach    // The user files could be anywhere supported by Flysystem.
109fd6c003fSGreg Roach    public const DATA_DIR = self::ROOT_DIR . 'data/';
110fd6c003fSGreg Roach
1118d0ebef0SGreg Roach    // Location of the file containing the database connection details.
112baef639bSGreg Roach    public const CONFIG_FILE = self::DATA_DIR . 'config.ini.php';
113f397d0fdSGreg Roach
114f397d0fdSGreg Roach    // Location of the file that triggers maintenance mode.
115baef639bSGreg Roach    public const OFFLINE_FILE = self::DATA_DIR . 'offline.txt';
116f397d0fdSGreg Roach
117f397d0fdSGreg Roach    // Location of our modules.
118b50f90f8SGreg Roach    public const MODULES_PATH = 'modules_v4/';
119f397d0fdSGreg Roach    public const MODULES_DIR  = self::ROOT_DIR . self::MODULES_PATH;
1208d0ebef0SGreg Roach
1218d0ebef0SGreg Roach    // Enable debugging on development builds.
12216d6367aSGreg Roach    public const DEBUG = self::STABILITY !== '';
1238d0ebef0SGreg Roach
1248d0ebef0SGreg Roach    // We want to know about all PHP errors during development, and fewer in production.
125721424feSGreg Roach    public const ERROR_REPORTING = self::DEBUG ? E_ALL : E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED;
1268d0ebef0SGreg Roach
127158900c2SGreg Roach    // Page layouts for various page types.
128158900c2SGreg Roach    public const LAYOUT_ADMINISTRATION = 'layouts/administration';
129158900c2SGreg Roach    public const LAYOUT_AJAX           = 'layouts/ajax';
130208909d8SGreg Roach    public const LAYOUT_DEFAULT        = 'layouts/default';
131158900c2SGreg Roach    public const LAYOUT_ERROR          = 'layouts/error';
132208909d8SGreg Roach
1338d0ebef0SGreg Roach    // The name of the application.
13416d6367aSGreg Roach    public const NAME = 'webtrees';
1358d0ebef0SGreg Roach
1368d0ebef0SGreg Roach    // Required version of database tables/columns/indexes/etc.
13790949315SGreg Roach    public const SCHEMA_VERSION = 45;
1388d0ebef0SGreg Roach
139721424feSGreg Roach    // e.g. "-dev", "-alpha", "-beta", etc.
140b268e252SGreg Roach    public const STABILITY = '-dev';
1418d0ebef0SGreg Roach
142*c4cbcd7bSGreg Roach    // Version number.
143b268e252SGreg Roach    public const VERSION = '2.1.3' . self::STABILITY;
1448d0ebef0SGreg Roach
145f397d0fdSGreg Roach    // Project website.
146db1aabc1SGreg Roach    public const URL = 'https://webtrees.net/';
1478d0ebef0SGreg Roach
148*c4cbcd7bSGreg Roach    // FAQ links.
1493282ccecSGreg Roach    public const URL_FAQ_EMAIL = 'https://webtrees.net/faq/email';
1503282ccecSGreg Roach
151*c4cbcd7bSGreg Roach    // GEDCOM specification.
1520f3c4769SGreg Roach    public const GEDCOM_PDF = 'https://webtrees.net/downloads/gedcom-5-5-1.pdf';
1539d7cdf93SGreg Roach
15482e92bfaSGreg Roach    private const MIDDLEWARE = [
15582e92bfaSGreg Roach        EmitResponse::class,
1569c88b4e9SGreg Roach        SecurityHeaders::class,
15782e92bfaSGreg Roach        ReadConfigIni::class,
1584874f72dSGreg Roach        BaseUrl::class,
1595fb051e9SGreg Roach        HandleExceptions::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,
168*c4cbcd7bSGreg Roach        CheckForNewVersion::class,
16982e92bfaSGreg Roach        CheckForMaintenanceMode::class,
17082e92bfaSGreg Roach        UseTheme::class,
17182e92bfaSGreg Roach        DoHousekeeping::class,
17282e92bfaSGreg Roach        UseTransaction::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
1808d0ebef0SGreg Roach    /**
18100c45d23SGreg Roach     * Initialise the application.
18200c45d23SGreg Roach     *
1838d0ebef0SGreg Roach     * @return void
1848d0ebef0SGreg Roach     */
18500c45d23SGreg Roach    public function bootstrap(): void
1868d0ebef0SGreg Roach    {
1878d0ebef0SGreg Roach        // Show all errors and warnings in development, fewer in production.
188cfbf56adSGreg Roach        error_reporting(self::ERROR_REPORTING);
189f397d0fdSGreg Roach        set_error_handler($this->phpErrorHandler());
190785274b8SGreg Roach
191785274b8SGreg Roach        // All modern software uses UTF-8 encoding.
192785274b8SGreg Roach        mb_internal_encoding('UTF-8');
193785274b8SGreg Roach
194785274b8SGreg Roach        // Use UTC internally and convert to local time when displaying datetimes.
195785274b8SGreg Roach        date_default_timezone_set('UTC');
196785274b8SGreg Roach
197785274b8SGreg Roach        // Factory objects
198785274b8SGreg Roach        Registry::cache(new CacheFactory());
199f882f05dSGreg Roach        Registry::calendarDateFactory(new CalendarDateFactory());
200785274b8SGreg Roach        Registry::elementFactory(new ElementFactory());
2011c6adce8SGreg Roach        Registry::encodingFactory(new EncodingFactory());
202d97083feSGreg Roach        Registry::familyFactory(new FamilyFactory());
203d97083feSGreg Roach        Registry::filesystem(new FilesystemFactory());
204785274b8SGreg Roach        Registry::gedcomRecordFactory(new GedcomRecordFactory());
205785274b8SGreg Roach        Registry::headerFactory(new HeaderFactory());
206785274b8SGreg Roach        Registry::imageFactory(new ImageFactory());
207785274b8SGreg Roach        Registry::individualFactory(new IndividualFactory());
208785274b8SGreg Roach        Registry::locationFactory(new LocationFactory());
2094d35caa7SGreg Roach        Registry::markdownFactory(new MarkdownFactory());
210785274b8SGreg Roach        Registry::mediaFactory(new MediaFactory());
211785274b8SGreg Roach        Registry::noteFactory(new NoteFactory());
212785274b8SGreg Roach        Registry::repositoryFactory(new RepositoryFactory());
213208909d8SGreg Roach        Registry::responseFactory(new ResponseFactory(new Psr17Factory(), new Psr17Factory()));
214208909d8SGreg Roach        Registry::routeFactory(new RouteFactory());
215785274b8SGreg Roach        Registry::slugFactory(new SlugFactory());
216785274b8SGreg Roach        Registry::sourceFactory(new SourceFactory());
217785274b8SGreg Roach        Registry::submissionFactory(new SubmissionFactory());
218785274b8SGreg Roach        Registry::submitterFactory(new SubmitterFactory());
2197e128bbfSGreg Roach        Registry::surnameTraditionFactory(new SurnameTraditionFactory());
220d97083feSGreg Roach        Registry::timestampFactory(new TimestampFactory());
221785274b8SGreg Roach        Registry::xrefFactory(new XrefFactory());
2221c6adce8SGreg Roach
2231c6adce8SGreg Roach        stream_filter_register(GedcomEncodingFilter::class, GedcomEncodingFilter::class);
224785274b8SGreg Roach    }
225785274b8SGreg Roach
226785274b8SGreg Roach    /**
227785274b8SGreg Roach     * Respond to a CLI request.
228785274b8SGreg Roach     *
229785274b8SGreg Roach     * @return void
230785274b8SGreg Roach     */
231785274b8SGreg Roach    public function cliRequest(): void
232785274b8SGreg Roach    {
233785274b8SGreg Roach        // CLI handler will go here.
234785274b8SGreg Roach    }
235785274b8SGreg Roach
236785274b8SGreg Roach    /**
237dbfdfe6bSGreg Roach     * Respond to an HTTP request.
238785274b8SGreg Roach     *
239785274b8SGreg Roach     * @return ResponseInterface
240785274b8SGreg Roach     */
241785274b8SGreg Roach    public function httpRequest(): ResponseInterface
242785274b8SGreg Roach    {
2434348fc02SGreg Roach        $psr17factory = new Psr17Factory();
244785274b8SGreg Roach
2454348fc02SGreg Roach        // PSR7 messages and PSR17 message-factories
2464348fc02SGreg Roach        self::set(ResponseFactoryInterface::class, $psr17factory);
2474348fc02SGreg Roach        self::set(ServerRequestFactoryInterface::class, $psr17factory);
2484348fc02SGreg Roach        self::set(StreamFactoryInterface::class, $psr17factory);
2494348fc02SGreg Roach        self::set(UploadedFileFactoryInterface::class, $psr17factory);
2504348fc02SGreg Roach        self::set(UriFactoryInterface::class, $psr17factory);
2514348fc02SGreg Roach
2524348fc02SGreg Roach        $server_request_creator = new ServerRequestCreator($psr17factory, $psr17factory, $psr17factory, $psr17factory);
2534348fc02SGreg Roach
2544348fc02SGreg Roach        $request = $server_request_creator->fromGlobals();
255785274b8SGreg Roach
256785274b8SGreg Roach        return self::dispatch($request, self::MIDDLEWARE);
257785274b8SGreg Roach    }
258785274b8SGreg Roach
259785274b8SGreg Roach    /**
260785274b8SGreg Roach     * @param ServerRequestInterface            $request
261785274b8SGreg Roach     * @param array<string|MiddlewareInterface> $middleware
262785274b8SGreg Roach     *
263785274b8SGreg Roach     * @return ResponseInterface
264785274b8SGreg Roach     */
265785274b8SGreg Roach    public static function dispatch(ServerRequestInterface $request, array $middleware): ResponseInterface
266785274b8SGreg Roach    {
267785274b8SGreg Roach        $dispatcher = new Dispatcher($middleware, self::container());
268785274b8SGreg Roach
269785274b8SGreg Roach        return $dispatcher->dispatch($request);
270f397d0fdSGreg Roach    }
2718d0ebef0SGreg Roach
272f397d0fdSGreg Roach    /**
273f397d0fdSGreg Roach     * An error handler that can be passed to set_error_handler().
274f397d0fdSGreg Roach     *
275f397d0fdSGreg Roach     * @return Closure
276f397d0fdSGreg Roach     */
277f397d0fdSGreg Roach    private function phpErrorHandler(): Closure
278f397d0fdSGreg Roach    {
279f397d0fdSGreg Roach        return static function (int $errno, string $errstr, string $errfile, int $errline): bool {
280f397d0fdSGreg Roach            // Ignore errors that are silenced with '@'
281f397d0fdSGreg Roach            if (error_reporting() & $errno) {
282f397d0fdSGreg Roach                throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
283f397d0fdSGreg Roach            }
284f397d0fdSGreg Roach
285f397d0fdSGreg Roach            return true;
286f397d0fdSGreg Roach        };
287f397d0fdSGreg Roach    }
288f397d0fdSGreg Roach
289f397d0fdSGreg Roach    /**
290785274b8SGreg Roach     * @return ContainerInterface
291f397d0fdSGreg Roach     */
292785274b8SGreg Roach    public static function container(): ContainerInterface
293f397d0fdSGreg Roach    {
294785274b8SGreg Roach        return Container::getInstance();
295785274b8SGreg Roach    }
296785274b8SGreg Roach
297785274b8SGreg Roach    /**
298785274b8SGreg Roach     * Make an object, using dependency injection.
299785274b8SGreg Roach     *
300785274b8SGreg Roach     * @param string $class
301785274b8SGreg Roach     *
302785274b8SGreg Roach     * @return mixed
303785274b8SGreg Roach     */
304785274b8SGreg Roach    public static function make(string $class)
305785274b8SGreg Roach    {
306785274b8SGreg Roach        return Container::getInstance()->make($class);
307785274b8SGreg Roach    }
308785274b8SGreg Roach
309785274b8SGreg Roach    /**
310785274b8SGreg Roach     * Write a value into the container.
311785274b8SGreg Roach     *
312785274b8SGreg Roach     * @param string        $abstract
313785274b8SGreg Roach     * @param string|object $concrete
314785274b8SGreg Roach     */
315785274b8SGreg Roach    public static function set(string $abstract, $concrete): void
316785274b8SGreg Roach    {
317785274b8SGreg Roach        if (is_string($concrete)) {
318785274b8SGreg Roach            Container::getInstance()->bind($abstract, $concrete);
319785274b8SGreg Roach        } else {
320785274b8SGreg Roach            Container::getInstance()->instance($abstract, $concrete);
321785274b8SGreg Roach        }
3228d0ebef0SGreg Roach    }
3238d0ebef0SGreg Roach}
324