Отделение классов от кода.
This commit is contained in:
parent
3502904656
commit
f12e7c8cd7
21
classes/Battles/Register.php
Normal file
21
classes/Battles/Register.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Battles;
|
||||||
|
|
||||||
|
use Battles\Database\Db;
|
||||||
|
|
||||||
|
class Register
|
||||||
|
{
|
||||||
|
public static function addUser(string $login, string $password, string $email, string $birthday): int
|
||||||
|
{
|
||||||
|
if (Db::getInstance()->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;
|
||||||
|
}
|
||||||
|
}
|
68
classes/Battles/RememberPassword.php
Normal file
68
classes/Battles/RememberPassword.php
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Battles;
|
||||||
|
|
||||||
|
use tidy, Battles\Database\Db;
|
||||||
|
|
||||||
|
class RememberPassword
|
||||||
|
{
|
||||||
|
const OK_MAIL_SENT = 'Письмо отправлено!';
|
||||||
|
const OK_PASSWORD_CHANGED = 'Пароль изменён!';
|
||||||
|
const ERROR_MAIL_NOT_SENT = 'Письмо не отправлено!';
|
||||||
|
const ERROR_WRONG_LOGIN = 'Такого пользователя не существует!';
|
||||||
|
const ERROR_TOO_MANY_TRIES = 'Вы уже отправляли себе письмо сегодня!';
|
||||||
|
const ERROR_OLD_HASH = 'Ссылка устарела!';
|
||||||
|
const ERROR_WRONG_HASH = 'Неверная ссылка!';
|
||||||
|
|
||||||
|
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',
|
||||||
|
];
|
||||||
|
|
||||||
|
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('Здравствуйте!<br><br>Кто-то запросил восстановление пароля к вашему персонажу %s <br><br>
|
||||||
|
Для смены пароля пройдите по <a href="//%s/rememberpassword.php?change=%s">данной ссылке</a>.<br><br>
|
||||||
|
Ссылка будет действовать до <em>%s</em>',$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;
|
||||||
|
}
|
||||||
|
}
|
27
register.php
27
register.php
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Battles\Template, Battles\Database\Db;
|
use Battles\Template, Battles\Register;
|
||||||
|
|
||||||
require_once "config.php";
|
require_once "config.php";
|
||||||
|
|
||||||
@ -15,26 +15,11 @@ if ($_COOKIE[GAMEDOMAIN] ?? null) {
|
|||||||
$law2 = filter_input(INPUT_POST, 'law2', FILTER_VALIDATE_BOOLEAN);
|
$law2 = filter_input(INPUT_POST, 'law2', FILTER_VALIDATE_BOOLEAN);
|
||||||
|
|
||||||
if ($login && $password && $email && $birthday && $law && $law2) {
|
if ($login && $password && $email && $birthday && $law && $law2) {
|
||||||
$newUser = new class {
|
$uid = Register::addUser($login, $password, $email, $birthday);
|
||||||
public static function addUser(string $login, string $password, string $email, string $birthday): bool
|
setcookie(GAMEDOMAIN, $uid, time() + 3600);
|
||||||
{
|
setcookie("battle", time());
|
||||||
if (Db::getInstance()->ofetch('SELECT 1 FROM users WHERE login = ? OR email = ?', [$login, $email])) {
|
$_SESSION['uid'] = $uid;
|
||||||
return false;
|
$_SESSION['sid'] = session_id();
|
||||||
}
|
|
||||||
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);
|
|
||||||
header('Location: fight.php');
|
header('Location: fight.php');
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
@ -1,77 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Battles\Database\Db;
|
use Battles\Template, Battles\RememberPassword;
|
||||||
use Battles\Template;
|
|
||||||
|
|
||||||
require_once("config.php");
|
require_once("config.php");
|
||||||
const OK_MAIL_SENT = 'Письмо отправлено!';
|
|
||||||
const OK_PASSWORD_CHANGED = 'Пароль изменён!';
|
|
||||||
const ERROR_MAIL_NOT_SENT = 'Письмо не отправлено!';
|
|
||||||
const ERROR_WRONG_LOGIN = 'Такого пользователя не существует!';
|
|
||||||
const ERROR_TOO_MANY_TRIES = 'Вы уже отправляли себе письмо сегодня!';
|
|
||||||
const ERROR_OLD_HASH = 'Ссылка устарела!';
|
|
||||||
const ERROR_WRONG_HASH = 'Неверная ссылка!';
|
|
||||||
$login = filter_input(INPUT_POST, 'loginid', FILTER_SANITIZE_SPECIAL_CHARS);
|
$login = filter_input(INPUT_POST, 'loginid', FILTER_SANITIZE_SPECIAL_CHARS);
|
||||||
$password = isset($_POST['psw']) ? password_hash($_POST['psw'], PASSWORD_DEFAULT) : null;
|
$password = isset($_POST['psw']) ? password_hash($_POST['psw'], PASSWORD_DEFAULT) : null;
|
||||||
$_GET['change'] = $_GET['change'] ?? null;
|
$_GET['change'] = $_GET['change'] ?? null;
|
||||||
$newPassword = $_POST['newpasswd'] ?? 0;
|
$newPassword = $_POST['newpasswd'] ?? 0;
|
||||||
$hashCheck = $_POST['hashcheck'] ?? 0;
|
$hashCheck = $_POST['hashcheck'] ?? 0;
|
||||||
|
$operation = new RememberPassword();
|
||||||
$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',
|
|
||||||
];
|
|
||||||
|
|
||||||
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 = "Здравствуйте!<br><br>
|
|
||||||
Кто-то запросил восстановление пароля к вашему персонажу " . $to . ".<br><br>
|
|
||||||
Для смены пароля пройдите по
|
|
||||||
<a href='" . GAMEDOMAIN . "/rememberpassword.php?change=" . $hash . "'> данной ссылке</a>.<br><br>
|
|
||||||
Ссылка будет действовать до <em>" . date('d-M-Y', strtotime(date('Y-m-d', strtotime('+1days')))) . "</em>.
|
|
||||||
";
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if ($login) {
|
if ($login) {
|
||||||
$statusMessage = $operation->sendRecoveryMail($login);
|
$statusMessage = $operation->sendRecoveryMail($login);
|
||||||
|
Loading…
Reference in New Issue
Block a user