* @link    		http://www.pioder.pl/
* @license	see LICENSE.txt
**/
if ( !defined('IN_uF') )
{
	die('Hacking attempt');
}
class User
{
	function LastUser()
	{
		global $lastuser;
		$usr = $lastuser['nick'];
		$uid = $lastuser['u_id'];
  		return ''.$usr.'';
	}
	
	function UserInformation($uid, $inf)
	{
		$sql = "SELECT `$inf` FROM ".USERS_TABLE."  WHERE u_id='$uid';";
		$query = DataBase::sql_query($sql,GENERAL,'Could not obtain user information.');
		$result = DataBase::fetch($query);
		$result = $result[$inf];
		return $result;
	}
	
	function UserIdByNick($nick)
	{
		$sql = "SELECT * FROM `".USERS_TABLE."`  WHERE `nick`='$nick';";
		$result = DataBase::fetch(DataBase::sql_query($sql,GENERAL,'Could not obtain user information.'));
		$result = $result['u_id'];
		/*if ($result=='')
		{
    			message_forum('nick failed','admin_groups.php');
 		}*/
		return $result;
	}
	
	function AddToGroup($uid, $gid)
	{
		$last= DataBase::new_id(USERS_GROUP_TABLE);
		$sql = "INSERT INTO `".USERS_GROUP_TABLE."` VALUES ('$last','$uid', '$gid')";
		DataBase::sql_query($sql,GENERAL,'Could not add user to group.');
	}
	
	function DeleteFromGroup($uid, $gid)
	{
		$sql = "DELETE FROM `".USERS_GROUP_TABLE."` WHERE `u_id`='$uid' AND `g_id`='$gid'";
		DataBase::sql_query($sql,GENERAL,'Could not delete user for group.');
	}
		
	function LogedAs($sid, $uid)
	{
		global $lng;
		global $userdata;
		if ($uid>0)
		{
			$nick = $userdata['nick'];
			return(''.$lng['youareloggedas'].' '.$nick.'');
		}
		else
		{
			return($lng['youarenotlogd']);
		}
	}
	
	function PostWithForum($posts)
	{
		$result2 = TotalPosts();
		if($result2>0)
  		{
   			$result3 = ($posts/$result2*100);
   			$result3 = round($result3,2);
  			return($result3);
		}
		else
		{
 			return(0);
 		}
		
	}
	
	function LastRegVisit($uid, $mode)
	{
		global $lng;	
		$result = User::UserInformation($uid,$mode);
		if ($result=='0') 
		{
			return($lng['never']); 
		}	
		else
		{
			$date = date('d-m-Y, G:i',$result);
			return($date);
		}
	}
	
	function UserRank($rank)
	{
		global $lng;
		global $forum_config;	
		switch($rank)
		{
    			case '0': {$result=$lng['user']; break; }
    			case '1': {$result=''.$lng['mod'].''; break; }
    			case '2': {$result=''.$lng['admin'].''; break; }
		}
		return($result);
	}
	
	function RankAdminMod($uid)
	{
		if ($uid>0)
		{
			$sql = "SELECT * FROM ".USERS_TABLE." WHERE u_id='$uid'";
			$query = DataBase::sql_query($sql,GENERAL,'Could not obtain user`s rank information.');
			$result = DataBase::fetch($query);
			$rank = $result['rank'];
			if (($rank=='1') or ($rank=='2'))
			{
				return '1';
			}
			else
			{
				return '0';
			}
 		}
 		else
 		{
			return '0'; 
		}
	}
	
	function UpdateProfile($uid, $gg, $email, $interests, $sig, $avatar, $allow_qr, $allow_email, $allow_gg, $skin, $lang, $limit_tpid, $limit_ftid, $limit_users, $allow_shoutbox)
	{
		$sql ="UPDATE `".USERS_TABLE."` SET 
		`gg` = '$gg',
		`email` = '$email',
		`allow_gg` = '$allow_gg',
		`allow_email` = '$allow_email',
		`allow_qr` = '$allow_qr',
		`interests` = '$interests',
		`sig` = '$sig',
		`avatar` = '$avatar',
		`skin`='$skin',
		`lang`='$lang',
		`limit_tpid` = '$limit_tpid',
		`limit_ftid` = '$limit_ftid',
		`view_shoutbox` = '$allow_shoutbox',
		`limit_users` = '$limit_users'
		WHERE `u_id` ='$uid' LIMIT 1 ;";
		DataBase::sql_query($sql,CRITICAL,'Could not update user information');
	}
	
