<?php

################################################
# EVILCMS FLATFILE RECODE - 100% FUNCTIONALITY #
################################################

Session_Start();

Class EvilCMS
{
	Public $DB = "";
	Public $DCR = array();
	Public $Conf = array();
	Public $Details = array();
	Public $UserFiles = array("app", "badgeonoff", "badges", "birth", "consolemission", "credits", "curbadge", "email", "favourites", "figure", "film", "friendlist", "habbobirth", "hand", "hcdays", "hcupdate", "inquiries", "lastonline", "mission", "name", "num", "pass", "periods", "poolfigure", "rank", "roomlist", "rooms", "sex", "showexpire", "tickets", "transactions", "directmail/count");
	Public $User = array();
	Public $SessionName = "EVILCMS_SESSID";
	Public $LoggedIn = False;
	
	Function Error($exception)
	{
		echo "<b>An Error Occured in EvilCMS (".$this->Details['BUILD']."):</b> <span style='color: red;'>" . $exception . "</span>";
		exit();
	}
	
	Function __Construct($DB, $DCR, $Conf, $Details)
	{
		$this->DB = $DB;
		$this->Conf['HOST'] = $Conf[0];
		$this->Conf['PORT'] = $Conf[1];
		$this->Conf['WWW'] = $Conf[2];
		$this->Conf['NEWSFILE'] = $Conf[3];
		$this->Conf['USE_SSO'] = $Conf[4];
		If($Conf[4] == 1): $this->UserFiles[count($this->UserFiles) + 1] = "sso/ticket"; endif;
		$this->DCR['BASE'] = $DCR[0];
		$this->DCR['DCR'] = $DCR[1];
		$this->DCR['EXT_TEXT'] = $DCR[2];
		$this->DCR['EXT_VARS'] = $DCR[3];
		$this->Details['HOTEL_NAME'] = $Details[0];
		$this->Details['IN_MAINTNENCE'] = $Details[1];
		$this->Details['BUILD'] = "BUILD".$Details[2]."_".md5($Details[2])."_1";
		$this->Details['HOTEL_VERSION'] = $Details[2];
		$this->Details['USE_CHAT'] = $Details[3];
	}
	
	Function Contains($Needle, $Haystack)
	{
		return (str_replace($Needle, $Needle, $Haystack)) ? 1 : 0;
	}
	
	Function Redirect($page, $time)
	{
		Echo "<meta http-equiv=\"refresh\" content=\"".$time.";url=".$page."\">";
	}
	
	Function printLoader($Ticket = 0)
	{
		$PresetLoader = '<embed src="%DCR%" bgColor="#000000" width="720" height="540" align="middle" swRemote=" swSaveEnabled=\'true\' swVolume=\'true\' swRestart=\'false\' swPausePlay=\'false\' swFastForward=\'false\' swTitle=\'massivewake\' swContextMenu=\'true\'" swStretchStyle="none" swText="" type="application/x-director" pluginspage="http://www.macromedia.com/shockwave/download/" '.(($this->Conf['USE_SSO'] == 1) ? 'sw7="use.sso.ticket=1;sso.ticket=%TICKET%" ' : '').'sw6="external.texts.txt=%EXT_TEXT%" sw2="connection.info.host=%HOST%;connection.info.port=%PORT%" sw4="connection.mus.host=;connection.mus.port=30000" sw3="client.reload.url=%RELOAD%" sw1="site.url=%RELOAD%;url.prefix=%RELOAD%" sw5="external.variables.txt=%EXT_VAR%"></embed>';
		return str_ireplace("%DCR%", $this->DCR['DCR'], str_ireplace("%EXT_TEXT%", $this->DCR['EXT_TEXT'], str_ireplace("%EXT_VAR%", $this->DCR['EXT_VARS'], str_ireplace("%RELOAD%", $this->Conf['WWW']."/ClientError.php", str_ireplace("%HOST%", $this->Conf['HOST'], str_ireplace("%PORT%", $this->Conf['PORT'], str_ireplace("%TICKET%", $Ticket, $PresetLoader)))))));
	}
	
	Function User_Exist($Username)
	{
		return (file_exists($this->DB."habbos/".$Username)) ? 1 : 0;
	}
	
	Function User_GetValue($Username, $Variable)
	{
		return File_Get_Contents($this->DB."habbos/".$Username."/".$Variable.".txt");
	}
	
	Function User_SetValue($Username, $Variable, $Value)
	{
		$FileHandler = FOpen($this->DB."habbos/".strtolower($Username)."/".$Variable.".txt", "w");
		FWrite($FileHandler, $Value);
		FClose($FileHandler);
		Return 1;
	}
	
	Function User_Load($Username)
	{
		ForEach($this->UserFiles as $Key) {
			$this->User[$Key] = $this->User_GetValue($Username, $Key);
		}
		Return $this->User;
	}
	
	Function User_GetBan($Username)
	{
		return (file_exists($this->DB."user_bans/".$Username.".txt") && file_exists($this->DB."user_bans/".$Username.".reason")) ? 1 : 0;
	}
	
	Function User_hasBadge($Username, $badgeCode)
	{
		$readBadgeFile = Explode(";", $this->User_GetValue($Username, "badges"));
		return (in_array($badgeCode, $readBadgeFile)) ? 1 : 0;
	}
	
	Function User_getBadges($Username)
	{
		$readBadgeFile = Explode(";", $this->User_GetValue($Username, "badges"));
		$badgeAr = array();
		$badgeCount = 0;
		Foreach($readBadgeFile as $badgeCode) {
			If($badgeCode != "") {
				$badgeAr[$badgeCount] = $badgeCode;
				$badgeCount++;
			}
		}
		Return (count($badgeAr) >= 1) ? $badgeAr : 0;
	}
	
	Function User_SSO()
	{
		return md5("EvilCMS-".$this->Details['BUILD']); // As long as it returns more than 10 alphanumeric values then it is valid.
	}

	Function Login($Username, $Password)
	{
		If($this->User_Exist($Username)) {
			If($this->User_GetValue($Username, "pass") == $Password) {
				if($this->Details['USE_SSO']): $this->User_SetValue($Username, "sso/ticket", $this->User_SSO()); Endif;
				$_SESSION[$this->SessionName] = $Username;
				Return 1;
			}else{
				Return 0;
			}
		}else{
			Return 0;
		}
	}
	
	Function Articles_Load($Id)
	{
		$Articles = Parse_Ini_File($this->Conf['NEWSFILE'], True);
		$Article = array();
		If(Isset($Articles[$Id]["Title"])) {
			$Article['Title'] = $Articles[$Id]["Title"];
			$Article['Poster'] = $Articles[$Id]["Poster"];
			$Article['Content'] = str_ireplace("%USERNAME%", $this->User['name'], str_ireplace("%HOTEL_NAME%", $this->Details['HOTEL_NAME'], $Articles[$Id]["Content"]));
			$Article['Category'] = $Articles[$Id]["Category"];
			$Article['Type'] = $Articles[$Id]["Type"];
			$Article['Date'] = $Articles[$Id]["Date"];
			Return $Article;
		}else{
			Return 0;
		}
	}
	
	Function Articles_List()
	{
		$Articles = Parse_Ini_File($this->Conf['NEWSFILE'], True);
		$Count = 0;
		If(Count($Articles) == 0) {
			Echo "There are no articles written.";
		}else{
			While($Count < count($Articles)) {
				Echo "[".$Articles[$Count]["Type"]."] <a href='?p=news&id=".$Count."'>".$Articles[$Count]["Title"]."</a><br />";
				$Count++;
			}
		}
	}

	Function Prechecks()
	{
		If(Isset($_SESSION[$this->SessionName]) && $this->User_Exist($_SESSION[$this->SessionName])) {
			$this->LoggedIn = True;
		}
	}
	
	Function Server_GetStarterValue($Variable)
	{
		Return file_get_contents($this->DB."new_habbo/".$Variable.".txt");
	}
	
	Function Server_SetStarterValue($Variable, $Value)
	{
		$FileHandler = FOpen($this->DB."new_habbo/".$Variable.".txt", "w");
		FWrite($FileHandler, $Value);
		FClose($FileHandler);
		Return 1;
	}
	
	Function printAllUsers($formName)
	{
		$ret = "<select name=\"".$formName."\">\n";
		if ($handle = opendir($this->DB."habbos/")) {
			while (false !== ($file = readdir($handle))) {
				if ($file != "." && $file != ".." && $file != "count.txt") {
					$ret .= "<option value=\"".$file."\">".$this->User_GetValue($file, "name")."</option>\n";
				}
			}
		closedir($handle);
		}
		$ret .= "</select>\n";
		return $ret;
	}
	
	Function printSettings($formName)
	{
		$ret = "<select name=\"".$formName."\">\n";
		$ret .= "<option value=\"credits\">Credits</option>\n";
		$ret .= "<option value=\"mission\">Mission</option>\n";
		$ret .= "<option value=\"consolemission\">Console Mission</option>\n";
		$ret .= "<option value=\"email\">Email</option>\n";
		$ret .= "<option value=\"tickets\">Tickets</option>\n";
		$ret .= "<option value=\"film\">Films</option>\n";
		$ret .= "<option value=\"hcdays\">HC Days</option>\n";
		$ret .= "<option value=\"rank\">Rank</option>\n";
		$ret .= "<option value=\"badges\">Badges</option>\n";
		$ret .= "<option value=\"hand\">Hand</option>\n";
		$ret .= "</select>\n";
		return $ret;
	}
	
	Function Register($Username, $Password, $Email)
	{
		MkDir($this->DB."habbos/".$Username."/");
		MkDir($this->DB."habbos/".$Username."/directmail/");
		Foreach($this->UserFiles as $File) {
			If(file_exists($this->DB."new_habbo/".$File.".txt")) {
				$this->User_SetValue($Username, $File, $this->Server_GetStarterValue($File));
			}
		}
		
		$k = file_get_contents($this->DB."habbos/count.txt");
		$fileHandler22 = fopen($this->DB."habbos/count.txt", "w") or die("error");
		fwrite($fileHandler22, $k+1) or die("error");
		fclose($fileHandler22);
		
		$this->User_SetValue($Username, "name", $Username);
		$this->User_SetValue($Username, "pass", $Password);
		$this->User_SetValue($Username, "email", $Email);
		$this->User_SetValue($Username, "transactions", "");
		$this->User_SetValue($Username, "habbobirth", 0);
		$this->User_SetValue($Username, "hcupdate", 0);
		$this->User_SetValue($Username, "num", $k+1);
		If($this->Details['USE_SSO'] == 1): $this->User_SetValue($Username, "sso/ticket", $this->User_SSO()); Endif;
		$this->Login($Username, $Password);
		$this->Redirect($CMS->Conf['WWW']."/Index.php", 0);
		Return 1;
	}
	
	Function Users_GetByRank($Rank)
	{
		$ret = array();
		if ($handle = opendir($this->DB."habbos/")) {
			while (false !== ($file = readdir($handle))) {
				if ($file != "." && $file != ".." && $file != "count.txt") {
					If($this->User_GetValue($file, "rank") == $Rank) {
						$t = count($ret) + 1;
						$ret[$t] = $file;
					}
				}
			}
		closedir($handle);
		}
		Return $ret;
	}
}

?>