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


define("IN_HABBO", TRUE);

session_start();
// ##################################################################################

@include('./INC/class.config.php');
@include('../INC/class.config.php');
@include("./INC/class.functions.php");
@include("../INC/class.functions.php");

mysql_query("UPDATE users SET ip_last = '".$remote_ip."' WHERE username = '".$myrow['username']."'"); 

// Launch the installer if needed
if(empty($sqlusername) || empty($sqldb) || empty($sqlhostname)){

	header("location:install.php");
	exit;

} else {

	if(file_exists('install.php') || file_exists('upgrade.php') && $bypass_check != true){
		echo "<h1>Security Alert</h1><hr>It appears you have already executed the installation script or written your configuration file. To start using your site, for security reasons, please delete install.php and/or upgrade.php from the HoloCMS directory to proceed. If you have not yet completed installation or wish to execute it again, please <a href='install.php'>click here</a>.<hr><i>HoloCMS</i>";
		exit;
	}elseif(file_exists('check.php')){
		header("location:check.php");
	} else {

		include('INC/class.mysql.php');

	}

}
$configsql = mysql_query("SELECT * FROM site_config LIMIT 1") or die(mysql_error());
$config = mysql_fetch_assoc($configsql);
$maintenance = $config['maintenance'];

$remote_ip = $_SERVER[REMOTE_ADDR];

$H = date('H');
$i = date('i');
$s = date('s');
$m = date('m');
$d = date('d');
$Y = date('Y');
$j = date('j');
$n = date('n');
$M = date('M');
$A = date('A');
$date_normal = date('d-m-Y',mktime($m,$d,$Y));
$date_reversed = date('Y-m-d', mktime($m,$d,$y));
$date_name = getMonth($m)." ".$d.", ".$Y." ".$H.":".$i.":".$s." ".$A;
$date_full = date('d-m-Y H:i:s',mktime($H,$i,$s,$m,$d,$Y));
$date_time = date('H:i:s',mktime($H,$i,$s));
$time_compare = (time() - 501);

$s1ql = mysql_query("SELECT * FROM server_status LIMIT 1");
$r1ow = mysql_fetch_assoc($s1ql);
$online_count = $r1ow['users_online'];


function getMonth($number)
{
	switch ($number) {
	case "01":
	$return = "jan";
	break;
	case "02":
	$return = "fev";
	break;
	case "03":
	$return = "mar";
	break;
	case "04":
	$return = "avril";
	break;
	case "05":
	$return = "mai";
	break;
	case "06":
	$return = "jun";
	break;
	case "07":
	$return = "jul";
	break;
	case "08":
	$return = "aout";
	break;
	case "09":
	$return = "sep";
	break;
	case "10":
	$return = "oct";
	break;
	case "11":
	$return = "nov";
	break;
	case "12":
	$return = "dec";
	break;
	}
	return $return;
}

// ##################################################################################
if(session_is_registered('username')){

	$rawname = $_SESSION['username']; // Has slashes added and lacking proper capitals
	$rawpass = $_SESSION['password']; // HoloHash()'ed user password

	$usersql = mysql_query("SELECT * FROM users WHERE username = '".$rawname."' AND password = '".$rawpass."' LIMIT 1");
	$myrow = mysql_fetch_assoc($usersql);

	$password_correct = mysql_num_rows($usersql);

	$my_id = $myrow['id'];
	$user_rank = $myrow['rank'];

	$check = mysql_query("SELECT * FROM bans WHERE bantype = 'user' AND value = '".$myrow['username']."' LIMIT 1") or die(mysql_error());
	$is_banned = mysql_num_rows($check);

	if($password_correct !== 1){ // Invalid credentials. Possible session hijack attempt, so we log the user out.

		session_destroy();
		header("location:index.php?error=1");
		exit;

	} else if(getBanUser($myrow['username'])){

		$_SESSION['username'];
		$_SESSION['password'];
		$check = mysql_query("SELECT * FROM bans WHERE bantype = 'user' AND value = '".$myrow['username']."' LIMIT 1") or die(mysql_error());
		$is_banned = mysql_num_rows($check);

		$bandata = mysql_fetch_assoc($check);
		$login_error = "Tu est bannis, pour la raison suivante : \"".$bandata['reason']."\". Le ban expire le ".date('d F, Y', $bandata['expire']).".";
		include('logout.php');
		session_destroy();
		exit;

		} else {

			// ban expired
			mysql_query("DELETE FROM bans WHERE bantype = 'user' AND value = '".$myrow['username']."' LIMIT 1") or die(mysql_error());

		}
	

	$logged_in = true;
	$name = HabboCMSTEXT($myrow['username']);
	
	$piks = HabboCMSTEXT($myrow['piks']);
	$fonction = HabboCMSTEXT($myrow['fonction']);
	$registred = HabboCMSTEXT($myrow['account_created']);
	
	$mail = true;
	$explode = explode("@", $myrow['mail']);
	$mail = $explode[0];
        
        
        $sql_staff = mysql_query("SELECT * FROM users");
        $motto = mysql_fetch_array($sql_staff);
        $motto = $sql_staff['motto'];
        
        
	
// ######################################################################### IP STAFF
	
	$retour = mysql_query("SELECT COUNT(ip) AS acces FROM ip_staff WHERE ip = '".$remote_ip."' and pseudo = '".$name."'");
	$donnees = mysql_fetch_array($retour);
	$acces = $donnees['acces'];
	if($user_rank > 5 and $acces < "1"){
	session_destroy();
	header("location: staff_session"); 
	exit;
	}
// #########################################################################
	

} else {

	$user_rank = 0;
	$name = "Invité";
	$my_id = "HabboSSO";
	$myticket = "ST-inv-habbo-fe";
	$logged_in = false;

}

