♻️ Port the register functions
This commit is contained in:
parent
3d59a9f033
commit
134a5dcb52
3 changed files with 127 additions and 141 deletions
|
@ -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.<br /><br />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 <a href=\"users.php?do=verify\">Verification Page</a> to enter your code and start playing.");
|
||||
} else {
|
||||
$messages->put("danger", "Your account was created succesfully.<br /><br />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.<br /><br />You may now continue to the <a href=\"login.php?do=login\">Login Page</a> 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 = <<<END
|
||||
You or someone using your email address recently signed up for an account on the $gamename server, located at $gameurl.
|
||||
|
||||
This email is sent to verify your registration email. In order to begin using your account, you must verify your email address.
|
||||
Please visit the Verification Page ($verurl) and enter the code below to activate your account.
|
||||
Verification code: $vercode
|
||||
|
||||
If you were not the person who signed up for the game, please disregard this message. You will not be emailed again.
|
||||
END;
|
||||
|
||||
$status = mymail($emailaddress, "$gamename Account Verification", $email);
|
||||
return $status;
|
||||
|
||||
}
|
33
theme/default/site/register.twig
Normal file
33
theme/default/site/register.twig
Normal file
|
@ -0,0 +1,33 @@
|
|||
{% extends "base.twig" %}
|
||||
|
||||
{% block content %}
|
||||
<form action="/site/login/register" method="post">
|
||||
<table width="80%">
|
||||
<tr><td width="20%">Username:</td><td><input type="text" name="username" size="30" maxlength="30" value="{{field_username}}" /><br />Usernames must be 30 alphanumeric characters or less.<br /><br /><br /></td></tr>
|
||||
<tr><td>Password:</td><td><input type="password" name="password1" size="30" maxlength="10" /></td></tr>
|
||||
<tr><td>Verify Password:</td><td><input type="password" name="password2" size="30" maxlength="10" /><br />Passwords must be 10 alphanumeric characters or less.<br /><br /><br /></td></tr>
|
||||
<tr><td>Email Address:</td><td><input type="text" name="email1" size="30" maxlength="100" value="{{field_email1}}" /></td></tr>
|
||||
<tr><td>Verify Email:</td>
|
||||
<td><input type="text" name="email2" size="30" maxlength="100" />
|
||||
{% if (verifytext) %}
|
||||
<br />
|
||||
<span class="small">{{verifytext}}</span>
|
||||
{% endif %}
|
||||
<br /><br /><br />
|
||||
</td></tr>
|
||||
<tr><td>Character Name:</td><td><input type="text" name="charname" size="30" maxlength="30" value="{{field_charname}}" /></td></tr>
|
||||
<tr><td>Character Class:</td><td><select name="charclass">
|
||||
<option value="1" {% if (field_charclass == 1) %}selected{% endif %}>{{class1name}}</option>
|
||||
<option value="2" {% if (field_charclass == 2) %}selected{% endif %}>{{class2name}}</option>
|
||||
<option value="3" {% if (field_charclass == 3) %}selected{% endif %}>{{class3name}}</option>
|
||||
</select></td></tr>
|
||||
<tr><td>Difficulty:</td><td><select name="difficulty">
|
||||
<option value="1" {% if (field_difficulty == 1) %}selected{% endif %}>{{diff1name}}</option>
|
||||
<option value="2" {% if (field_difficulty == 2) %}selected{% endif %}>{{diff2name}}</option>
|
||||
<option value="3" {% if (field_difficulty == 3) %}selected{% endif %}>{{diff3name}}</option>
|
||||
</select></td></tr>
|
||||
<tr><td colspan="2">See <a href="help.php">Help</a> for more information about character classes and difficulty levels.<br /><br /></td></tr>
|
||||
<tr><td colspan="2"><input type="submit" name="submit" value="Submit" /> <input type="reset" name="reset" value="Reset" /></td></tr>
|
||||
</table>
|
||||
</form>
|
||||
{% endblock %}
|
141
users.php
141
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.<br />"; }
|
||||
if (preg_match("/[^A-z0-9_\-]/", $username)==1) { $errors++; $errorlist .= "Username must be alphanumeric.<br />"; } // 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.<br />"; }
|
||||
|
||||
// Process charname.
|
||||
if ($charname == "") { $errors++; $errorlist .= "Character Name field is required.<br />"; }
|
||||
if (preg_match("/[^A-z0-9_\-]/", $charname)==1) { $errors++; $errorlist .= "Character Name must be alphanumeric.<br />"; } // 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.<br />"; }
|
||||
|
||||
// Process email address.
|
||||
if ($email1 == "" || $email2 == "") { $errors++; $errorlist .= "Email fields are required.<br />"; }
|
||||
if ($email1 != $email2) { $errors++; $errorlist .= "Emails don't match.<br />"; }
|
||||
if (! is_email($email1)) { $errors++; $errorlist .= "Email isn't valid.<br />"; }
|
||||
$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.<br />"; }
|
||||
|
||||
// Process password.
|
||||
if (trim($password1) == "") { $errors++; $errorlist .= "Password field is required.<br />"; }
|
||||
if (preg_match("/[^A-z0-9_\-]/", $password1)==1) { $errors++; $errorlist .= "Password must be alphanumeric.<br />"; } // Thanks to "Carlos Pires" from php.net!
|
||||
if ($password1 != $password2) { $errors++; $errorlist .= "Passwords don't match.<br />"; }
|
||||
$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.<br /><br />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 <a href=\"users.php?do=verify\">Verification Page</a> to enter your code and start playing.";
|
||||
} else {
|
||||
$page = "Your account was created successfully.<br /><br />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.<br /><br />You may now continue to the <a href=\"login.php?do=login\">Login Page</a> and continue playing ".$controlrow["gamename"]."!";
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
$page = "The following error(s) occurred when your account was being made:<br /><span style=\"color:red;\">$errorlist</span><br />Please go back and try again.";
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
$page = gettemplate("register");
|
||||
if ($controlrow["verifyemail"] == 1) {
|
||||
$controlrow["verifytext"] = "<br /><span class=\"small\">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.</span>";
|
||||
} else {
|
||||
$controlrow["verifytext"] = "";
|
||||
}
|
||||
$page = parsetemplate($page, $controlrow);
|
||||
|
||||
}
|
||||
|
||||
$topnav = "<a href=\"login.php?do=login\"><img src=\"templates/assets/img/button_login.gif\" alt=\"Log In\" border=\"0\" /></a><a href=\"users.php?do=register\"><img src=\"templates/assets/img/button_register.gif\" alt=\"Register\" border=\"0\" /></a><a href=\"help.php\"><img src=\"templates/assets/img/button_help.gif\" alt=\"Help\" border=\"0\" /></a>";
|
||||
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 = <<<END
|
||||
You or someone using your email address recently signed up for an account on the $gamename server, located at $gameurl.
|
||||
|
||||
This email is sent to verify your registration email. In order to begin using your account, you must verify your email address.
|
||||
Please visit the Verification Page ($verurl) and enter the code below to activate your account.
|
||||
Verification code: $vercode
|
||||
|
||||
If you were not the person who signed up for the game, please disregard this message. You will not be emailed again.
|
||||
END;
|
||||
|
||||
$status = mymail($emailaddress, "$gamename Account Verification", $email);
|
||||
return $status;
|
||||
|
||||
}
|
||||
|
||||
function mymail($to, $title, $body, $from = '') { // thanks to arto dot PLEASE dot DO dot NOT dot SPAM at artoaaltonen dot fi.
|
||||
|
||||
$controlquery = doquery("SELECT * FROM {{table}} WHERE id='1' LIMIT 1", "control");
|
||||
$controlrow = mysqli_fetch_array($controlquery);
|
||||
extract($controlrow);
|
||||
|
||||
|
||||
$from = trim($from);
|
||||
|
||||
if (!$from) {
|
||||
$from = '<'.$controlrow["adminemail"].'>';
|
||||
}
|
||||
|
||||
$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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
?>
|
Loading…
Add table
Reference in a new issue