<?
session_start();
require_once('classes/body.class.php');
require_once('classes/db.class.php');
require_once('classes/system.class.php');
$title= new Body;
$title->SetTitle(homeurl.' - Авторизация');
$title->GetHeader();
if ($b == true){
header('location:/');
die();
}
//echo'<div class="row"><div class="col-sm-6">';
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i><a href="/"style="text-decoration:none; color:white;"> Главная</a> :: Авторизация</div></div></div>';
if (isset($_POST['save'])) {
$err = [];
$pass = check($_POST['pass']);
$user_nick = check($_POST['user_nick']);
if (empty($user_nick) OR empty($pass)) {
$err[] = 'Не введен E-mail/Логин или Пароль!';
}
if (mb_strlen($user_nick)>30 || mb_strlen($user_nick)<2) {
$err[] = 'Слишком длинный или короткий логин/E-mail!';
}
if (strlen($pass)>15 OR strlen($pass)<4) {
$err[] = 'Слишком длинный или короткий пароль!';
}
if (!preg_match('!^[A-z0-9]+$!i',$pass)){
$err[] = 'В пароле обнаружены запрещенные знаки!';
}
if (DB::$dbs->querySingle("SELECT COUNT(id) FROM `blogger` WHERE `email` = ?", [$user_nick])==1) {
$sql = DB::$dbs->queryFetch("SELECT * FROM `blogger` WHERE `email` = ? LIMIT 1", [$user_nick]);
} elseif (DB::$dbs->querySingle("SELECT COUNT(id) FROM `blogger` WHERE `nick` = ?", [$user_nick])==1) {
$sql = DB::$dbs->queryFetch("SELECT * FROM `blogger` WHERE `nick` = ? LIMIT 1", [$user_nick]);
}
$arr = DB::$dbs->queryFetch("SELECT * FROM `blogger` WHERE `id` = ? LIMIT 1",array($sql['id']));
if ($arr == 0) {
$err[] = 'Данного пользователя не существует!';
}
$password = sha1($pass.'gearnet');
if ($arr['pass'] != $password) {
$err[] = 'Не верно введен пароль.';
if ($arr['id'] != 0) DB::$dbs->query("INSERT INTO `auth_log` SET `user_id` = ?, `ip` = ?, `soft` = ?, `time` = ?, `type` = ?", [$arr['id'],check($_SERVER['REMOTE_ADDR']),check($_SERVER['HTTP_USER_AGENT']),time(),2]);
}
if (empty($err)) {
setcookie('id', $arr['id'], time()+3600*24*365, '/');
setcookie('pass', $arr['pass'], time()+3600*24*365, '/');
if ($arr['id'] != 0) DB::$dbs->query("INSERT INTO `auth_log` SET `user_id` = ?, `ip` = ?, `soft` = ?, `time` = ?, `type` = ?", [$arr['id'],check($_SERVER['REMOTE_ADDR']),check($_SERVER['HTTP_USER_AGENT']),time(),1]);
header('location: /');
die();
} else {
echo '<div class="alert alert-danger">';
foreach($err as $error) {
echo '<b>'.$error.'</b><br/>';
}
echo '</div>';
}
}
echo '<div class="list-group-item media"><div class="media-body"><div class="list-group-item-heading"><form method="post" action="?">
Логин либо E-mail:<br/><div class="form-group"><input class="form-control"name="user_nick" type="text"/></div>
Пароль:<br/><div class="form-group"><input class="form-control"name="pass" type="password"/><div/>
<div class="checkbox"><label><input type="checkbox" name="store_login" value="1">Запомнить меня</label></div>
<input class="btn btn-primary pull-left" value="Войти в аккаунт" name="save" type="submit"/></form></div>
<a class="btn btn-primary pull-left" href="/join">Регистрация</a><a class="btn btn-primary pull-left" href="/recovery">Восстановление пароля</a></div></div></div></div>';


///Подключаем меню
$title->GetMenu();

///Подключаем футер
$title->GetFooter();
?>