<?php

if (!empty($_POST)) {
	$sql = "SELECT * FROM users WHERE user_active = 1 AND (user_name = '{$_POST['username']}' OR user_mail = '{$_POST['username']}') AND password = '".md5($_POST['password'])."'";

	$o = new Db();
	$user = $o->select($sql,true);
}
if (isset($user[0]) && isset($user[0]['user_id']) && is_numeric($user[0]['user_id'])) {
	$ip = $_SERVER['REMOTE_ADDR'];
	$sql = "UPDATE users SET last_login_ip = '0' WHERE user_id = {$user[0]['user_id']}";
	$oDb->query($sql);
	$sql = "UPDATE users SET last_login_ip = '{$ip}' WHERE user_id = {$user[0]['user_id']}";
	$oDb->query($sql);
	
	setcookie("login",$user[0]['user_id'],time()+3600,"/");

	$arr_data = array(
		'time' 	=> time(),
		'uid'	=> $user[0]['user_id'],
		'browser' =>$_SERVER['HTTP_USER_AGENT'],
		'ip'	=> $_SERVER['SERVER_ADDR']
		
	);
	$db->log('login',json_encode($arr_data));
	header("location: /");
	
}

$no_page_wrapper = true;

?>