From 134a5dcb527d7723e9dc1774a2c1575d19f340c4 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 12 Apr 2025 14:49:20 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Port=20the=20register=20fu?= =?UTF-8?q?nctions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/site/login.php | 94 +++++++++++++++++++++ theme/default/site/register.twig | 33 ++++++++ users.php | 141 ------------------------------- 3 files changed, 127 insertions(+), 141 deletions(-) create mode 100644 theme/default/site/register.twig diff --git a/controllers/site/login.php b/controllers/site/login.php index 8f38ffa..9e0ac88 100644 --- a/controllers/site/login.php +++ b/controllers/site/login.php @@ -18,4 +18,98 @@ function index_post() { $cookie = $row["id"] . " " . $row["username"] . " " . md5($row["password"] . "--" . $dbsettings["secretword"]) . " " . $rememberme; setcookie("dkgame", $cookie, $expiretime, "/", "", 0); $router->redirect("/"); +} + +function register() { // Register a new account. + global $controlrow; + global $renderer; + + if ($controlrow["verifyemail"] == 1) { + $renderer->prepare("verifytext", "A verification code will be sent to the address above, and you will not be able to log in without first entering the code. Please be sure to enter your correct email address."); + } + + $renderer->prepare("class1name", $controlrow["class1name"]); + $renderer->prepare("class2name", $controlrow["class2name"]); + $renderer->prepare("class3name", $controlrow["class3name"]); + $renderer->prepare("diff1name", $controlrow["diff1name"]); + $renderer->prepare("diff2name", $controlrow["diff2name"]); + $renderer->prepare("diff3name", $controlrow["diff3name"]); + + $renderer->display("register"); +} + +function register_post() { + global $renderer; + global $messages; + global $router; + global $controlrow; + global $postData; + + $postData->addField("Username", ["required", "alphanumeric", "unique"], "username", ["field"=>"username", "table"=>"users"]); + $postData->addField("Character Name", ["required", "alphanumeric", "unique"], "charname", ["field"=>"charname", "table"=>"users"]); + $postData->addFields("Email", ["required", "mail", "equals", "unique"], ["email1", "email2"], ["field"=>"email", "table"=>"users"]); + $postData->addFields("Password", ["required", "alphanumeric", "equals"], ["password1", "password2"]); + $postData->addField("Difficulty", [], "difficulty"); + $postData->addField("Character Class", [], "charclass"); + + if ($postData->validate() == false) { + $renderer->addPostFields(["username", "charname", "email1", "charclass", "difficulty"]); + + $renderer->prepare("class1name", $controlrow["class1name"]); + $renderer->prepare("class2name", $controlrow["class2name"]); + $renderer->prepare("class3name", $controlrow["class3name"]); + $renderer->prepare("diff1name", $controlrow["diff1name"]); + $renderer->prepare("diff2name", $controlrow["diff2name"]); + $renderer->prepare("diff3name", $controlrow["diff3name"]); + + $messages->putList("danger", "The following error(s) occurred when your account was being made:", $postData->errors); + $renderer->display("register"); + } + + $username = $postData->getField("username"); + $email = $postData->getField("email1"); + $charname = $postData->getField("charname"); + $charclass = $postData->getField("charclass"); + $difficulty = $postData->getField("difficulty"); + + $password = md5($password1); + + $verifycode = ($controlrow["verifyemail"] == 1) + ? substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-', 8)), 0, 8) + : '1'; + + $query = doquery("INSERT INTO {{table}} SET regdate=NOW(),verify='$verifycode',username='$username',password='$password',email='$email',charname='$charname',charclass='$charclass',difficulty='$difficulty'", "users") or die(mysql_error()); + + if ($controlrow["verifyemail"] == 1) { + if (__sendregmail($email, $verifycode) == true) { + $messages->put("success", "Your account was created successfully.

You should receive an Account Verification email shortly. You will need the verification code contained in that email before you are allowed to log in. Once you have received the email, please visit the Verification Page to enter your code and start playing."); + } else { + $messages->put("danger", "Your account was created succesfully.

However, there was a problem sending your verification email. Please check with the game administrator to help resolve this problem."); + } + } else { + $messages->put("success", "Your account was created succesfully.

You may now continue to the Login Page and start playing ".$controlrow["gamename"]."!"); + } + $router->redirect(); +} + +function __sendregmail($emailaddress, $vercode) { + + $controlquery = doquery("SELECT * FROM {{table}} WHERE id='1' LIMIT 1", "control"); + $controlrow = mysqli_fetch_array($controlquery); + extract($controlrow); + $verurl = $gameurl . "?do=verify"; + +$email = << + + + + + + + + + + + + +
Username:
Usernames must be 30 alphanumeric characters or less.


Password:
Verify Password:
Passwords must be 10 alphanumeric characters or less.


Email Address:
Verify Email: + {% if (verifytext) %} +
+ {{verifytext}} + {% endif %} +


+
Character Name:
Character Class:
Difficulty:
See Help for more information about character classes and difficulty levels.

+ +{% endblock %} \ No newline at end of file diff --git a/users.php b/users.php index ba513da..2ef417d 100644 --- a/users.php +++ b/users.php @@ -18,88 +18,6 @@ if (isset($_GET["do"])) { } -function register() { // Register a new account. - - $controlquery = doquery("SELECT * FROM {{table}} WHERE id='1' LIMIT 1", "control"); - $controlrow = mysqli_fetch_array($controlquery); - - if (isset($_POST["submit"])) { - - extract($_POST); - - $errors = 0; $errorlist = ""; - - // Process username. - if ($username == "") { $errors++; $errorlist .= "Username field is required.
"; } - if (preg_match("/[^A-z0-9_\-]/", $username)==1) { $errors++; $errorlist .= "Username must be alphanumeric.
"; } // Thanks to "Carlos Pires" from php.net! - $usernamequery = doquery("SELECT username FROM {{table}} WHERE username='$username' LIMIT 1","users"); - if (mysqli_num_rows($usernamequery) > 0) { $errors++; $errorlist .= "Username already taken - unique username required.
"; } - - // Process charname. - if ($charname == "") { $errors++; $errorlist .= "Character Name field is required.
"; } - if (preg_match("/[^A-z0-9_\-]/", $charname)==1) { $errors++; $errorlist .= "Character Name must be alphanumeric.
"; } // Thanks to "Carlos Pires" from php.net! - $characternamequery = doquery("SELECT charname FROM {{table}} WHERE charname='$charname' LIMIT 1","users"); - if (mysqli_num_rows($characternamequery) > 0) { $errors++; $errorlist .= "Character Name already taken - unique Character Name required.
"; } - - // Process email address. - if ($email1 == "" || $email2 == "") { $errors++; $errorlist .= "Email fields are required.
"; } - if ($email1 != $email2) { $errors++; $errorlist .= "Emails don't match.
"; } - if (! is_email($email1)) { $errors++; $errorlist .= "Email isn't valid.
"; } - $emailquery = doquery("SELECT email FROM {{table}} WHERE email='$email1' LIMIT 1","users"); - if (mysqli_num_rows($emailquery) > 0) { $errors++; $errorlist .= "Email already taken - unique email address required.
"; } - - // Process password. - if (trim($password1) == "") { $errors++; $errorlist .= "Password field is required.
"; } - if (preg_match("/[^A-z0-9_\-]/", $password1)==1) { $errors++; $errorlist .= "Password must be alphanumeric.
"; } // Thanks to "Carlos Pires" from php.net! - if ($password1 != $password2) { $errors++; $errorlist .= "Passwords don't match.
"; } - $password = md5($password1); - - if ($errors == 0) { - - if ($controlrow["verifyemail"] == 1) { - $verifycode = ""; - for ($i=0; $i<8; $i++) { - $verifycode .= chr(rand(65,90)); - } - } else { - $verifycode='1'; - } - - $query = doquery("INSERT INTO {{table}} SET id='',regdate=NOW(),verify='$verifycode',username='$username',password='$password',email='$email1',charname='$charname',charclass='$charclass',difficulty='$difficulty'", "users") or die(mysql_error()); - - if ($controlrow["verifyemail"] == 1) { - if (sendregmail($email1, $verifycode) == true) { - $page = "Your account was created successfully.

You should receive an Account Verification email shortly. You will need the verification code contained in that email before you are allowed to log in. Once you have received the email, please visit the Verification Page to enter your code and start playing."; - } else { - $page = "Your account was created successfully.

However, there was a problem sending your verification email. Please check with the game administrator to help resolve this problem."; - } - } else { - $page = "Your account was created succesfully.

You may now continue to the Login Page and continue playing ".$controlrow["gamename"]."!"; - } - - } else { - - $page = "The following error(s) occurred when your account was being made:
$errorlist
Please go back and try again."; - - } - - } else { - - $page = gettemplate("register"); - if ($controlrow["verifyemail"] == 1) { - $controlrow["verifytext"] = "
A verification code will be sent to the address above, and you will not be able to log in without first entering the code. Please be sure to enter your correct email address."; - } else { - $controlrow["verifytext"] = ""; - } - $page = parsetemplate($page, $controlrow); - - } - - $topnav = "\"Log\"Register\"\"Help\""; - display($page, "Register", false, false, false); - -} - function verify() { if (isset($_POST["submit"])) { @@ -188,63 +106,4 @@ END; } -function sendregmail($emailaddress, $vercode) { - - $controlquery = doquery("SELECT * FROM {{table}} WHERE id='1' LIMIT 1", "control"); - $controlrow = mysqli_fetch_array($controlquery); - extract($controlrow); - $verurl = $gameurl . "?do=verify"; - -$email = <<'; - } - - $rp = $controlrow["adminemail"]; - $org = '$gameurl'; - $mailer = 'PHP'; - - $head = ''; - $head .= "Content-Type: text/plain \r\n"; - $head .= "Date: ". date('r'). " \r\n"; - $head .= "Return-Path: $rp \r\n"; - $head .= "From: $from \r\n"; - $head .= "Sender: $from \r\n"; - $head .= "Reply-To: $from \r\n"; - $head .= "Organization: $org \r\n"; - $head .= "X-Sender: $from \r\n"; - $head .= "X-Priority: 3 \r\n"; - $head .= "X-Mailer: $mailer \r\n"; - - $body = str_replace("\r\n", "\n", $body); - $body = str_replace("\n", "\r\n", $body); - - return mail($to, $title, $body, $head); - -} - - ?> \ No newline at end of file