1430ed1cbSGreg Roach<?php 2430ed1cbSGreg Roach 3430ed1cbSGreg Roach/** 4430ed1cbSGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6430ed1cbSGreg Roach * This program is free software: you can redistribute it and/or modify 7430ed1cbSGreg Roach * it under the terms of the GNU General Public License as published by 8430ed1cbSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9430ed1cbSGreg Roach * (at your option) any later version. 10430ed1cbSGreg Roach * This program is distributed in the hope that it will be useful, 11430ed1cbSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12430ed1cbSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13430ed1cbSGreg Roach * GNU General Public License for more details. 14430ed1cbSGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16430ed1cbSGreg Roach */ 17430ed1cbSGreg Roach 18430ed1cbSGreg Roachdeclare(strict_types=1); 19430ed1cbSGreg Roach 20430ed1cbSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21430ed1cbSGreg Roach 22430ed1cbSGreg Roachuse Exception; 23430ed1cbSGreg Roachuse Fisharebest\Localization\Locale; 24430ed1cbSGreg Roachuse Fisharebest\Localization\Locale\LocaleEnUs; 25430ed1cbSGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface; 26430ed1cbSGreg Roachuse Fisharebest\Webtrees\Auth; 271fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 286f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB; 2955037a57SGreg Roachuse Fisharebest\Webtrees\Factories\CacheFactory; 30430ed1cbSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 31430ed1cbSGreg Roachuse Fisharebest\Webtrees\I18N; 32430ed1cbSGreg Roachuse Fisharebest\Webtrees\Module\ModuleLanguageInterface; 336b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 34430ed1cbSGreg Roachuse Fisharebest\Webtrees\Services\MigrationService; 35430ed1cbSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 36430ed1cbSGreg Roachuse Fisharebest\Webtrees\Services\ServerCheckService; 37430ed1cbSGreg Roachuse Fisharebest\Webtrees\Services\UserService; 38430ed1cbSGreg Roachuse Fisharebest\Webtrees\Session; 39b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 40430ed1cbSGreg Roachuse Fisharebest\Webtrees\Webtrees; 4152550490SGreg Roachuse Illuminate\Support\Collection; 42430ed1cbSGreg Roachuse Psr\Http\Message\ResponseInterface; 43430ed1cbSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 44430ed1cbSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 45430ed1cbSGreg Roachuse Throwable; 46430ed1cbSGreg Roach 47430ed1cbSGreg Roachuse function e; 48430ed1cbSGreg Roachuse function file_get_contents; 49430ed1cbSGreg Roachuse function file_put_contents; 50430ed1cbSGreg Roachuse function ini_get; 51430ed1cbSGreg Roachuse function random_bytes; 52430ed1cbSGreg Roachuse function realpath; 53430ed1cbSGreg Roachuse function redirect; 54430ed1cbSGreg Roachuse function substr; 55430ed1cbSGreg Roachuse function touch; 56430ed1cbSGreg Roachuse function unlink; 57430ed1cbSGreg Roachuse function view; 58430ed1cbSGreg Roach 59430ed1cbSGreg Roach/** 60430ed1cbSGreg Roach * Controller for the installation wizard 61430ed1cbSGreg Roach */ 62430ed1cbSGreg Roachclass SetupWizard implements RequestHandlerInterface 63430ed1cbSGreg Roach{ 64430ed1cbSGreg Roach use ViewResponseTrait; 65430ed1cbSGreg Roach 6652550490SGreg Roach private const DEFAULT_DBTYPE = DB::MYSQL; 67430ed1cbSGreg Roach private const DEFAULT_PREFIX = 'wt_'; 68430ed1cbSGreg Roach private const DEFAULT_DATA = [ 69430ed1cbSGreg Roach 'baseurl' => '', 70430ed1cbSGreg Roach 'lang' => '', 71430ed1cbSGreg Roach 'dbtype' => self::DEFAULT_DBTYPE, 72430ed1cbSGreg Roach 'dbhost' => '', 73430ed1cbSGreg Roach 'dbport' => '', 74430ed1cbSGreg Roach 'dbuser' => '', 75430ed1cbSGreg Roach 'dbpass' => '', 76430ed1cbSGreg Roach 'dbname' => '', 77430ed1cbSGreg Roach 'tblpfx' => self::DEFAULT_PREFIX, 78*3e581f76SJulien BARBEY 'dbkey' => '', 79*3e581f76SJulien BARBEY 'dbcert' => '', 80*3e581f76SJulien BARBEY 'dbca' => '', 81*3e581f76SJulien BARBEY 'dbverify' => '', 82430ed1cbSGreg Roach 'wtname' => '', 83430ed1cbSGreg Roach 'wtuser' => '', 84430ed1cbSGreg Roach 'wtpass' => '', 85430ed1cbSGreg Roach 'wtemail' => '', 86430ed1cbSGreg Roach ]; 87430ed1cbSGreg Roach 88354679afSGreg Roach private const DEFAULT_PORTS = [ 8952550490SGreg Roach DB::MYSQL => '3306', 9052550490SGreg Roach DB::POSTGRES => '5432', 9152550490SGreg Roach DB::SQLITE => '', 9252550490SGreg Roach DB::SQL_SERVER => '', // Do not use default, as it is valid to have no port number. 93354679afSGreg Roach ]; 94354679afSGreg Roach 95c4943cffSGreg Roach private MigrationService $migration_service; 96430ed1cbSGreg Roach 97c4943cffSGreg Roach private ModuleService $module_service; 98430ed1cbSGreg Roach 99c4943cffSGreg Roach private ServerCheckService $server_check_service; 100430ed1cbSGreg Roach 101c4943cffSGreg Roach private UserService $user_service; 102430ed1cbSGreg Roach 103430ed1cbSGreg Roach /** 104430ed1cbSGreg Roach * @param MigrationService $migration_service 105430ed1cbSGreg Roach * @param ModuleService $module_service 106430ed1cbSGreg Roach * @param ServerCheckService $server_check_service 107430ed1cbSGreg Roach * @param UserService $user_service 108430ed1cbSGreg Roach */ 109430ed1cbSGreg Roach public function __construct( 110430ed1cbSGreg Roach MigrationService $migration_service, 111430ed1cbSGreg Roach ModuleService $module_service, 112430ed1cbSGreg Roach ServerCheckService $server_check_service, 113430ed1cbSGreg Roach UserService $user_service 114430ed1cbSGreg Roach ) { 115430ed1cbSGreg Roach $this->user_service = $user_service; 116430ed1cbSGreg Roach $this->migration_service = $migration_service; 117430ed1cbSGreg Roach $this->module_service = $module_service; 118430ed1cbSGreg Roach $this->server_check_service = $server_check_service; 119430ed1cbSGreg Roach } 120430ed1cbSGreg Roach 121430ed1cbSGreg Roach /** 122430ed1cbSGreg Roach * Installation wizard - check user input and proceed to the next step. 123430ed1cbSGreg Roach * 124430ed1cbSGreg Roach * @param ServerRequestInterface $request 125430ed1cbSGreg Roach * 126430ed1cbSGreg Roach * @return ResponseInterface 127430ed1cbSGreg Roach */ 128430ed1cbSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 129430ed1cbSGreg Roach { 130430ed1cbSGreg Roach $this->layout = 'layouts/setup'; 131430ed1cbSGreg Roach 13255037a57SGreg Roach // Some functions need a cache, but we don't have one yet. 1336b9cb339SGreg Roach Registry::cache(new CacheFactory()); 13455037a57SGreg Roach 135430ed1cbSGreg Roach // We will need an IP address for the logs. 136b55cbc6bSGreg Roach $ip_address = Validator::serverParams($request)->string('REMOTE_ADDR', '127.0.0.1'); 137430ed1cbSGreg Roach $request = $request->withAttribute('client-ip', $ip_address); 138430ed1cbSGreg Roach 139d35568b4SGreg Roach Registry::container()->set(ServerRequestInterface::class, $request); 140430ed1cbSGreg Roach 141430ed1cbSGreg Roach $data = $this->userData($request); 142430ed1cbSGreg Roach 143b55cbc6bSGreg Roach $step = Validator::parsedBody($request)->integer('step', 1); 144430ed1cbSGreg Roach 145430ed1cbSGreg Roach $locales = $this->module_service 146430ed1cbSGreg Roach ->setupLanguages() 147f25fc0f9SGreg Roach ->map(static fn (ModuleLanguageInterface $module): LocaleInterface => $module->locale()); 148430ed1cbSGreg Roach 149430ed1cbSGreg Roach if ($data['lang'] === '') { 150430ed1cbSGreg Roach $default = new LocaleEnUs(); 151430ed1cbSGreg Roach 152430ed1cbSGreg Roach $locale = Locale::httpAcceptLanguage($request->getServerParams(), $locales->all(), $default); 153430ed1cbSGreg Roach 154430ed1cbSGreg Roach $data['lang'] = $locale->languageTag(); 155430ed1cbSGreg Roach } 156430ed1cbSGreg Roach 157430ed1cbSGreg Roach I18N::init($data['lang'], true); 158430ed1cbSGreg Roach 159430ed1cbSGreg Roach $data['cpu_limit'] = $this->maxExecutionTime(); 160dc270d8cSGreg Roach $data['locales'] = $locales; 161430ed1cbSGreg Roach $data['memory_limit'] = $this->memoryLimit(); 162430ed1cbSGreg Roach 163430ed1cbSGreg Roach // Only show database errors after the user has chosen a driver. 164430ed1cbSGreg Roach if ($step >= 4) { 165430ed1cbSGreg Roach $data['errors'] = $this->server_check_service->serverErrors($data['dbtype']); 166430ed1cbSGreg Roach $data['warnings'] = $this->server_check_service->serverWarnings($data['dbtype']); 167430ed1cbSGreg Roach } else { 168430ed1cbSGreg Roach $data['errors'] = $this->server_check_service->serverErrors(); 169430ed1cbSGreg Roach $data['warnings'] = $this->server_check_service->serverWarnings(); 170430ed1cbSGreg Roach } 171430ed1cbSGreg Roach 172430ed1cbSGreg Roach if (!$this->checkFolderIsWritable(Webtrees::DATA_DIR)) { 173430ed1cbSGreg Roach $data['errors']->push( 174430ed1cbSGreg Roach '<code>' . e(realpath(Webtrees::DATA_DIR)) . '</code><br>' . 175430ed1cbSGreg Roach I18N::translate('Oops! webtrees was unable to create files in this folder.') . ' ' . 176430ed1cbSGreg Roach I18N::translate('This usually means that you need to change the folder permissions to 777.') 177430ed1cbSGreg Roach ); 178430ed1cbSGreg Roach } 179430ed1cbSGreg Roach 180430ed1cbSGreg Roach switch ($step) { 181430ed1cbSGreg Roach default: 182430ed1cbSGreg Roach case 1: 183430ed1cbSGreg Roach return $this->step1Language($data); 184430ed1cbSGreg Roach case 2: 185430ed1cbSGreg Roach return $this->step2CheckServer($data); 186430ed1cbSGreg Roach case 3: 187430ed1cbSGreg Roach return $this->step3DatabaseType($data); 188430ed1cbSGreg Roach case 4: 189430ed1cbSGreg Roach return $this->step4DatabaseConnection($data); 190430ed1cbSGreg Roach case 5: 191430ed1cbSGreg Roach return $this->step5Administrator($data); 192430ed1cbSGreg Roach case 6: 193430ed1cbSGreg Roach return $this->step6Install($data); 194430ed1cbSGreg Roach } 195430ed1cbSGreg Roach } 196430ed1cbSGreg Roach 197430ed1cbSGreg Roach /** 198430ed1cbSGreg Roach * @param ServerRequestInterface $request 199430ed1cbSGreg Roach * 200430ed1cbSGreg Roach * @return array<string,mixed> 201430ed1cbSGreg Roach */ 202430ed1cbSGreg Roach private function userData(ServerRequestInterface $request): array 203430ed1cbSGreg Roach { 204430ed1cbSGreg Roach $data = []; 205430ed1cbSGreg Roach 206430ed1cbSGreg Roach foreach (self::DEFAULT_DATA as $key => $default) { 207748dbe15SGreg Roach $data[$key] = Validator::parsedBody($request)->string($key, $default); 208430ed1cbSGreg Roach } 209430ed1cbSGreg Roach 210430ed1cbSGreg Roach return $data; 211430ed1cbSGreg Roach } 212430ed1cbSGreg Roach 213430ed1cbSGreg Roach /** 214430ed1cbSGreg Roach * The server's memory limit 215430ed1cbSGreg Roach * 216430ed1cbSGreg Roach * @return int 217430ed1cbSGreg Roach */ 218430ed1cbSGreg Roach private function maxExecutionTime(): int 219430ed1cbSGreg Roach { 220430ed1cbSGreg Roach return (int) ini_get('max_execution_time'); 221430ed1cbSGreg Roach } 222430ed1cbSGreg Roach 223430ed1cbSGreg Roach /** 224430ed1cbSGreg Roach * The server's memory limit (in MB). 225430ed1cbSGreg Roach * 226430ed1cbSGreg Roach * @return int 227430ed1cbSGreg Roach */ 228430ed1cbSGreg Roach private function memoryLimit(): int 229430ed1cbSGreg Roach { 230430ed1cbSGreg Roach $memory_limit = ini_get('memory_limit'); 231430ed1cbSGreg Roach 232430ed1cbSGreg Roach $number = (int) $memory_limit; 233430ed1cbSGreg Roach 234430ed1cbSGreg Roach switch (substr($memory_limit, -1)) { 235430ed1cbSGreg Roach case 'g': 236430ed1cbSGreg Roach case 'G': 237430ed1cbSGreg Roach return $number * 1024; 238430ed1cbSGreg Roach case 'm': 239430ed1cbSGreg Roach case 'M': 240430ed1cbSGreg Roach return $number; 241430ed1cbSGreg Roach case 'k': 242430ed1cbSGreg Roach case 'K': 243430ed1cbSGreg Roach return (int) ($number / 1024); 244430ed1cbSGreg Roach default: 245430ed1cbSGreg Roach return (int) ($number / 1048576); 246430ed1cbSGreg Roach } 247430ed1cbSGreg Roach } 248430ed1cbSGreg Roach 249430ed1cbSGreg Roach /** 250430ed1cbSGreg Roach * Check we can write to the data folder. 251430ed1cbSGreg Roach * 252430ed1cbSGreg Roach * @param string $data_dir 253430ed1cbSGreg Roach * 254430ed1cbSGreg Roach * @return bool 255430ed1cbSGreg Roach */ 256430ed1cbSGreg Roach private function checkFolderIsWritable(string $data_dir): bool 257430ed1cbSGreg Roach { 258430ed1cbSGreg Roach $text1 = random_bytes(32); 259430ed1cbSGreg Roach 260430ed1cbSGreg Roach try { 261430ed1cbSGreg Roach file_put_contents($data_dir . 'test.txt', $text1); 262430ed1cbSGreg Roach $text2 = file_get_contents(Webtrees::DATA_DIR . 'test.txt'); 263430ed1cbSGreg Roach unlink(Webtrees::DATA_DIR . 'test.txt'); 26428d026adSGreg Roach } catch (Exception) { 265430ed1cbSGreg Roach return false; 266430ed1cbSGreg Roach } 267430ed1cbSGreg Roach 268430ed1cbSGreg Roach return $text1 === $text2; 269430ed1cbSGreg Roach } 270430ed1cbSGreg Roach 271430ed1cbSGreg Roach /** 272430ed1cbSGreg Roach * @param array<string,mixed> $data 273430ed1cbSGreg Roach * 274430ed1cbSGreg Roach * @return ResponseInterface 275430ed1cbSGreg Roach */ 276430ed1cbSGreg Roach private function step1Language(array $data): ResponseInterface 277430ed1cbSGreg Roach { 278430ed1cbSGreg Roach return $this->viewResponse('setup/step-1-language', $data); 279430ed1cbSGreg Roach } 280430ed1cbSGreg Roach 281430ed1cbSGreg Roach /** 282430ed1cbSGreg Roach * @param array<string,mixed> $data 283430ed1cbSGreg Roach * 284430ed1cbSGreg Roach * @return ResponseInterface 285430ed1cbSGreg Roach */ 286430ed1cbSGreg Roach private function step2CheckServer(array $data): ResponseInterface 287430ed1cbSGreg Roach { 288430ed1cbSGreg Roach return $this->viewResponse('setup/step-2-server-checks', $data); 289430ed1cbSGreg Roach } 290430ed1cbSGreg Roach 291430ed1cbSGreg Roach /** 292430ed1cbSGreg Roach * @param array<string,mixed> $data 293430ed1cbSGreg Roach * 294430ed1cbSGreg Roach * @return ResponseInterface 295430ed1cbSGreg Roach */ 296430ed1cbSGreg Roach private function step3DatabaseType(array $data): ResponseInterface 297430ed1cbSGreg Roach { 298430ed1cbSGreg Roach if ($data['errors']->isNotEmpty()) { 299430ed1cbSGreg Roach return $this->viewResponse('setup/step-2-server-checks', $data); 300430ed1cbSGreg Roach } 301430ed1cbSGreg Roach 302430ed1cbSGreg Roach return $this->viewResponse('setup/step-3-database-type', $data); 303430ed1cbSGreg Roach } 304430ed1cbSGreg Roach 305430ed1cbSGreg Roach /** 306430ed1cbSGreg Roach * @param array<string,mixed> $data 307430ed1cbSGreg Roach * 308430ed1cbSGreg Roach * @return ResponseInterface 309430ed1cbSGreg Roach */ 310430ed1cbSGreg Roach private function step4DatabaseConnection(array $data): ResponseInterface 311430ed1cbSGreg Roach { 312430ed1cbSGreg Roach if ($data['errors']->isNotEmpty()) { 313430ed1cbSGreg Roach return $this->step3DatabaseType($data); 314430ed1cbSGreg Roach } 315430ed1cbSGreg Roach 316430ed1cbSGreg Roach return $this->viewResponse('setup/step-4-database-' . $data['dbtype'], $data); 317430ed1cbSGreg Roach } 318430ed1cbSGreg Roach 319430ed1cbSGreg Roach /** 320430ed1cbSGreg Roach * @param array<string,mixed> $data 321430ed1cbSGreg Roach * 322430ed1cbSGreg Roach * @return ResponseInterface 323430ed1cbSGreg Roach */ 324430ed1cbSGreg Roach private function step5Administrator(array $data): ResponseInterface 325430ed1cbSGreg Roach { 326354679afSGreg Roach // Use default port, if none specified. 327354679afSGreg Roach $data['dbport'] = $data['dbport'] ?: self::DEFAULT_PORTS[$data['dbtype']]; 328354679afSGreg Roach 329430ed1cbSGreg Roach try { 330430ed1cbSGreg Roach $this->connectToDatabase($data); 331430ed1cbSGreg Roach } catch (Throwable $ex) { 332430ed1cbSGreg Roach $data['errors']->push($ex->getMessage()); 333430ed1cbSGreg Roach 334430ed1cbSGreg Roach // Don't jump to step 4, as the error will make it jump to step 3. 335430ed1cbSGreg Roach return $this->viewResponse('setup/step-4-database-' . $data['dbtype'], $data); 336430ed1cbSGreg Roach } 337430ed1cbSGreg Roach 338430ed1cbSGreg Roach return $this->viewResponse('setup/step-5-administrator', $data); 339430ed1cbSGreg Roach } 340430ed1cbSGreg Roach 341430ed1cbSGreg Roach /** 342430ed1cbSGreg Roach * @param array<string,mixed> $data 343430ed1cbSGreg Roach * 344430ed1cbSGreg Roach * @return ResponseInterface 345430ed1cbSGreg Roach */ 346430ed1cbSGreg Roach private function step6Install(array $data): ResponseInterface 347430ed1cbSGreg Roach { 348430ed1cbSGreg Roach $error = $this->checkAdminUser($data['wtname'], $data['wtuser'], $data['wtpass'], $data['wtemail']); 349430ed1cbSGreg Roach 350430ed1cbSGreg Roach if ($error !== '') { 351430ed1cbSGreg Roach $data['errors']->push($error); 352430ed1cbSGreg Roach 353430ed1cbSGreg Roach return $this->step5Administrator($data); 354430ed1cbSGreg Roach } 355430ed1cbSGreg Roach 356430ed1cbSGreg Roach try { 357430ed1cbSGreg Roach $this->createConfigFile($data); 358430ed1cbSGreg Roach } catch (Throwable $exception) { 359430ed1cbSGreg Roach return $this->viewResponse('setup/step-6-failed', ['exception' => $exception]); 360430ed1cbSGreg Roach } 361430ed1cbSGreg Roach 362430ed1cbSGreg Roach // Done - start using webtrees! 363430ed1cbSGreg Roach return redirect($data['baseurl']); 364430ed1cbSGreg Roach } 365430ed1cbSGreg Roach 366430ed1cbSGreg Roach /** 367430ed1cbSGreg Roach * @param string $wtname 368430ed1cbSGreg Roach * @param string $wtuser 369430ed1cbSGreg Roach * @param string $wtpass 370430ed1cbSGreg Roach * @param string $wtemail 371430ed1cbSGreg Roach * 372430ed1cbSGreg Roach * @return string 373430ed1cbSGreg Roach */ 37424f2a3afSGreg Roach private function checkAdminUser(string $wtname, string $wtuser, string $wtpass, string $wtemail): string 375430ed1cbSGreg Roach { 376430ed1cbSGreg Roach if ($wtname === '' || $wtuser === '' || $wtpass === '' || $wtemail === '') { 377430ed1cbSGreg Roach return I18N::translate('You must enter all the administrator account fields.'); 378430ed1cbSGreg Roach } 379430ed1cbSGreg Roach 380430ed1cbSGreg Roach if (mb_strlen($wtpass) < 6) { 381430ed1cbSGreg Roach return I18N::translate('The password needs to be at least six characters long.'); 382430ed1cbSGreg Roach } 383430ed1cbSGreg Roach 384430ed1cbSGreg Roach return ''; 385430ed1cbSGreg Roach } 386430ed1cbSGreg Roach 387430ed1cbSGreg Roach /** 388430ed1cbSGreg Roach * @param array<string,mixed> $data 389430ed1cbSGreg Roach * 390430ed1cbSGreg Roach * @return void 391430ed1cbSGreg Roach */ 392430ed1cbSGreg Roach private function createConfigFile(array $data): void 393430ed1cbSGreg Roach { 394430ed1cbSGreg Roach // Create/update the database tables. 395430ed1cbSGreg Roach $this->connectToDatabase($data); 396430ed1cbSGreg Roach $this->migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 397430ed1cbSGreg Roach 398430ed1cbSGreg Roach // Add some default/necessary configuration data. 399430ed1cbSGreg Roach $this->migration_service->seedDatabase(); 400430ed1cbSGreg Roach 401430ed1cbSGreg Roach // If we are re-installing, then this user may already exist. 402430ed1cbSGreg Roach $admin = $this->user_service->findByIdentifier($data['wtemail']); 403430ed1cbSGreg Roach if ($admin === null) { 404430ed1cbSGreg Roach $admin = $this->user_service->findByIdentifier($data['wtuser']); 405430ed1cbSGreg Roach } 406430ed1cbSGreg Roach // Create the user 407430ed1cbSGreg Roach if ($admin === null) { 408430ed1cbSGreg Roach $admin = $this->user_service->create($data['wtuser'], $data['wtname'], $data['wtemail'], $data['wtpass']); 4091fe542e9SGreg Roach $admin->setPreference(UserInterface::PREF_LANGUAGE, $data['lang']); 4101fe542e9SGreg Roach $admin->setPreference(UserInterface::PREF_IS_VISIBLE_ONLINE, '1'); 411430ed1cbSGreg Roach } else { 412430ed1cbSGreg Roach $admin->setPassword($_POST['wtpass']); 413430ed1cbSGreg Roach } 414430ed1cbSGreg Roach // Make the user an administrator 4151fe542e9SGreg Roach $admin->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1'); 4161fe542e9SGreg Roach $admin->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '1'); 4171fe542e9SGreg Roach $admin->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, '1'); 418430ed1cbSGreg Roach 419430ed1cbSGreg Roach // Write the config file. We already checked that this would work. 420430ed1cbSGreg Roach $config_ini_php = view('setup/config.ini', $data); 421430ed1cbSGreg Roach 422430ed1cbSGreg Roach file_put_contents(Webtrees::CONFIG_FILE, $config_ini_php); 423430ed1cbSGreg Roach 424430ed1cbSGreg Roach // Login as the new user 425d35568b4SGreg Roach $request = Registry::container()->get(ServerRequestInterface::class) 426430ed1cbSGreg Roach ->withAttribute('base_url', $data['baseurl']); 427430ed1cbSGreg Roach 428430ed1cbSGreg Roach Session::start($request); 429430ed1cbSGreg Roach Auth::login($admin); 430430ed1cbSGreg Roach Session::put('language', $data['lang']); 431430ed1cbSGreg Roach } 432430ed1cbSGreg Roach 433430ed1cbSGreg Roach /** 434430ed1cbSGreg Roach * @param array<string,mixed> $data 435430ed1cbSGreg Roach * 436430ed1cbSGreg Roach * @return void 437430ed1cbSGreg Roach */ 438430ed1cbSGreg Roach private function connectToDatabase(array $data): void 439430ed1cbSGreg Roach { 440430ed1cbSGreg Roach // Try to create the database, if it does not already exist. 441430ed1cbSGreg Roach switch ($data['dbtype']) { 44252550490SGreg Roach case DB::SQLITE: 44352550490SGreg Roach touch(Webtrees::ROOT_DIR . 'data/' . $data['dbname'] . '.sqlite'); 444430ed1cbSGreg Roach break; 445430ed1cbSGreg Roach 44652550490SGreg Roach case DB::MYSQL: 44752550490SGreg Roach DB::connect( 44852550490SGreg Roach driver: $data['dbtype'], 44952550490SGreg Roach host: $data['dbhost'], 45052550490SGreg Roach port: $data['dbport'], 45152550490SGreg Roach database: '', 45252550490SGreg Roach username: $data['dbuser'], 45352550490SGreg Roach password: $data['dbpass'], 45452550490SGreg Roach prefix: $data['tblpfx'], 45552550490SGreg Roach key: $data['dbkey'], 45652550490SGreg Roach certificate: $data['dbcert'], 45752550490SGreg Roach ca: $data['dbca'], 45852550490SGreg Roach verify_certificate: (bool) $data['dbverify'], 45952550490SGreg Roach ); 46052550490SGreg Roach DB::exec('CREATE DATABASE IF NOT EXISTS `' . $data['dbname'] . '` COLLATE utf8mb4_unicode_ci'); 461430ed1cbSGreg Roach break; 462430ed1cbSGreg Roach } 463430ed1cbSGreg Roach 46452550490SGreg Roach DB::connect( 46552550490SGreg Roach driver: $data['dbtype'], 46652550490SGreg Roach host: $data['dbhost'], 46752550490SGreg Roach port: $data['dbport'], 46852550490SGreg Roach database: $data['dbname'], 46952550490SGreg Roach username: $data['dbuser'], 47052550490SGreg Roach password: $data['dbpass'], 47152550490SGreg Roach prefix: $data['tblpfx'], 47252550490SGreg Roach key: $data['dbkey'], 47352550490SGreg Roach certificate: $data['dbcert'], 47452550490SGreg Roach ca: $data['dbca'], 47552550490SGreg Roach verify_certificate: (bool) $data['dbverify'], 47652550490SGreg Roach ); 477430ed1cbSGreg Roach } 478430ed1cbSGreg Roach} 479