
<?php
#######################################################|#
#|                    HabboMCMS - TheRox
#|                    Powered by HoloCMS 
#|            Copyright 2011-2012 ALL RIGHTS RESERVED
#######################################################|#

	function Filter($var)
	{
	$var = mysql_real_escape_string(htmlspecialchars(nl2br(trim($var))));
		return $var;
	}
        
        function HabboCMSTEXT($str, $advanced=false) {
		$str = stripslashes($str);
		if($advanced != true){ $str = htmlspecialchars($str,ENT_COMPAT,"UTF-8"); }
		return $str;
        }
	
 
	function HASHS($str) 
	{ 
		$config_hash = "xCg532%@%gdvf^5DGaa6&*rFTfg^FD4\$OIFThrR_gh(ugf*/"; 
		$str = mysql_real_escape_string(htmlspecialchars(stripslashes(nl2br(trim(sha1($str . $config_hash))))));;
		return $str; 
	} 
	
	function mysql_evaluate($query, $default_value="undefined") 
	{
		$result = mysql_query($query) or die(mysql_error());

		if(mysql_num_rows($result) < 1){
		return $default_value;
		} else {
		return mysql_result($result, 0);
		}
	}
	function GenerateRandom($type = "sso", $length = 0)
	{
		switch($type)
		{
			case "sso":
				$data = GenerateRandom("random",8)."-".GenerateRandom("random",4)."-".GenerateRandom("random",4)."-".GenerateRandom("random",4)."-".GenerateRandom("random",12);
				return $data;
			break; 
			case "app_key":
				$data = strtoupper(GenerateRandom("random",32)).".resin-fe-".GenerateRandom("random_number",1);
				return $data;
			break; 
			case "random":
				$data = "";
				$possible = "0123456789abcdef"; 
				$i = 0;
				while ($i < $length) { 
					$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
					$data .= $char;
					$i++;
				}
				return $data;
			break; 
			case "random_number":
				$data = "";
				$possible = "0123456789"; 
				$i = 0;
				while ($i < $length) { 
					$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
					$data .= $char;
					$i++;
				}
				return $data;
			break;
		}
	}

	function UpdateSSO($my_id)
	{
			$myticket = GenerateRandom();
			mysql_query("UPDATE users SET auth_ticket = '".$myticket."' WHERE id = '".$my_id."' LIMIT 1") or die(mysql_error());
			return $myticket;
		}
		
			function getHC($id)
		{
			$sql = mysql_query("SELECT * FROM user_subscriptions WHERE subscription_id = 'habbo_club' AND user_id = '".$id."' LIMIT 1");

			if (mysql_num_rows($sql) == 0)
			{
				return 0;
			}

			$data = mysql_fetch_assoc($sql);
			$diff = $data['timestamp_expire'] - time();
			
			if ($diff <= 0)
			{
				return 0;
			}
			
			return ceil($diff / 86400);
			
	}
		function getVIP($id)
	{
		$sql = mysql_query("SELECT * FROM user_subscriptions WHERE subscription_id = 'habbo_vip' AND user_id = '".$id."' LIMIT 1");

		if (mysql_num_rows($sql) == 0)
		{
			return 0;
		}
		
		$data = mysql_fetch_assoc($sql);
		$diff = $data['timestamp_expire'] - time();
		
		if ($diff <= 0)
		{
			return 0;
		}
		
		return ceil($diff / 86400);
		
	}
	
		function getClubDays($id)
	{
		$sql = mysql_query("SELECT timestamp_activated, timestamp_expire FROM user_subscriptions WHERE user_id = '".$id."' LIMIT 1");

		if (mysql_num_rows($sql) == 0)
		{
			return 0;
		}
		
		$data = mysql_fetch_assoc($sql);
		$diff = $data['timestamp_expire'] - time();
		
		if ($diff <= 0)
		{
			return 0;
		}
		
		return ceil($diff / 86400);
	}
	
	function IsEven($intNumber)
	{
		if($intNumber % 2 == 0){
			return true;
		} else {
			return false;
		}
	}

	function showfriendsOnline($my_id, $show_names = false)
	{

		$get_myfriends = mysql_query("SELECT * FROM messenger_friendships WHERE user_two_id = '".$my_id."'") or die(mysql_error());
		$time_compare = (time() - 501);
		$counter = 0;
		$users = "";
		
		while ($row = mysql_fetch_array($get_myfriends)){
		
		if($row['user_two_id'] == $my_id){
		$get_friend = mysql_query("SELECT username FROM users WHERE id = '".$row['user_one_id']."' AND online = '1' LIMIT 1");
		} else {
		$get_friend = mysql_query("SELECT username FROM users WHERE id = '".$row['user_two_id']."' AND online = '1' LIMIT 1");
		}
		
		$row2 = mysql_fetch_assoc($get_friend);	
		$friend_exist = mysql_num_rows($get_friend);
		
		if($friend_exist > 0){
		$counter++;
		$users = $users."<a href=\"./user_profile?name=".$row2['username']."\">".$row2['username']."</a>, ";
		}
		
		}
	  
	    if($show_names){
		return $users;
		} else {
		return $counter;
		}
	}

	function showfriendsOffline($my_id)
	{

		$get_myfriends = mysql_query("SELECT * FROM messenger_friendships WHERE (user_two_id = '".$my_id."')") or die(mysql_error());
		$time_compare = (time() - 501);
		$counter = 0;
		
		while ($row = mysql_fetch_array($get_myfriends)){
		if($row['user_one_id'] == $my_id){
		$get_friend = mysql_query("SELECT username FROM users WHERE id = '".$row['user_two_id']."' AND online = '0' LIMIT 1");
		} else {
		$get_friend = mysql_query("SELECT username FROM users WHERE id = '".$row['user_one_id']."' AND online = '0' LIMIT 1");
		}
		
		$row2 = mysql_fetch_assoc($get_friend);	
		$friend_exist = mysql_num_rows($get_friend);
		
		if($friend_exist > 0){
		$counter++;
		}
	  }
	  
		return $counter;
	}
	
	function IsUserOnline($intUID){
	$result = mysql_query("SELECT online FROM users WHERE id = '".$intUID."' LIMIT 1") or die(mysql_error());
	$timeout = 600; // 10 minutes ?

	if(mysql_num_rows($result) < 1){
		return false;
	} else {
		$result = mysql_fetch_array($result);
		$result = $result[0];
		$result = $result + $timeout;
		if($result >= time()){
			return true;
		} else {
			return false;
		}
	}
}
	
	function getContent($strKey){

	$tmp = mysql_query("SELECT contentvalue FROM site_content WHERE contentkey = '".Filter($strKey)."' LIMIT 1") or die(mysql_error());
	$tmp = mysql_fetch_assoc($tmp);
	return $tmp['contentvalue'];
	}
	
	
	function GetUserGroupBadge($my_id){
	$check = mysql_query("SELECT groupid FROM groups_memberships WHERE userid = '".$my_id."' AND is_current = '1' LIMIT 1") or die(mysql_error());
	$has_badge = mysql_num_rows($check);

	if($has_badge > 0){

		$row = mysql_fetch_assoc($check);
		$groupid = $row['groupid'];

		$check = mysql_query("SELECT badge FROM groups_details WHERE id = '".$groupid."' LIMIT 1") or die(mysql_error());

		$row = mysql_fetch_assoc($check);
		$badge = $row['badge'];

		return $badge;

	} else {

		return false;

	}
}
	
	function GetUserBadge($strName){ // supports user IDs also

	if(is_numeric($strName)){
		$check = mysql_query("SELECT id FROM users WHERE id = '".$strName."' AND badge_status = '1' LIMIT 1") or die(mysql_error());
	} else {
		$check = mysql_query("SELECT id FROM users WHERE username = '".Filter($strName)."' AND badge_status = '1' LIMIT 1") or die(mysql_error());
	}

	$exists = mysql_num_rows($check);

		if($exists > 0){
			$usrrow = mysql_fetch_assoc($check);
			$check = mysql_query("SELECT * FROM user_badges WHERE user_id = '".$usrrow['id']."' AND badge_slot = '1' LIMIT 1") or die(mysql_error());
			$hasbadge = mysql_num_rows($check);
			if($hasbadge > 0){
				$badgerow = mysql_fetch_assoc($check);
				return $badgerow['badge_id'];
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
		function GetUserGroup($my_id){
		$check = mysql_query("SELECT groupid FROM groups_memberships WHERE userid = '".$my_id."' AND is_current = '1' LIMIT 1") or die(mysql_error());
		$has_fave = mysql_num_rows($check);

		if($has_fave > 0){

			$row = mysql_fetch_assoc($check);
			$groupid = $row['groupid'];

			return $groupid;

		} else {

			return false;

		}
}

	function charset_decode_utf_8($string) {
	      /* Only do the slow convert if there are 8-bit characters */
	    /* avoid using 0xA0 (\240) in ereg ranges. RH73 does not like that */
	    if (! ereg("[\200-\237]", $string) and ! ereg("[\241-\377]", $string))
	        return $string;

	    // decode three byte unicode characters
	    $string = preg_replace("/([\340-\357])([\200-\277])([\200-\277])/e",       
	    "'&#'.((ord('\\1')-224)*4096 + (ord('\\2')-128)*64 + (ord('\\3')-128)).';'",   
	    $string);

	    // decode two byte unicode characters
	    $string = preg_replace("/([\300-\337])([\200-\277])/e",
	    "'&#'.((ord('\\1')-192)*64+(ord('\\2')-128)).';'",
	    $string);

	    return $string;
	}

	function GetBrowser($user_agent) 
	{
	     $navegadores = array(
	          'Opera' => 'Opera',
	          'Mozilla Firefox'=> '(Firebird)|(Firefox)',
	          'Galeon' => 'Galeon',
	          'Mozilla'=>'Gecko',
	          'MyIE'=>'MyIE',
	          'Lynx' => 'Lynx',
	          'Netscape' => '(Mozilla/4\.75)|(Netscape6)|(Mozilla/4\.08)|(Mozilla/4\.5)|(Mozilla/4\.6)|(Mozilla/4\.79)',
	          'Konqueror'=>'Konqueror',
			  'Internet Explorer 9' => '(MSIE 9\.[0-9]+)',
			  'Internet Explorer 8' => '(MSIE 8\.[0-9]+)',
	          'Internet Explorer 7' => '(MSIE 7\.[0-9]+)',
	          'Internet Explorer 6' => '(MSIE 6\.[0-9]+)',
	          'Internet Explorer 5' => '(MSIE 5\.[0-9]+)',
	          'Internet Explorer 4' => '(MSIE 4\.[0-9]+)',
	);

		foreach($navegadores as $navegador=>$pattern)
		{
			if (eregi($pattern, $user_agent))
			{
				return $navegador;
			}
	    }
		
		return 'Deconnexion';
	}

// ##################################################################################
?>