game/_incl_data/class/UserRegister.php

247 lines
10 KiB
PHP
Raw Normal View History

<?php
2023-01-10 16:29:32 +00:00
use Core\Config;
2022-12-30 19:03:37 +00:00
use Core\Db;
class UserRegister
{
2023-01-10 16:29:32 +00:00
private string $error = '';
private string $login = '';
private string $email = '';
private string $password = '';
private int $class = 0;
private array $classBonuses = [
1 => [
's1=62|s2=3|s3=3|s4=30|rinv=40|m9=5|m6=10|s7=0|a1=0|a2=9|a3=0|a4=0|a5=0|mg1=0|mg2=0|mg3=0|mg4=0|mg5=0|mg6=0|mg7=0|s5=0|s6=0|a6=0|s8=0|s9=0|s10=0|s11=0|s12=0|s13=0|s14=0|s15=0',
'items' => [6433],
'effects' => [1, 20, 25, 37, 38, 283, 296, 412, 472],
],
2 => [
's1=21|s2=31|s3=16|s4=30|rinv=40|m9=5|m6=10|s7=0|a1=9|a2=0|a3=0|a4=0|a5=0|mg1=0|mg2=0|mg3=0|mg4=0|mg5=0|mg6=0|mg7=0|s5=0|s6=0',
'items' => [6431],
'effects' => [8, 20, 25, 37, 38, 283, 296, 412, 472],
],
3 => [
's1=42|s2=3|s3=3|s4=50|rinv=40|m9=5|m6=10|s7=0|a1=0|a2=0|a3=9|a4=0|a5=0|mg1=0|mg2=0|mg3=0|mg4=0|mg5=0|mg6=0|mg7=0|s5=0|s6=0|a6=0|s8=0|s9=0|s10=0|s11=0|s12=0|s13=0|s14=0|s15=0',
'items' => [6432],
'effects' => [1, 20, 25, 37, 38, 283, 296, 412, 472],
],
4 => [
's1=24|s2=15|s3=29|s4=30|rinv=40|m9=5|m6=10|s7=0|a1=0|a2=0|a3=0|a4=9|a5=0|mg1=0|mg2=0|mg3=0|mg4=0|mg5=0|mg6=0|mg7=0|s5=0|s6=0|a6=0|s8=0|s9=0|s10=0|s11=0|s12=0|s13=0|s14=0|s15=0',
'items' => [6430],
'effects' => [7, 20, 25, 37, 38, 283, 296, 412, 472],
],
5 => [
's1=14|s2=3|s3=3|s4=25|rinv=40|m9=5|m6=10|s7=0|a1=0|a2=0|a3=0|a4=0|a5=3|mg1=0|mg2=6|mg3=0|mg4=0|mg5=0|mg6=0|mg7=0|s5=13|s6=40|a6=0|s8=0|s9=0|s10=0|s11=0|s12=0|s13=0|s14=0|s15=0',
'items' => [6435],
'effects' => [9, 32, 53, 439, 25, 37, 38, 283, 296, 412, 472],
],
6 => [
's1=14|s2=3|s3=3|s4=25|rinv=40|m9=5|m6=10|s7=0|a1=0|a2=0|a3=0|a4=0|a5=3|mg1=6|mg2=0|mg3=0|mg4=0|mg5=0|mg6=0|mg7=0|s5=13|s6=40|a6=0|s8=0|s9=0|s10=0|s11=0|s12=0|s13=0|s14=0|s15=0',
'items' => [6447],
'effects' => [9, 32, 51, 439, 25, 37, 38, 283, 296, 412, 472],
],
7 => [
's1=14|s2=3|s3=3|s4=25|rinv=40|m9=5|m6=10|s7=0|a1=0|a2=0|a3=0|a4=0|a5=3|mg1=0|mg2=0|mg3=0|mg4=6|mg5=0|mg6=0|mg7=0|s5=13|s6=40|a6=0|s8=0|s9=0|s10=0|s11=0|s12=0|s13=0|s14=0|s15=0',
'items' => [6436],
'effects' => [9, 32, 54, 439, 25, 37, 38, 283, 296, 412, 472],
],
8 => [
's1=14|s2=3|s3=3|s4=25|rinv=40|m9=5|m6=10|s7=0|a1=0|a2=0|a3=0|a4=0|a5=3|mg1=0|mg2=0|mg3=6|mg4=0|mg5=0|mg6=0|mg7=0|s5=13|s6=40|a6=0|s8=0|s9=0|s10=0|s11=0|s12=0|s13=0|s14=0|s15=0',
'items' => [6448],
'effects' => [9, 32, 52, 439, 25, 37, 38, 283, 296, 412, 472],
],
9 => [
's1=15|s2=23|s3=30|s4=30|rinv=40|m9=5|m6=10|s7=0|a1=9|a2=0|a3=0|a4=0|a5=0|mg1=0|mg2=0|mg3=0|mg4=0|mg5=0|mg6=0|mg7=0|s5=0|s6=0',
'items' => [6530],
'effects' => [8, 20, 25, 37, 38, 283, 296, 412, 472],
],
];
private array $restrictedLogins = [
'ангел', 'angel', 'администрация', 'administration', 'Комментатор',
'Мироздатель', 'Мусорщик', 'Падальщик', 'Повелитель',
'Архивариус', 'Пересмешник', 'Волынщик', 'Лорд Разрушитель',
'Милосердие', 'Справедливость', 'Искушение', 'Вознесение',
];
2022-12-30 19:03:37 +00:00
public function hasMixedLatCur(string $txt): bool
{
2022-12-30 19:03:37 +00:00
$en = preg_match("/^(([0-9A-z -])+)$/i", $txt);
2023-01-10 16:29:32 +00:00
$ru = preg_match("/^(([0-9А-я _-])+)$/i", $txt);
2022-12-30 19:03:37 +00:00
return ($ru && $en) || (!$ru && !$en);
}
2023-01-10 16:29:32 +00:00
public function hasGoodLogin(string $login): bool
2022-12-30 19:03:37 +00:00
{
2023-01-10 16:29:32 +00:00
$isRestricted = in_array($login, $this->restrictedLogins);
2022-12-30 19:03:37 +00:00
$isRegistered = Db::getValue('select count(*) from users where login = ?', [$login]);
2023-01-10 16:29:32 +00:00
//Бывшие в употреблении логины. Дичь, но ладно.
2022-12-30 19:03:37 +00:00
$wasRegistered = Db::getValue('select count(*) from lastnames where login = ?', [$login]);
$login = str_replace(' ', ' ', $login);
if (strlen($login) > 20) {
2023-01-10 16:29:32 +00:00
$this->error = 'Логин должен содержать не более 20 символов.';
2022-12-30 19:03:37 +00:00
}
if (strlen($login) < 4) {
2023-01-10 16:29:32 +00:00
$this->error = 'Логин должен содержать не менее 4 символов.';
2022-12-30 19:03:37 +00:00
}
if ($this->hasMixedLatCur($login)) {
2023-01-10 16:29:32 +00:00
$this->error = 'В логине разрешено использовать только буквы одного алфавита русского или английского. Нельзя смешивать.';
}
2022-12-30 19:03:37 +00:00
if (substr_count($login, ' ') + substr_count($login, '-') + substr_count($login, '_') > 2) {
2023-01-10 16:29:32 +00:00
$this->error = 'Не более двух разделителей одновременно (пробел, тире, нижнее подчеркивание).';
}
2023-01-10 16:29:32 +00:00
if (!empty($this->error)) {
$this->error .= '<br>Пример правильного никнейма: Петя Убиватор, Коля, xalop.<br>Пример неправильного никнейма: )))), kolя)=-/.';
2022-12-30 19:03:37 +00:00
}
if ($isRegistered || $wasRegistered || $isRestricted) {
2023-01-10 16:29:32 +00:00
$this->error = 'Логин ' . $login . ' уже занят, выберите другой.';
2022-12-30 19:03:37 +00:00
}
2023-01-10 18:13:19 +00:00
return !$this->error;
}
2023-01-10 16:29:32 +00:00
public function hasGoodEmail(string $email): bool
{
2022-12-30 19:03:37 +00:00
$isRegistered = Db::getValue('select count(*) from users where mail = ?', [$email]);
$isBlocked = Db::getValue('select count(*) from ban_email where email = ?', [$email]);
2023-01-10 16:29:32 +00:00
$allowedDomains = Db::getColumn('select email from trust_email');
2022-12-30 19:03:37 +00:00
if (!filter_var($email, FILTER_VALIDATE_EMAIL) || $isRegistered) {
2023-01-10 16:29:32 +00:00
$this->error = 'Неправильный email.';
2022-12-30 19:03:37 +00:00
}
if ($isBlocked) {
2023-01-10 16:29:32 +00:00
$this->error = 'Адрес заблокирован.';
2022-12-30 19:03:37 +00:00
}
2023-01-10 16:29:32 +00:00
if (!in_array(explode('@', $email)[1], $allowedDomains)) {
$this->error = 'Данный почтовый сервис использовать нельзя.';
2022-12-30 19:03:37 +00:00
}
2023-01-10 18:13:19 +00:00
return !$this->error;
2022-12-30 19:03:37 +00:00
}
2023-01-10 16:29:32 +00:00
public function hasGoodEmailCode(string $email, int $code): bool
2022-12-30 19:03:37 +00:00
{
2023-01-10 16:29:32 +00:00
$check = Db::getValue('select code from secure_code where email = ? order by time desc limit 1', [$email]);
2022-12-30 19:03:37 +00:00
if ($code !== $check) {
2023-01-10 16:29:32 +00:00
$this->error = 'Неверный проверочный код.';
} else {
Db::sql('delete from secure_code where email = ?', [$email]);
2022-12-30 19:03:37 +00:00
}
2023-01-10 16:29:32 +00:00
return !$this->error;
}
2023-01-10 16:29:32 +00:00
public function hasGoodPassword(string $password1, string $password2): bool
2022-12-30 19:03:37 +00:00
{
if ($password1 !== $password2) {
2023-01-10 16:29:32 +00:00
$this->error = 'Пароли не совпадают';
}
if (strlen($password1) < 8) {
$this->error = 'Пароль должен быть больше 8 символов';
2022-12-30 19:03:37 +00:00
}
2023-01-10 18:13:19 +00:00
return !$this->error;
2022-12-30 19:03:37 +00:00
}
2023-01-10 18:13:19 +00:00
public function new(string $login, string $password, string $email, $ref, $birthday, int $sex, int $class): string
2022-12-30 19:03:37 +00:00
{
2023-01-10 18:29:27 +00:00
if (!$this->hasGoodLogin($login)) { //fixme: грязный хак, но лучше не придумывается с этой пошаговостью.
2023-01-10 16:29:32 +00:00
return 0;
}
$this->class = $class;
$ip = UserIp::get();
2023-01-10 18:13:19 +00:00
$sex -= 10; //На входе 10\11, а передать надо 0\1.
2022-12-30 19:03:37 +00:00
Db::sql(
2023-01-10 16:29:32 +00:00
'insert into users
(`real`,login,host_reg,pass,ip,ipreg,room,timereg,mail,bithday,sex,fnq,molch2,level,clan_zv,money,online)
values (1,?,?,?,?,?,9,unix_timestamp(),?,?,?,0,unix_timestamp() + 86400,8,0,200,unix_timestamp())',
2022-12-30 19:03:37 +00:00
[
2023-01-10 16:29:32 +00:00
$this->login,
2023-01-06 19:26:10 +00:00
$ref ?? '',
2023-01-10 16:29:32 +00:00
$this->password,
$ip,
$ip,
2023-01-10 16:29:32 +00:00
$this->email,
2022-12-30 19:03:37 +00:00
date('d.m.Y', strtotime($birthday)),
$sex,
2022-12-30 19:03:37 +00:00
]
);
2023-01-10 16:29:32 +00:00
$uid = Db::lastInsertId();
2023-01-10 18:13:19 +00:00
$this->login = $login;
2023-01-10 16:29:32 +00:00
$this->setCookies();
$this->setStatsItemsEffects();
User::setOnline($uid);
Db::sql('insert into online (uid, timeStart) values (?,unix_timestamp())', [$uid]);
return $uid;
}
2022-12-30 19:03:37 +00:00
2023-01-10 16:29:32 +00:00
private function setCookies()
{
setcookie('login', 1, 1, '/', Config::get('host'), true);
setcookie('registrationModal', 1, 1, '/', Config::get('host'), true);
2023-01-10 18:13:19 +00:00
setcookie('login', $this->login, strtotime('+7 days'), '/', Config::get('host'), true);
2023-01-10 16:29:32 +00:00
setcookie('registrationModal', 'true', strtotime('+7 days'), '/', Config::get('host'), true);
}
private function setStatsItemsEffects()
{
$u = User::start();
$uid = (int)Db::lastInsertId();
if ($_SESSION['ref'] === 1) {
$this->classBonuses[$this->class]['items'][] = 4811;
}
Db::sql(
'insert into stats (id,stats,ability,skills,exp) values (?,?,-75,-9,100000)',
[
$uid,
$this->classBonuses[$this->class][0],
]
);
foreach ($this->classBonuses[$this->class]['items'] as $item) {
$itemId = $u->addItem($item, $uid);
Db::sql(
'update items_users
set `1price` = (select price_1 from items_shop where items_shop.item_id = ? and sid = 1)
where id = ?',
[$item, $itemId]
);
}
$this->addEffects($uid, $this->classBonuses[$this->class]['effects']);
}
private function addEffects(int $uid, int ...$eids)
{
$in = implode(',', $eids);
$q =
"insert into eff_users (
overType,
id_eff,
uid,
name,
timeUse,
data
)
select
oneType,
id2,
$uid,
mname,
unix_timestamp(),
mdata
from
eff_main
where
id2 in ($in)";
Db::sql($q);
}
/**
* @return string
*/
public function getError(): string
{
return $this->error;
2022-12-30 19:03:37 +00:00
}
}