Squashed commit of the following:

commit 52bbe64d73
Author: Jay <github.hybridsupply@grandel.anonaddy.me>
Date:   Mon Nov 15 12:32:44 2021 +0100

    Add login

commit dce62230b5
Author: Jay <github.hybridsupply@grandel.anonaddy.me>
Date:   Mon Nov 15 12:20:55 2021 +0100

    Improve installer detection

commit 7d5f19c3b8
Author: Jay <github.hybridsupply@grandel.anonaddy.me>
Date:   Mon Nov 15 09:45:36 2021 +0100

    Improve installation

commit 39b2ef2c03
Author: Jay <github.hybridsupply@grandel.anonaddy.me>
Date:   Mon Nov 15 08:28:08 2021 +0100

    Update .gitattributes

commit 364dd117d4
Author: Jay <github.hybridsupply@grandel.anonaddy.me>
Date:   Mon Nov 15 08:27:09 2021 +0100

    Update .gitattributes

commit e76392487d
Author: grandeljay <github.jay@grandel.anonaddy.me>
Date:   Sun Nov 14 09:13:09 2021 +0100

    Center main content

commit 511de407b0
Author: grandeljay <github.jay@grandel.anonaddy.me>
Date:   Sun Nov 14 09:13:01 2021 +0100

    Connect to database

commit 44c1d26d89
Author: grandeljay <github.jay@grandel.anonaddy.me>
Date:   Sun Nov 14 09:12:49 2021 +0100

    Improve installer

commit 38ace568a1
Author: Jay <github.hybridsupply@grandel.anonaddy.me>
Date:   Fri Nov 12 17:01:04 2021 +0100

    Write config
This commit is contained in:
Jay 2021-11-15 12:33:38 +01:00
parent 3eda1f7b16
commit a34cf3ca38
10 changed files with 328 additions and 38 deletions

3
.gitattributes vendored
View file

@ -1,2 +1,5 @@
# Auto detect text files and perform LF normalization
* text=auto
# Conform to PSR-12 coding standard
*.php eol=lf

View file

