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 */ 17*fcfa147eSGreg 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; 256ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 266ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 276ccdf4f0SGreg Roachuse Psr\Http\Server\MiddlewareInterface; 286ccdf4f0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 296ccdf4f0SGreg Roach 306ccdf4f0SGreg Roach/** 316ccdf4f0SGreg Roach * Middleware to connect to the database. 326ccdf4f0SGreg Roach */ 336ccdf4f0SGreg Roachclass UseDatabase implements MiddlewareInterface 346ccdf4f0SGreg Roach{ 356ccdf4f0SGreg Roach /** 366ccdf4f0SGreg Roach * @param ServerRequestInterface $request 376ccdf4f0SGreg Roach * @param RequestHandlerInterface $handler 386ccdf4f0SGreg Roach * 396ccdf4f0SGreg Roach * @return ResponseInterface 406ccdf4f0SGreg Roach */ 416ccdf4f0SGreg Roach public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 426ccdf4f0SGreg Roach { 43e16a1bfdSGreg Roach // Earlier versions of webtrees did not have a dbtype config option. They always used mysql. 44e16a1bfdSGreg Roach $driver = $request->getAttribute('dbtype', 'mysql'); 456ccdf4f0SGreg Roach 46e16a1bfdSGreg Roach $dbname = $request->getAttribute('dbname'); 476ccdf4f0SGreg Roach 48e16a1bfdSGreg Roach if ($driver === 'sqlite') { 49e16a1bfdSGreg Roach $dbname = Webtrees::ROOT_DIR . 'data/' . $dbname . '.sqlite'; 506ccdf4f0SGreg Roach } 516ccdf4f0SGreg Roach 52e16a1bfdSGreg Roach $capsule = new DB(); 53e16a1bfdSGreg Roach 54e16a1bfdSGreg Roach $capsule->addConnection([ 55e16a1bfdSGreg Roach 'driver' => $driver, 56e16a1bfdSGreg Roach 'host' => $request->getAttribute('dbhost'), 57e16a1bfdSGreg Roach 'port' => $request->getAttribute('dbport'), 58e16a1bfdSGreg Roach 'database' => $dbname, 59e16a1bfdSGreg Roach 'username' => $request->getAttribute('dbuser'), 60e16a1bfdSGreg Roach 'password' => $request->getAttribute('dbpass'), 61e16a1bfdSGreg Roach 'prefix' => $request->getAttribute('tblpfx'), 62e16a1bfdSGreg Roach 'prefix_indexes' => true, 63e16a1bfdSGreg Roach // For MySQL 64e16a1bfdSGreg Roach 'charset' => 'utf8', 65e16a1bfdSGreg Roach 'collation' => 'utf8_unicode_ci', 66e16a1bfdSGreg Roach 'timezone' => '+00:00', 67e16a1bfdSGreg Roach 'engine' => 'InnoDB', 68e16a1bfdSGreg Roach 'modes' => [ 69e16a1bfdSGreg Roach 'ANSI', 70e16a1bfdSGreg Roach 'STRICT_TRANS_TABLES', 71e16a1bfdSGreg Roach 'NO_ZERO_IN_DATE', 72e16a1bfdSGreg Roach 'NO_ZERO_DATE', 73e16a1bfdSGreg Roach 'ERROR_FOR_DIVISION_BY_ZERO', 74e16a1bfdSGreg Roach ], 75e16a1bfdSGreg Roach // For SQLite 76e16a1bfdSGreg Roach 'foreign_key_constraints' => true, 77e16a1bfdSGreg Roach ]); 78e16a1bfdSGreg Roach 79e16a1bfdSGreg Roach $capsule->setAsGlobal(); 80e16a1bfdSGreg Roach 81e16a1bfdSGreg Roach Builder::macro('whereContains', function ($column, string $search, string $boolean = 'and'): Builder { 82e16a1bfdSGreg Roach $search = strtr($search, ['\\' => '\\\\', '%' => '\\%', '_' => '\\_', ' ' => '%']); 83e16a1bfdSGreg Roach 84e16a1bfdSGreg Roach return $this->where($column, 'LIKE', '%' . $search . '%', $boolean); 85e16a1bfdSGreg Roach }); 86e16a1bfdSGreg Roach 87e16a1bfdSGreg Roach return $handler->handle($request); 886ccdf4f0SGreg Roach } 896ccdf4f0SGreg Roach} 90