xref: /webtrees/app/Http/Middleware/UseDatabase.php (revision 9b93b7c342a543e7bd55b5a09d97ba0c39fc32b0)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Http\Middleware;
19
20use Fisharebest\Webtrees\Webtrees;
21use Illuminate\Database\Capsule\Manager as DB;
22use Illuminate\Database\Query\Builder;
23use Psr\Http\Message\ResponseInterface;
24use Psr\Http\Message\ServerRequestInterface;
25use Psr\Http\Server\MiddlewareInterface;
26use Psr\Http\Server\RequestHandlerInterface;
27
28/**
29 * Middleware to connect to the database.
30 */
31class UseDatabase implements MiddlewareInterface
32{
33    /**
34     * @param ServerRequestInterface  $request
35     * @param RequestHandlerInterface $handler
36     *
37     * @return ResponseInterface
38     */
39    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
40    {
41        // Earlier versions of webtrees did not have a dbtype config option.  They always used mysql.
42        $driver = $request->getAttribute('dbtype', 'mysql');
43
44        $dbname = $request->getAttribute('dbname');
45
46        if ($driver === 'sqlite') {
47            $dbname = Webtrees::ROOT_DIR . 'data/' . $dbname . '.sqlite';
48        }
49
50        $capsule = new DB();
51
52        $capsule->addConnection([
53            'driver'                  => $driver,
54            'host'                    => $request->getAttribute('dbhost'),
55            'port'                    => $request->getAttribute('dbport'),
56            'database'                => $dbname,
57            'username'                => $request->getAttribute('dbuser'),
58            'password'                => $request->getAttribute('dbpass'),
59            'prefix'                  => $request->getAttribute('tblpfx'),
60            'prefix_indexes'          => true,
61            // For MySQL
62            'charset'                 => 'utf8',
63            'collation'               => 'utf8_unicode_ci',
64            'timezone'                => '+00:00',
65            'engine'                  => 'InnoDB',
66            'modes'                   => [
67                'ANSI',
68                'STRICT_TRANS_TABLES',
69                'NO_ZERO_IN_DATE',
70                'NO_ZERO_DATE',
71                'ERROR_FOR_DIVISION_BY_ZERO',
72            ],
73            // For SQLite
74            'foreign_key_constraints' => true,
75        ]);
76
77        $capsule->setAsGlobal();
78
79        Builder::macro('whereContains', function ($column, string $search, string $boolean = 'and'): Builder {
80            $search = strtr($search, ['\\' => '\\\\', '%' => '\\%', '_' => '\\_', ' ' => '%']);
81
82            return $this->where($column, 'LIKE', '%' . $search . '%', $boolean);
83        });
84
85        return $handler->handle($request);
86    }
87}
88