2018-01-28 16:40:49 +00:00
|
|
|
|
<?php
|
2021-01-29 15:21:50 +00:00
|
|
|
|
|
|
|
|
|
use Battles\Database\DBPDO;
|
|
|
|
|
use Battles\Template;
|
|
|
|
|
|
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);
|
2021-01-29 15:21:50 +00:00
|
|
|
|
$password = isset($_POST['psw']) ? password_hash($_POST['psw'], PASSWORD_DEFAULT) : null;
|
|
|
|
|
$_GET['change'] = $_GET['change'] ?? null;
|
2020-08-27 17:35:30 +00:00
|
|
|
|
$newPassword = $_POST['newpasswd'] ?? 0;
|
|
|
|
|
$hashCheck = $_POST['hashcheck'] ?? 0;
|
2021-01-29 15:21:50 +00:00
|
|
|
|
$db = new DBPDO();
|
2020-08-27 17:35:30 +00:00
|
|
|
|
|
2021-01-29 15:21:50 +00:00
|
|
|
|
$operation = new class {
|
|
|
|
|
private function mailSend(string $to, string $message): bool
|
|
|
|
|
{
|
|
|
|
|
$from = "=?UTF-8?B?" . base64_encode('Noreply') . "?= <noreply@" . GAMEDOMAIN . ">";
|
|
|
|
|
$subject = "=?UTF-8?B?" . base64_encode('Восстановление забытого пароля') . "?=";
|
|
|
|
|
$headers = [
|
|
|
|
|
'From' => $from,
|
|
|
|
|
'MIME-Version' => '1.0',
|
|
|
|
|
'Content-type' => 'text/html; charset=UTF-8',
|
|
|
|
|
];
|
2018-01-28 16:40:49 +00:00
|
|
|
|
|
2021-01-29 15:21:50 +00:00
|
|
|
|
if (extension_loaded('tidy')) {
|
|
|
|
|
$cleaner = new tidy();
|
|
|
|
|
$message = $cleaner->repairString($message, ['show-errors' => 0, 'show-warnings' => false], 'utf8');
|
|
|
|
|
}
|
|
|
|
|
return mail($to, $subject, $message, $headers);
|
|
|
|
|
}
|
2020-09-30 19:12:34 +00:00
|
|
|
|
|
2021-01-29 15:21:50 +00:00
|
|
|
|
public function sendRecoveryMail(string $to): string
|
|
|
|
|
{
|
|
|
|
|
$db = new DBPDO();
|
|
|
|
|
$check = $db->ofetch('SELECT email FROM users WHERE login = ?', $to);
|
|
|
|
|
if (!$check) {
|
|
|
|
|
return ERROR_WRONG_LOGIN;
|
|
|
|
|
}
|
|
|
|
|
if ($db->ofetch('SELECT 1 FROM users_recovery WHERE login = ?', $to)) {
|
|
|
|
|
return ERROR_TOO_MANY_TRIES;
|
|
|
|
|
}
|
|
|
|
|
$hash = bin2hex(random_bytes(8));
|
|
|
|
|
$db->execute('INSERT INTO users_recovery (login, hash, ip, date) VALUES (?,?,?,?)', [$to, $hash, date('Y-m-d', strtotime('+1days')), $_SERVER['REMOTE_ADDR']]);
|
|
|
|
|
$message = "Здравствуйте!<br><br>
|
|
|
|
|
Кто-то запросил восстановление пароля к вашему персонажу " . $to . ".<br><br>
|
2019-02-16 00:48:48 +00:00
|
|
|
|
Для смены пароля пройдите по
|
|
|
|
|
<a href='" . GAMEDOMAIN . "/rememberpassword.php?change=" . $hash . "'> данной ссылке</a>.<br><br>
|
2021-01-29 15:21:50 +00:00
|
|
|
|
Ссылка будет действовать до <em>" . date('d-M-Y', strtotime(date('Y-m-d', strtotime('+1days')))) . "</em>.
|
2018-01-28 16:40:49 +00:00
|
|
|
|
";
|
2021-01-29 15:21:50 +00:00
|
|
|
|
return self::mailSend($check->email, $message) ? OK_MAIL_SENT : ERROR_MAIL_NOT_SENT;
|
2020-08-27 13:50:58 +00:00
|
|
|
|
}
|
2021-01-29 15:21:50 +00:00
|
|
|
|
|
|
|
|
|
public function isAllowed($hash)
|
|
|
|
|
{
|
|
|
|
|
return DBPDO::INIT()->fetch('SELECT 1 FROM users_recovery WHERE hash = ? AND date < ?', [$hash, date('Y-m-d')]) ? true : ERROR_OLD_HASH;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setNewPassword(string $newPassword, string $hash):string
|
|
|
|
|
{
|
|
|
|
|
$db = new DBPDO();
|
|
|
|
|
$row = $db->ofetch('SELECT login FROM users_recovery WHERE hash = ?', $hash);
|
|
|
|
|
if (!$row) {
|
|
|
|
|
return ERROR_WRONG_HASH;
|
|
|
|
|
}
|
|
|
|
|
$newPassword = password_hash($newPassword, PASSWORD_DEFAULT);
|
|
|
|
|
$db->execute('UPDATE users SET pass = ? WHERE login = ?', [$newPassword, $row->login]);
|
|
|
|
|
$db->execute('DELETE FROM users_recovery WHERE hash = ?', $hash);
|
|
|
|
|
return OK_PASSWORD_CHANGED;
|
2020-08-27 13:50:58 +00:00
|
|
|
|
}
|
2021-01-29 15:21:50 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if ($login) {
|
|
|
|
|
$statusMessage = $operation->sendRecoveryMail($login);
|
2019-02-16 00:48:48 +00:00
|
|
|
|
}
|
2021-01-29 15:21:50 +00:00
|
|
|
|
|
2020-08-27 17:35:30 +00:00
|
|
|
|
if ($newPassword && $hashCheck) {
|
2021-01-29 15:21:50 +00:00
|
|
|
|
$statusMessage = $operation->setNewPassword($newPassword, $hashCheck);
|
2018-12-10 16:37:50 +00:00
|
|
|
|
}
|
2021-01-29 15:21:50 +00:00
|
|
|
|
Template::header('Восстановление пароля');
|
2018-01-28 16:40:49 +00:00
|
|
|
|
?>
|
2020-09-30 19:12:34 +00:00
|
|
|
|
<a href="/"> ← на главную</a>
|
2018-12-10 16:15:11 +00:00
|
|
|
|
<?php if (!empty($statusMessage)): ?>
|
|
|
|
|
<h3><?php echo $statusMessage; ?></h3>
|
2018-01-28 16:40:49 +00:00
|
|
|
|
<?php endif; ?>
|
2021-01-29 15:21:50 +00:00
|
|
|
|
<?php if ($operation->isAllowed($_GET['change']) === true): ?>
|
2019-02-16 00:48:48 +00:00
|
|
|
|
<h1>Создайте себе новый пароль</h1>
|
|
|
|
|
<form method="post">
|
2021-01-29 15:21:50 +00:00
|
|
|
|
<label>
|
|
|
|
|
<input required name="newpasswd" placeholder="Новый пароль"> Новый пароль.
|
|
|
|
|
</label>
|
|
|
|
|
<input type="hidden" name="hashcheck" value="<?= $_GET['change'] ?>"><br>
|
2019-02-16 00:48:48 +00:00
|
|
|
|
<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">
|
2021-01-29 15:21:50 +00:00
|
|
|
|
<label>
|
|
|
|
|
<input required name="loginid" placeholder="Имя персонажа" value="<?= $login ?>"> Имя персонажа.
|
|
|
|
|
</label><br>
|
2019-02-16 00:50:07 +00:00
|
|
|
|
<input type=submit>
|
|
|
|
|
</form>
|
2020-09-30 19:12:34 +00:00
|
|
|
|
<?php endif;
|