xref: /webtrees/app/Http/Middleware/UseDatabase.php (revision b8d46257f9ef0575cb2b5cc7741e75beca7221bf)
16ccdf4f0SGreg Roach<?php
23976b470SGreg Roach
36ccdf4f0SGreg Roach/**
46ccdf4f0SGreg Roach * webtrees: online genealogy
56ccdf4f0SGreg Roach * Copyright (C) 2019 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
22*b8d46257SGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpServerErrorException;
236ccdf4f0SGreg Roachuse Fisharebest\Webtrees\Webtrees;
24e16a1bfdSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
25e16a1bfdSGreg Roachuse Illuminate\Database\Query\Builder;
265c98992aSGreg Roachuse LogicException;
2760a0fc35SGreg Roachuse PDO;
28*b8d46257SGreg Roachuse PDOException;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
316ccdf4f0SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
326ccdf4f0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
336ccdf4f0SGreg Roach
346ccdf4f0SGreg Roach/**
356ccdf4f0SGreg Roach * Middleware to connect to the database.
366ccdf4f0SGreg Roach */
376ccdf4f0SGreg Roachclass UseDatabase implements MiddlewareInterface
386ccdf4f0SGreg Roach{
39*b8d46257SGreg Roach    // The following errors are likely to be caused by server issues, not by webtrees.
40*b8d46257SGreg Roach    private const SERVER_ERRORS = [
41*b8d46257SGreg Roach        'mysql'  => [1203],
42*b8d46257SGreg Roach        'pgsql'  => [],
43*b8d46257SGreg Roach        'sqlite' => [],
44*b8d46257SGreg Roach        'sqlsvr' => [],
45*b8d46257SGreg Roach    ];
46*b8d46257SGreg Roach
476ccdf4f0SGreg Roach    /**
486ccdf4f0SGreg Roach     * @param ServerRequestInterface  $request
496ccdf4f0SGreg Roach     * @param RequestHandlerInterface $handler
506ccdf4f0SGreg Roach     *
516ccdf4f0SGreg Roach     * @return ResponseInterface
526ccdf4f0SGreg Roach     */
536ccdf4f0SGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
546ccdf4f0SGreg Roach    {
55e16a1bfdSGreg Roach        // Earlier versions of webtrees did not have a dbtype config option.  They always used mysql.
56e16a1bfdSGreg Roach        $driver = $request->getAttribute('dbtype', 'mysql');
576ccdf4f0SGreg Roach
58e16a1bfdSGreg Roach        $dbname = $request->getAttribute('dbname');
596ccdf4f0SGreg Roach
60e16a1bfdSGreg Roach        if ($driver === 'sqlite') {
61e16a1bfdSGreg Roach            $dbname = Webtrees::ROOT_DIR . 'data/' . $dbname . '.sqlite';
626ccdf4f0SGreg Roach        }
636ccdf4f0SGreg Roach
64e16a1bfdSGreg Roach        $capsule = new DB();
65e16a1bfdSGreg Roach
66e16a1bfdSGreg Roach        $capsule->addConnection([
67e16a1bfdSGreg Roach            'driver'                  => $driver,
68e16a1bfdSGreg Roach            'host'                    => $request->getAttribute('dbhost'),
69e16a1bfdSGreg Roach            'port'                    => $request->getAttribute('dbport'),
70e16a1bfdSGreg Roach            'database'                => $dbname,
71e16a1bfdSGreg Roach            'username'                => $request->getAttribute('dbuser'),
72e16a1bfdSGreg Roach            'password'                => $request->getAttribute('dbpass'),
73e16a1bfdSGreg Roach            'prefix'                  => $request->getAttribute('tblpfx'),
74e16a1bfdSGreg Roach            'prefix_indexes'          => true,
7560a0fc35SGreg Roach            'options'                 => [
7660a0fc35SGreg Roach                // Some drivers do this and some don't.  Make them consistent.
7760a0fc35SGreg Roach                PDO::ATTR_STRINGIFY_FETCHES => true,
7860a0fc35SGreg Roach            ],
79e16a1bfdSGreg Roach            // For MySQL
80e16a1bfdSGreg Roach            'charset'                 => 'utf8',
81e16a1bfdSGreg Roach            'collation'               => 'utf8_unicode_ci',
82e16a1bfdSGreg Roach            'timezone'                => '+00:00',
83e16a1bfdSGreg Roach            'engine'                  => 'InnoDB',
84e16a1bfdSGreg Roach            'modes'                   => [
85e16a1bfdSGreg Roach                'ANSI',
869d1d823fSGreg Roach                'STRICT_ALL_TABLES',
872fef74aaSGreg Roach                // Use SQL injection(!) to override MAX_JOIN_SIZE setting.
882fef74aaSGreg Roach                "', SQL_BIG_SELECTS=1, @dummy='"
89e16a1bfdSGreg Roach            ],
90e16a1bfdSGreg Roach            // For SQLite
91e16a1bfdSGreg Roach            'foreign_key_constraints' => true,
92e16a1bfdSGreg Roach        ]);
93e16a1bfdSGreg Roach
94e16a1bfdSGreg Roach        $capsule->setAsGlobal();
95e16a1bfdSGreg Roach
96e16a1bfdSGreg Roach        Builder::macro('whereContains', function ($column, string $search, string $boolean = 'and'): Builder {
975c98992aSGreg Roach            // Assertion helps static analysis tools understand where we will be using this closure.
985c98992aSGreg Roach            assert($this instanceof Builder, new LogicException());
995c98992aSGreg Roach
100e16a1bfdSGreg Roach            $search = strtr($search, ['\\' => '\\\\', '%' => '\\%', '_' => '\\_', ' ' => '%']);
101e16a1bfdSGreg Roach
102e16a1bfdSGreg Roach            return $this->where($column, 'LIKE', '%' . $search . '%', $boolean);
103e16a1bfdSGreg Roach        });
104e16a1bfdSGreg Roach
105*b8d46257SGreg Roach        try {
106e16a1bfdSGreg Roach            return $handler->handle($request);
107*b8d46257SGreg Roach        } catch (PDOException $exception) {
108*b8d46257SGreg Roach            if (in_array($exception->errorInfo[1], self::SERVER_ERRORS[$driver], true)) {
109*b8d46257SGreg Roach                $message = 'A database error occurred.  This is most likely caused by an issue with your server.' . PHP_EOL . PHP_EOL;
110*b8d46257SGreg Roach                $message .= $exception->getMessage() . PHP_EOL . PHP_EOL;
111*b8d46257SGreg Roach                $message .= $exception->getFile() . ':' . $exception->getLine();
112*b8d46257SGreg Roach                throw new HttpServerErrorException($message);
113*b8d46257SGreg Roach            }
114*b8d46257SGreg Roach
115*b8d46257SGreg Roach            throw $exception;
116*b8d46257SGreg Roach        }
1176ccdf4f0SGreg Roach    }
1186ccdf4f0SGreg Roach}
119