xref: /webtrees/app/Http/Middleware/UseDatabase.php (revision 52550490b7095dd69811f3ec21ed5a3ca1a8968d)
16ccdf4f0SGreg Roach<?php
23976b470SGreg Roach
36ccdf4f0SGreg Roach/**
46ccdf4f0SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
66ccdf4f0SGreg Roach * This program is free software: you can redistribute it and/or modify
76ccdf4f0SGreg Roach * it under the terms of the GNU General Public License as published by
86ccdf4f0SGreg Roach * the Free Software Foundation, either version 3 of the License, or
96ccdf4f0SGreg Roach * (at your option) any later version.
106ccdf4f0SGreg Roach * This program is distributed in the hope that it will be useful,
116ccdf4f0SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
126ccdf4f0SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
136ccdf4f0SGreg Roach * GNU General Public License for more details.
146ccdf4f0SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
166ccdf4f0SGreg Roach */
17fcfa147eSGreg Roach
186ccdf4f0SGreg Roachdeclare(strict_types=1);
196ccdf4f0SGreg Roach
206ccdf4f0SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
216ccdf4f0SGreg Roach
226f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
23b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
246ccdf4f0SGreg Roachuse Fisharebest\Webtrees\Webtrees;
2560a0fc35SGreg Roachuse PDO;
26b8d46257SGreg Roachuse PDOException;
276ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
296ccdf4f0SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
306ccdf4f0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
316e9f3eb9SGreg Roachuse RuntimeException;
326ccdf4f0SGreg Roach
336ccdf4f0SGreg Roach/**
346ccdf4f0SGreg Roach * Middleware to connect to the database.
356ccdf4f0SGreg Roach */
366ccdf4f0SGreg Roachclass UseDatabase implements MiddlewareInterface
376ccdf4f0SGreg Roach{
386ccdf4f0SGreg Roach    /**
396ccdf4f0SGreg Roach     * @param ServerRequestInterface  $request
406ccdf4f0SGreg Roach     * @param RequestHandlerInterface $handler
416ccdf4f0SGreg Roach     *
426ccdf4f0SGreg Roach     * @return ResponseInterface
436ccdf4f0SGreg Roach     */
446ccdf4f0SGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
456ccdf4f0SGreg Roach    {
46*52550490SGreg Roach        DB::connect(
47*52550490SGreg Roach            driver: Validator::attributes($request)->string('dbtype', DB::MYSQL),
48*52550490SGreg Roach            host: Validator::attributes($request)->string('dbhost'),
49*52550490SGreg Roach            port: Validator::attributes($request)->string('dbport'),
50*52550490SGreg Roach            database: Validator::attributes($request)->string('dbname'),
51*52550490SGreg Roach            username: Validator::attributes($request)->string('dbuser'),
52*52550490SGreg Roach            password: Validator::attributes($request)->string('dbpass'),
53*52550490SGreg Roach            prefix: Validator::attributes($request)->string('tblpfx'),
54*52550490SGreg Roach            key: Validator::attributes($request)->string('dbkey', ''),
55*52550490SGreg Roach            certificate: Validator::attributes($request)->string('dbcert', ''),
56*52550490SGreg Roach            ca: Validator::attributes($request)->string('dbca', ''),
57*52550490SGreg Roach            verify_certificate: Validator::attributes($request)->boolean('dbverify', false),
58*52550490SGreg Roach        );
59b8d46257SGreg Roach
60ff020ee8SGreg Roach        return $handler->handle($request);
616ccdf4f0SGreg Roach    }
626ccdf4f0SGreg Roach}
63