// #########################################################################

if(!session_is_registered(username) && $_COOKIE['remember'] == "remember"){

	// Get variables stored in cookies; the username and sha1 hashed password
	$cname = Filter($_COOKIE['hrusername']);
	$cpass_hash = $_COOKIE['hrpassword'];

	// Now fetch the password that belongs to this user from the database
	$csql = mysql_query("SELECT password FROM users WHERE username = '".$cname."' LIMIT 1") or die(mysql_error());
	$cnum = mysql_num_rows($csql);

		// If no results are returned (invalid username, destroy the cookie
		if($cnum < 1){
			setcookie("remember", "", time()-60*60*24*100, "/"); setcookie("cookpass", "", time()-60*60*24*100, "/");
			setcookie("hrusername", "", time()-60*60*24*100, "/"); setcookie("cookpass", "", time()-60*60*24*100, "/");
			setcookie("hrpassword", "", time()-60*60*24*100, "/"); setcookie("cookpass", "", time()-60*60*24*100, "/");
		} else {

			// We found a user, now get his password and hash it
			$crow = mysql_fetch_assoc($csql);
			$correct_pass = $crow['password'];

			// Check if the hashed database password and hash in the cookie match
			// If no, destroy the cookie. If yes, log the user in.
			if($cpass_hash == $correct_pass){
				$_SESSION['username'] = $cname;
				$_SESSION['password'] = $crow['password'];
				mysql_query("UPDATE users SET last_online = '".$date_full."', browser = '".$_SERVER['HTTP_USER_AGENT']."' WHERE username = '" . $cname . "'") or die(mysql_error());
				header("Location: security_check.php");
				exit;
			} else {
				setcookie("remember", "", time()-60*60*24*100, "/"); setcookie("cookpass", "", time()-60*60*24*100, "/");
				setcookie("hrusername", "", time()-60*60*24*100, "/"); setcookie("cookpass", "", time()-60*60*24*100, "/");
				setcookie("hrpassword", "", time()-60*60*24*100, "/"); setcookie("cookpass", "", time()-60*60*24*100, "/");
			}

		}

}

// #########################################################################

function IsUserBanned($my_id){

	$check = mysql_query("SELECT * FROM bans WHERE value = '".$myrow['username']."' LIMIT 1") or die(mysql_error());
	$is_banned = mysql_num_rows($check);

	if(!is_numeric($my_id)){ return false; }

	if($is_banned > 0){
		$bandata = mysql_fetch_assoc($check);
		$reason = $bandata['reason'];
		$expire = $bandata['expire'];

		$xbits = explode(" ", $expire);
		$xtime = explode(":", $xbits[1]);
		$xdate = explode("-", $xbits[0]);

		$stamp_now = time();
		$stamp_expire = mktime($xtime[0], $xtime[1], $xtime[2], $xdate[0], $xdate[1], $xdate[2]);

		if($stamp_now < $stamp_expire){
			return true;
		} else { // ban expired
			mysql_query("DELETE FROM bans WHERE value = '".$myrow['username']."' LIMIT 1") or die(mysql_error());
			return false;
		}
	} else {
		return false;
	}
}

	function getBanUser($name)
	{
		$sql = mysql_query("SELECT * FROM bans WHERE bantype = 'user' AND value = '".$name."' LIMIT 1");

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

		$data = mysql_fetch_assoc($sql);
		$diff = $data['expire'] - time();
		
		if ($diff <= 0)
		{
			return true;
		}
		
		return ceil($diff / 86400);
		
	}

####################################################################################

function getBanReason($my_id, $ip="", $date=false)
{
	$check = mysql_query("SELECT * FROM bans WHERE bantype = 'user' AND value = '".$myrow['username']."' LIMIT 1") or die(mysql_error());
	$is_banned = mysql_num_rows($check);

	if($is_banned > 0)
	{
		$bandata = mysql_fetch_assoc($check);
		
		if(!$date)
		{
			return $bandata['reason'];
		}
		else
		{
			return $bandata['expire'];
		}
	}
}

