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); $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> </head>
<body> <body>
<?php <?php

View file

@ -71,7 +71,7 @@ switch ($step) {
<section> <section>
<h1>Install</h1> <h1>Install</h1>
<h2>Step <?= $step ?></h2> <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"> <form action="?page=install" method="post">
<input type="hidden" name="step" value="<?= $step + 1; ?>" /> <input type="hidden" name="step" value="<?= $step + 1; ?>" />
@ -84,12 +84,30 @@ switch ($step) {
break; break;
case 3: case 3:
$database->query('CREATE TABLE IF NOT EXISTS `users` ( /**
* Users
*/
$database->query('CREATE TABLE `users` (
`id` int AUTO_INCREMENT, `id` int AUTO_INCREMENT,
`email` varchar(64), `email` varchar(64) NOT NULL UNIQUE,
`password` varchar(128), `password` varchar(128) NOT NULL,
PRIMARY KEY (id) 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> <main>
<section> <section>

View file

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

View file

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