Improve register form

This commit is contained in:
grandeljay 2022-02-26 17:25:17 +01:00
parent 5b1e8ef269
commit f2e3b0c900
3 changed files with 124 additions and 29 deletions

View file

@ -4,6 +4,7 @@ $(function() {
fields: { fields: {
email: 'email', email: 'email',
password: ['minLength[6]', 'empty'], password: ['minLength[6]', 'empty'],
planet: ['minLength[3]', 'empty'],
} }
}); });
}); });

View file

@ -337,4 +337,15 @@ class Page
</html> </html>
<?php <?php
} }
public function messages(array $messages): string
{
$html = '';
foreach ($messages as $message) {
$html .= $message;
}
return $html;
}
} }

View file

@ -1,7 +1,7 @@
<?php <?php
/** /**
* register.php * Register a new user
* *
* @author Jay Trees <github.jay@grandel.anonaddy.me> * @author Jay Trees <github.jay@grandel.anonaddy.me>
*/ */
@ -9,24 +9,76 @@
use wishthis\Page; use wishthis\Page;
$page = new page(__FILE__, 'Register'); $page = new page(__FILE__, 'Register');
$messages = array();
if (isset($_POST['email'], $_POST['password'])) { if (isset($_POST['email'], $_POST['password']) && !empty($_POST['planet'])) {
$users = $database->query('SELECT * FROM `users`;')->fetchAll(); $users = $database->query('SELECT * FROM `users`;')->fetchAll();
$emails = array_map(
function($user) {
return $user['email'];
},
$users
);
if (0 === count($users)) { $isHuman = false;
$database->query('INSERT INTO `users` $planet = strtolower($_POST['planet']);
(`email`, `password`, `power`) VALUES $planetName = strtoupper($planet[0]) . substr($planet, 1);
("' . $_POST['email'] . '", "' . sha1($_POST['password']) . '", 100) $planets = array(
;'); 'mercury',
} else { 'venus',
$database->query('INSERT INTO `users` 'earth',
(`email`, `password`) VALUES 'mars',
("' . $_POST['email'] . '", "' . sha1($_POST['password']) . '") 'jupiter',
;'); 'saturn',
'uranus',
'neptune',
);
$not_planets = array(
'pluto',
'sun'
);
if (in_array($planet, array_merge($planets, $not_planets))) {
$isHuman = true;
} }
header('Location: /?page=login'); if (in_array($planet, $not_planets)) {
die(); $messages[] = Page::warning('<strong>' . $planetName . '</strong> is not a planet but I\'ll let it slide, since only a human would make this kind of mistake.', 'Invalid planet');
}
if ($isHuman) {
if (0 === count($users)) {
$database->query('INSERT INTO `users`
(
`email`,
`password`,
`power`
) VALUES (
"' . $_POST['email'] . '",
"' . sha1($_POST['password']) . '",
100
)
;');
} else {
if (in_array($_POST['email'], $emails)) {
$messages[] = Page::error('An account with this email address already exists.', 'Invalid email address');
} else {
$database->query('INSERT INTO `users`
(
`email`,
`password`
) VALUES (
"' . $_POST['email'] . '",
"' . sha1($_POST['password']) . '"
)
;');
$messages[] = Page::success('Your account was successfully created.', 'Success');
}
}
} else {
$messages[] = Page::error('<strong>' . $planetName . '</strong> is not a planet in our solar system. Read this for more information: <a href="https://www.space.com/16080-solar-system-planets.html" target="_blank">Solar system planets: Order of the 8 (or 9) planets</a>', 'Invalid planet');
}
} }
$page->header(); $page->header();
@ -34,10 +86,18 @@ $page->navigation();
?> ?>
<main> <main>
<div class="ui container"> <div class="ui container">
<div class="ui segment">
<h1 class="ui header"><?= $page->title ?></h1> <h1 class="ui header"><?= $page->title ?></h1>
<?= $page->messages($messages) ?>
<div class="ui segment">
<form class="ui form" method="post"> <form class="ui form" method="post">
<div class="ui divided relaxed stackable two column grid">
<div class=" row">
<div class="column">
<h2 class="ui header">Account details</h2>
<div class="field"> <div class="field">
<label>Email</label> <label>Email</label>
<input type="email" name="email" placeholder="john.doe@domain.tld" /> <input type="email" name="email" placeholder="john.doe@domain.tld" />
@ -46,13 +106,36 @@ $page->navigation();
<label>Password</label> <label>Password</label>
<input type="password" name="password" /> <input type="password" name="password" />
</div> </div>
</div>
<div class="column">
<h2 class="ui header">Authentication</h2>
<p>
Prove you are a Human, Lizard-person or Zuck-Like creature.
Please name a planet from our solar system.
</p>
<div class="field">
<label>Planet</label>
<input type="text" name="planet" />
</div>
<p>Robots are obivously from another solar system so this will keep them at bay.</p>
</div>
</div>
<div class="row">
<div class="column">
<div class="ui error message"></div> <div class="ui error message"></div>
<input class="ui primary button" type="submit" value="Register" /> <input class="ui primary button" type="submit" value="Register" />
<a href="/?page=login">Login</a> <a class="ui tertiary button" href="/?page=login">Login</a>
</div>
</div>
</div>
</form> </form>
</div> </div>
</div> </div>
</main> </main>
<?php <?php