xref: /webtrees/app/Http/RequestHandlers/SetupWizard.php (revision 748dbe155a6d19d66918ad136947fa23ee8f8469)
1430ed1cbSGreg Roach<?php
2430ed1cbSGreg Roach
3430ed1cbSGreg Roach/**
4430ed1cbSGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 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;
38b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
39430ed1cbSGreg Roachuse Fisharebest\Webtrees\Webtrees;
40430ed1cbSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
41430ed1cbSGreg Roachuse Psr\Http\Message\ResponseInterface;
42430ed1cbSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
43430ed1cbSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
44430ed1cbSGreg Roachuse Throwable;
45430ed1cbSGreg Roach
46430ed1cbSGreg Roachuse function app;
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
66430ed1cbSGreg Roach    private const DEFAULT_DBTYPE = '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,
78430ed1cbSGreg Roach        'wtname'  => '',
79430ed1cbSGreg Roach        'wtuser'  => '',
80430ed1cbSGreg Roach        'wtpass'  => '',
81430ed1cbSGreg Roach        'wtemail' => '',
82430ed1cbSGreg Roach    ];
83430ed1cbSGreg Roach
84354679afSGreg Roach    private const DEFAULT_PORTS = [
85354679afSGreg Roach        'mysql'  => '3306',
86354679afSGreg Roach        'pgsql'  => '5432',
87354679afSGreg Roach        'sqlite' => '',
888e9b0f08SGreg Roach        'sqlsrv' => '', // Do not use default, as it is valid to have no port number.
89354679afSGreg Roach    ];
90354679afSGreg Roach
91c4943cffSGreg Roach    private MigrationService $migration_service;
92430ed1cbSGreg Roach
93c4943cffSGreg Roach    private ModuleService $module_service;
94430ed1cbSGreg Roach
95c4943cffSGreg Roach    private ServerCheckService $server_check_service;
96430ed1cbSGreg Roach
97c4943cffSGreg Roach    private UserService $user_service;
98430ed1cbSGreg Roach
99430ed1cbSGreg Roach    /**
100430ed1cbSGreg Roach     * SetupWizard constructor.
101430ed1cbSGreg Roach     *
102430ed1cbSGreg Roach     * @param MigrationService   $migration_service
103430ed1cbSGreg Roach     * @param ModuleService      $module_service
104430ed1cbSGreg Roach     * @param ServerCheckService $server_check_service
105430ed1cbSGreg Roach     * @param UserService        $user_service
106430ed1cbSGreg Roach     */
107430ed1cbSGreg Roach    public function __construct(
108430ed1cbSGreg Roach        MigrationService $migration_service,
109430ed1cbSGreg Roach        ModuleService $module_service,
110430ed1cbSGreg Roach        ServerCheckService $server_check_service,
111430ed1cbSGreg Roach        UserService $user_service
112430ed1cbSGreg Roach    ) {
113430ed1cbSGreg Roach        $this->user_service         = $user_service;
114430ed1cbSGreg Roach        $this->migration_service    = $migration_service;
115430ed1cbSGreg Roach        $this->module_service       = $module_service;
116430ed1cbSGreg Roach        $this->server_check_service = $server_check_service;
117430ed1cbSGreg Roach    }
118430ed1cbSGreg Roach
119430ed1cbSGreg Roach    /**
120430ed1cbSGreg Roach     * Installation wizard - check user input and proceed to the next step.
121430ed1cbSGreg Roach     *
122430ed1cbSGreg Roach     * @param ServerRequestInterface $request
123430ed1cbSGreg Roach     *
124430ed1cbSGreg Roach     * @return ResponseInterface
125430ed1cbSGreg Roach     */
126430ed1cbSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
127430ed1cbSGreg Roach    {
128430ed1cbSGreg Roach        $this->layout = 'layouts/setup';
129430ed1cbSGreg Roach
13055037a57SGreg Roach        // Some functions need a cache, but we don't have one yet.
1316b9cb339SGreg Roach        Registry::cache(new CacheFactory());
13255037a57SGreg Roach
133430ed1cbSGreg Roach        // We will need an IP address for the logs.
134b55cbc6bSGreg Roach        $ip_address = Validator::serverParams($request)->string('REMOTE_ADDR', '127.0.0.1');
135430ed1cbSGreg Roach        $request    = $request->withAttribute('client-ip', $ip_address);
136430ed1cbSGreg Roach
137430ed1cbSGreg Roach        app()->instance(ServerRequestInterface::class, $request);
138430ed1cbSGreg Roach
139430ed1cbSGreg Roach        $data = $this->userData($request);
140430ed1cbSGreg Roach
141b55cbc6bSGreg Roach        $step = Validator::parsedBody($request)->integer('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        $data = [];
205430ed1cbSGreg Roach
206430ed1cbSGreg Roach        foreach (self::DEFAULT_DATA as $key => $default) {
207*748dbe15SGreg 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');
264430ed1cbSGreg Roach        } catch (Exception $ex) {
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
425430ed1cbSGreg Roach        $request = app(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        $capsule = new DB();
441430ed1cbSGreg Roach
442430ed1cbSGreg Roach        // Try to create the database, if it does not already exist.
443430ed1cbSGreg Roach        switch ($data['dbtype']) {
444430ed1cbSGreg Roach            case 'sqlite':
445430ed1cbSGreg Roach                $data['dbname'] = Webtrees::ROOT_DIR . 'data/' . $data['dbname'] . '.sqlite';
446430ed1cbSGreg Roach                touch($data['dbname']);
447430ed1cbSGreg Roach                break;
448430ed1cbSGreg Roach
449430ed1cbSGreg Roach            case 'mysql':
450430ed1cbSGreg Roach                $capsule->addConnection([
451430ed1cbSGreg Roach                    'driver'                  => $data['dbtype'],
452430ed1cbSGreg Roach                    'host'                    => $data['dbhost'],
453430ed1cbSGreg Roach                    'port'                    => $data['dbport'],
454430ed1cbSGreg Roach                    'database'                => '',
455430ed1cbSGreg Roach                    'username'                => $data['dbuser'],
456430ed1cbSGreg Roach                    'password'                => $data['dbpass'],
457430ed1cbSGreg Roach                ], 'temp');
458430ed1cbSGreg Roach                $capsule->getConnection('temp')->statement('CREATE DATABASE IF NOT EXISTS `' . $data['dbname'] . '` COLLATE utf8_unicode_ci');
459430ed1cbSGreg Roach                break;
460430ed1cbSGreg Roach        }
461430ed1cbSGreg Roach
462430ed1cbSGreg Roach        // Connect to the database.
463430ed1cbSGreg Roach        $capsule->addConnection([
464430ed1cbSGreg Roach            'driver'                  => $data['dbtype'],
465430ed1cbSGreg Roach            'host'                    => $data['dbhost'],
466430ed1cbSGreg Roach            'port'                    => $data['dbport'],
467430ed1cbSGreg Roach            'database'                => $data['dbname'],
468430ed1cbSGreg Roach            'username'                => $data['dbuser'],
469430ed1cbSGreg Roach            'password'                => $data['dbpass'],
470430ed1cbSGreg Roach            'prefix'                  => $data['tblpfx'],
471430ed1cbSGreg Roach            'prefix_indexes'          => true,
472430ed1cbSGreg Roach            // For MySQL
473430ed1cbSGreg Roach            'charset'                 => 'utf8',
474430ed1cbSGreg Roach            'collation'               => 'utf8_unicode_ci',
475430ed1cbSGreg Roach            'timezone'                => '+00:00',
476430ed1cbSGreg Roach            'engine'                  => 'InnoDB',
477430ed1cbSGreg Roach            'modes'                   => [
478430ed1cbSGreg Roach                'ANSI',
479430ed1cbSGreg Roach                'STRICT_TRANS_TABLES',
480430ed1cbSGreg Roach                'NO_ZERO_IN_DATE',
481430ed1cbSGreg Roach                'NO_ZERO_DATE',
482430ed1cbSGreg Roach                'ERROR_FOR_DIVISION_BY_ZERO',
483430ed1cbSGreg Roach            ],
484430ed1cbSGreg Roach            // For SQLite
485430ed1cbSGreg Roach            'foreign_key_constraints' => true,
486430ed1cbSGreg Roach        ]);
487430ed1cbSGreg Roach
488430ed1cbSGreg Roach        $capsule->setAsGlobal();
489430ed1cbSGreg Roach    }
490430ed1cbSGreg Roach}
491