xref: /webtrees/app/Webtrees.php (revision 56afe05fa72eb037740c3a05c02828a7ba30f9a9)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 Closure;
23use ErrorException;
24use Fisharebest\Webtrees\Factories\CacheFactory;
25use Fisharebest\Webtrees\Factories\ElementFactory;
26use Fisharebest\Webtrees\Factories\FamilyFactory;
27use Fisharebest\Webtrees\Factories\FilesystemFactory;
28use Fisharebest\Webtrees\Factories\GedcomRecordFactory;
29use Fisharebest\Webtrees\Factories\HeaderFactory;
30use Fisharebest\Webtrees\Factories\ImageFactory;
31use Fisharebest\Webtrees\Factories\IndividualFactory;
32use Fisharebest\Webtrees\Factories\LocationFactory;
33use Fisharebest\Webtrees\Factories\MediaFactory;
34use Fisharebest\Webtrees\Factories\NoteFactory;
35use Fisharebest\Webtrees\Factories\RepositoryFactory;
36use Fisharebest\Webtrees\Factories\SlugFactory;
37use Fisharebest\Webtrees\Factories\SourceFactory;
38use Fisharebest\Webtrees\Factories\SubmissionFactory;
39use Fisharebest\Webtrees\Factories\SubmitterFactory;
40use Fisharebest\Webtrees\Factories\XrefFactory;
41use Fisharebest\Webtrees\Http\Middleware\BadBotBlocker;
42use Fisharebest\Webtrees\Http\Middleware\BootModules;
43use Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode;
44use Fisharebest\Webtrees\Http\Middleware\ClientIp;
45use Fisharebest\Webtrees\Http\Middleware\CompressResponse;
46use Fisharebest\Webtrees\Http\Middleware\DoHousekeeping;
47use Fisharebest\Webtrees\Http\Middleware\EmitResponse;
48use Fisharebest\Webtrees\Http\Middleware\HandleExceptions;
49use Fisharebest\Webtrees\Http\Middleware\LoadRoutes;
50use Fisharebest\Webtrees\Http\Middleware\NoRouteFound;
51use Fisharebest\Webtrees\Http\Middleware\PhpEnvironment;
52use Fisharebest\Webtrees\Http\Middleware\ReadConfigIni;
53use Fisharebest\Webtrees\Http\Middleware\Router;
54use Fisharebest\Webtrees\Http\Middleware\SecurityHeaders;
55use Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema;
56use Fisharebest\Webtrees\Http\Middleware\UseDatabase;
57use Fisharebest\Webtrees\Http\Middleware\UseDebugbar;
58use Fisharebest\Webtrees\Http\Middleware\UseLanguage;
59use Fisharebest\Webtrees\Http\Middleware\UseSession;
60use Fisharebest\Webtrees\Http\Middleware\UseTheme;
61use Fisharebest\Webtrees\Http\Middleware\UseTransaction;
62use Fisharebest\Webtrees\Http\Middleware\BaseUrl;
63use Nyholm\Psr7\Factory\Psr17Factory;
64
65use function error_reporting;
66use function set_error_handler;
67
68use const E_ALL;
69use const E_DEPRECATED;
70use const E_USER_DEPRECATED;
71
72/**
73 * Definitions for the webtrees application.
74 */
75class Webtrees
76{
77    // The root folder of this installation
78    public const ROOT_DIR = __DIR__ . '/../';
79
80    // This is the location of system data, such as temporary and cache files.
81    // The system files are always in this location.
82    // It is also the default location of user data, such as media and GEDCOM files.
83    // The user files could be anywhere supported by Flysystem.
84    public const DATA_DIR  = self::ROOT_DIR . 'data/';
85
86    // Location of the file containing the database connection details.
87    public const CONFIG_FILE = self::DATA_DIR . 'config.ini.php';
88
89    // Location of the file that triggers maintenance mode.
90    public const OFFLINE_FILE = self::DATA_DIR . 'offline.txt';
91
92    // Location of our modules.
93    public const MODULES_PATH = 'modules_v4/';
94    public const MODULES_DIR  = self::ROOT_DIR . self::MODULES_PATH;
95
96    // Enable debugging on development builds.
97    public const DEBUG = self::STABILITY !== '';
98
99    // We want to know about all PHP errors during development, and fewer in production.
100    public const ERROR_REPORTING = self::DEBUG ? E_ALL : E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED;
101
102    // The name of the application.
103    public const NAME = 'webtrees';
104
105    // Required version of database tables/columns/indexes/etc.
106    public const SCHEMA_VERSION = 45;
107
108    // e.g. "-dev", "-alpha", "-beta", etc.
109    public const STABILITY = '-dev';
110
111    // Version number
112    public const VERSION = '2.1.0' . self::STABILITY;
113
114    // Project website.
115    public const URL = 'https://webtrees.net/';
116
117    // FAQ links
118    public const URL_FAQ_EMAIL = 'https://webtrees.net/faq/email';
119
120    // Project website.
121    public const GEDCOM_PDF = 'https://webtrees.net/downloads/gedcom-551.pdf';
122
123    private const MIDDLEWARE = [
124        PhpEnvironment::class,
125        EmitResponse::class,
126        SecurityHeaders::class,
127        ReadConfigIni::class,
128        BaseUrl::class,
129        HandleExceptions::class,
130        ClientIp::class,
131        CompressResponse::class,
132        BadBotBlocker::class,
133        UseDatabase::class,
134        UseDebugbar::class,
135        UpdateDatabaseSchema::class,
136        UseSession::class,
137        UseLanguage::class,
138        CheckForMaintenanceMode::class,
139        UseTheme::class,
140        DoHousekeeping::class,
141        UseTransaction::class,
142        LoadRoutes::class,
143        BootModules::class,
144        Router::class,
145        NoRouteFound::class,
146    ];
147
148    /**
149     * @return void
150     */
151    public function registerErrorHandler(): void
152    {
153        // Show all errors and warnings in development, fewer in production.
154        error_reporting(self::ERROR_REPORTING);
155
156        set_error_handler($this->phpErrorHandler());
157    }
158
159    /**
160     * An error handler that can be passed to set_error_handler().
161     *
162     * @return Closure
163     */
164    private function phpErrorHandler(): Closure
165    {
166        return static function (int $errno, string $errstr, string $errfile, int $errline): bool {
167            // Ignore errors that are silenced with '@'
168            if (error_reporting() & $errno) {
169                throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
170            }
171
172            return true;
173        };
174    }
175
176    /**
177     * @return void
178     */
179    public function registerFactories(): void
180    {
181        Registry::cache(new CacheFactory());
182        Registry::familyFactory(new FamilyFactory());
183        Registry::filesystem(new FilesystemFactory());
184        Registry::elementFactory(new ElementFactory());
185        Registry::gedcomRecordFactory(new GedcomRecordFactory());
186        Registry::headerFactory(new HeaderFactory());
187        Registry::imageFactory(new ImageFactory());
188        Registry::individualFactory(new IndividualFactory());
189        Registry::locationFactory(new LocationFactory());
190        Registry::mediaFactory(new MediaFactory());
191        Registry::noteFactory(new NoteFactory());
192        Registry::repositoryFactory(new RepositoryFactory());
193        Registry::slugFactory(new SlugFactory());
194        Registry::sourceFactory(new SourceFactory());
195        Registry::submissionFactory(new SubmissionFactory());
196        Registry::submitterFactory(new SubmitterFactory());
197        Registry::xrefFactory(new XrefFactory());
198
199        Registry::serverRequestFactory(new Psr17Factory());
200        Registry::streamFactory(new Psr17Factory());
201        Registry::responseFactory(new Psr17Factory());
202        Registry::uploadedFileFactory(new Psr17Factory());
203        Registry::uriFactory(new Psr17Factory());
204    }
205
206    /**
207     * The webtrees application is built from middleware.
208     *
209     * @return array<string>
210     */
211    public function middleware(): array
212    {
213        return self::MIDDLEWARE;
214    }
215}
216