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