	function UpdateAdminPools($uid, $posts, $rank, $active, $nick)
	{
		$sql ="UPDATE `".USERS_TABLE."` SET 
		`posts` = '$posts',
		`rank` = '$rank',
		`active` = '$active',
		`nick` = '$nick'
		WHERE `u_id` ='$uid' LIMIT 1 ;";
		DataBase::sql_query($sql,CRITICAL,'Could not update user information');
	}
	
	function UpdatePassword($uid, $pass)
	{
		$sql ="UPDATE `".USERS_TABLE."` SET 
		`pass` = '$pass'
		WHERE `u_id` ='$uid' LIMIT 1 ;";
		DataBase::sql_query($sql,CRITICAL,'Could not update user information');
	}
	
	function CreateProfile($nick, $pass, $email, $gg, $allow_gg, $allow_email, $allow_qr, $sig, $av, $interests)
	{
  		global $forum_config;
		$last = DataBase::new_id(USERS_TABLE);
  		$time = time();
  		$sql = "INSERT INTO ".USERS_TABLE."  VALUES 
		('$last', '$nick', '$pass', '$email', '0', '$time', '0', '$gg', '$allow_gg', '$allow_email', '$allow_qr', '".$forum_config['view_shoutbox']."', '".$forum_config['defaultskin']."', '".$forum_config['defaultlang']."','".$forum_config['limit_tpid']."', '".$forum_config['limit_ftid']."', '".$forum_config['limit_users']."', '$sig', '$av', '1','0','$interests');";
  		DataBase::sql_query($sql,CRITICAL,'Could not create new user');
	}
	
	function UserPMs()
	{
		$uid = $_SESSION['uid'];
		if($uid>0)
		{
			$sql = "SELECT count(*) as `m_id` FROM ".PM_INBOX_TABLE." WHERE `u_id`='$uid' AND `read`='0'";
			$result = DataBase::fetch(DataBase::sql_query($sql,GENERAL,'Could not obtain amounts PM of User.'));
			$result = $result['m_id'];
			if ($result>0)
			{
				return ''.$result.'';
			}
			else
			{
				return $result;
			}
		}
	}
	
	function UserAddWarn($uid, $value, $motive)
	{
		global $lng;
		$sql = "INSERT INTO `".WARNINGS_TABLE."` ( `w_id` , `u_id` , `value` , `motive` ) VALUES('','$uid','$value','$motive');";
		DataBase::sql_query($sql,GENERAL,'Could not add new warn.');
		if (User::UserLevelWarns($uid)==100)
		{
    			$ban_ip = '0.0.0.0';
			$ban_uid = $uid;	
			$ban_motive = $lng['warns_ban'];
 			$sql = "INSERT INTO ".BANLIST_TABLE." VALUES ('', '$ban_uid',  '$ban_ip', '$ban_motive')";
			DataBase::sql_query($sql,GENERAL,'Could not update add ban.');		
		}
	}
	
	function UserLevelWarns($uid)
	{
		$level =0;
		$sql = "SELECT `u_id`,`value` FROM `".WARNINGS_TABLE."` WHERE `u_id`='$uid'";
		$query = DataBase::sql_query($sql,'GENERAL','Could not obtain user warns information');
		$result = DataBase::num_rows($query);
		while ($result = @DataBase::fetch($query))
		{
    			if ($result['value']=='-')
    			{
	        		$level = $level -1;
 			}
 			else
 			{
	        		$level = $level +1;
 			}
		}
		//version 1.0 Alpha 2
		//delete second query
		if ($level>0)
		{
    			$level = $level*10;
  		}
		return $level;
	}
}
?>