<?
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="container-fluid page_header"><div class="container"><div class="row"><div class="col-xs-12 col-sm-8 col-md-9 col-lg-9"><h1>Авторизация </h1><ol class="breadcrumb" xmlns:v="http://'.homeurl.'/#"><li typeof="v:Breadcrumb"><a href="/" rel="v:url" property="v:title">'.homeurl.'</a></li><li class="active" typeof="v:Breadcrumb"><span property="v:title">Авторизация </span></li></ol></div><div class="col-xs-12 col-sm-4 col-md-3 col-lg-3 text-right page_header_sidebar"></div></div></div></div>';
echo'<div class="container page_body"><div class="row"><div class="col-xs-12 page_content col-lg-9"><div class="navbar page_content_navbar collapse hidden-lg"><div class="container-fluid"><div class="navbar-collapse"></div></div></div><div class="text_box hidden-lg pr_block_small hidden"></div><div class="container-fluid cols_table">';

echo '<div class="catSides"><a href="/"><i class="md-format-list-bulleted"></i> Главная </a> - Авторизация</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="error">';
foreach($err as $error) {
echo '<b>'.$error.'</b><br/>';
}
echo '</div>';
}
}
echo '<div class="row text_box"><div class="pull-right text-muted top_right_box"></div><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 href="/join" class="hidden-xs">Не зарегистрированы?</a><span class="text-muted divider hidden-xs">&bull;</span><a href="/recovery">Забыли пароль?</a></div></div>';


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

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