@ -25,6 +25,14 @@ form {
display: inline-block;
}
section {
max-width: 80ch;
padding: 2rem;
margin: 0 auto;
background-color: #fff;
}
/**
* Buttons
*/
@ -50,3 +58,23 @@ input[type="submit"] {
a.button {
text-decoration: none;
}
/**
* Inputs
*/
fieldset {
margin: 0 0 1em 0;
padding: 0;
border: none;
}
input[type="email"],
input[type="password"],
input[type="text"] {
width: 100%;
padding: 0.4em;
font-size: inherit;
border: 1px solid #f8f8f8;
}

View file

@ -0,0 +1,50 @@
<?php
/**
* database.php
*
* Establishes a database connection using its credentials.
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
namespace wishthis;
class Database
{
public \PDO $pdo;
public function __construct(
public string $host,
public string $database,
public string $user,
public string $password,
) {
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->database . ';port=3306;charset=utf8';
$options = array();
try {
$this->pdo = new \PDO($dsn, $this->user, $this->password, $options);
} catch (\PDOException $PDOE) {
throw new \PDOException($PDOE->getMessage(), (int)$PDOE->getCode());
}
}
public function query(string $query)
{
return $this->pdo->query(
$query,
\PDO::FETCH_ASSOC
);
}
public function getOption(string $key): string
{
$option = $this->query(
'SELECT * FROM `options`
WHERE `key` = "' . $key . '";'
)->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

@ -5,3 +5,8 @@
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
define('DATABASE_HOST', 'localhost');
define('DATABASE_NAME', 'wishthis');
define('DATABASE_USER', 'root');
define('DATABASE_PASSWORD', '');

View file

@ -12,7 +12,12 @@ $page = new page(__FILE__, 'Home');
$page->header();
?>
<h1>Hello</h1>
<main>
<section>
<h1>Welcome to wishthis</h1>
<a href="?page=register">Register</a>
</section>
</main>
<?php
$page->footer();

View file

@ -6,46 +6,117 @@
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
use wishthis\Page;
use wishthis\{Page, Database};
$page = new page(__FILE__, 'Home');
$page->header();
if (isset($_POST['action']) && 'install' === $_POST['action']) {
$configDirectory = 'includes/config';
$configPath = $configDirectory . '/config.php';
$configSamplePath = $configDirectory . '/config-sample.php';
$configContents = file_get_contents($configSamplePath);
$step = isset($_POST['step']) ? $_POST['step'] : 1;
file_put_contents($configPath, $configContents);
switch ($step) {
case 1:
?>
<main>
<section>
<h1>Install</h1>
<h2>Step <?= $step ?></h2>
<p>Welcome to the wishthis installer.</p>
<p>wishthis needs a database to function properly. Please enter your credentials.</p>
?>
<main>
<section>
<h1>Success</h1>
<p>wishthis has been successfully installed.</p>
<form action="?page=install" method="post">
<input type="hidden" name="step" value="<?= $step + 1; ?>" />
<a class="button primary" href="">Continue</a>
</section>
</main>
<?php
} else {
?>
<main>
<section>
<h1>Install</h1>
<p>Welcome to the wishthis installer.</p>
<fieldset>
<label>Host</label>
<input type="text" name="DATABASE_HOST" placeholder="localhost" value="localhost" />
</fieldset>
<p>Click Install to begin the installation.</p>
<fieldset>
<label>Name</label>
<input type="text" name="DATABASE_NAME" placeholder="wishthis" value="wishthis" />
</fieldset>
<form method="post">
<input type="hidden" name="action" value="install" />
<fieldset>
<label>Username</label>
<input type="text" name="DATABASE_USER" placeholder="root" value="root" />
</fieldset>
<input type="submit" value="Install" />
</form>
</section>
</main>
<?php
<fieldset>
<label>Password</label>
<input type="text" name="DATABASE_PASSWORD" />
</fieldset>
<input type="submit" value="Continue" />
</form>
</section>
</main>
<?php
break;
case 2:
$configDirectory = 'includes/config';
$configPath = $configDirectory . '/config.php';
$configSamplePath = $configDirectory . '/config-sample.php';
$configContents = file_get_contents($configSamplePath);
foreach ($_POST as $key => $value) {
if ('DATABASE' === substr($key, 0, 8)) {
$configContents = preg_replace('/(' . $key . '.+?\').*?(\')/', '$1' . $value . '$2', $configContents);
}
}
file_put_contents($configPath, $configContents);
?>
<main>
<section>
<h1>Install</h1>
<h2>Step <?= $step ?></h2>
<p>Click Continue to test the database connection.</p>
<form action="?page=install" method="post">
<input type="hidden" name="step" value="<?= $step + 1; ?>" />
<input type="submit" value="Continue" />
</form>
</section>
</main>
<?php
break;
case 3:
/**
* 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>
<h1>Success</h1>
<a href="?page=login">Login</a>
</section>
</main>
<?php
break;
}
$page->footer();

49
includes/pages/login.php Normal file
View file

@ -0,0 +1,49 @@
<?php
/**
* login.php
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
use wishthis\Page;
$page = new page(__FILE__, 'Home');
if (isset($_POST['email'], $_POST['password'])) {
$user = $database->query(
'SELECT * FROM `users`
WHERE `email` = "' . $_POST['email'] . '"
AND `password` = "' . sha1($_POST['password']) . '";'
)->fetch();
$_SESSION['user'] = $user;
header('Location: ?page=home');
die();
}
$page->header();
?>
<main>
<section>
<h1>Login</h1>
<form method="post">
<fieldset>
<label>Email</label>
<input type="email" name="email" placeholder="john.doe@domain.tld" />
</fieldset>
<fieldset>
<label>Password</label>
<input type="password" name="password" />
</fieldset>
<input type="submit" value="Login" />
</form>
</section>
</main>
<?php
$page->footer();

View file

@ -0,0 +1,46 @@
<?php
/**
* register.php
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
use wishthis\Page;
$page = new page(__FILE__, 'Home');
if (isset($_POST['email'], $_POST['password'])) {
$database->query('INSERT INTO `users`
(`email`, `password`) VALUES
("' . $_POST['email'] . '", "' . sha1($_POST['password']) . '")
;');
header('Location: ?page=login');
die();
}
$page->header();
?>
<main>
<section>
<h1>Register</h1>
<form method="post">
<fieldset>
<label>Email</label>
<input type="email" name="email" placeholder="john.doe@domain.tld" />
</fieldset>
<fieldset>
<label>Password</label>
<input type="password" name="password" />
</fieldset>
<input type="submit" value="Register" />
</form>
</section>
</main>
<?php
$page->footer();

View file

@ -15,14 +15,49 @@ autoInclude(__DIR__ . '/includes/classes');
autoInclude(__DIR__ . '/includes/functions');
/**
* Install
* Config
*/
$configPath = 'includes/config/config.php';
if (!file_exists($configPath)) {
$page = 'install';
if (file_exists($configPath)) {
require $configPath;
}
/**
* Database
*/
$database = false;
if (
defined('DATABASE_HOST')
&& defined('DATABASE_NAME')
&& defined('DATABASE_USER')
&& defined('DATABASE_PASSWORD')
) {
$database = new wishthis\Database(
DATABASE_HOST,
DATABASE_NAME,
DATABASE_USER,
DATABASE_PASSWORD
);
}
/**
* Install
*/
if ($database) {
try {
$database->getOption('isInstalled');
} catch (\Throwable $th) {
$page = 'install';
}
}
/**
* Session
*/
session_start();
/**
* Page
*/
@ -31,6 +66,4 @@ if (!isset($page)) {
}
$pagePath = 'includes/pages/' . $page . '.php';
if (file_exists($pagePath)) {
require $pagePath;
}
require $pagePath;