Файл: app/join/index.php
Строк: 39
<?php
class join extends Controller
{
function main()
{
if ($this->user->isAuth())
{
exit(header("location: /"));
}
$this->tpl->title = 'Регистрация';
$this->tpl->err = '';
if (filter_has_var(INPUT_POST, 'submit'))
{
$filter = [
'login' => trim(filter_input(INPUT_POST, 'login', FILTER_UNSAFE_RAW)),
'pass1' => trim(filter_input(INPUT_POST, 'pass1', FILTER_UNSAFE_RAW)),
'pass2' => trim(filter_input(INPUT_POST, 'pass2', FILTER_UNSAFE_RAW)),
'captcha' => trim(filter_input(INPUT_POST, 'captcha', FILTER_SANITIZE_NUMBER_INT))
];
if (mb_strlen ($filter['login']) < 2 || mb_strlen ($filter['login']) > 32)
{
$this->tpl->err = 'Неверный формат логина.';
}
elseif (preg_match ('/[^A-Za-z0-9-]/', $filter['login']))
{
$this->tpl->err = 'Логин должен иметь только латинские буквы и цифры.';
}
elseif (DB::run()->query("SELECT COUNT(`id`) FROM `users` WHERE `login` = '" . $filter['login'] . "'")->fetchColumn() != 0)
{
$this->tpl->err = 'Данный логин уже зарегистрирован.';
}
elseif (mb_strlen ($filter['pass1']) < 6)
{
$this->tpl->err = 'Короткий пароль. Минимум 6 символов.';
}
elseif ($filter['pass1'] != $filter['pass2'])
{
$this->tpl->err = 'Пароли не совпадают.';
}
elseif ($filter['captcha'] !== @$_SESSION['captcha'])
{
$this->tpl->err = 'Неверный проверочный код.';
}
else
{
$token = md5(mt_rand(10000, 99999));
$stmt = DB::run()->prepare('INSERT INTO `users` (`login`, `password`, `token`) VALUES (:login, :password, :token) ');
$stmt->execute([
':login' => $filter['login'],
':password' => $filter['pass1'],
':token' => $token,
]);
setcookie('token', $token, time() + 60 * 60 * 24 * 31 * 365, '/');
exit(header('location: /'));
}
}
$this->tpl->run();
}
}