register: add registration PHP page

This commit is contained in:
darcy (iris system) 2021-11-25 21:15:31 +13:00
parent bfdcfd827e
commit 57326ec6c5
2 changed files with 116 additions and 0 deletions

View File

@ -3,6 +3,11 @@ description: neo-tildetel - a PBX for the tildeverse
url: https://tel.tilde.org.nz
baseurl: ''
smolcaptcha_url: "https://httpbin.org/status/418"
smolcaptcha_client: ""
register_hook: "https://httpbin.org/status/418"
register_hook_secret: ""
kramdown:
smart_quotes: apos,apos,quot,quot
typographic_symbols:

111
register/register.php Normal file
View File

@ -0,0 +1,111 @@
---
layout: page
title: register for tel.tilde.org.nz
php_session: true
---
<?php
function exit_with_banner(?string $message) {
unset($_SESSION['captcha']);
echo '<div class="banner">';
if (!is_null($message)) {
echo "An error occurred: {$message}";
} else {
echo "An unknown error occurred.";
}
echo '</div>';
exit;
}
function verify_captcha(string $result): bool {
$ch = curl_init("{{ site.smolcaptcha_url }}/api/verify");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
"client" => "{{ site.smolcaptcha_client }}",
"captcha" => $_SESSION['captcha'],
"result" => $result,
]);
$resp = curl_exec($ch);
curl_close($ch);
return ($resp !== null && trim($resp) === "ok");
}
if (!array_key_exists("captcha", $_SESSION) || is_null($_SESSION['captcha'])) {
$ch = curl_init("{{ site.smolcaptcha_url }}/api/generate");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
"client" => "{{ site.smolcaptcha_client }}",
]);
$resp = curl_exec($ch);
curl_close($ch);
if ($resp === false) exit_with_banner("CAPTCHA request failed, please alert darcy in <code>#tildetel</code>");
$_SESSION['captcha'] = $resp;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$form_user = array_key_exists('user', $_POST) ? trim($_POST['user']) : null;
$form_tilde = array_key_exists('tilde', $_POST) ? trim($_POST['tilde']) : null;
$form_msg = array_key_exists('msg', $_POST) ? trim($_POST['msg']) : "(no message)";
if ($form_user === null || $form_user === "" || $form_tilde === null || $form_tilde === "") {
exit_with_banner("A required field was not provided.");
}
$captcha = array_key_exists('captcha', $_POST) ? trim($_POST['captcha']) : "";
if (!verify_captcha($captcha)) {
exit_with_banner("CAPTCHA verification failed");
}
$ch = curl_init("{{ site.register_hook }}");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
"secret" => "{{ site.register_hook_secret }}",
"user" => $form_user,
"tilde" => $form_tilde,
"msg" => $form_msg,
]);
$resp = curl_exec($ch);
curl_close($ch);
unset($_SESSION['captcha']);
if (!$resp) {
exit_with_banner("Couldn't submit registration request, please try again");
} else {
echo '<div class="banner">Your registration request was submitted successfully!</div>';
}
} else {
?>
<p>If you had an account on the old <code>tilde.tel</code>, please include your previous number in the message field.</p>
<form method="POST" class="form">
<label for="reg_user">Username:</label>
<input id="reg_user" name="user" type="text" placeholder="Username">
<label for="reg_tilde">Tilde / Pubnix:</label>
<input id="reg_tilde" name="tilde" type="text" placeholder="Tilde / Pubnix">
<label for="reg_msg">Message (optional):</label>
<textarea id="reg_msg" name="reg_msg"></textarea>
<div class="form-captcha">
<label for="reg_captcha">What is the answer to the below equation?</label>
<img class="form-captcha-img" src="{{ site.smolcaptcha_url }}/render/<?php echo $_SESSION['captcha'] ?>">
<input id="reg_captcha" name="captcha" type="text" placeholder="Your answer">
</div>
<button class="button" type="submit">
Submit registration request
</button>
</form>
<?php } ?>