2018-01-28 16:40:49 +00:00
|
|
|
|
<?php
|
2018-12-10 16:39:09 +00:00
|
|
|
|
require_once("config.php");
|
2020-08-27 13:50:58 +00:00
|
|
|
|
define('OK_MAIL_SENT', 'Письмо отправлено!');
|
|
|
|
|
define('OK_PASSWORD_CHANGED', 'Пароль изменён!');
|
|
|
|
|
define('ERROR_MAIL_NOT_SENT', 'Письмо не отправлено!');
|
|
|
|
|
define('ERROR_WRONG_LOGIN', 'Такого пользователя не существует!');
|
|
|
|
|
define('ERROR_TOO_MANY_TRIES', 'Вы уже отправляли себе письмо сегодня!');
|
|
|
|
|
define('ERROR_OLD_HASH', 'Ссылка устарела!');
|
|
|
|
|
define('ERROR_WRONG_HASH', 'Неверная ссылка!');
|
2020-08-27 17:35:30 +00:00
|
|
|
|
$login = filter_input(INPUT_POST, 'loginid', FILTER_SANITIZE_SPECIAL_CHARS);
|
2019-02-16 00:00:25 +00:00
|
|
|
|
$password = password_hash(filter_input(INPUT_POST, 'psw'), PASSWORD_DEFAULT);
|
2020-08-27 17:35:30 +00:00
|
|
|
|
$allowChange = false;
|
|
|
|
|
$changePassword = filter_input(INPUT_GET, 'change');
|
|
|
|
|
$newPassword = $_POST['newpasswd'] ?? 0;
|
|
|
|
|
$hashCheck = $_POST['hashcheck'] ?? 0;
|
|
|
|
|
|
2018-01-28 16:40:49 +00:00
|
|
|
|
function mail_send($to, $from_user, $from_email, $subject = '(No subject)', $message = '')
|
|
|
|
|
{
|
2018-12-10 16:15:11 +00:00
|
|
|
|
$from_user = "=?UTF-8?B?" . base64_encode($from_user) . "?=";
|
|
|
|
|
$subject = "=?UTF-8?B?" . base64_encode($subject) . "?=";
|
2018-01-28 16:40:49 +00:00
|
|
|
|
|
2018-12-10 16:15:11 +00:00
|
|
|
|
$headers = "From: $from_user <$from_email>\r\n" .
|
2018-01-28 16:40:49 +00:00
|
|
|
|
"MIME-Version: 1.0" . "\r\n" .
|
|
|
|
|
"Content-type: text/html; charset=UTF-8" . "\r\n";
|
|
|
|
|
|
|
|
|
|
return mail($to, $subject, $message, $headers);
|
|
|
|
|
}
|
|
|
|
|
if ($login) {
|
2020-08-27 17:20:31 +00:00
|
|
|
|
$loginCheck = db::c()->query('SELECT email FROM users WHERE login = "?s"', $login)->fetch_assoc();
|
|
|
|
|
if ($loginCheck) {
|
|
|
|
|
$recovery = db::c()->query('SELECT 1 FROM users_recovery WHERE login = "?s"', $login)->getNumRows() ?? 0;
|
|
|
|
|
if (!$recovery) {
|
2020-08-27 13:50:58 +00:00
|
|
|
|
$hash = bin2hex(random_bytes(8));
|
|
|
|
|
$lasttime = date('Y-m-d', strtotime('+1days'));
|
|
|
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
|
|
|
|
db::c()->query('INSERT INTO users_recovery (login, hash, ip, date) VALUES ("?s", "?s", "?s", "?s")', $login, $hash, $lasttime, $ip);
|
|
|
|
|
$message = "Здравствуйте!<br><br>
|
2019-02-16 00:48:48 +00:00
|
|
|
|
Кто-то запросил восстановление пароля к вашему персонажу " . $login . ".<br><br>
|
|
|
|
|
Для смены пароля пройдите по
|
|
|
|
|
<a href='" . GAMEDOMAIN . "/rememberpassword.php?change=" . $hash . "'> данной ссылке</a>.<br><br>
|
2020-08-27 17:26:34 +00:00
|
|
|
|
Ссылка будет действовать до <em>" . date('d-M-Y', strtotime($lasttime)) . "</em>.
|
2018-01-28 16:40:49 +00:00
|
|
|
|
";
|
2020-08-27 17:27:32 +00:00
|
|
|
|
if (mail_send($loginCheck['email'], "Noreply", "noreply@" . GAMEDOMAIN, "Восстановление забытого пароля", $message)) {
|
2020-08-27 13:50:58 +00:00
|
|
|
|
$statusMessage = OK_MAIL_SENT;
|
|
|
|
|
} else {
|
|
|
|
|
$statusMessage = ERROR_MAIL_NOT_SENT;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-08-27 17:20:31 +00:00
|
|
|
|
$statusMessage = ERROR_TOO_MANY_TRIES;
|
2020-08-27 13:50:58 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-08-27 17:22:43 +00:00
|
|
|
|
$statusMessage = ERROR_WRONG_LOGIN;
|
2020-08-27 13:50:58 +00:00
|
|
|
|
}
|
2018-01-28 16:40:49 +00:00
|
|
|
|
}
|
2019-02-16 00:48:48 +00:00
|
|
|
|
if ($changePassword) {
|
2020-08-27 13:50:58 +00:00
|
|
|
|
if (db::c()->query('SELECT 1 FROM users_recovery WHERE `hash` = "?s" AND `date` < "?s"', $changePassword, date('Y-m-d'))->getNumRows()) {
|
2019-02-16 00:48:48 +00:00
|
|
|
|
$allowChange = true;
|
2020-08-27 13:50:58 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$statusMessage = ERROR_OLD_HASH;
|
|
|
|
|
}
|
2019-02-16 00:48:48 +00:00
|
|
|
|
}
|
2020-08-27 17:35:30 +00:00
|
|
|
|
if ($newPassword && $hashCheck) {
|
|
|
|
|
$query = db::c()->query('SELECT login FROM users_recovery WHERE hash = "?s"', $hashCheck);
|
2019-02-16 00:48:48 +00:00
|
|
|
|
if ($query->getNumRows()) {
|
2020-08-27 17:35:30 +00:00
|
|
|
|
$query = $query->fetch_assoc();
|
|
|
|
|
$passwordHashed = password_hash($newPassword, PASSWORD_DEFAULT);
|
2020-08-27 13:50:58 +00:00
|
|
|
|
db::c()->query('UPDATE users SET pass = "?s" WHERE login = "?s"', $passwordHashed, $query['login']);
|
2020-08-27 17:38:50 +00:00
|
|
|
|
db::c()->query('DELETE FROM users_recovery WHERE hash = "?s"', $hashCheck);
|
2020-08-27 13:50:58 +00:00
|
|
|
|
$statusMessage = OK_PASSWORD_CHANGED;
|
|
|
|
|
} else {
|
|
|
|
|
$statusMessage = ERROR_WRONG_HASH;
|
|
|
|
|
}
|
2018-12-10 16:37:50 +00:00
|
|
|
|
}
|
2018-01-28 16:40:49 +00:00
|
|
|
|
?>
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html lang="ru_RU">
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="utf-8">
|
2018-12-10 16:15:11 +00:00
|
|
|
|
<link href="css/main.css" rel="stylesheet"/>
|
2018-01-28 16:40:49 +00:00
|
|
|
|
<title>Восстановление пароля</title>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
2018-12-10 16:15:11 +00:00
|
|
|
|
<a href="/"> ← на главную</a>
|
|
|
|
|
<?php if (!empty($statusMessage)): ?>
|
|
|
|
|
<h3><?php echo $statusMessage; ?></h3>
|
2018-01-28 16:40:49 +00:00
|
|
|
|
<?php endif; ?>
|
2019-02-16 00:51:20 +00:00
|
|
|
|
<?php if ($allowChange === true): ?>
|
2019-02-16 00:48:48 +00:00
|
|
|
|
<h1>Создайте себе новый пароль</h1>
|
|
|
|
|
<form method="post">
|
|
|
|
|
<input required name="newpasswd" placeholder="Новый пароль">
|
|
|
|
|
<input type="hidden" name="hashcheck" value="<?= $changePassword ?>"><br>
|
|
|
|
|
<input type=submit>
|
|
|
|
|
</form>
|
|
|
|
|
<?php else: ?>
|
2019-02-16 00:50:07 +00:00
|
|
|
|
<h1>Восстановление пароля</h1>
|
|
|
|
|
<p>Для восстановления пароля введите свой login и нажмите кнопку «Отправить письмо».<br>
|
|
|
|
|
Письмо будет выслано на email адрес, указанный вами при регистрации.<br>
|
|
|
|
|
Восстанавливать пароль можно только раз в сутки.</p>
|
|
|
|
|
<form method="post">
|
|
|
|
|
<input required name="loginid" placeholder="Имя персонажа" value="<?= $login ?>"><br>
|
|
|
|
|
<input type=submit>
|
|
|
|
|
</form>
|
2019-02-16 00:48:48 +00:00
|
|
|
|
<?php endif; ?>
|
2018-01-28 16:40:49 +00:00
|
|
|
|
</body>
|
|
|
|
|
</html>
|