<?php
// класс для работы с пользователем
class user{
protected $_update = array();
protected $_data = array();

function __construct($id){
$this->_user($id);
}

protected function _user($id)
{
$this->_data['id'] = 0;
$this->_data['sex'] = 1;
if ($id===0)
{
global $dcms;
// для системных уведомлений
$this->_data['login'] = '['.$dcms->system_nick.']';
$this->_data['group'] = 6;
return true;
}


static $cache;
$id=(int)$id;


if (isset($cache[$id])){
$this->_data = $cache[$id];
}
else
{
$q=mysql_query("SELECT * FROM `users` WHERE `id` = '$id' LIMIT 1");
if (mysql_num_rows($q))$this->_data = mysql_fetch_assoc($q);
$cache[$id]=$this->_data;
}


}


protected function _is_ban()
{
static $is_ban;

if (!isset($is_ban[$this->_data['id']])){
$is_ban[$this->_data['id']]=mysql_result(mysql_query("SELECT COUNT(*) FROM `ban`
 WHERE `id_user` = '{$this->_data['id']}' AND `time_start` < '".TIME."' AND `time_end` > '".TIME."'"),0);
}

return !empty($is_ban[$this->_data['id']]);
}



public function is_friend($ank = false)
{
if (!is_object($ank))
{
global $user;
$ank=$user;
}
if ($this->_data['id'] == $ank->id)return true;
return mysql_result(mysql_query("SELECT COUNT(*) FROM `friends` WHERE `id_user` = '{$this->_data['id']}' AND `id_friend` = '$ank->id' AND `confirm` = '1' LIMIT 1"),0);
}

protected function _is_online($id_user)
{
static $online;

if (!isset($online))
{
$q = mysql_query("SELECT `id_user` FROM `users_online`");

while ($on = mysql_fetch_assoc($q)) {
	$online[$on['id_user']] = true;
}

}

return isset($online[$id_user]);
}

public function nick()
{
if (empty($this -> _data['login']))return '[Нет данных]';

$is_online = $this -> _is_online($this -> _data['id']);


return '<span class="'.($is_online?'nick_on':'nick_off').'">'.$this -> _data['login'].'</span>'.
($this -> _is_ban()?' [x]':'');
}

public function show()
{
if (!empty($this -> _data['login']))
return '<a href="/profile.view.php?id='.$this->_data['id'].'">'.$this->nick().'</a>';
else
return '[Нет данных]';
}

public function icon()
{
if (!empty($this->_data['group']) && $this->_data['group'] >= 2)
return array('size'=>'small','src'=>'/sys/images/icons/admin.'.$this->_data['sex'].'.png');

if ($this->_data)
return array('size'=>'small','src'=>'/sys/images/icons/user.'.$this->_data['sex'].'.png');


return array('size'=>'small','src'=>'/sys/images/icons/guest.png');
}


function __get($n){

global $dcms;
switch ($n) {
case 'is_ban': return $this -> _is_ban();
case 'online': return (bool)($this->_data['last_visit'] > TIME - SESSION_LIFE_TIME );
case 'group_name': return groups::name($this->_data['group']);
case 'items_per_page': return !empty($this->_data['items_per_page_'.$dcms->browser_type])?$this->_data['items_per_page_'.$dcms->browser_type]:$dcms->items_per_page;
case 'theme': return @$this->_data['theme_'.$dcms->browser_type];
case 'nick':return @$this->nick();

default:return !isset($this->_data["$n"])?false:$this->_data["$n"];
}
}

function __set($n, $v){
if (empty($this->_data['id']))return false;
global $dcms;
switch ($n) {
case 'items_per_page': $n.='_'.$dcms->browser_type; break;
case 'theme': $n.='_'.$dcms->browser_type; break;
default :break;
}

if (isset($this-> _data[$n])){
$this-> _data[$n] = $v;
$this-> _update[$n] = $v;
}
else trigger_error("Поле $n не существует");


}

// отправка сообщения (по-умолчанию от системы)
public function mess($msg,$id_user = 0)
{
if (empty($this->_data['id']))return false;
mysql_query("UPDATE `users` SET `mail_new_count` = `mail_new_count` + '1' WHERE `id` = '".$this->_data['id']."' LIMIT 1");
mysql_query("INSERT INTO `mail` (`id_user`,`id_sender`,`time`,`mess`)
VALUES ('".$this->_data['id']."', '$id_user', '".TIME."', '".my_esc($msg)."' )");
return true;
}


function __destruct(){

if ($this->_update)
{
$sql = array();
foreach ($this->_update as $key => $value) {
$sql[]="`".my_esc($key)."` = '".my_esc($value)."'";
}
mysql_query("UPDATE `users` SET ".implode(', ',$sql)." WHERE `id` = '".$this->_data['id']."' LIMIT 1");
}

}


}
?>