xref: /webtrees/app/Webtrees.php (revision 721424fe1290c31caffbe585da4fc5e0b2464f64)
18d0ebef0SGreg Roach<?php
23976b470SGreg Roach
38d0ebef0SGreg Roach/**
48d0ebef0SGreg Roach * webtrees: online genealogy
516cd81adSGreg Roach * Copyright (C) 2020 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
158d0ebef0SGreg Roach * along with this program. If not, see <http://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;
24089dadacSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BadBotBlocker;
25f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BootModules;
26f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode;
274874f72dSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ClientIp;
28f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\DoHousekeeping;
29f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\EmitResponse;
30f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\HandleExceptions;
3171378461SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\LoadRoutes;
32f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\NoRouteFound;
33f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\PhpEnvironment;
34e16a1bfdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\ReadConfigIni;
35bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\RegisterFactories;
369e5d8e6fSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\Router;
379c88b4e9SGreg Roachuse Fisharebest\Webtrees\Http\Middleware\SecurityHeaders;
38f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema;
39f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseCache;
40f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseDatabase;
41f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseDebugbar;
42f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseFilesystem;
43150f35adSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseLanguage;
44f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseSession;
45f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseTheme;
46f397d0fdSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\UseTransaction;
474874f72dSGreg Roachuse Fisharebest\Webtrees\Http\Middleware\BaseUrl;
48f397d0fdSGreg Roachuse Nyholm\Psr7\Factory\Psr17Factory;
49f397d0fdSGreg Roachuse Psr\Http\Message\ResponseFactoryInterface;
50f397d0fdSGreg Roachuse Psr\Http\Message\ServerRequestFactoryInterface;
51f397d0fdSGreg Roachuse Psr\Http\Message\StreamFactoryInterface;
52f397d0fdSGreg Roachuse Psr\Http\Message\UploadedFileFactoryInterface;
53f397d0fdSGreg Roachuse Psr\Http\Message\UriFactoryInterface;
543976b470SGreg Roach
55f397d0fdSGreg Roachuse function app;
568d0ebef0SGreg Roachuse function error_reporting;
578d0ebef0SGreg Roachuse function set_error_handler;
588d0ebef0SGreg Roach
59*721424feSGreg Roachuse const E_ALL;
60*721424feSGreg Roachuse const E_DEPRECATED;
61054771e9SGreg Roachuse const E_USER_DEPRECATED;
62054771e9SGreg Roach
638d0ebef0SGreg Roach/**
648d0ebef0SGreg Roach * Definitions for the webtrees application.
658d0ebef0SGreg Roach */
668d0ebef0SGreg Roachclass Webtrees
678d0ebef0SGreg Roach{
68f397d0fdSGreg Roach    // The root folder of this installation
69f397d0fdSGreg Roach    public const ROOT_DIR = __DIR__ . '/../';
70f397d0fdSGreg Roach
71baef639bSGreg Roach    // This is the location of system data, such as temporary and cache files.
72baef639bSGreg Roach    // The system files are always in this location.
73baef639bSGreg Roach    // It is also the default location of user data, such as media and GEDCOM files.
74baef639bSGreg Roach    // The user files could be anywhere supported by Flysystem.
75fd6c003fSGreg Roach    public const DATA_DIR  = self::ROOT_DIR . 'data/';
76fd6c003fSGreg Roach
778d0ebef0SGreg Roach    // Location of the file containing the database connection details.
78baef639bSGreg Roach    public const CONFIG_FILE = self::DATA_DIR . 'config.ini.php';
79f397d0fdSGreg Roach
80f397d0fdSGreg Roach    // Location of the file that triggers maintenance mode.
81baef639bSGreg Roach    public const OFFLINE_FILE = self::DATA_DIR . 'offline.txt';
82f397d0fdSGreg Roach
83f397d0fdSGreg Roach    // Location of our modules.
84b50f90f8SGreg Roach    public const MODULES_PATH = 'modules_v4/';
85f397d0fdSGreg Roach    public const MODULES_DIR  = self::ROOT_DIR . self::MODULES_PATH;
868d0ebef0SGreg Roach
878d0ebef0SGreg Roach    // Enable debugging on development builds.
8816d6367aSGreg Roach    public const DEBUG = self::STABILITY !== '';
898d0ebef0SGreg Roach
908d0ebef0SGreg Roach    // We want to know about all PHP errors during development, and fewer in production.
91*721424feSGreg Roach    public const ERROR_REPORTING = self::DEBUG ? E_ALL : E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED;
928d0ebef0SGreg Roach
938d0ebef0SGreg Roach    // The name of the application.
9416d6367aSGreg Roach    public const NAME = 'webtrees';
958d0ebef0SGreg Roach
968d0ebef0SGreg Roach    // Required version of database tables/columns/indexes/etc.
97c7aa856bSGreg Roach    public const SCHEMA_VERSION = 44;
988d0ebef0SGreg Roach
99*721424feSGreg Roach    // e.g. "-dev", "-alpha", "-beta", etc.
100*721424feSGreg Roach    public const STABILITY = '';
1018d0ebef0SGreg Roach
1028d0ebef0SGreg Roach    // Version number
103*721424feSGreg Roach    public const VERSION = '2.0.7' . self::STABILITY;
1048d0ebef0SGreg Roach
105f397d0fdSGreg Roach    // Project website.
106db1aabc1SGreg Roach    public const URL = 'https://webtrees.net/';
1078d0ebef0SGreg Roach
1083282ccecSGreg Roach    // FAQ linnks
1093282ccecSGreg Roach    public const URL_FAQ_EMAIL = 'https://webtrees.net/faq/email';
1103282ccecSGreg Roach
1119d7cdf93SGreg Roach    // Project website.
112db1aabc1SGreg Roach    public const GEDCOM_PDF = 'https://webtrees.net/downloads/gedcom-551.pdf';
1139d7cdf93SGreg Roach
11482e92bfaSGreg Roach    private const MIDDLEWARE = [
11582e92bfaSGreg Roach        PhpEnvironment::class,
11682e92bfaSGreg Roach        EmitResponse::class,
1179c88b4e9SGreg Roach        SecurityHeaders::class,
11882e92bfaSGreg Roach        ReadConfigIni::class,
1194874f72dSGreg Roach        BaseUrl::class,
1205fb051e9SGreg Roach        HandleExceptions::class,
1214874f72dSGreg Roach        ClientIp::class,
122089dadacSGreg Roach        UseCache::class,
123089dadacSGreg Roach        BadBotBlocker::class,
12482e92bfaSGreg Roach        UseDatabase::class,
12582e92bfaSGreg Roach        UseDebugbar::class,
12682e92bfaSGreg Roach        UpdateDatabaseSchema::class,
12782e92bfaSGreg Roach        UseFilesystem::class,
12882e92bfaSGreg Roach        UseSession::class,
129150f35adSGreg Roach        UseLanguage::class,
1306b5b5551SGreg Roach        RegisterFactories::class,
13182e92bfaSGreg Roach        CheckForMaintenanceMode::class,
13282e92bfaSGreg Roach        UseTheme::class,
13382e92bfaSGreg Roach        DoHousekeeping::class,
13482e92bfaSGreg Roach        UseTransaction::class,
13571378461SGreg Roach        LoadRoutes::class,
13682e92bfaSGreg Roach        BootModules::class,
13782e92bfaSGreg Roach        Router::class,
13882e92bfaSGreg Roach        NoRouteFound::class,
13982e92bfaSGreg Roach    ];
14082e92bfaSGreg Roach
1418d0ebef0SGreg Roach    /**
1428d0ebef0SGreg Roach     * Initialise the application.
1438d0ebef0SGreg Roach     *
1448d0ebef0SGreg Roach     * @return void
1458d0ebef0SGreg Roach     */
146f397d0fdSGreg Roach    public function bootstrap(): void
1478d0ebef0SGreg Roach    {
1488d0ebef0SGreg Roach        // Show all errors and warnings in development, fewer in production.
149cfbf56adSGreg Roach        error_reporting(self::ERROR_REPORTING);
1508d0ebef0SGreg Roach
151f397d0fdSGreg Roach        set_error_handler($this->phpErrorHandler());
152f397d0fdSGreg Roach    }
1538d0ebef0SGreg Roach
154f397d0fdSGreg Roach    /**
155f397d0fdSGreg Roach     * An error handler that can be passed to set_error_handler().
156f397d0fdSGreg Roach     *
157f397d0fdSGreg Roach     * @return Closure
158f397d0fdSGreg Roach     */
159f397d0fdSGreg Roach    private function phpErrorHandler(): Closure
160f397d0fdSGreg Roach    {
161f397d0fdSGreg Roach        return static function (int $errno, string $errstr, string $errfile, int $errline): bool {
162f397d0fdSGreg Roach            // Ignore errors that are silenced with '@'
163f397d0fdSGreg Roach            if (error_reporting() & $errno) {
164f397d0fdSGreg Roach                throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
165f397d0fdSGreg Roach            }
166f397d0fdSGreg Roach
167f397d0fdSGreg Roach            return true;
168f397d0fdSGreg Roach        };
169f397d0fdSGreg Roach    }
170f397d0fdSGreg Roach
171f397d0fdSGreg Roach    /**
172f397d0fdSGreg Roach     * We can use any PSR-7 / PSR-17 compatible message factory.
173f397d0fdSGreg Roach     *
174f397d0fdSGreg Roach     * @return void
175f397d0fdSGreg Roach     */
176f397d0fdSGreg Roach    public function selectMessageFactory(): void
177f397d0fdSGreg Roach    {
178f397d0fdSGreg Roach        app()->bind(ResponseFactoryInterface::class, Psr17Factory::class);
179f397d0fdSGreg Roach        app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class);
180f397d0fdSGreg Roach        app()->bind(StreamFactoryInterface::class, Psr17Factory::class);
181f397d0fdSGreg Roach        app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class);
182f397d0fdSGreg Roach        app()->bind(UriFactoryInterface::class, Psr17Factory::class);
183f397d0fdSGreg Roach    }
184f397d0fdSGreg Roach
185f397d0fdSGreg Roach    /**
186f397d0fdSGreg Roach     * The webtrees application is built from middleware.
187f397d0fdSGreg Roach     *
1887770f26cSGreg Roach     * @return string[]
189f397d0fdSGreg Roach     */
190f397d0fdSGreg Roach    public function middleware(): array
191f397d0fdSGreg Roach    {
19282e92bfaSGreg Roach        return self::MIDDLEWARE;
1938d0ebef0SGreg Roach    }
1948d0ebef0SGreg Roach}
195