xref: /webtrees/app/Http/Middleware/UseDatabase.php (revision b5961194694c0b5b2dc4269689207eb972e3b20c)
16ccdf4f0SGreg Roach<?php
23976b470SGreg Roach
36ccdf4f0SGreg Roach/**
46ccdf4f0SGreg Roach * webtrees: online genealogy
56e9f3eb9SGreg Roach * Copyright (C) 2020 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
156ccdf4f0SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
166ccdf4f0SGreg Roach */
17fcfa147eSGreg Roach
186ccdf4f0SGreg Roachdeclare(strict_types=1);
196ccdf4f0SGreg Roach
206ccdf4f0SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
216ccdf4f0SGreg Roach
226ccdf4f0SGreg Roachuse Fisharebest\Webtrees\Webtrees;
23e16a1bfdSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
24e16a1bfdSGreg Roachuse Illuminate\Database\Query\Builder;
255c98992aSGreg Roachuse LogicException;
2660a0fc35SGreg Roachuse PDO;
27b8d46257SGreg Roachuse PDOException;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
306ccdf4f0SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
316ccdf4f0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
326e9f3eb9SGreg Roachuse RuntimeException;
336ccdf4f0SGreg Roach
34*b5961194SGreg Roachuse function addcslashes;
35*b5961194SGreg Roachuse function trigger_error;
36*b5961194SGreg Roach
37*b5961194SGreg Roachuse const E_USER_DEPRECATED;
38*b5961194SGreg Roach
396ccdf4f0SGreg Roach/**
406ccdf4f0SGreg Roach * Middleware to connect to the database.
416ccdf4f0SGreg Roach */
426ccdf4f0SGreg Roachclass UseDatabase implements MiddlewareInterface
436ccdf4f0SGreg Roach{
446ccdf4f0SGreg Roach    /**
456ccdf4f0SGreg Roach     * @param ServerRequestInterface  $request
466ccdf4f0SGreg Roach     * @param RequestHandlerInterface $handler
476ccdf4f0SGreg Roach     *
486ccdf4f0SGreg Roach     * @return ResponseInterface
496ccdf4f0SGreg Roach     */
506ccdf4f0SGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
516ccdf4f0SGreg Roach    {
52e16a1bfdSGreg Roach        // Earlier versions of webtrees did not have a dbtype config option.  They always used mysql.
53e16a1bfdSGreg Roach        $driver = $request->getAttribute('dbtype', 'mysql');
546ccdf4f0SGreg Roach
55e16a1bfdSGreg Roach        $dbname = $request->getAttribute('dbname');
566ccdf4f0SGreg Roach
57e16a1bfdSGreg Roach        if ($driver === 'sqlite') {
58e16a1bfdSGreg Roach            $dbname = Webtrees::ROOT_DIR . 'data/' . $dbname . '.sqlite';
596ccdf4f0SGreg Roach        }
606ccdf4f0SGreg Roach
61e16a1bfdSGreg Roach        $capsule = new DB();
62e16a1bfdSGreg Roach
63*b5961194SGreg Roach        // Newer versions of webtrees support utf8mb4.  Older ones only support 3-byte utf8
64*b5961194SGreg Roach        if ($driver === 'mysql' && $request->getAttribute('mysql_utf8mb4') === '1') {
65*b5961194SGreg Roach            $charset   = 'utf8mb4';
66*b5961194SGreg Roach            $collation = 'utf8mb4_unicode_ci';
67*b5961194SGreg Roach        } else {
68*b5961194SGreg Roach            $charset   = 'utf8';
69*b5961194SGreg Roach            $collation = 'utf8_unicode_ci';
70*b5961194SGreg Roach        }
71*b5961194SGreg Roach
72e16a1bfdSGreg Roach        $capsule->addConnection([
73e16a1bfdSGreg Roach            'driver'                  => $driver,
74e16a1bfdSGreg Roach            'host'                    => $request->getAttribute('dbhost'),
75e16a1bfdSGreg Roach            'port'                    => $request->getAttribute('dbport'),
76e16a1bfdSGreg Roach            'database'                => $dbname,
77e16a1bfdSGreg Roach            'username'                => $request->getAttribute('dbuser'),
78e16a1bfdSGreg Roach            'password'                => $request->getAttribute('dbpass'),
79e16a1bfdSGreg Roach            'prefix'                  => $request->getAttribute('tblpfx'),
80e16a1bfdSGreg Roach            'prefix_indexes'          => true,
8160a0fc35SGreg Roach            'options'                 => [
8260a0fc35SGreg Roach                // Some drivers do this and some don't.  Make them consistent.
8360a0fc35SGreg Roach                PDO::ATTR_STRINGIFY_FETCHES => true,
8460a0fc35SGreg Roach            ],
85e16a1bfdSGreg Roach            // For MySQL
86*b5961194SGreg Roach            'charset'                 => $charset,
87*b5961194SGreg Roach            'collation'               => $collation,
88e16a1bfdSGreg Roach            'timezone'                => '+00:00',
89e16a1bfdSGreg Roach            'engine'                  => 'InnoDB',
90e16a1bfdSGreg Roach            'modes'                   => [
91e16a1bfdSGreg Roach                'ANSI',
929d1d823fSGreg Roach                'STRICT_ALL_TABLES',
932fef74aaSGreg Roach                // Use SQL injection(!) to override MAX_JOIN_SIZE setting.
942fef74aaSGreg Roach                "', SQL_BIG_SELECTS=1, @dummy='"
95e16a1bfdSGreg Roach            ],
96e16a1bfdSGreg Roach            // For SQLite
97e16a1bfdSGreg Roach            'foreign_key_constraints' => true,
98e16a1bfdSGreg Roach        ]);
99e16a1bfdSGreg Roach
100e16a1bfdSGreg Roach        $capsule->setAsGlobal();
101e16a1bfdSGreg Roach
102e16a1bfdSGreg Roach        Builder::macro('whereContains', function ($column, string $search, string $boolean = 'and'): Builder {
1035c98992aSGreg Roach            // Assertion helps static analysis tools understand where we will be using this closure.
1045c98992aSGreg Roach            assert($this instanceof Builder, new LogicException());
1055c98992aSGreg Roach
106*b5961194SGreg Roach            trigger_error('Builder::whereContains() is deprecated. Use LIKE.', E_USER_DEPRECATED);
107e16a1bfdSGreg Roach
108*b5961194SGreg Roach            return $this->where($column, 'LIKE', '%' . addcslashes($search, '\\%_') . '%', $boolean);
109e16a1bfdSGreg Roach        });
110e16a1bfdSGreg Roach
111b8d46257SGreg Roach        try {
112ff020ee8SGreg Roach            // Eager-load the connection, to prevent database credentials appearing in error logs.
113ff020ee8SGreg Roach            DB::connection()->getPdo();
114b8d46257SGreg Roach        } catch (PDOException $exception) {
1156e9f3eb9SGreg Roach            throw new RuntimeException($exception->getMessage());
116b8d46257SGreg Roach        }
117b8d46257SGreg Roach
118ff020ee8SGreg Roach        return $handler->handle($request);
1196ccdf4f0SGreg Roach    }
1206ccdf4f0SGreg Roach}
121