xref: /webtrees/app/Http/RequestHandlers/SetupWizard.php (revision c4943cff72f95a28fbb9404e3c20b169ff098e5c)
1430ed1cbSGreg Roach<?php
2430ed1cbSGreg Roach
3430ed1cbSGreg Roach/**
4430ed1cbSGreg Roach * webtrees: online genealogy
51fe542e9SGreg Roach * Copyright (C) 2021 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;
2855037a57SGreg Roachuse Fisharebest\Webtrees\Factories\CacheFactory;
29430ed1cbSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
30430ed1cbSGreg Roachuse Fisharebest\Webtrees\I18N;
31430ed1cbSGreg Roachuse Fisharebest\Webtrees\Module\ModuleLanguageInterface;
326b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
33430ed1cbSGreg Roachuse Fisharebest\Webtrees\Services\MigrationService;
34430ed1cbSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
35430ed1cbSGreg Roachuse Fisharebest\Webtrees\Services\ServerCheckService;
36430ed1cbSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
37430ed1cbSGreg Roachuse Fisharebest\Webtrees\Session;
38430ed1cbSGreg Roachuse Fisharebest\Webtrees\Webtrees;
39430ed1cbSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
40430ed1cbSGreg Roachuse Psr\Http\Message\ResponseInterface;
41430ed1cbSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
42430ed1cbSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
43430ed1cbSGreg Roachuse Throwable;
44430ed1cbSGreg Roach
45430ed1cbSGreg Roachuse function app;
46430ed1cbSGreg Roachuse function e;
47430ed1cbSGreg Roachuse function file_get_contents;
48430ed1cbSGreg Roachuse function file_put_contents;
49430ed1cbSGreg Roachuse function ini_get;
50430ed1cbSGreg Roachuse function random_bytes;
51430ed1cbSGreg Roachuse function realpath;
52430ed1cbSGreg Roachuse function redirect;
53430ed1cbSGreg Roachuse function substr;
54430ed1cbSGreg Roachuse function touch;
55430ed1cbSGreg Roachuse function unlink;
56430ed1cbSGreg Roachuse function view;
57430ed1cbSGreg Roach
58430ed1cbSGreg Roach/**
59430ed1cbSGreg Roach * Controller for the installation wizard
60430ed1cbSGreg Roach */
61430ed1cbSGreg Roachclass SetupWizard implements RequestHandlerInterface
62430ed1cbSGreg Roach{
63430ed1cbSGreg Roach    use ViewResponseTrait;
64430ed1cbSGreg Roach
65430ed1cbSGreg Roach    private const DEFAULT_DBTYPE = 'mysql';
66430ed1cbSGreg Roach    private const DEFAULT_PREFIX = 'wt_';
67430ed1cbSGreg Roach    private const DEFAULT_DATA   = [
68430ed1cbSGreg Roach        'baseurl' => '',
69430ed1cbSGreg Roach        'lang'    => '',
70430ed1cbSGreg Roach        'dbtype'  => self::DEFAULT_DBTYPE,
71430ed1cbSGreg Roach        'dbhost'  => '',
72430ed1cbSGreg Roach        'dbport'  => '',
73430ed1cbSGreg Roach        'dbuser'  => '',
74430ed1cbSGreg Roach        'dbpass'  => '',
75430ed1cbSGreg Roach        'dbname'  => '',
76430ed1cbSGreg Roach        'tblpfx'  => self::DEFAULT_PREFIX,
77430ed1cbSGreg Roach        'wtname'  => '',
78430ed1cbSGreg Roach        'wtuser'  => '',
79430ed1cbSGreg Roach        'wtpass'  => '',
80430ed1cbSGreg Roach        'wtemail' => '',
81430ed1cbSGreg Roach    ];
82430ed1cbSGreg Roach
83354679afSGreg Roach    private const DEFAULT_PORTS = [
84354679afSGreg Roach        'mysql'  => '3306',
85354679afSGreg Roach        'pgsql'  => '5432',
86354679afSGreg Roach        'sqlite' => '',
87354679afSGreg Roach        'sqlsvr' => '1433',
88354679afSGreg Roach    ];
89354679afSGreg Roach
90*c4943cffSGreg Roach    private MigrationService $migration_service;
91430ed1cbSGreg Roach
92*c4943cffSGreg Roach    private ModuleService $module_service;
93430ed1cbSGreg Roach
94*c4943cffSGreg Roach    private ServerCheckService $server_check_service;
95430ed1cbSGreg Roach
96*c4943cffSGreg Roach    private UserService $user_service;
97430ed1cbSGreg Roach
98430ed1cbSGreg Roach    /**
99430ed1cbSGreg Roach     * SetupWizard constructor.
100430ed1cbSGreg Roach     *
101430ed1cbSGreg Roach     * @param MigrationService   $migration_service
102430ed1cbSGreg Roach     * @param ModuleService      $module_service
103430ed1cbSGreg Roach     * @param ServerCheckService $server_check_service
104430ed1cbSGreg Roach     * @param UserService        $user_service
105430ed1cbSGreg Roach     */
106430ed1cbSGreg Roach    public function __construct(
107430ed1cbSGreg Roach        MigrationService $migration_service,
108430ed1cbSGreg Roach        ModuleService $module_service,
109430ed1cbSGreg Roach        ServerCheckService $server_check_service,
110430ed1cbSGreg Roach        UserService $user_service
111430ed1cbSGreg Roach    ) {
112430ed1cbSGreg Roach        $this->user_service         = $user_service;
113430ed1cbSGreg Roach        $this->migration_service    = $migration_service;
114430ed1cbSGreg Roach        $this->module_service       = $module_service;
115430ed1cbSGreg Roach        $this->server_check_service = $server_check_service;
116430ed1cbSGreg Roach    }
117430ed1cbSGreg Roach
118430ed1cbSGreg Roach    /**
119430ed1cbSGreg Roach     * Installation wizard - check user input and proceed to the next step.
120430ed1cbSGreg Roach     *
121430ed1cbSGreg Roach     * @param ServerRequestInterface $request
122430ed1cbSGreg Roach     *
123430ed1cbSGreg Roach     * @return ResponseInterface
124430ed1cbSGreg Roach     */
125430ed1cbSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
126430ed1cbSGreg Roach    {
127430ed1cbSGreg Roach        $this->layout = 'layouts/setup';
128430ed1cbSGreg Roach
12955037a57SGreg Roach        // Some functions need a cache, but we don't have one yet.
1306b9cb339SGreg Roach        Registry::cache(new CacheFactory());
13155037a57SGreg Roach
132430ed1cbSGreg Roach        // We will need an IP address for the logs.
133430ed1cbSGreg Roach        $ip_address  = $request->getServerParams()['REMOTE_ADDR'] ?? '127.0.0.1';
134430ed1cbSGreg Roach        $request     = $request->withAttribute('client-ip', $ip_address);
135430ed1cbSGreg Roach
136430ed1cbSGreg Roach        app()->instance(ServerRequestInterface::class, $request);
137430ed1cbSGreg Roach
138430ed1cbSGreg Roach        $data = $this->userData($request);
139430ed1cbSGreg Roach
140430ed1cbSGreg Roach        $params = (array) $request->getParsedBody();
141430ed1cbSGreg Roach        $step   = (int) ($params['step'] ?? '1');
142430ed1cbSGreg Roach
143430ed1cbSGreg Roach        $locales = $this->module_service
144430ed1cbSGreg Roach            ->setupLanguages()
145430ed1cbSGreg Roach            ->map(static function (ModuleLanguageInterface $module): LocaleInterface {
146430ed1cbSGreg Roach                return $module->locale();
147430ed1cbSGreg Roach            });
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        $params = (array) $request->getParsedBody();
205430ed1cbSGreg Roach
206430ed1cbSGreg Roach        $data = [];
207430ed1cbSGreg Roach
208430ed1cbSGreg Roach        foreach (self::DEFAULT_DATA as $key => $default) {
209430ed1cbSGreg Roach            $data[$key] = $params[$key] ?? $default;
210430ed1cbSGreg Roach        }
211430ed1cbSGreg Roach
212430ed1cbSGreg Roach        return $data;
213430ed1cbSGreg Roach    }
214430ed1cbSGreg Roach
215430ed1cbSGreg Roach    /**
216430ed1cbSGreg Roach     * The server's memory limit
217430ed1cbSGreg Roach     *
218430ed1cbSGreg Roach     * @return int
219430ed1cbSGreg Roach     */
220430ed1cbSGreg Roach    private function maxExecutionTime(): int
221430ed1cbSGreg Roach    {
222430ed1cbSGreg Roach        return (int) ini_get('max_execution_time');
223430ed1cbSGreg Roach    }
224430ed1cbSGreg Roach
225430ed1cbSGreg Roach    /**
226430ed1cbSGreg Roach     * The server's memory limit (in MB).
227430ed1cbSGreg Roach     *
228430ed1cbSGreg Roach     * @return int
229430ed1cbSGreg Roach     */
230430ed1cbSGreg Roach    private function memoryLimit(): int
231430ed1cbSGreg Roach    {
232430ed1cbSGreg Roach        $memory_limit = ini_get('memory_limit');
233430ed1cbSGreg Roach
234430ed1cbSGreg Roach        $number = (int) $memory_limit;
235430ed1cbSGreg Roach
236430ed1cbSGreg Roach        switch (substr($memory_limit, -1)) {
237430ed1cbSGreg Roach            case 'g':
238430ed1cbSGreg Roach            case 'G':
239430ed1cbSGreg Roach                return $number * 1024;
240430ed1cbSGreg Roach            case 'm':
241430ed1cbSGreg Roach            case 'M':
242430ed1cbSGreg Roach                return $number;
243430ed1cbSGreg Roach            case 'k':
244430ed1cbSGreg Roach            case 'K':
245430ed1cbSGreg Roach                return (int) ($number / 1024);
246430ed1cbSGreg Roach            default:
247430ed1cbSGreg Roach                return (int) ($number / 1048576);
248430ed1cbSGreg Roach        }
249430ed1cbSGreg Roach    }
250430ed1cbSGreg Roach
251430ed1cbSGreg Roach    /**
252430ed1cbSGreg Roach     * Check we can write to the data folder.
253430ed1cbSGreg Roach     *
254430ed1cbSGreg Roach     * @param string $data_dir
255430ed1cbSGreg Roach     *
256430ed1cbSGreg Roach     * @return bool
257430ed1cbSGreg Roach     */
258430ed1cbSGreg Roach    private function checkFolderIsWritable(string $data_dir): bool
259430ed1cbSGreg Roach    {
260430ed1cbSGreg Roach        $text1 = random_bytes(32);
261430ed1cbSGreg Roach
262430ed1cbSGreg Roach        try {
263430ed1cbSGreg Roach            file_put_contents($data_dir . 'test.txt', $text1);
264430ed1cbSGreg Roach            $text2 = file_get_contents(Webtrees::DATA_DIR . 'test.txt');
265430ed1cbSGreg Roach            unlink(Webtrees::DATA_DIR . 'test.txt');
266430ed1cbSGreg Roach        } catch (Exception $ex) {
267430ed1cbSGreg Roach            return false;
268430ed1cbSGreg Roach        }
269430ed1cbSGreg Roach
270430ed1cbSGreg Roach        return $text1 === $text2;
271430ed1cbSGreg Roach    }
272430ed1cbSGreg Roach
273430ed1cbSGreg Roach    /**
274430ed1cbSGreg Roach     * @param array<string,mixed> $data
275430ed1cbSGreg Roach     *
276430ed1cbSGreg Roach     * @return ResponseInterface
277430ed1cbSGreg Roach     */
278430ed1cbSGreg Roach    private function step1Language(array $data): ResponseInterface
279430ed1cbSGreg Roach    {
280430ed1cbSGreg Roach        return $this->viewResponse('setup/step-1-language', $data);
281430ed1cbSGreg Roach    }
282430ed1cbSGreg Roach
283430ed1cbSGreg Roach    /**
284430ed1cbSGreg Roach     * @param array<string,mixed> $data
285430ed1cbSGreg Roach     *
286430ed1cbSGreg Roach     * @return ResponseInterface
287430ed1cbSGreg Roach     */
288430ed1cbSGreg Roach    private function step2CheckServer(array $data): ResponseInterface
289430ed1cbSGreg Roach    {
290430ed1cbSGreg Roach        return $this->viewResponse('setup/step-2-server-checks', $data);
291430ed1cbSGreg Roach    }
292430ed1cbSGreg Roach
293430ed1cbSGreg Roach    /**
294430ed1cbSGreg Roach     * @param array<string,mixed> $data
295430ed1cbSGreg Roach     *
296430ed1cbSGreg Roach     * @return ResponseInterface
297430ed1cbSGreg Roach     */
298430ed1cbSGreg Roach    private function step3DatabaseType(array $data): ResponseInterface
299430ed1cbSGreg Roach    {
300430ed1cbSGreg Roach        if ($data['errors']->isNotEmpty()) {
301430ed1cbSGreg Roach            return $this->viewResponse('setup/step-2-server-checks', $data);
302430ed1cbSGreg Roach        }
303430ed1cbSGreg Roach
304430ed1cbSGreg Roach        return $this->viewResponse('setup/step-3-database-type', $data);
305430ed1cbSGreg Roach    }
306430ed1cbSGreg Roach
307430ed1cbSGreg Roach    /**
308430ed1cbSGreg Roach     * @param array<string,mixed> $data
309430ed1cbSGreg Roach     *
310430ed1cbSGreg Roach     * @return ResponseInterface
311430ed1cbSGreg Roach     */
312430ed1cbSGreg Roach    private function step4DatabaseConnection(array $data): ResponseInterface
313430ed1cbSGreg Roach    {
314430ed1cbSGreg Roach        if ($data['errors']->isNotEmpty()) {
315430ed1cbSGreg Roach            return $this->step3DatabaseType($data);
316430ed1cbSGreg Roach        }
317430ed1cbSGreg Roach
318430ed1cbSGreg Roach        return $this->viewResponse('setup/step-4-database-' . $data['dbtype'], $data);
319430ed1cbSGreg Roach    }
320430ed1cbSGreg Roach
321430ed1cbSGreg Roach    /**
322430ed1cbSGreg Roach     * @param array<string,mixed> $data
323430ed1cbSGreg Roach     *
324430ed1cbSGreg Roach     * @return ResponseInterface
325430ed1cbSGreg Roach     */
326430ed1cbSGreg Roach    private function step5Administrator(array $data): ResponseInterface
327430ed1cbSGreg Roach    {
328354679afSGreg Roach        // Use default port, if none specified.
329354679afSGreg Roach        $data['dbport'] = $data['dbport'] ?: self::DEFAULT_PORTS[$data['dbtype']];
330354679afSGreg Roach
331430ed1cbSGreg Roach        try {
332430ed1cbSGreg Roach            $this->connectToDatabase($data);
333430ed1cbSGreg Roach        } catch (Throwable $ex) {
334430ed1cbSGreg Roach            $data['errors']->push($ex->getMessage());
335430ed1cbSGreg Roach
336430ed1cbSGreg Roach            // Don't jump to step 4, as the error will make it jump to step 3.
337430ed1cbSGreg Roach            return $this->viewResponse('setup/step-4-database-' . $data['dbtype'], $data);
338430ed1cbSGreg Roach        }
339430ed1cbSGreg Roach
340430ed1cbSGreg Roach        return $this->viewResponse('setup/step-5-administrator', $data);
341430ed1cbSGreg Roach    }
342430ed1cbSGreg Roach
343430ed1cbSGreg Roach    /**
344430ed1cbSGreg Roach     * @param array<string,mixed> $data
345430ed1cbSGreg Roach     *
346430ed1cbSGreg Roach     * @return ResponseInterface
347430ed1cbSGreg Roach     */
348430ed1cbSGreg Roach    private function step6Install(array $data): ResponseInterface
349430ed1cbSGreg Roach    {
350430ed1cbSGreg Roach        $error = $this->checkAdminUser($data['wtname'], $data['wtuser'], $data['wtpass'], $data['wtemail']);
351430ed1cbSGreg Roach
352430ed1cbSGreg Roach        if ($error !== '') {
353430ed1cbSGreg Roach            $data['errors']->push($error);
354430ed1cbSGreg Roach
355430ed1cbSGreg Roach            return $this->step5Administrator($data);
356430ed1cbSGreg Roach        }
357430ed1cbSGreg Roach
358430ed1cbSGreg Roach        try {
359430ed1cbSGreg Roach            $this->createConfigFile($data);
360430ed1cbSGreg Roach        } catch (Throwable $exception) {
361430ed1cbSGreg Roach            return $this->viewResponse('setup/step-6-failed', ['exception' => $exception]);
362430ed1cbSGreg Roach        }
363430ed1cbSGreg Roach
364430ed1cbSGreg Roach        // Done - start using webtrees!
365430ed1cbSGreg Roach        return redirect($data['baseurl']);
366430ed1cbSGreg Roach    }
367430ed1cbSGreg Roach
368430ed1cbSGreg Roach    /**
369430ed1cbSGreg Roach     * @param string $wtname
370430ed1cbSGreg Roach     * @param string $wtuser
371430ed1cbSGreg Roach     * @param string $wtpass
372430ed1cbSGreg Roach     * @param string $wtemail
373430ed1cbSGreg Roach     *
374430ed1cbSGreg Roach     * @return string
375430ed1cbSGreg Roach     */
37624f2a3afSGreg Roach    private function checkAdminUser(string $wtname, string $wtuser, string $wtpass, string $wtemail): string
377430ed1cbSGreg Roach    {
378430ed1cbSGreg Roach        if ($wtname === '' || $wtuser === '' || $wtpass === '' || $wtemail === '') {
379430ed1cbSGreg Roach            return I18N::translate('You must enter all the administrator account fields.');
380430ed1cbSGreg Roach        }
381430ed1cbSGreg Roach
382430ed1cbSGreg Roach        if (mb_strlen($wtpass) < 6) {
383430ed1cbSGreg Roach            return I18N::translate('The password needs to be at least six characters long.');
384430ed1cbSGreg Roach        }
385430ed1cbSGreg Roach
386430ed1cbSGreg Roach        return '';
387430ed1cbSGreg Roach    }
388430ed1cbSGreg Roach
389430ed1cbSGreg Roach    /**
390430ed1cbSGreg Roach     * @param array<string,mixed> $data
391430ed1cbSGreg Roach     *
392430ed1cbSGreg Roach     * @return void
393430ed1cbSGreg Roach     */
394430ed1cbSGreg Roach    private function createConfigFile(array $data): void
395430ed1cbSGreg Roach    {
396430ed1cbSGreg Roach        // Create/update the database tables.
397430ed1cbSGreg Roach        $this->connectToDatabase($data);
398430ed1cbSGreg Roach        $this->migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION);
399430ed1cbSGreg Roach
400430ed1cbSGreg Roach        // Add some default/necessary configuration data.
401430ed1cbSGreg Roach        $this->migration_service->seedDatabase();
402430ed1cbSGreg Roach
403430ed1cbSGreg Roach        // If we are re-installing, then this user may already exist.
404430ed1cbSGreg Roach        $admin = $this->user_service->findByIdentifier($data['wtemail']);
405430ed1cbSGreg Roach        if ($admin === null) {
406430ed1cbSGreg Roach            $admin = $this->user_service->findByIdentifier($data['wtuser']);
407430ed1cbSGreg Roach        }
408430ed1cbSGreg Roach        // Create the user
409430ed1cbSGreg Roach        if ($admin === null) {
410430ed1cbSGreg Roach            $admin = $this->user_service->create($data['wtuser'], $data['wtname'], $data['wtemail'], $data['wtpass']);
4111fe542e9SGreg Roach            $admin->setPreference(UserInterface::PREF_LANGUAGE, $data['lang']);
4121fe542e9SGreg Roach            $admin->setPreference(UserInterface::PREF_IS_VISIBLE_ONLINE, '1');
413430ed1cbSGreg Roach        } else {
414430ed1cbSGreg Roach            $admin->setPassword($_POST['wtpass']);
415430ed1cbSGreg Roach        }
416430ed1cbSGreg Roach        // Make the user an administrator
4171fe542e9SGreg Roach        $admin->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1');
4181fe542e9SGreg Roach        $admin->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '1');
4191fe542e9SGreg Roach        $admin->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, '1');
420430ed1cbSGreg Roach
421430ed1cbSGreg Roach        // Write the config file. We already checked that this would work.
422430ed1cbSGreg Roach        $config_ini_php = view('setup/config.ini', $data);
423430ed1cbSGreg Roach
424430ed1cbSGreg Roach        file_put_contents(Webtrees::CONFIG_FILE, $config_ini_php);
425430ed1cbSGreg Roach
426430ed1cbSGreg Roach        // Login as the new user
427430ed1cbSGreg Roach        $request = app(ServerRequestInterface::class)
428430ed1cbSGreg Roach            ->withAttribute('base_url', $data['baseurl']);
429430ed1cbSGreg Roach
430430ed1cbSGreg Roach        Session::start($request);
431430ed1cbSGreg Roach        Auth::login($admin);
432430ed1cbSGreg Roach        Session::put('language', $data['lang']);
433430ed1cbSGreg Roach    }
434430ed1cbSGreg Roach
435430ed1cbSGreg Roach    /**
436430ed1cbSGreg Roach     * @param array<string,mixed> $data
437430ed1cbSGreg Roach     *
438430ed1cbSGreg Roach     * @return void
439430ed1cbSGreg Roach     */
440430ed1cbSGreg Roach    private function connectToDatabase(array $data): void
441430ed1cbSGreg Roach    {
442430ed1cbSGreg Roach        $capsule = new DB();
443430ed1cbSGreg Roach
444430ed1cbSGreg Roach        // Try to create the database, if it does not already exist.
445430ed1cbSGreg Roach        switch ($data['dbtype']) {
446430ed1cbSGreg Roach            case 'sqlite':
447430ed1cbSGreg Roach                $data['dbname'] = Webtrees::ROOT_DIR . 'data/' . $data['dbname'] . '.sqlite';
448430ed1cbSGreg Roach                touch($data['dbname']);
449430ed1cbSGreg Roach                break;
450430ed1cbSGreg Roach
451430ed1cbSGreg Roach            case 'mysql':
452430ed1cbSGreg Roach                $capsule->addConnection([
453430ed1cbSGreg Roach                    'driver'                  => $data['dbtype'],
454430ed1cbSGreg Roach                    'host'                    => $data['dbhost'],
455430ed1cbSGreg Roach                    'port'                    => $data['dbport'],
456430ed1cbSGreg Roach                    'database'                => '',
457430ed1cbSGreg Roach                    'username'                => $data['dbuser'],
458430ed1cbSGreg Roach                    'password'                => $data['dbpass'],
459430ed1cbSGreg Roach                ], 'temp');
460430ed1cbSGreg Roach                $capsule->getConnection('temp')->statement('CREATE DATABASE IF NOT EXISTS `' . $data['dbname'] . '` COLLATE utf8_unicode_ci');
461430ed1cbSGreg Roach                break;
462430ed1cbSGreg Roach        }
463430ed1cbSGreg Roach
464430ed1cbSGreg Roach        // Connect to the database.
465430ed1cbSGreg Roach        $capsule->addConnection([
466430ed1cbSGreg Roach            'driver'                  => $data['dbtype'],
467430ed1cbSGreg Roach            'host'                    => $data['dbhost'],
468430ed1cbSGreg Roach            'port'                    => $data['dbport'],
469430ed1cbSGreg Roach            'database'                => $data['dbname'],
470430ed1cbSGreg Roach            'username'                => $data['dbuser'],
471430ed1cbSGreg Roach            'password'                => $data['dbpass'],
472430ed1cbSGreg Roach            'prefix'                  => $data['tblpfx'],
473430ed1cbSGreg Roach            'prefix_indexes'          => true,
474430ed1cbSGreg Roach            // For MySQL
475430ed1cbSGreg Roach            'charset'                 => 'utf8',
476430ed1cbSGreg Roach            'collation'               => 'utf8_unicode_ci',
477430ed1cbSGreg Roach            'timezone'                => '+00:00',
478430ed1cbSGreg Roach            'engine'                  => 'InnoDB',
479430ed1cbSGreg Roach            'modes'                   => [
480430ed1cbSGreg Roach                'ANSI',
481430ed1cbSGreg Roach                'STRICT_TRANS_TABLES',
482430ed1cbSGreg Roach                'NO_ZERO_IN_DATE',
483430ed1cbSGreg Roach                'NO_ZERO_DATE',
484430ed1cbSGreg Roach                'ERROR_FOR_DIVISION_BY_ZERO',
485430ed1cbSGreg Roach            ],
486430ed1cbSGreg Roach            // For SQLite
487430ed1cbSGreg Roach            'foreign_key_constraints' => true,
488430ed1cbSGreg Roach        ]);
489430ed1cbSGreg Roach
490430ed1cbSGreg Roach        $capsule->setAsGlobal();
491430ed1cbSGreg Roach    }
492430ed1cbSGreg Roach}
493