Теперь используя анонимный класс.
This commit is contained in:
parent
06963ce428
commit
14e8c647ce
@ -1,4 +1,8 @@
|
||||
<?php
|
||||
|
||||
use Battles\Database\DBPDO;
|
||||
use Battles\Template;
|
||||
|
||||
require_once("config.php");
|
||||
define('OK_MAIL_SENT', 'Письмо отправлено!');
|
||||
define('OK_PASSWORD_CHANGED', 'Пароль изменён!');
|
||||
@ -8,81 +12,90 @@ define('ERROR_TOO_MANY_TRIES', 'Вы уже отправляли себе пис
|
||||
define('ERROR_OLD_HASH', 'Ссылка устарела!');
|
||||
define('ERROR_WRONG_HASH', 'Неверная ссылка!');
|
||||
$login = filter_input(INPUT_POST, 'loginid', FILTER_SANITIZE_SPECIAL_CHARS);
|
||||
$password = password_hash($_POST['psw'], PASSWORD_DEFAULT) ?? null;
|
||||
$allowChange = false;
|
||||
$changePassword = $_GET['change'] ?? 0;
|
||||
$password = isset($_POST['psw']) ? password_hash($_POST['psw'], PASSWORD_DEFAULT) : null;
|
||||
$_GET['change'] = $_GET['change'] ?? null;
|
||||
$newPassword = $_POST['newpasswd'] ?? 0;
|
||||
$hashCheck = $_POST['hashcheck'] ?? 0;
|
||||
$db = new DBPDO();
|
||||
|
||||
function mail_send($to, $from_user, $from_email, $subject = '(No subject)', $message = '')
|
||||
$operation = new class {
|
||||
private function mailSend(string $to, string $message): bool
|
||||
{
|
||||
$from_user = "=?UTF-8?B?" . base64_encode($from_user) . "?=";
|
||||
$subject = "=?UTF-8?B?" . base64_encode($subject) . "?=";
|
||||
|
||||
$headers = "From: $from_user <$from_email>\r\n" .
|
||||
"MIME-Version: 1.0" . "\r\n" .
|
||||
"Content-type: text/html; charset=UTF-8" . "\r\n";
|
||||
$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',
|
||||
];
|
||||
|
||||
if (extension_loaded('tidy')) {
|
||||
$cleaner = new tidy();
|
||||
$message = $cleaner->repairString($message, ['show-errors' => 0, 'show-warnings' => false], 'utf8');
|
||||
}
|
||||
return mail($to, $subject, $message, $headers);
|
||||
}
|
||||
|
||||
if ($login) {
|
||||
$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) {
|
||||
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));
|
||||
$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);
|
||||
$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>
|
||||
Кто-то запросил восстановление пароля к вашему персонажу " . $login . ".<br><br>
|
||||
Кто-то запросил восстановление пароля к вашему персонажу " . $to . ".<br><br>
|
||||
Для смены пароля пройдите по
|
||||
<a href='" . GAMEDOMAIN . "/rememberpassword.php?change=" . $hash . "'> данной ссылке</a>.<br><br>
|
||||
Ссылка будет действовать до <em>" . date('d-M-Y', strtotime($lasttime)) . "</em>.
|
||||
Ссылка будет действовать до <em>" . date('d-M-Y', strtotime(date('Y-m-d', strtotime('+1days')))) . "</em>.
|
||||
";
|
||||
if (mail_send($loginCheck['email'], "Noreply", "noreply@" . GAMEDOMAIN, "Восстановление забытого пароля", $message)) {
|
||||
$statusMessage = OK_MAIL_SENT;
|
||||
} else {
|
||||
$statusMessage = ERROR_MAIL_NOT_SENT;
|
||||
return self::mailSend($check->email, $message) ? OK_MAIL_SENT : ERROR_MAIL_NOT_SENT;
|
||||
}
|
||||
} else {
|
||||
$statusMessage = ERROR_TOO_MANY_TRIES;
|
||||
|
||||
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;
|
||||
}
|
||||
} else {
|
||||
$statusMessage = ERROR_WRONG_LOGIN;
|
||||
|
||||
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;
|
||||
}
|
||||
if ($changePassword) {
|
||||
if (db::c()->query('SELECT 1 FROM users_recovery WHERE `hash` = "?s" AND `date` < "?s"', $changePassword, date('Y-m-d'))->getNumRows()) {
|
||||
$allowChange = true;
|
||||
} else {
|
||||
$statusMessage = ERROR_OLD_HASH;
|
||||
}
|
||||
};
|
||||
|
||||
if ($login) {
|
||||
$statusMessage = $operation->sendRecoveryMail($login);
|
||||
}
|
||||
|
||||
if ($newPassword && $hashCheck) {
|
||||
$query = db::c()->query('SELECT login FROM users_recovery WHERE hash = "?s"', $hashCheck);
|
||||
if ($query->getNumRows()) {
|
||||
$query = $query->fetch_assoc();
|
||||
$passwordHashed = password_hash($newPassword, PASSWORD_DEFAULT);
|
||||
db::c()->query('UPDATE users SET pass = "?s" WHERE login = "?s"', $passwordHashed, $query['login']);
|
||||
db::c()->query('DELETE FROM users_recovery WHERE hash = "?s"', $hashCheck);
|
||||
$statusMessage = OK_PASSWORD_CHANGED;
|
||||
} else {
|
||||
$statusMessage = ERROR_WRONG_HASH;
|
||||
$statusMessage = $operation->setNewPassword($newPassword, $hashCheck);
|
||||
}
|
||||
}
|
||||
\Battles\Template::header('Восстановление пароля');
|
||||
Template::header('Восстановление пароля');
|
||||
?>
|
||||
<a href="/"> ← на главную</a>
|
||||
<?php if (!empty($statusMessage)): ?>
|
||||
<h3><?php echo $statusMessage; ?></h3>
|
||||
<?php endif; ?>
|
||||
<?php if ($allowChange === true): ?>
|
||||
<?php if ($operation->isAllowed($_GET['change']) === true): ?>
|
||||
<h1>Создайте себе новый пароль</h1>
|
||||
<form method="post">
|
||||
<input required name="newpasswd" placeholder="Новый пароль">
|
||||
<input type="hidden" name="hashcheck" value="<?= $changePassword ?>"><br>
|
||||
<label>
|
||||
<input required name="newpasswd" placeholder="Новый пароль"> Новый пароль.
|
||||
</label>
|
||||
<input type="hidden" name="hashcheck" value="<?= $_GET['change'] ?>"><br>
|
||||
<input type=submit>
|
||||
</form>
|
||||
<?php else: ?>
|
||||
@ -91,7 +104,9 @@ if ($newPassword && $hashCheck) {
|
||||
Письмо будет выслано на email адрес, указанный вами при регистрации.<br>
|
||||
Восстанавливать пароль можно только раз в сутки.</p>
|
||||
<form method="post">
|
||||
<input required name="loginid" placeholder="Имя персонажа" value="<?= $login ?>"><br>
|
||||
<label>
|
||||
<input required name="loginid" placeholder="Имя персонажа" value="<?= $login ?>"> Имя персонажа.
|
||||
</label><br>
|
||||
<input type=submit>
|
||||
</form>
|
||||
<?php endif;
|
Loading…
Reference in New Issue
Block a user