diff --git a/classes/Battles/Register.php b/classes/Battles/Register.php new file mode 100644 index 0000000..4dd5e17 --- /dev/null +++ b/classes/Battles/Register.php @@ -0,0 +1,21 @@ +execute('select count(*) from users where login = ? or email = ?', [$login, $email])->fetchColumn()) { + return 0; + } + Db::getInstance()->execute('insert into users (login,pass,email,borndate,ip,session_id,shadow) values (?,?,?,?,?,?,?)', + [$login, $password, $email, $birthday, $_SERVER['REMOTE_ADDR'], session_id(), '0.png']); + $userId = Db::getInstance()->lastInsertId(); + Db::getInstance()->execute('insert into online (user_id, login_time, room, real_time) values (?,?,1,?)', [$userId, time(), time()]); + Db::getInstance()->execute('insert into bank (user_id) values ?', $userId); + return $userId; + } +} \ No newline at end of file diff --git a/classes/Battles/RememberPassword.php b/classes/Battles/RememberPassword.php new file mode 100644 index 0000000..caa92ef --- /dev/null +++ b/classes/Battles/RememberPassword.php @@ -0,0 +1,68 @@ +"; + $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); + } + + public function sendRecoveryMail(string $to): string + { + $check = Db::getInstance()->ofetch('SELECT email FROM users WHERE login = ?', $to); + if (!empty(Db::getInstance()->fetchColumn('select email from users where login = ?', $to))) { + return self::ERROR_WRONG_LOGIN; + } + if (!empty(Db::getInstance()->ofetch('SELECT 1 FROM users_recovery WHERE login = ?', $to))) { + return self::ERROR_TOO_MANY_TRIES; + } + + $hash = uniqid(); + $tomorrow = date('d-M-Y', strtotime('+1 days')); + Db::getInstance()->execute('INSERT INTO users_recovery (login, hash, ip, date) VALUES (?,?,?,?)', [$to, $hash, $tomorrow, $_SERVER['REMOTE_ADDR']]); + $message = sprintf('Здравствуйте!

Кто-то запросил восстановление пароля к вашему персонажу %s

+ Для смены пароля пройдите по данной ссылке.

+ Ссылка будет действовать до %s',$to, GAMEDOMAIN, $hash, $tomorrow); + return self::mailSend($check->email, $message) ? self::OK_MAIL_SENT : self::ERROR_MAIL_NOT_SENT; + } + public function isAllowed($hash) + { + return Db::getInstance()->execute('SELECT count(*) FROM users_recovery WHERE hash = ? AND date < ?', [$hash, date('d-M-Y')])->fetchColumn() ? true : self::ERROR_OLD_HASH; + } + + public function setNewPassword(string $newPassword, string $hash):string + { + $login = Db::getInstance()->execute('select login from users_recovery where hash = ?', $hash)->fetchColumn(); + if (empty($login)) { + return self::ERROR_WRONG_HASH; + } + $newPassword = password_hash($newPassword, PASSWORD_DEFAULT); + Db::getInstance()->execute('UPDATE users SET pass = ? WHERE login = ?', [$newPassword, $login]); + Db::getInstance()->execute('DELETE FROM users_recovery WHERE hash = ?', $hash); + return self::OK_PASSWORD_CHANGED; + } +} \ No newline at end of file diff --git a/register.php b/register.php index 4485193..8d54518 100644 --- a/register.php +++ b/register.php @@ -1,6 +1,6 @@ ofetch('SELECT 1 FROM users WHERE login = ? OR email = ?', [$login, $email])) { - return false; - } - Db::getInstance()->execute('INSERT INTO users (login,pass,email,borndate,ip,session_id,shadow) - VALUES (?,?,?,?,?,?,?)', [$login, $password, $email, $birthday, $_SERVER['REMOTE_ADDR'], session_id(), '0.png']); - $userId = Db::getInstance()->lastInsertId(); - Db::getInstance()->execute('INSERT INTO online (user_id, login_time, room, real_time) VALUES (?,?,1,?)', [$userId, time(), time()]); - Db::getInstance()->execute('INSERT INTO bank (user_id) VALUES ?', $userId); - setcookie(GAMEDOMAIN, $userId, time() + 3600); - setcookie("battle", time()); - $_SESSION['uid'] = $userId; - $_SESSION['sid'] = session_id(); - return true; - } - }; - - $newUser::addUser($login, $password, $email, $birthday); + $uid = Register::addUser($login, $password, $email, $birthday); + setcookie(GAMEDOMAIN, $uid, time() + 3600); + setcookie("battle", time()); + $_SESSION['uid'] = $uid; + $_SESSION['sid'] = session_id(); header('Location: fight.php'); exit; } diff --git a/rememberpassword.php b/rememberpassword.php index 4dda593..fc5d638 100644 --- a/rememberpassword.php +++ b/rememberpassword.php @@ -1,77 +1,14 @@ "; - $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); - } - - public function sendRecoveryMail(string $to): string - { - $check = Db::getInstance()->ofetch('SELECT email FROM users WHERE login = ?', $to); - if (!empty(Db::getInstance()->fetchColumn('select email from users where login = ?', $to))) { - return ERROR_WRONG_LOGIN; - } - if (!empty(Db::getInstance()->ofetch('SELECT 1 FROM users_recovery WHERE login = ?', $to))) { - return ERROR_TOO_MANY_TRIES; - } - $hash = bin2hex(random_bytes(8)); - Db::getInstance()->execute('INSERT INTO users_recovery (login, hash, ip, date) VALUES (?,?,?,?)', [$to, $hash, date('Y-m-d', strtotime('+1days')), $_SERVER['REMOTE_ADDR']]); - $message = "Здравствуйте!

- Кто-то запросил восстановление пароля к вашему персонажу " . $to . ".

- Для смены пароля пройдите по - данной ссылке.

- Ссылка будет действовать до " . date('d-M-Y', strtotime(date('Y-m-d', strtotime('+1days')))) . ". - "; - return self::mailSend($check->email, $message) ? OK_MAIL_SENT : ERROR_MAIL_NOT_SENT; - } - - public function isAllowed($hash) - { - return Db::getInstance()->execute('SELECT 1 FROM users_recovery WHERE hash = ? AND date < ?', [$hash, date('Y-m-d')])->fetchColumn() ? true : ERROR_OLD_HASH; - } - - public function setNewPassword(string $newPassword, string $hash):string - { - $login = Db::getInstance()->execute('select login from users_recovery where hash = ?', $hash)->fetchColumn(); - if (empty($login)) { - return ERROR_WRONG_HASH; - } - $newPassword = password_hash($newPassword, PASSWORD_DEFAULT); - Db::getInstance()->execute('UPDATE users SET pass = ? WHERE login = ?', [$newPassword, $login]); - Db::getInstance()->execute('DELETE FROM users_recovery WHERE hash = ?', $hash); - return OK_PASSWORD_CHANGED; - } -}; +$operation = new RememberPassword(); if ($login) { $statusMessage = $operation->sendRecoveryMail($login);