First implementation #1

Closed
krombel wants to merge 22 commits from first_implementation into master
3 changed files with 148 additions and 92 deletions
Showing only changes of commit 4e33985cfc - Show all commits

View File

@@ -1,9 +1,17 @@
<?php <?php
// This file is meant to do tasks that failed on the first attempt // This file is meant to do tasks that failed on the first attempt
require_once("config.php");
require_once("mail_templates.php");
require_once("database.php");
$sql = "SELECT first_name, last_name, username, email, state FROM registrations WHERE admin_token = '" . $token $sql = "SELECT id, first_name, last_name, username, email, state, verify_token FROM registrations "
. "' AND state = " . RegisterState::PendingAdminVerify . " LIMIT 1;"; ."WHERE state = ". RegisterState::PendingEmailSend
. " OR state = " . RegisterState::PendingAdminSend
. " OR state = " . RegisterState::PendingRegistration
. " OR state = " . RegisterState::PendingSendRegistrationMail
. " OR state = " . RegisterState::RegistrationDeclined
. " OR state = " . RegisterState::AllDone . ";";
foreach ($db->query($sql) as $row) { foreach ($db->query($sql) as $row) {
// will only be executed once // will only be executed once
$first_name = $row["first_name"]; $first_name = $row["first_name"];
@@ -13,9 +21,46 @@ foreach ($db->query($sql) as $row) {
$state = $row["state"]; $state = $row["state"];
try { try {
switch ($state) { switch ($state) {
case RegisterState::RegistrationAccepted: case RegisterState::PendingEmailSend:
$verify_url = $webroot . "/verify.php?t=" . $row["verify_token"];
$success = send_mail_pending_verification(
$homeserver,
$row["first_name"] . " " . $row["last_name"],
$row["email"],
$row["verify_url"]);
if ($success) {
$db->exec("UPDATE registrations SET state = " . RegisterState::PendingEmailVerify)
. " WHERE id = " . $row["id"] . ";");
} else {
throw new Exception("Could not send mail to ".$row["first_name"]." ".$row["last_name"]."(".$row["id"].")");
}
break;
case RegisterState::PendingAdminSend:
require_once("../MatrixConnection.php");
$adminUrl = $webroot . "/verify_admin.php?t=" . $admin_token;
$mxConn = new MatrixConnection($homeserver, $access_token);
$mxMsg = new MatrixMessage();
$mxMsg->set_body($first_name . ' ' . $last_name . "möchte sich registrieren und hat folgende Notiz hinterlassen:\r\n"
. $note . "\r\n"
. "Zum Bearbeiten hier klicken:\r\n" . $adminUrl);
$mxMsg->set_formatted_body($first_name . ' ' . $last_name . " möchte sich registrieren und hat folgende Notiz hinterlassen:<br />"
. $note . "<br />"
. "Zum Bearbeiten <a href=\"". $adminUrl . "\">hier</a> klicken");
$mxMsg->set_type("m.text");
$response = $mxConn->send($register_room, $mxMsg);
if ($response) {
$db->exec("UPDATE registrations SET state = " . RegisterState::PendingAdminVerify
. " WHERE id = " . $row["id"] . "';");
send_mail_pending_approval($homeserver, $first_name . " " . $last_name, $email);
} else {
throw new Exception("Could not send notification for ".$row["first_name"]." ".$row["last_name"]."(".$row["id"].") to admins.");
}
break;
case RegisterState::PendingRegistration:
// Registration got accepted but registration failed // Registration got accepted but registration failed
// register user // register user
@@ -38,8 +83,12 @@ foreach ($db->query($sql) as $row) {
throw new Exception($language["REGISTRATION_FAILED"]); throw new Exception($language["REGISTRATION_FAILED"]);
} }
break; break;
case RegisterState::PendingSendRegistrationMail:
print ("Error: Unhandled state: PendingSendRegistrationMail for " . $first_name . " " . $last_name . " (" . $username . ")");
break;
case RegisterState::RegistrationDeclined: case RegisterState::RegistrationDeclined:
case RegisterState::AllDone:
// do reqular cleanup
break; break;
} }
} catch (Exception $e) { } catch (Exception $e) {

View File

@@ -19,7 +19,7 @@ abstract class RegisterState
// State to allow persisting in the database although an admin declined it. // State to allow persisting in the database although an admin declined it.
// Will be removed regularly // Will be removed regularly
const RegistrationAccepted = 12; const RegistrationAccepted = 6;
const RegistrationDeclined = 13; const RegistrationDeclined = 13;
// User got successfully registered. Will be cleaned up later // User got successfully registered. Will be cleaned up later

View File

@@ -61,8 +61,8 @@ try {
} }
if ($action == RegisterState::RegistrationAccepted) { if ($action == RegisterState::RegistrationAccepted) {
$db->exec("UPDATE registrations SET state = " . RegisterState::RegistrationAccepted $db->exec("UPDATE registrations SET state = " . RegisterState::PendingRegistration
. " WHERE admin_token = \"" . $token. "\";"); . " WHERE admin_token = '" . $token. "';");
// register user // register user
require_once("../MatrixConnection.php"); require_once("../MatrixConnection.php");
@@ -74,7 +74,14 @@ try {
$res = $mxConn->register($username, $password, $registration_shared_secret); $res = $mxConn->register($username, $password, $registration_shared_secret);
if ($res) { if ($res) {
// send registration_success // send registration_success
send_mail_registration_success($homeserver, $first_name . " " . $last_name, $email, $username, $password, $howToURL); $res = send_mail_registration_success($homeserver, $first_name . " " . $last_name, $email, $username, $password, $howToURL);
if ($res) {
$db->exec("UPDATE registrations SET state = " . RegisterState::AllDone
. " WHERE admin_token = '" . $token. "';");
} else {
$db->exec("UPDATE registrations SET state = " . RegisterState::PendingSendRegistrationMail
. " WHERE admin_token = '" . $token. "';");
}
} else { } else {
send_mail_registration_allowed_but_failed($homeserver, $first_name . " " . $last_name, $email); send_mail_registration_allowed_but_failed($homeserver, $first_name . " " . $last_name, $email);
$mxMsg = new MatrixMessage(); $mxMsg = new MatrixMessage();
@@ -90,7 +97,7 @@ try {
print("<p>" . $language["ADMIN_REGISTER_ACCEPTED_BODY"] . "</p>"); print("<p>" . $language["ADMIN_REGISTER_ACCEPTED_BODY"] . "</p>");
} elseif ($action == RegisterState::RegistrationDeclined) { } elseif ($action == RegisterState::RegistrationDeclined) {
$db->exec("UPDATE registrations SET state = " . RegisterState::RegistrationDeclined $db->exec("UPDATE registrations SET state = " . RegisterState::RegistrationDeclined
. " WHERE admin_token = \"" . $token. "\";"); . " WHERE admin_token = '" . $token. "';");
send_mail_registration_decline($homeserver, $first_name . " " . $last_name, $email, $decline_reason); send_mail_registration_decline($homeserver, $first_name . " " . $last_name, $email, $decline_reason);
print("<title>" . $language["ADMIN_VERIFY_SITE_TITLE"] . "</title>"); print("<title>" . $language["ADMIN_VERIFY_SITE_TITLE"] . "</title>");
print("</head><body>"); print("</head><body>");