<?php define('WEARNINGS', 1); 
define('H', $_SERVER['DOCUMENT_ROOT'] . '/');
foreach (array('starting', 'compress', 'worldcms', 'function', 'account', 'encryption') as $kernel){
	require_once H.'kernel/' . $kernel . '.php'; }

$show_all=true; // показ для всех
$input_page=true;

only_unreg();
if (isset($_GET['id']) && isset($_GET['pass']))
{
	if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` = '".intval($_GET['id'])."' AND `pass` = '".shif($_GET['pass'])."' LIMIT 1"), 0)==1)
	{
		$user = get_user($_GET['id']);
		$_SESSION['id_user'] = $user['id'];

		mysql_query("UPDATE `user` SET `date_aut` = ".time()." WHERE `id` = '$user[id]' LIMIT 1");
		mysql_query("UPDATE `user` SET `date_last` = ".time()." WHERE `id` = '$user[id]' LIMIT 1");
	}
	else $_SESSION['err'] = 'Неправильный логин или пароль';
}
elseif (isset($_POST['nick']) && isset($_POST['pass']))
{
	if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `nick` = '".my_esc($_POST['nick'])."' AND `pass` = '".shif($_POST['pass'])."' LIMIT 1"), 0))
	{
		$user = mysql_fetch_assoc(mysql_query("SELECT `id` FROM `user` WHERE `nick` = '".my_esc($_POST['nick'])."' AND `pass` = '".shif($_POST['pass'])."' LIMIT 1"));
		$_SESSION['id_user'] = $user['id'];
		$user = get_user($user['id']);
		
		// сохранение данных в COOKIE
		if (isset($_POST['aut_save']) && $_POST['aut_save'])
		{
			setcookie('id_user', $user['id'], time()+60*60*24*365);
			setcookie('pass', cookie_encrypt($_POST['pass'],$user['id']), time()+60*60*24*365);
		}

		mysql_query("UPDATE `user` SET `date_aut` = '$time', `date_last` = '$time' WHERE `id` = '$user[id]' LIMIT 1");
	}
	else $_SESSION['err'] = 'Неправильный логин или пароль';
}
elseif (isset($_COOKIE['id_user']) && isset($_COOKIE['pass']) && $_COOKIE['id_user'] && $_COOKIE['pass'])
{
	if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `id` = ".intval($_COOKIE['id_user'])." AND `pass` = '".shif(cookie_decrypt($_COOKIE['pass'],intval($_COOKIE['id_user'])))."' LIMIT 1"), 0)==1)
	{
		$user = get_user($_COOKIE['id_user']);
		$_SESSION['id_user'] = $user['id'];
		mysql_query("UPDATE `user` SET `date_aut` = '$time', `date_last` = '$time' WHERE `id` = '$user[id]' LIMIT 1");
		$user['type_input'] = 'cookie';
	}
	else
	{
		$_SESSION['err'] = 'Ошибка авторизации по COOKIE';
		setcookie('id_user');
		setcookie('pass');
	}
}
else $_SESSION['err'] = 'Ошибка авторизации';


if (!isset($user))
{
	header('Location: /module/sign-in/');
	exit;
}


// Пишем ip пользователя

if (isset($ip2['add']))mysql_query("UPDATE `user` SET `ip` = ".ip2long($ip2['add'])." WHERE `id` = '$user[id]' LIMIT 1");
else mysql_query("UPDATE `user` SET `ip` = null WHERE `id` = '$user[id]' LIMIT 1");
if (isset($ip2['cl']))mysql_query("UPDATE `user` SET `ip_cl` = ".ip2long($ip2['cl'])." WHERE `id` = '$user[id]' LIMIT 1");
else mysql_query("UPDATE `user` SET `ip_cl` = null WHERE `id` = '$user[id]' LIMIT 1");
if (isset($ip2['xff']))mysql_query("UPDATE `user` SET `ip_xff` = ".ip2long($ip2['xff'])." WHERE `id` = '$user[id]' LIMIT 1");
else mysql_query("UPDATE `user` SET `ip_xff` = null WHERE `id` = '$user[id]' LIMIT 1");
if ($ua)mysql_query("UPDATE `user` SET `ua` = '".my_esc($ua)."' WHERE `id` = '$user[id]' LIMIT 1");

// Непонятная сессия
mysql_query("UPDATE `user` SET `sess` = '$sess' WHERE `id` = '$user[id]' LIMIT 1");

if (isset($_GET['return']))
header('Location: '.urldecode($_GET['return']));
else header("Location: /index.php".SID);
exit;
?>