253 lines
9.8 KiB
PHP
253 lines
9.8 KiB
PHP
<?php
|
|
|
|
// users.php :: Handles user account functions.
|
|
|
|
require_once '../src/lib.php';
|
|
|
|
if (!file_exists('../.installed')) redirect('install.php');
|
|
|
|
$controlrow = get_control_row();
|
|
|
|
$do = $_GET['do'] ?? 'register';
|
|
match ($do) {
|
|
'verify' => verify(),
|
|
'lostpassword' => lostpassword(),
|
|
'changepassword' => changepassword(),
|
|
default => register()
|
|
};
|
|
|
|
/**
|
|
* Register a new account.
|
|
*/
|
|
function register()
|
|
{
|
|
global $controlrow;
|
|
|
|
if (isset($_POST["submit"])) {
|
|
$u = trim($_POST['username'] ?? '');
|
|
$e = trim($_POST['email1'] ?? '');
|
|
$e2 = trim($_POST['email2'] ?? '');
|
|
$p = $_POST['password1'] ?? '';
|
|
$p2 = $_POST['password2'] ?? '';
|
|
|
|
$errors = [];
|
|
|
|
// Process username.
|
|
if (empty($u) || strlen($u) < 3 || strlen($u) > 18 || !ctype_alnum(str_replace(' ', '', $u))) {
|
|
$errors[] = 'Username is required and must be between 3 and 18 characters long and contain only
|
|
alphanumeric characters and spaces.';
|
|
}
|
|
|
|
if (db()->exists('users', 'username', $u)) {
|
|
$errors[] = 'Username already taken. Try another.';
|
|
}
|
|
|
|
// Process email address.
|
|
if (empty($e) || strlen($e) > 255 || !filter_var($e, FILTER_VALIDATE_EMAIL)) {
|
|
$errors[] = 'Email is required must be a valid email address.';
|
|
}
|
|
|
|
if ($e !== $e2) {
|
|
$errors[] = 'Verify Email must match.';
|
|
}
|
|
|
|
if (db()->exists('users', 'email', $e)) {
|
|
$errors[] = 'Email already taken. Forgot your password?';
|
|
}
|
|
|
|
// Process password.
|
|
if (empty($p) || strlen($p) < 6) {
|
|
$errors[] = 'Password is required and must be at least 6 characters long.';
|
|
}
|
|
|
|
if ($p2 !== $p) {
|
|
$errors[] = 'Verify Password must match.';
|
|
}
|
|
|
|
$password = password_hash($p, PASSWORD_ARGON2ID);
|
|
|
|
if (count($errors) !== 0) {
|
|
$err = "<ul>";
|
|
foreach ($errors as $error) $err .= "<li>$error</li>";
|
|
$err .= "</ul>";
|
|
$page = "The following error(s) occurred when your account was being made:<br><span style=\"color:red;\">$err</span><br>Please go back and try again.";
|
|
} else {
|
|
$token = ($controlrow['verifyemail'] == true) ? token(8) : 'g2g';
|
|
db()->query('INSERT INTO users (verify, username, password, email, charclass) VALUES (?, ?, ?, ?, ?)', [
|
|
$token, $u, $password, $e, $_POST['charclass'] ?? 1
|
|
]);
|
|
|
|
if ($controlrow['verifyemail'] == true) {
|
|
if (sendregmail($e, $token)) {
|
|
$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 {
|
|
if ($controlrow["verifyemail"] == true) {
|
|
$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(gettemplate("register"), $controlrow);
|
|
}
|
|
|
|
$topnav = "<a href=\"login.php?do=login\"><img src=\"images/button_login.gif\" alt=\"Log In\" border=\"0\" /></a><a href=\"users.php?do=register\"><img src=\"images/button_register.gif\" alt=\"Register\" border=\"0\" /></a><a href=\"help.php\"><img src=\"images/button_help.gif\" alt=\"Help\" border=\"0\" /></a>";
|
|
display($page, "Register", false, false, false);
|
|
}
|
|
|
|
function verify()
|
|
{
|
|
if (isset($_POST["submit"])) {
|
|
$u = trim($_POST['username'] ?? '');
|
|
$e = trim($_POST['email'] ?? '');
|
|
$t = trim($_POST['token'] ?? '');
|
|
|
|
$query = db()->query('SELECT id FROM users WHERE username=? AND email=? AND verify=? LIMIT 1;', [$u, $e, $t]);
|
|
if ($query === false) exit('Verification failed. Go back, double-check your details, and try again.');
|
|
|
|
db()->query("UPDATE users SET verify='g2g' WHERE username=?;", [$u]);
|
|
|
|
display("Your account was verified successfully.<br><br>You may now continue to the <a href=\"login.php?do=login\">Login Page</a> and start playing the game.<br><br>Thanks for playing!","Verify Email",false,false,false);
|
|
}
|
|
|
|
$topnav = "<a href=\"login.php?do=login\"><img src=\"images/button_login.gif\" alt=\"Log In\" border=\"0\" /></a><a href=\"users.php?do=register\"><img src=\"images/button_register.gif\" alt=\"Register\" border=\"0\" /></a><a href=\"help.php\"><img src=\"images/button_help.gif\" alt=\"Help\" border=\"0\" /></a>";
|
|
display(gettemplate("verify"), "Verify Email", false, false, false);
|
|
}
|
|
|
|
function lostpassword()
|
|
{
|
|
if (isset($_POST["submit"])) {
|
|
$e = trim($_POST['email'] ?? '');
|
|
|
|
if (!db()->exists('users', 'email', $e)) exit("No account with that email address.");
|
|
|
|
$newpass = token(16);
|
|
$hashed = password_hash($newpass, PASSWORD_ARGON2ID);
|
|
|
|
db()->query('UPDATE users SET password=? WHERE email=?;', [$hashed, $e]);
|
|
|
|
if (sendpassemail($e, $newpass)) {
|
|
display("Your new password was emailed to the address you provided.<br><br>Once you receive it, you may <a href=\"login.php?do=login\">Log In</a> and continue playing.<br><br>Thank you.","Lost Password",false,false,false);
|
|
} else {
|
|
display("There was an error sending your new password.<br><br>Please check with the game administrator for more information.<br><br>We apologize for the inconvience.","Lost Password",false,false,false);
|
|
}
|
|
}
|
|
|
|
$topnav = "<a href=\"login.php?do=login\"><img src=\"images/button_login.gif\" alt=\"Log In\" border=\"0\" /></a><a href=\"users.php?do=register\"><img src=\"images/button_register.gif\" alt=\"Register\" border=\"0\" /></a><a href=\"help.php\"><img src=\"images/button_help.gif\" alt=\"Help\" border=\"0\" /></a>";
|
|
display(gettemplate("lostpassword"), "Lost Password", false, false, false);
|
|
|
|
}
|
|
|
|
function changepassword()
|
|
{
|
|
if (isset($_POST["submit"])) {
|
|
$u = trim($_POST['username'] ?? '');
|
|
$p = $_POST['password'] ?? '';
|
|
$np = $_POST['new_password'] ?? '';
|
|
$np2 = $_POST['new_password2'] ?? '';
|
|
|
|
$user = db()->query("SELECT password FROM users WHERE username=? LIMIT 1;", [$u]);
|
|
$user = $user->fetchArray(SQLITE3_ASSOC);
|
|
if ($user === false) exit("No account with that username.");
|
|
|
|
if (!password_verify($p, $user['password'])) exit("The old password you provided was incorrect.");
|
|
|
|
if (empty($np) || strlen($np) < 6) {
|
|
$errors[] = 'New password is required and must be at least 6 characters long.';
|
|
}
|
|
|
|
if ($np2 !== $np) {
|
|
$errors[] = 'Verify New Password must match.';
|
|
}
|
|
|
|
$realnewpass = password_hash($np, PASSWORD_ARGON2ID);
|
|
db()->query('UPDATE users SET password=? WHERE username=?;', [$realnewpass, $u]);
|
|
|
|
set_cookie('dkgame', '', -3600);
|
|
|
|
display("Your password was changed successfully.<br><br>You have been logged out of the game to avoid errors.<br><br>Please <a href=\"login.php?do=login\">log back in</a> to continue playing.","Change Password",false,false,false);
|
|
}
|
|
|
|
$topnav = "<a href=\"login.php?do=login\"><img src=\"images/button_login.gif\" alt=\"Log In\" border=\"0\" /></a><a href=\"users.php?do=register\"><img src=\"images/button_register.gif\" alt=\"Register\" border=\"0\" /></a><a href=\"help.php\"><img src=\"images/button_help.gif\" alt=\"Help\" border=\"0\" /></a>";
|
|
display(gettemplate("changepassword"), "Change Password", false, false, false);
|
|
}
|
|
|
|
function sendpassemail($emailaddress, $password)
|
|
{
|
|
global $controlrow;
|
|
extract($controlrow);
|
|
|
|
$email = <<<HTML
|
|
You or someone using your email address submitted a Lost Password application on the $gamename server, located at $gameurl.
|
|
|
|
We have issued you a new password so you can log back into the game.
|
|
|
|
Your new password is: $password
|
|
|
|
Thanks for playing.
|
|
HTML;
|
|
|
|
$status = mymail($emailaddress, "$gamename Lost Password", $email);
|
|
return $status;
|
|
}
|
|
|
|
function sendregmail($emailaddress, $vercode)
|
|
{
|
|
global $controlrow;
|
|
extract($controlrow);
|
|
$verurl = $gameurl . "?do=verify";
|
|
|
|
$email = <<<HTML
|
|
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.
|
|
HTML;
|
|
|
|
$status = mymail($emailaddress, "$gamename Account Verification", $email);
|
|
return $status;
|
|
}
|
|
|
|
/**
|
|
* thanks to arto dot PLEASE dot DO dot NOT dot SPAM at artoaaltonen dot fi.
|
|
*/
|
|
function mymail($to, $title, $body, $from = '')
|
|
{
|
|
global $controlrow;
|
|
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);
|
|
}
|