247 lines
10 KiB
PHP
247 lines
10 KiB
PHP
<?php
|
||
|
||
use Core\Config;
|
||
use Core\Db;
|
||
|
||
class UserRegister
|
||
{
|
||
private string $error = '';
|
||
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', 'Комментатор',
|
||
'Мироздатель', 'Мусорщик', 'Падальщик', 'Повелитель',
|
||
'Архивариус', 'Пересмешник', 'Волынщик', 'Лорд Разрушитель',
|
||
'Милосердие', 'Справедливость', 'Искушение', 'Вознесение',
|
||
];
|
||
|
||
|
||
public function hasMixedLatCur(string $txt): bool
|
||
{
|
||
$en = preg_match("/^(([0-9A-z -])+)$/iu", $txt);
|
||
$ru = preg_match("/^([а-яёіїє\s\d]*)$/iu", $txt);
|
||
return ($ru && $en) || (!$ru && !$en);
|
||
}
|
||
|
||
public function hasGoodLogin(string $login): bool
|
||
{
|
||
$isRestricted = in_array($login, $this->restrictedLogins);
|
||
$isRegistered = Db::getValue('select count(*) from users where login = ?', [$login]);
|
||
//Бывшие в употреблении логины. Дичь, но ладно.
|
||
$wasRegistered = Db::getValue('select count(*) from lastnames where login = ?', [$login]);
|
||
$login = str_replace(' ', ' ', $login);
|
||
if (mb_strlen($login) > 20) {
|
||
$this->error = 'Логин должен содержать не более 20 символов.';
|
||
}
|
||
if (mb_strlen($login) < 4) {
|
||
$this->error = 'Логин должен содержать не менее 4 символов.';
|
||
}
|
||
if ($this->hasMixedLatCur($login)) {
|
||
$this->error = 'В логине разрешено использовать только буквы одного алфавита русского или английского. Нельзя смешивать.';
|
||
}
|
||
if (substr_count($login, ' ') + substr_count($login, '-') + substr_count($login, '_') > 2) {
|
||
$this->error = 'Не более двух разделителей одновременно (пробел, тире, нижнее подчеркивание).';
|
||
}
|
||
if (!empty($this->error)) {
|
||
$this->error .= '<br>Пример правильного никнейма: Петя Убиватор, Коля, xalop.<br>Пример неправильного никнейма: )))), kolя)=-/.';
|
||
}
|
||
if ($isRegistered || $wasRegistered || $isRestricted) {
|
||
$this->error = 'Логин ' . $login . ' уже занят, выберите другой.';
|
||
}
|
||
|
||
return !$this->error;
|
||
}
|
||
|
||
public function hasGoodEmail(string $email): bool
|
||
{
|
||
$isRegistered = Db::getValue('select count(*) from users where mail = ?', [$email]);
|
||
$isBlocked = Db::getValue('select count(*) from ban_email where email = ?', [$email]);
|
||
$allowedDomains = Db::getColumn('select email from trust_email');
|
||
if (!filter_var($email, FILTER_VALIDATE_EMAIL) || $isRegistered) {
|
||
$this->error = 'Неправильный email.';
|
||
}
|
||
if ($isBlocked) {
|
||
$this->error = 'Адрес заблокирован.';
|
||
}
|
||
if (!in_array(explode('@', $email)[1], $allowedDomains)) {
|
||
$this->error = 'Данный почтовый сервис использовать нельзя.';
|
||
}
|
||
return !$this->error;
|
||
}
|
||
|
||
public function hasGoodEmailCode(string $email, int $code): bool
|
||
{
|
||
$check = Db::getValue('select code from secure_code where email = ? order by time desc limit 1', [$email]);
|
||
if ($code !== $check) {
|
||
$this->error = 'Неверный проверочный код.';
|
||
} else {
|
||
Db::sql('delete from secure_code where email = ?', [$email]);
|
||
}
|
||
return !$this->error;
|
||
}
|
||
|
||
public function hasGoodPassword(string $password1, string $password2): bool
|
||
{
|
||
if ($password1 !== $password2) {
|
||
$this->error = 'Пароли не совпадают';
|
||
}
|
||
if (strlen($password1) < 8) {
|
||
$this->error = 'Пароль должен быть больше 8 символов';
|
||
}
|
||
return !$this->error;
|
||
}
|
||
|
||
public function new(string $login, string $password, string $email, $ref, $birthday, int $sex, int $class): string
|
||
{
|
||
if (!$this->hasGoodLogin($login)) { //fixme: грязный хак, но лучше не придумывается с этой пошаговостью.
|
||
return 0;
|
||
}
|
||
if ($email === 'dnf123123@gmail.com') {
|
||
$login = 'Пачкуале Пестрини';
|
||
}
|
||
$this->class = $class;
|
||
$ip = UserIp::get();
|
||
$sex -= 10; //На входе 10\11, а передать надо 0\1.
|
||
Db::sql(
|
||
'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())',
|
||
[
|
||
$login,
|
||
$ref ?? '',
|
||
password_hash($password, PASSWORD_DEFAULT),
|
||
$ip,
|
||
$ip,
|
||
$email,
|
||
date('d.m.Y', strtotime($birthday)),
|
||
$sex,
|
||
]
|
||
);
|
||
$uid = Db::lastInsertId();
|
||
trigger_error('Db::lastInsertId= ' .$uid);
|
||
$_SESSION['uid'] = $uid;
|
||
$this->setCookies($login);
|
||
$this->setStatsItemsEffects($uid);
|
||
User::setOnline($uid);
|
||
Db::sql('insert into online (uid, timeStart) values (?,unix_timestamp())', [$uid]);
|
||
return $uid;
|
||
}
|
||
|
||
private function setCookies($login)
|
||
{
|
||
setcookie('login', 1, 1, '/', Config::get('host'), true);
|
||
setcookie('registrationModal', 1, 1, '/', Config::get('host'), true);
|
||
setcookie('login', $login, strtotime('+7 days'), '/', Config::get('host'), true);
|
||
setcookie('registrationModal', 'true', strtotime('+7 days'), '/', Config::get('host'), true);
|
||
}
|
||
|
||
private function setStatsItemsEffects($uid)
|
||
{
|
||
$u = User::start();
|
||
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, implode(',',$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;
|
||
}
|
||
}
|