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