#############################################################################
	if(session_is_registered(username)){
	$blob = time();
	mysql_query("UPDATE users SET last_online = '".$blob."', ip_last = '".$remote_ip."', browser = '".$_SERVER['HTTP_USER_AGENT']."' WHERE id = '".$my_id."' LIMIT 1") or die(mysql_error());
	}

	if($user_rank > 5){
		if(session_is_registered(hkusername) && session_is_registered(hkpassword)){
			$rank['iAdmin'] = "1";
		} else {
			$rank['iAdmin'] = "0";
		}
	} else {
		$rank['iAdmin'] = "0";
	}

	if($maintenance == "1" && !$is_maintenance && $rank['iAdmin'] < 1){
		header("Location: maintenance");
		exit;
	} elseif($rank['iAdmin'] == 1 && $maintenance == 1){
		$notify_maintenance = true;
	}
	
	function bbcode_format($str){

		// Parse smilies
	/*		$smilies = array(":)",";)",":P",";P",":p",";p","(L)","(l)",":o",":O");
			$smilies_replace = array(
			" <img src='./web-gallery/smilies/smile.gif' alt='Smiley' title='Smiley' border='0'> ",
			" <img src='./web-gallery/smilies/wink.gif' alt='Smiley' title='Smiley' border='0'> ",
			" <img src='./web-gallery/smilies/tongue.gif' alt='Smiley' title='Smiley' border='0'> ",
			" <img src='./web-gallery/smilies/winktongue.gif' alt='Smiley' title='Smiley' border='0'> ",
			" <img src='./web-gallery/smilies/tongue.gif' alt='Smiley' title='Smiley' border='0'> ",
			" <img src='./web-gallery/smilies/winktongue.gif' alt='Smiley' title='Smiley' border='0'> ",
			" <img src='./web-gallery/smilies/heart.gif' alt='Smiley' title='Smiley' border='0'> ",
			" <img src='./web-gallery/smilies/heart.gif' alt='Smiley' title='Smiley' border='0'> ",
			" <img src='./web-gallery/smilies/shocked.gif' alt='Smiley' title='Smiley' border='0'> ",
			" <img src='./web-gallery/smilies/shocked.gif' alt='Smiley' title='Smiley' border='0'> ");
		$str = str_replace($smilies,$smilies_replace,$str);
	*/
		// Parse BB code
	        $simple_search = array(
	                                '/\[b\](.*?)\[\/b\]/is',
	                                '/\[i\](.*?)\[\/i\]/is',
	                                '/\[u\](.*?)\[\/u\]/is',
	                                '/\[s\](.*?)\[\/s\]/is',
	                                '/\[quote\](.*?)\[\/quote\]/is',
	                                '/\[link\=(.*?)\](.*?)\[\/link\]/is',
	                                '/\[url\=(.*?)\](.*?)\[\/url\]/is',
									'/\[img\](.*?)\[\/img\]/is',
									'/\[video_adm\](.*?)\[\/video_adm\]/is',
									'/\[center\](.*?)\[\/center\]/is',
	                                '/\[color\=(.*?)\](.*?)\[\/color\]/is',
	                                '/\[size=small\](.*?)\[\/size\]/is',
	                                '/\[size=large\](.*?)\[\/size\]/is',
	                                '/\[code\](.*?)\[\/code\]/is',
	                                '/\[habbo\=(.*?)\](.*?)\[\/habbo\]/is',
	                                '/\[room\=(.*?)\](.*?)\[\/room\]/is',
	                                '/\[group\=(.*?)\](.*?)\[\/group\]/is'
	                                );

	        $simple_replace = array(
	                                "<b>$1</b>",
	                                "<i>$1</i>",
	                                "<u>$1</u>",
	                                "<s>$1</s>",
	                                "<div class=\"bbcode-quote\">$1</div>",
	                                "<a href=\"$1\">$2</a>",
	                                "<a href=\"$1\">$2</a>",
	                                "<img src=\"$1\">$2</img>",
									"<iframe width=\"250\" height=\"157\" src=\"$1\" frameborder=\"0\">$2</iframe>",
									"<center>$1</center>",
	                                "<span style=\"color: $1;\">$2</span>",
	                                "<span style=\"font-size: 9px;\">$1</span>",
	                                "<span style=\"font-size: 14px;\">$1</span>",
	                                "<pre>$1</pre>",
	                                "<a href=\"./home/$1/id\">$2</a>",
	                                "<a onclick=\"roomForward(this, '$1', 'private'); return false;\" target=\"client\" href=\"./client?forwardId=2&roomId=$1\">$2</a>",
	                                "<a href=\"./groups/$1/id\">$2</a>"
	                                );

	        $str = preg_replace ($simple_search, $simple_replace, $str);
 
	        return $str;
	}
?>