* @link    		http://www.pioder.pl/
* @license	see LICENSE.txt
**/
if ( !defined('IN_uF') )
{
	die('Hacking attempt');
}
$db;
class DataBase
{
	function db_connect()
	{
		global $db;
		
		$db = new mysqli(DB_HOST, DB_USER, DB_PASS);
		if (mysqli_connect_errno() != 0)
		{
			message_die(CRITICAL,' Could not connect to database server.',$db->error);
		}
		
		if (!$db->set_charset("UTF8"))
	 	{
			message_die(CRITICAL, 'Could not set character set UTF-8', $db->error);
		}
		if (!$db->select_db(DB_NAME))
		{
			message_die(CRITICAL,' Could not select database.', $db->error); 
		}
	}
	
	function sql_query($sql, $type, $msg, $no_count = false)
	{
		global $db;
		
		if (isset($_COOKIE['queries']))
		{
			$no_count = false;
			if(!$no_count)
			{
				$_COOKIE['queries'] = $_COOKIE['queries']+1;
			}
		}
		//echo ''.$sql.'
'; //for optimizing only
		if (!$query = $db->query($sql))
		{
			message_die($type, $msg, $db->error);
		}
		
		return $query;
	}
	
	function fetch($query)
	{	
   		return $query->fetch_assoc();
	}
	
	function num_rows($query)
	{
		if ($query===true || $query===false)
		{
			return 0;
		}
		else
		{
    			return mysqli_num_rows($query);
		}
	}
	
	function new_id($table)
	{
		global $db;
		
		//check table id
		switch($table)
		{
    			case BANLIST_TABLE: { $id = 'b_id'; break; }
    			case CATS_TABLE: { $id = 'c_id'; break; }
    			case CENSORLIST_TABLE: { $id = 'w_id'; break; }
    			case FORUMS_TABLE: { $id = 'f_id'; break; }
    			case GROUPS_TABLE: { $id = 'g_id'; break; }
    			case PM_INBOX_TABLE: { $id = 'm_id'; break; }
    			case PM_SENTBOX_TABLE: { $id = 'm_id'; break; }
    			case POSTS_TABLE: { $id = 'p_id'; break; }
    			case TOPICS_TABLE: { $id = 't_id'; break; }
			case SESSIONS_TABLE: { $id = 's_id'; break; }
    			case SKINS_TABLE: { $id = 's_id'; break; }
    			case SMILES_TABLE: { $id = 's_id'; break; }
    			case USERS_TABLE: { $id = 'u_id'; break; }
    			case USERS_GROUP_TABLE: { $id = 'id'; break; }
    			case WARNINGS_TABLE: { $id = 'w_id'; break; }
 		}
 		$sql = "SELECT `$id` FROM $table ORDER BY `$id` DESC LIMIT 1";
 		$return_id = DataBase::fetch(DataBase::sql_query($sql,GENERAL,'Error with obtain last id.
 File: db.php, Line: '.__LINE__));
 		$return_id = $return_id[$id];
 		$return_id = $return_id+1;
 		return $return_id;
 		unset($table, $return_id, $id, $sql);
	}
}
?>