Improve installer detection

This commit is contained in:
Jay 2021-11-15 12:20:55 +01:00
parent 7d5f19c3b8
commit dce62230b5
5 changed files with 55 additions and 8 deletions

View file

@ -34,4 +34,15 @@ class Database
{
$this->pdo->query($query);
}
public function getOption(string $key): string
{
$option = $this->pdo->query(
'SELECT * FROM `options`
WHERE `key` = "' . $key . '";',
\PDO::FETCH_ASSOC
)->fetch();
return $option['value'];
}
}

View file

@ -51,7 +51,7 @@ class Page
}
?>
<title><?= $this->title ?></title>
<title><?= $this->title ?> - wishthis</title>
</head>
<body>
<?php

View file

@ -71,7 +71,7 @@ switch ($step) {
<section>
<h1>Install</h1>
<h2>Step <?= $step ?></h2>
<p>Klick Continue to test the database connection.</p>
<p>Click Continue to test the database connection.</p>
<form action="?page=install" method="post">
<input type="hidden" name="step" value="<?= $step + 1; ?>" />
@ -84,12 +84,30 @@ switch ($step) {
break;
case 3:
$database->query('CREATE TABLE IF NOT EXISTS `users` (
`id` int AUTO_INCREMENT,
`email` varchar(64),
`password` varchar(128),
/**
* Users
*/
$database->query('CREATE TABLE `users` (
`id` int AUTO_INCREMENT,
`email` varchar(64) NOT NULL UNIQUE,
`password` varchar(128) NOT NULL,
PRIMARY KEY (id)
);');
/**
* Options
*/
$database->query('CREATE TABLE `options` (
`id` int AUTO_INCREMENT,
`key` varchar(64) NOT NULL UNIQUE,
`value` varchar(128) NOT NULL,
PRIMARY KEY (id)
);');
$database->query('INSERT INTO `options`
(`key`, `value`) VALUES
("isInstalled", true)
;');
?>
<main>
<section>

View file

@ -11,6 +11,13 @@ use wishthis\Page;
$page = new page(__FILE__, 'Home');
$page->header();
if (isset($_POST['email'], $_POST['password'])) {
$database->query('INSERT INTO `users`
(`email`, `password`) VALUES
("' . $_POST['email'] . '", "' . sha1($_POST['password']) . '")
;');
}
?>
<main>
<section>

View file

@ -26,6 +26,8 @@ if (file_exists($configPath)) {
/**
* Database
*/
$database = false;
if (
defined('DATABASE_HOST')
&& defined('DATABASE_NAME')
@ -38,8 +40,17 @@ if (
DATABASE_USER,
DATABASE_PASSWORD
);
} else {
$page = 'install';
}
/**
* Install
*/
if ($database) {
try {
$database->getOption('isInstalled');
} catch (\Throwable $th) {
$page = 'install';
}
}
/**