From 57326ec6c53db19105daa80648eed610bcb7661e Mon Sep 17 00:00:00 2001 From: Iris System Date: Thu, 25 Nov 2021 21:15:31 +1300 Subject: [PATCH] register: add registration PHP page --- _config.yml | 5 ++ register/register.php | 111 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 register/register.php diff --git a/_config.yml b/_config.yml index 2dce671..a279b27 100644 --- a/_config.yml +++ b/_config.yml @@ -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: diff --git a/register/register.php b/register/register.php new file mode 100644 index 0000000..6927a82 --- /dev/null +++ b/register/register.php @@ -0,0 +1,111 @@ +--- +layout: page +title: register for tel.tilde.org.nz +php_session: true +--- + +'; + if (!is_null($message)) { + echo "An error occurred: {$message}"; + } else { + echo "An unknown error occurred."; + } + echo ''; + 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 #tildetel"); + $_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 ''; + } +} else { +?> + +

If you had an account on the old tilde.tel, please include your previous number in the message field.

+ +
+ + + + + + + + + +
+ + + +
+ + +
+ +