xref: /webtrees/app/Webtrees.php (revision 6f59525087dd4ee1d17c7375524d95ace7942861)
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\MarkdownFactory;
34use Fisharebest\Webtrees\Factories\MediaFactory;
35use Fisharebest\Webtrees\Factories\NoteFactory;
36use Fisharebest\Webtrees\Factories\RepositoryFactory;
37use Fisharebest\Webtrees\Factories\SlugFactory;
38use Fisharebest\Webtrees\Factories\SourceFactory;
39use Fisharebest\Webtrees\Factories\SubmissionFactory;
40use Fisharebest\Webtrees\Factories\SubmitterFactory;
41use Fisharebest\Webtrees\Factories\XrefFactory;
42use Fisharebest\Webtrees\Http\Middleware\BadBotBlocker;
43use Fisharebest\Webtrees\Http\Middleware\BootModules;
44use Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode;
45use Fisharebest\Webtrees\Http\Middleware\ClientIp;
46use Fisharebest\Webtrees\Http\Middleware\CompressResponse;
47use Fisharebest\Webtrees\Http\Middleware\ContentLength;
48use Fisharebest\Webtrees\Http\Middleware\DoHousekeeping;
49use Fisharebest\Webtrees\Http\Middleware\EmitResponse;
50use Fisharebest\Webtrees\Http\Middleware\HandleExceptions;
51use Fisharebest\Webtrees\Http\Middleware\LoadRoutes;
52use Fisharebest\Webtrees\Http\Middleware\NoRouteFound;
53use Fisharebest\Webtrees\Http\Middleware\ReadConfigIni;
54use Fisharebest\Webtrees\Http\Middleware\Router;
55use Fisharebest\Webtrees\Http\Middleware\SecurityHeaders;
56use Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema;
57use Fisharebest\Webtrees\Http\Middleware\UseDatabase;
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 Illuminate\Container\Container;
64use Middleland\Dispatcher;
65use Nyholm\Psr7\Factory\Psr17Factory;
66use Nyholm\Psr7Server\ServerRequestCreator;
67use Psr\Container\ContainerInterface;
68use Psr\Http\Message\ResponseFactoryInterface;
69use Psr\Http\Message\ResponseInterface;
70use Psr\Http\Message\ServerRequestFactoryInterface;
71use Psr\Http\Message\ServerRequestInterface;
72use Psr\Http\Message\StreamFactoryInterface;
73use Psr\Http\Message\UploadedFileFactoryInterface;
74use Psr\Http\Message\UriFactoryInterface;
75use Psr\Http\Server\MiddlewareInterface;
76use Symfony\Contracts\Translation\TranslatorInterface;
77
78use function class_alias;
79use function date_default_timezone_set;
80use function error_reporting;
81use function is_string;
82use function mb_internal_encoding;
83use function set_error_handler;
84
85use const E_ALL;
86use const E_DEPRECATED;
87use const E_USER_DEPRECATED;
88
89/**
90 * Definitions for the webtrees application.
91 */
92class Webtrees
93{
94    // The root folder of this installation
95    public const ROOT_DIR = __DIR__ . '/../';
96
97    // This is the location of system data, such as temporary and cache files.
98    // The system files are always in this location.
99    // It is also the default location of user data, such as media and GEDCOM files.
100    // The user files could be anywhere supported by Flysystem.
101    public const DATA_DIR = self::ROOT_DIR . 'data/';
102
103    // Location of the file containing the database connection details.
104    public const CONFIG_FILE = self::DATA_DIR . 'config.ini.php';
105
106    // Location of the file that triggers maintenance mode.
107    public const OFFLINE_FILE = self::DATA_DIR . 'offline.txt';
108
109    // Location of our modules.
110    public const MODULES_PATH = 'modules_v4/';
111    public const MODULES_DIR  = self::ROOT_DIR . self::MODULES_PATH;
112
113    // Enable debugging on development builds.
114    public const DEBUG = self::STABILITY !== '';
115
116    // We want to know about all PHP errors during development, and fewer in production.
117    public const ERROR_REPORTING = self::DEBUG ? E_ALL : E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED;
118
119    // The name of the application.
120    public const NAME = 'webtrees';
121
122    // Required version of database tables/columns/indexes/etc.
123    public const SCHEMA_VERSION = 45;
124
125    // e.g. "-dev", "-alpha", "-beta", etc.
126    public const STABILITY = '-alpha.2';
127
128    // Version number
129    public const VERSION = '2.1.0' . self::STABILITY;
130
131    // Project website.
132    public const URL = 'https://webtrees.net/';
133
134    // FAQ links
135    public const URL_FAQ_EMAIL = 'https://webtrees.net/faq/email';
136
137    // Project website.
138    public const GEDCOM_PDF = 'https://webtrees.net/downloads/gedcom-5-5-1.pdf';
139
140    private const MIDDLEWARE = [
141        EmitResponse::class,
142        SecurityHeaders::class,
143        ReadConfigIni::class,
144        BaseUrl::class,
145        HandleExceptions::class,
146        ClientIp::class,
147        ContentLength::class,
148        CompressResponse::class,
149        BadBotBlocker::class,
150        UseDatabase::class,
151        UpdateDatabaseSchema::class,
152        UseSession::class,
153        UseLanguage::class,
154        CheckForMaintenanceMode::class,
155        UseTheme::class,
156        DoHousekeeping::class,
157        UseTransaction::class,
158        LoadRoutes::class,
159        BootModules::class,
160        Router::class,
161        NoRouteFound::class,
162    ];
163
164    /**
165     * Initialise the application.
166     *
167     * @return void
168     */
169    public function bootstrap(): void
170    {
171        // Show all errors and warnings in development, fewer in production.
172        error_reporting(self::ERROR_REPORTING);
173        set_error_handler($this->phpErrorHandler());
174
175        // All modern software uses UTF-8 encoding.
176        mb_internal_encoding('UTF-8');
177
178        // Use UTC internally and convert to local time when displaying datetimes.
179        date_default_timezone_set('UTC');
180
181        // Factory objects
182        Registry::cache(new CacheFactory());
183        Registry::familyFactory(new FamilyFactory());
184        Registry::filesystem(new FilesystemFactory());
185        Registry::elementFactory(new ElementFactory());
186        Registry::gedcomRecordFactory(new GedcomRecordFactory());
187        Registry::headerFactory(new HeaderFactory());
188        Registry::imageFactory(new ImageFactory());
189        Registry::individualFactory(new IndividualFactory());
190        Registry::locationFactory(new LocationFactory());
191        Registry::markdownFactory(new MarkdownFactory());
192        Registry::mediaFactory(new MediaFactory());
193        Registry::noteFactory(new NoteFactory());
194        Registry::repositoryFactory(new RepositoryFactory());
195        Registry::slugFactory(new SlugFactory());
196        Registry::sourceFactory(new SourceFactory());
197        Registry::submissionFactory(new SubmissionFactory());
198        Registry::submitterFactory(new SubmitterFactory());
199        Registry::xrefFactory(new XrefFactory());
200
201        // Workaround for https://github.com/fisharebest/webtrees/issues/4179
202        //class_alias(TranslatorInterface::class, 'Symfony\\Component\\Translation\\TranslatorInterface');
203    }
204
205    /**
206     * Respond to a CLI request.
207     *
208     * @return void
209     */
210    public function cliRequest(): void
211    {
212        // CLI handler will go here.
213    }
214
215    /**
216     * Respond to an HTTP request.
217     *
218     * @return ResponseInterface
219     */
220    public function httpRequest(): ResponseInterface
221    {
222        $psr17factory = new Psr17Factory();
223
224        // PSR7 messages and PSR17 message-factories
225        self::set(ResponseFactoryInterface::class, $psr17factory);
226        self::set(ServerRequestFactoryInterface::class, $psr17factory);
227        self::set(StreamFactoryInterface::class, $psr17factory);
228        self::set(UploadedFileFactoryInterface::class, $psr17factory);
229        self::set(UriFactoryInterface::class, $psr17factory);
230
231        $server_request_creator = new ServerRequestCreator($psr17factory, $psr17factory, $psr17factory, $psr17factory);
232
233        $request = $server_request_creator->fromGlobals();
234
235        return self::dispatch($request, self::MIDDLEWARE);
236    }
237
238    /**
239     * @param ServerRequestInterface            $request
240     * @param array<string|MiddlewareInterface> $middleware
241     *
242     * @return ResponseInterface
243     */
244    public static function dispatch(ServerRequestInterface $request, array $middleware): ResponseInterface
245    {
246        $dispatcher = new Dispatcher($middleware, self::container());
247
248        return $dispatcher->dispatch($request);
249    }
250
251    /**
252     * An error handler that can be passed to set_error_handler().
253     *
254     * @return Closure
255     */
256    private function phpErrorHandler(): Closure
257    {
258        return static function (int $errno, string $errstr, string $errfile, int $errline): bool {
259            // Ignore errors that are silenced with '@'
260            if (error_reporting() & $errno) {
261                throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
262            }
263
264            return true;
265        };
266    }
267
268    /**
269     * @return ContainerInterface
270     */
271    public static function container(): ContainerInterface
272    {
273        return Container::getInstance();
274    }
275
276    /**
277     * Make an object, using dependency injection.
278     *
279     * @param string $class
280     *
281     * @return mixed
282     */
283    public static function make(string $class)
284    {
285        return Container::getInstance()->make($class);
286    }
287
288    /**
289     * Write a value into the container.
290     *
291     * @param string        $abstract
292     * @param string|object $concrete
293     */
294    public static function set(string $abstract, $concrete): void
295    {
296        if (is_string($concrete)) {
297            Container::getInstance()->bind($abstract, $concrete);
298        } else {
299            Container::getInstance()->instance($abstract, $concrete);
300        }
301    }
302}
303