function?auth_user?()?{
????global?
$db;

????
//?Get?vars
????
$username?=?r('username');
????
$password?=?r('password');
????
$lastactive?=?r('lastactive');

????if?(empty(
$username)?OR?empty($password)?OR?empty($lastactive))?{
????????
return_error('Invalid?user.?Please?logout?and?try?again.',?10);
????????die();
????}

????
//?Authenticate?user
????
$user?=?$db->GetRow("SELECT?userid,?username,?password,?lastactive?FROM?user?WHERE?username?=???AND?password?=??",?array($username,?$password));

????if?(
$user?==?false)?{
????????
return_error('Invalid?user.?Please?logout?and?try?again.',?10);
????????die();
????}

????return?
$user;
}

function?
send_userlist()?{
????global?
$db;

????
//?Remove?any?inactive?users?(no?ping?for?15?seconds)
????
remove_inactive_users();

????
//?Get?userlist
????
$userlist?=?$db->GetAll("SELECT?userid,?username?FROM?`user`");

????
//?Queue?command
????
json_queue_add?('add_userlist',?array('userlist'?=>?$userlist));

????return?
true;
}

function?
remove_inactive_users()?{
????global?
$db;

????
//?Remove?any?inactive?users?(no?ping?for?15?seconds)
????
$inactive?=?time()-15;
????
$db->execute?("DELETE?FROM?`user`?WHERE?lastactive?,?array($inactive));
}

function?
update_lastactive($user)?{
????global?
$db;

????
$lastactive?=?time();

????
//?Update?user
????
$db->execute?("UPDATE?`user`?SET?lastactive?=???WHERE?userid?=??",?array($lastactive,?$user['userid']));

????
//?Send?update?command
????
json_queue_add?('update_lastactive',?array('lastactive'?=>?$lastactive));

????return?
true;
}


?>