xref: /webtrees/app/Webtrees.php (revision 785274b8f4460085b440e020f349e7d085ed4abb)
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;
24*785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\CacheFactory;
25*785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\ElementFactory;
26*785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\FamilyFactory;
27*785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\FilesystemFactory;
28*785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\GedcomRecordFactory;
29*785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\HeaderFactory;
30*785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\ImageFactory;
31*785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\IndividualFactory;
32*785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\LocationFactory;
33*785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\MediaFactory;
34*785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\NoteFactory;
35*785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\RepositoryFactory;
36*785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SlugFactory;
37*785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SourceFactory;
38*785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SubmissionFactory;
39*785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\SubmitterFactory;
40*785274b8SGreg Roachuse Fisharebest\Webtrees\Factories\XrefFactory;
41089dadacSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BadBotBlocker;
42f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BootModules;
43f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode;
444874f72dSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ClientIp;
45179fae31SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CompressResponse;
46f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\DoHousekeeping;
47f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\EmitResponse;
48f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\HandleExceptions;
4971378461SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\LoadRoutes;
50f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\NoRouteFound;
51e16a1bfdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ReadConfigIni;
529e5d8e6fSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\Router;
539c88b4e9SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\SecurityHeaders;
54f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema;
55f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseDatabase;
56f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseDebugbar;
57150f35adSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseLanguage;
58f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseSession;
59f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseTheme;
60f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseTransaction;
614874f72dSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BaseUrl;
62*785274b8SGreg Roachuse Illuminate\Container\Container;
63*785274b8SGreg Roachuse Middleland\Dispatcher;
64f397d0fdSGreg Roachuse Nyholm\Psr7\Factory\Psr17Factory;
65*785274b8SGreg Roachuse Nyholm\Psr7Server\ServerRequestCreator;
66*785274b8SGreg Roachuse Psr\Container\ContainerInterface;
6700c45d23SGreg Roachuse Psr\Http\Message\ResponseFactoryInterface;
68*785274b8SGreg Roachuse Psr\Http\Message\ResponseInterface;
6900c45d23SGreg Roachuse Psr\Http\Message\ServerRequestFactoryInterface;
70*785274b8SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
7100c45d23SGreg Roachuse Psr\Http\Message\StreamFactoryInterface;
7200c45d23SGreg Roachuse Psr\Http\Message\UploadedFileFactoryInterface;
7300c45d23SGreg Roachuse Psr\Http\Message\UriFactoryInterface;
74*785274b8SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
753976b470SGreg Roach
76*785274b8SGreg Roachuse function date_default_timezone_set;
778d0ebef0SGreg Roachuse function error_reporting;
78*785274b8SGreg Roachuse function is_string;
79*785274b8SGreg Roachuse function mb_internal_encoding;
808d0ebef0SGreg Roachuse function set_error_handler;
818d0ebef0SGreg Roach
82721424feSGreg Roachuse const E_ALL;
83721424feSGreg Roachuse const E_DEPRECATED;
84054771e9SGreg Roachuse const E_USER_DEPRECATED;
85054771e9SGreg Roach
868d0ebef0SGreg Roach/**
878d0ebef0SGreg Roach * Definitions for the webtrees application.
888d0ebef0SGreg Roach */
898d0ebef0SGreg Roachclass Webtrees
908d0ebef0SGreg Roach{
91f397d0fdSGreg Roach    // The root folder of this installation
92f397d0fdSGreg Roach    public const ROOT_DIR = __DIR__ . '/../';
93f397d0fdSGreg Roach
94baef639bSGreg Roach    // This is the location of system data, such as temporary and cache files.
95baef639bSGreg Roach    // The system files are always in this location.
96baef639bSGreg Roach    // It is also the default location of user data, such as media and GEDCOM files.
97baef639bSGreg Roach    // The user files could be anywhere supported by Flysystem.
98fd6c003fSGreg Roach    public const DATA_DIR  = self::ROOT_DIR . 'data/';
99fd6c003fSGreg Roach
1008d0ebef0SGreg Roach    // Location of the file containing the database connection details.
101baef639bSGreg Roach    public const CONFIG_FILE = self::DATA_DIR . 'config.ini.php';
102f397d0fdSGreg Roach
103f397d0fdSGreg Roach    // Location of the file that triggers maintenance mode.
104baef639bSGreg Roach    public const OFFLINE_FILE = self::DATA_DIR . 'offline.txt';
105f397d0fdSGreg Roach
106f397d0fdSGreg Roach    // Location of our modules.
107b50f90f8SGreg Roach    public const MODULES_PATH = 'modules_v4/';
108f397d0fdSGreg Roach    public const MODULES_DIR  = self::ROOT_DIR . self::MODULES_PATH;
1098d0ebef0SGreg Roach
1108d0ebef0SGreg Roach    // Enable debugging on development builds.
11116d6367aSGreg Roach    public const DEBUG = self::STABILITY !== '';
1128d0ebef0SGreg Roach
1138d0ebef0SGreg Roach    // We want to know about all PHP errors during development, and fewer in production.
114721424feSGreg Roach    public const ERROR_REPORTING = self::DEBUG ? E_ALL : E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED;
1158d0ebef0SGreg Roach
1168d0ebef0SGreg Roach    // The name of the application.
11716d6367aSGreg Roach    public const NAME = 'webtrees';
1188d0ebef0SGreg Roach
1198d0ebef0SGreg Roach    // Required version of database tables/columns/indexes/etc.
12090949315SGreg Roach    public const SCHEMA_VERSION = 45;
1218d0ebef0SGreg Roach
122721424feSGreg Roach    // e.g. "-dev", "-alpha", "-beta", etc.
1237989fe57SGreg Roach    public const STABILITY = '-dev';
1248d0ebef0SGreg Roach
1258d0ebef0SGreg Roach    // Version number
126f32d77e6SGreg Roach    public const VERSION = '2.1.0' . self::STABILITY;
1278d0ebef0SGreg Roach
128f397d0fdSGreg Roach    // Project website.
129db1aabc1SGreg Roach    public const URL = 'https://webtrees.net/';
1308d0ebef0SGreg Roach
131fceda430SGreg Roach    // FAQ links
1323282ccecSGreg Roach    public const URL_FAQ_EMAIL = 'https://webtrees.net/faq/email';
1333282ccecSGreg Roach
1349d7cdf93SGreg Roach    // Project website.
135db1aabc1SGreg Roach    public const GEDCOM_PDF = 'https://webtrees.net/downloads/gedcom-551.pdf';
1369d7cdf93SGreg Roach
13782e92bfaSGreg Roach    private const MIDDLEWARE = [
13882e92bfaSGreg Roach        EmitResponse::class,
1399c88b4e9SGreg Roach        SecurityHeaders::class,
14082e92bfaSGreg Roach        ReadConfigIni::class,
1414874f72dSGreg Roach        BaseUrl::class,
1425fb051e9SGreg Roach        HandleExceptions::class,
1434874f72dSGreg Roach        ClientIp::class,
144179fae31SGreg Roach        CompressResponse::class,
145089dadacSGreg Roach        BadBotBlocker::class,
14682e92bfaSGreg Roach        UseDatabase::class,
14782e92bfaSGreg Roach        UseDebugbar::class,
14882e92bfaSGreg Roach        UpdateDatabaseSchema::class,
14982e92bfaSGreg Roach        UseSession::class,
150150f35adSGreg Roach        UseLanguage::class,
15182e92bfaSGreg Roach        CheckForMaintenanceMode::class,
15282e92bfaSGreg Roach        UseTheme::class,
15382e92bfaSGreg Roach        DoHousekeeping::class,
15482e92bfaSGreg Roach        UseTransaction::class,
15571378461SGreg Roach        LoadRoutes::class,
15682e92bfaSGreg Roach        BootModules::class,
15782e92bfaSGreg Roach        Router::class,
15882e92bfaSGreg Roach        NoRouteFound::class,
15982e92bfaSGreg Roach    ];
16082e92bfaSGreg Roach
1618d0ebef0SGreg Roach    /**
16200c45d23SGreg Roach     * Initialise the application.
16300c45d23SGreg Roach     *
1648d0ebef0SGreg Roach     * @return void
1658d0ebef0SGreg Roach     */
16600c45d23SGreg Roach    public function bootstrap(): void
1678d0ebef0SGreg Roach    {
1688d0ebef0SGreg Roach        // Show all errors and warnings in development, fewer in production.
169cfbf56adSGreg Roach        error_reporting(self::ERROR_REPORTING);
170f397d0fdSGreg Roach        set_error_handler($this->phpErrorHandler());
171*785274b8SGreg Roach
172*785274b8SGreg Roach        // All modern software uses UTF-8 encoding.
173*785274b8SGreg Roach        mb_internal_encoding('UTF-8');
174*785274b8SGreg Roach
175*785274b8SGreg Roach        // Use UTC internally and convert to local time when displaying datetimes.
176*785274b8SGreg Roach        date_default_timezone_set('UTC');
177*785274b8SGreg Roach
178*785274b8SGreg Roach        // Factory objects
179*785274b8SGreg Roach        Registry::cache(new CacheFactory());
180*785274b8SGreg Roach        Registry::familyFactory(new FamilyFactory());
181*785274b8SGreg Roach        Registry::filesystem(new FilesystemFactory());
182*785274b8SGreg Roach        Registry::elementFactory(new ElementFactory());
183*785274b8SGreg Roach        Registry::gedcomRecordFactory(new GedcomRecordFactory());
184*785274b8SGreg Roach        Registry::headerFactory(new HeaderFactory());
185*785274b8SGreg Roach        Registry::imageFactory(new ImageFactory());
186*785274b8SGreg Roach        Registry::individualFactory(new IndividualFactory());
187*785274b8SGreg Roach        Registry::locationFactory(new LocationFactory());
188*785274b8SGreg Roach        Registry::mediaFactory(new MediaFactory());
189*785274b8SGreg Roach        Registry::noteFactory(new NoteFactory());
190*785274b8SGreg Roach        Registry::repositoryFactory(new RepositoryFactory());
191*785274b8SGreg Roach        Registry::slugFactory(new SlugFactory());
192*785274b8SGreg Roach        Registry::sourceFactory(new SourceFactory());
193*785274b8SGreg Roach        Registry::submissionFactory(new SubmissionFactory());
194*785274b8SGreg Roach        Registry::submitterFactory(new SubmitterFactory());
195*785274b8SGreg Roach        Registry::xrefFactory(new XrefFactory());
196*785274b8SGreg Roach    }
197*785274b8SGreg Roach
198*785274b8SGreg Roach    /**
199*785274b8SGreg Roach     * Respond to a CLI request.
200*785274b8SGreg Roach     *
201*785274b8SGreg Roach     * @return void
202*785274b8SGreg Roach     */
203*785274b8SGreg Roach    public function cliRequest(): void
204*785274b8SGreg Roach    {
205*785274b8SGreg Roach        // CLI handler will go here.
206*785274b8SGreg Roach    }
207*785274b8SGreg Roach
208*785274b8SGreg Roach    /**
209*785274b8SGreg Roach     * Response to an HTTP request.
210*785274b8SGreg Roach     *
211*785274b8SGreg Roach     * @return ResponseInterface
212*785274b8SGreg Roach     */
213*785274b8SGreg Roach    public function httpRequest(): ResponseInterface
214*785274b8SGreg Roach    {
215*785274b8SGreg Roach        // PSR7 messages and PSR17 message-factories
216*785274b8SGreg Roach        self::set(ResponseFactoryInterface::class, Psr17Factory::class);
217*785274b8SGreg Roach        self::set(ServerRequestFactoryInterface::class, Psr17Factory::class);
218*785274b8SGreg Roach        self::set(StreamFactoryInterface::class, Psr17Factory::class);
219*785274b8SGreg Roach        self::set(UploadedFileFactoryInterface::class, Psr17Factory::class);
220*785274b8SGreg Roach        self::set(UriFactoryInterface::class, Psr17Factory::class);
221*785274b8SGreg Roach
222*785274b8SGreg Roach        $request = $this->captureRequest();
223*785274b8SGreg Roach
224*785274b8SGreg Roach        return self::dispatch($request, self::MIDDLEWARE);
225*785274b8SGreg Roach    }
226*785274b8SGreg Roach
227*785274b8SGreg Roach    /**
228*785274b8SGreg Roach     * @param ServerRequestInterface            $request
229*785274b8SGreg Roach     * @param array<string|MiddlewareInterface> $middleware
230*785274b8SGreg Roach     *
231*785274b8SGreg Roach     * @return ResponseInterface
232*785274b8SGreg Roach     */
233*785274b8SGreg Roach    public static function dispatch(ServerRequestInterface $request, array $middleware): ResponseInterface
234*785274b8SGreg Roach    {
235*785274b8SGreg Roach        $dispatcher = new Dispatcher($middleware, self::container());
236*785274b8SGreg Roach
237*785274b8SGreg Roach        return $dispatcher->dispatch($request);
238f397d0fdSGreg Roach    }
2398d0ebef0SGreg Roach
240f397d0fdSGreg Roach    /**
241f397d0fdSGreg Roach     * An error handler that can be passed to set_error_handler().
242f397d0fdSGreg Roach     *
243f397d0fdSGreg Roach     * @return Closure
244f397d0fdSGreg Roach     */
245f397d0fdSGreg Roach    private function phpErrorHandler(): Closure
246f397d0fdSGreg Roach    {
247f397d0fdSGreg Roach        return static function (int $errno, string $errstr, string $errfile, int $errline): bool {
248f397d0fdSGreg Roach            // Ignore errors that are silenced with '@'
249f397d0fdSGreg Roach            if (error_reporting() & $errno) {
250f397d0fdSGreg Roach                throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
251f397d0fdSGreg Roach            }
252f397d0fdSGreg Roach
253f397d0fdSGreg Roach            return true;
254f397d0fdSGreg Roach        };
255f397d0fdSGreg Roach    }
256f397d0fdSGreg Roach
257f397d0fdSGreg Roach    /**
258*785274b8SGreg Roach     * Build the request from the PHP super-globals.
25900c45d23SGreg Roach     *
260*785274b8SGreg Roach     * @return ServerRequestInterface
261f397d0fdSGreg Roach     */
262*785274b8SGreg Roach    private function captureRequest(): ServerRequestInterface
263f397d0fdSGreg Roach    {
264*785274b8SGreg Roach        return self::make(ServerRequestCreator::class)->fromGlobals();
265f397d0fdSGreg Roach    }
266f397d0fdSGreg Roach
267f397d0fdSGreg Roach    /**
268*785274b8SGreg Roach     * @return ContainerInterface
269f397d0fdSGreg Roach     */
270*785274b8SGreg Roach    public static function container(): ContainerInterface
271f397d0fdSGreg Roach    {
272*785274b8SGreg Roach        return Container::getInstance();
273*785274b8SGreg Roach    }
274*785274b8SGreg Roach
275*785274b8SGreg Roach    /**
276*785274b8SGreg Roach     * Make an object, using dependency injection.
277*785274b8SGreg Roach     *
278*785274b8SGreg Roach     * @param string $class
279*785274b8SGreg Roach     *
280*785274b8SGreg Roach     * @return mixed
281*785274b8SGreg Roach     */
282*785274b8SGreg Roach    public static function make(string $class)
283*785274b8SGreg Roach    {
284*785274b8SGreg Roach        return Container::getInstance()->make($class);
285*785274b8SGreg Roach    }
286*785274b8SGreg Roach
287*785274b8SGreg Roach    /**
288*785274b8SGreg Roach     * Write a value into the container.
289*785274b8SGreg Roach     *
290*785274b8SGreg Roach     * @param string        $abstract
291*785274b8SGreg Roach     * @param string|object $concrete
292*785274b8SGreg Roach     */
293*785274b8SGreg Roach    public static function set(string $abstract, $concrete): void
294*785274b8SGreg Roach    {
295*785274b8SGreg Roach        if (is_string($concrete)) {
296*785274b8SGreg Roach            Container::getInstance()->bind($abstract, $concrete);
297*785274b8SGreg Roach        } else {
298*785274b8SGreg Roach            Container::getInstance()->instance($abstract, $concrete);
299*785274b8SGreg Roach        }
3008d0ebef0SGreg Roach    }
3018d0ebef0SGreg Roach}
302