From 4e33985cfc231a8e3055b8c45e12b25b9cb24b80 Mon Sep 17 00:00:00 2001 From: Krombel Date: Fri, 23 Feb 2018 19:22:14 +0100 Subject: [PATCH] further implement cron.php; autoformat - set Register_Accept to 6 internally as it reflects the same state as PendingSendRequest - cleanups --- cron.php | 99 ++++++++++++++++++++-------- database.php | 2 +- public/verify_admin.php | 139 +++++++++++++++++++++------------------- 3 files changed, 148 insertions(+), 92 deletions(-) diff --git a/cron.php b/cron.php index 0a01e65..4caedad 100644 --- a/cron.php +++ b/cron.php @@ -1,9 +1,17 @@ query($sql) as $row) { // will only be executed once $first_name = $row["first_name"]; @@ -13,34 +21,75 @@ foreach ($db->query($sql) as $row) { $state = $row["state"]; try { - switch ($state) { - case RegisterState::RegistrationAccepted: - // Registration got accepted but registration failed + 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"]); - // register user - require_once("MatrixConnection.php"); - $mxConn = new MatrixConnection($homeserver, $access_token); - - // generate a password with 8 characters - $password = bin2hex(openssl_random_pseudo_bytes(4)); - - $res = $mxConn->register($username, $password, $shared_secret); - if ($res) { - // send registration_success - send_mail_registration_success($homeserver, $first_name . " " . $last_name, $email, $username, $password, $howToURL); - } else { - send_mail_registration_allowed_but_failed($homeserver, $first_name . " " . $last_name, $email); + 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:
" + . $note . "
" + . "Zum Bearbeiten hier klicken"); $mxMsg->set_type("m.text"); - $mxMsg->set_body("Fehler beim Registrieren von " . $first_name . " " . $last_name . "."); - $mxConn->send($mxMsg); - throw new Exception($language["REGISTRATION_FAILED"]); - } - break; + $response = $mxConn->send($register_room, $mxMsg); - case RegisterState::RegistrationDeclined: - break; + 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 + + // register user + require_once("MatrixConnection.php"); + $mxConn = new MatrixConnection($homeserver, $access_token); + + // generate a password with 8 characters + $password = bin2hex(openssl_random_pseudo_bytes(4)); + + $res = $mxConn->register($username, $password, $shared_secret); + if ($res) { + // send registration_success + send_mail_registration_success($homeserver, $first_name . " " . $last_name, $email, $username, $password, $howToURL); + } else { + send_mail_registration_allowed_but_failed($homeserver, $first_name . " " . $last_name, $email); + $mxMsg = new MatrixMessage(); + $mxMsg->set_type("m.text"); + $mxMsg->set_body("Fehler beim Registrieren von " . $first_name . " " . $last_name . "."); + $mxConn->send($mxMsg); + throw new Exception($language["REGISTRATION_FAILED"]); + } + break; + case RegisterState::PendingSendRegistrationMail: + print ("Error: Unhandled state: PendingSendRegistrationMail for " . $first_name . " " . $last_name . " (" . $username . ")"); + break; + case RegisterState::RegistrationDeclined: + case RegisterState::AllDone: + // do reqular cleanup + break; } } catch (Exception $e) { print("Error while handling cron for " . $first_name . " " . $last_name . " (" . $username . ")"); diff --git a/database.php b/database.php index eee73a0..bf57af9 100644 --- a/database.php +++ b/database.php @@ -19,7 +19,7 @@ abstract class RegisterState // State to allow persisting in the database although an admin declined it. // Will be removed regularly - const RegistrationAccepted = 12; + const RegistrationAccepted = 6; const RegistrationDeclined = 13; // User got successfully registered. Will be cleaned up later diff --git a/public/verify_admin.php b/public/verify_admin.php index 0b56954..7ba51fd 100644 --- a/public/verify_admin.php +++ b/public/verify_admin.php @@ -1,5 +1,5 @@ - + exec("UPDATE registrations SET state = " . RegisterState::RegistrationAccepted - . " WHERE admin_token = \"" . $token. "\";"); + $db->exec("UPDATE registrations SET state = " . RegisterState::PendingRegistration + . " WHERE admin_token = '" . $token. "';"); // register user require_once("../MatrixConnection.php"); @@ -74,7 +74,14 @@ try { $res = $mxConn->register($username, $password, $registration_shared_secret); if ($res) { // 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 { send_mail_registration_allowed_but_failed($homeserver, $first_name . " " . $last_name, $email); $mxMsg = new MatrixMessage(); @@ -90,7 +97,7 @@ try { print("

" . $language["ADMIN_REGISTER_ACCEPTED_BODY"] . "

"); } elseif ($action == 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); print("" . $language["ADMIN_VERIFY_SITE_TITLE"] . ""); print(""); @@ -98,71 +105,71 @@ try { print("

" . $language["ADMIN_REGISTER_DECLINED_BODY"] . "

"); } else { - print("" . $language["ADMIN_VERIFY_SITE_TITLE"] . ""); -?> - - - - - - -
+ + + + +
-
-
-
-

-
-
-
-
-
-
- -
-
-
-
- -
-
-
- -
- -
- -
- -
- - - - -
-
-
-
+
+
+
+

+
+
+
+
+
+
+ +
+
+
+
+ +
+
-
-