xref: /webtrees/app/Http/Middleware/UseDatabase.php (revision e873f434551745f888937263ff89e80db3b0f785)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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\Http\Middleware;
21
22use Fisharebest\Webtrees\DB;
23use Fisharebest\Webtrees\Validator;
24use Fisharebest\Webtrees\Webtrees;
25use PDO;
26use PDOException;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29use Psr\Http\Server\MiddlewareInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31use RuntimeException;
32
33/**
34 * Middleware to connect to the database.
35 */
36class UseDatabase implements MiddlewareInterface
37{
38    /**
39     * @param ServerRequestInterface  $request
40     * @param RequestHandlerInterface $handler
41     *
42     * @return ResponseInterface
43     */
44    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
45    {
46        DB::connect(
47            driver: Validator::attributes($request)->string('dbtype', DB::MYSQL),
48            host: Validator::attributes($request)->string('dbhost'),
49            port: Validator::attributes($request)->string('dbport'),
50            database: Validator::attributes($request)->string('dbname'),
51            username: Validator::attributes($request)->string('dbuser'),
52            password: Validator::attributes($request)->string('dbpass'),
53            prefix: Validator::attributes($request)->string('tblpfx'),
54            key: Validator::attributes($request)->string('dbkey', ''),
55            certificate: Validator::attributes($request)->string('dbcert', ''),
56            ca: Validator::attributes($request)->string('dbca', ''),
57            verify_certificate: Validator::attributes($request)->boolean('dbverify', false),
58        );
59
60        return $handler->handle($request);
61    }
62}
63