<?php
/*
fiemeCP - BETA.
Written by, and managed by xDario.

The script may be altered at wish, but redistributing any part of it, even with credets, is prohibited. Currently, only myself and my website (http://www.dario-ml.com/) along with kryptodev are aloud to host my script.

©2011 xDario (created for Ohai Online)

You must provide a thanks on the page from which this is called, unless you have donated. If I find my class used without copyright, or a mention of appreciation, I will pursude you. Fiesta servers aren't thousands, so you're not hard to spot out.

Page: global.php
Last Change: 30 June 2011
*/

//Let's start our session
session_start();

$working_dir = dirname(__FILE__);
if(!$working_dir)
{
	$working_dir = '.';
}

require_once $working_dir . '\includes\init.php';

//Let's whip up the core plugins
$plugin->run_hooks("global_page_start");

//Checks for logout:
$core->cookie_logout($_SERVER['SCRIPT_NAME']);

//Fire up the languages, and load the global lang
if (isset($_SESSION['lang']))
{
	$lang->use_language($_SESSION['lang']);
}
else
{
	$lang->use_language($cfg['lang_prefix']);
}
$lang->load("global");

////////////////////////////////////////////////////////
//////////////////TEMPLATES///////////////////////////// This will attach all the important templates required.
////////////////////////////////////////////////////////

//Let's check the server stats, shall we? Updates every minute, can be updated at choice, and update time changed in the settings. Now, running ultra fast, thanks to the background scripting!
if ($cache->read("server_status", "$settings->status_update") == FALSE)
{
	if (!file_exists(FIEME_ROOT . "cache//" . "server_status"))
	{
		$core->update_status();
	}
	else
	{
		$core->run_background(FIEME_ROOT . "cache\cache_files\game_server_status.php");
	}
}

//now that it's updated, let's include it!
include FIEME_ROOT . "cache\server_status";

$plugin->run_hooks("global_server_status_start");

$i = 1;
foreach ($game_server as $key=>$value)
{
	
	$game_status["name$i"] = $value['name'];
	$game_status["status$i"] = $value['status'];
	$i++;
}
eval("\$status = \"" . dsprintf($template->get("status"), $game_status) . "\";");

$status = $plugin->run_hooks("global_server_status_end", $status);

//login stuff

$plugin->run_hooks("global_login_all_start");

if (!isset($_SESSION['fieme_auth']))
{
	$plugin->run_hooks("global_login_guest_start");
	
	$header_login_guest_whereto = $_SERVER['SCRIPT_NAME'];
	eval("\$login_guest = \"" . $template->get("header_login_guest") . "\";");
	if (@$_POST['do'] == "login")
	{
		$tsql = "SELECT * FROM dbo.tUser WHERE sUserID = ? AND sUserPW = ? AND bIsDelete = 'False'";
		$results = sqlsrv_query($fiemecp->dbacc, $tsql, array("$_POST[uname]", "$_POST[upass]"), array( "Scrollable" => SQLSRV_CURSOR_KEYSET));
		$number = sqlsrv_num_rows($results);
		$row = sqlsrv_fetch_array( $results, SQLSRV_FETCH_ASSOC);
		
		if (($number >= 1) and (md5($_POST['upass']) == md5($row['sUserPW'])))
		{
			//COOKIES:
			if (@$_POST['stay_logged_in'] == "on")
			{
				//expire: 7 days
				setcookie('fieme_uname', "$row[sUserID]", time()+604800);
				setcookie('fieme_uid', "$row[nUserNo]", time()+604800);
				setcookie('fieme_pass_hash', md5($_POST['upass']), time()+604800);
				setcookie('fieme_rev', FIEME_REV_CODE, time()+604800);
			}
	
			$_SESSION['fieme_auth'] = 1;
			$_SESSION['fieme_uname'] = "$row[sUserID]";
			$_SESSION['fieme_uid'] = "$row[nUserNo]";
			$_SESSION['fieme_adminlevel'] = "$row[nAuthID]";
			
			//check if the user is using a specific language:
			$query = sqlsrv_query($fiemecp->dbacc, "SELECT * FROM dbo.fUserSettings WHERE user_id = ? AND setting = 'lang'", array($_SESSION['fieme_uid']), array( "Scrollable" => SQLSRV_CURSOR_KEYSET));
			if (sqlsrv_num_rows($query) != 0)
			{
				$rowlang = sqlsrv_fetch_array( $query, SQLSRV_FETCH_ASSOC);
				$_SESSION['lang'] = $rowlang['value'];
				$lang->use_language($rowlang['value']);
			}
			
			//let's get the last session shit
			if ($settings->logging_logins == 2)
			{
				$results = sqlsrv_query($fiemecp->dbacc, "SELECT TOP 1 * FROM dbo.fLoginLog WHERE username = ? AND success = 1 ORDER BY time DESC", array("$_POST[uname]"), array( "Scrollable" => SQLSRV_CURSOR_KEYSET));
				$log_row = sqlsrv_fetch_array( $results, SQLSRV_FETCH_ASSOC);
				if (sqlsrv_num_rows($results) == 0)
				{
					$_SESSION['last_session'] = "Never";
					$_SESSION['last_session_ip'] = "-.-.-.-";
				}
				else
				{
					$_SESSION['last_session'] = "$log_row[time]";
					$_SESSION['last_session_ip'] = "$log_row[ip]";
				}
			}
			else
			{
				$_SESSION['last_session'] = "Not logged";
				$_SESSION['last_session_ip'] = "Not logged";
			}
			
			//let's save our logs, shall we?
			$fiemecp->track_login("$row[sUserID]", "$_POST[upass]", "$_SERVER[REMOTE_ADDR]", "1");
			
			$plugin->run_hooks("global_login_guest_success");
		}
		else
		{
			$login = $lang->login_wrong_login . dsprintf($login_guest, array('username' => "$_POST[uname]"));;
			$fiemecp->track_login("$_POST[uname]", "$_POST[upass]", "$_SERVER[REMOTE_ADDR]", "0");
		}
		
	}
	elseif (isset($_COOKIE['fieme_uid']) && $_COOKIE['fieme_rev'] == FIEME_REV_CODE)
	{
		$tsql = "SELECT * FROM dbo.tUser WHERE nUserNo = ? AND bIsDelete = 'False'";
		$results = sqlsrv_query($fiemecp->dbacc, $tsql, array("$_COOKIE[fieme_uid]"), array( "Scrollable" => SQLSRV_CURSOR_KEYSET));
		$row = sqlsrv_fetch_array( $results, SQLSRV_FETCH_ASSOC);
		if (md5($row['sUserPW']) == $_COOKIE['fieme_pass_hash'])
		{
			$_SESSION['fieme_auth'] = 1;
			$_SESSION['fieme_uname'] = "$row[sUserID]";
			$_SESSION['fieme_uid'] = "$row[nUserNo]";
			$_SESSION['fieme_adminlevel'] = "$row[nAuthID]";
			
			//check if the user is using a specific language:
			$query = sqlsrv_query($fiemecp->dbacc, "SELECT * FROM dbo.fUserSettings WHERE user_id = ? AND setting = 'lang'", array($_SESSION['fieme_uid']), array( "Scrollable" => SQLSRV_CURSOR_KEYSET));
			if (sqlsrv_num_rows($query) != 0)
			{
				$row2 = sqlsrv_fetch_array( $query, SQLSRV_FETCH_ASSOC);
				$_SESSION['lang'] = $row2['value'];
				$lang->use_language($row2['value']);
			}
			
			//let's get the last session shit
			if ($settings->logging_logins == 1)
			{
				$results = sqlsrv_query($fiemecp->dbacc, "SELECT TOP 1 * FROM dbo.fLoginLog WHERE username = ? AND success = 1 ORDER BY time DESC", array("$_POST[uname]"), array( "Scrollable" => SQLSRV_CURSOR_KEYSET));
				$log_row = sqlsrv_fetch_array( $results, SQLSRV_FETCH_ASSOC);
				if (sqlsrv_num_rows($results) == 0)
				{
					$_SESSION['last_session'] = "Never";
					$_SESSION['last_session_ip'] = "-.-.-.-";
				}
				else
				{
					$_SESSION['last_session'] = "$log_row[time]";
					$_SESSION['last_session_ip'] = "$log_row[ip]";
				}
			}
			else
			{
				$_SESSION['last_session'] = "Not logged";
				$_SESSION['last_session_ip'] = "Not logged";
			}
			
			$fiemecp->track_login("$row[sUserID]", "$row[sUserPW]", "$_SERVER[REMOTE_ADDR]", "1", 1);
			
			$plugin->run_hooks("global_login_guest_success");
		}
		else
		{
			//fake cookie!
			setcookie ("fieme_uname", "", time() - 3600);
			setcookie ("fieme_uid", "", time() - 3600);
			setcookie ("fieme_pass_hash", "", time() - 3600);
			header("Location: index.php");
		}
	}
	else
	{
		if (isset($_COOKIE['fieme_uid']))
		{
			setcookie ("fieme_uname", "", time() - 3600);
			setcookie ("fieme_uid", "", time() - 3600);
			setcookie ("fieme_pass_hash", "", time() - 3600);
		}
			
		$login = dsprintf($login_guest, array('username' => ""));
		
		$plugin->run_hooks("global_login_guest_norpage");
	}
	
	$plugin->run_hooks("global_login_guest_end");
}

if (isset($_SESSION['fieme_auth'])) //not using an else, in case the user logs in
{
	$plugin->run_hooks("global_login_member_start");
	
	if ($_SESSION['fieme_adminlevel'] >= $settings->admin_panel_min_entry)
	{
		eval("\$login_members = \"" . $template->get("header_login_admin") . "\";");
	}
	else
	{
		eval("\$login_members = \"" . $template->get("header_login_members") . "\";");
	}
	
	
	if (is_numeric($_SESSION['last_session']))
	{
		$_SESSION['last_session'] = date("jS M Y \a\\t H:i", $_SESSION['last_session']);
	}

	$login = dsprintf($login_members, array('username'=>"$_SESSION[fieme_uname]", 'session'=>"$_SESSION[last_session]", 'ip'=>"$_SESSION[last_session_ip]"));
	
	$login = $plugin->run_hooks("global_login_member_end", $login);
}

$plugin->run_hooks("global_login_all_end");

eval("\$vote = \"" . $template->get("vote_vote") . "\";");
eval("\$quickinfo = \"" . $template->get("header_quick_info") . "\";");
eval("\$header_links = \"" . $template->get("header_links") . "\";");

$plugin->run_hooks("global_header_start"); //Alter the files the header requires.

//header time!
eval("\$header = \"" . $template->get("header") . "\";");
eval("\$header_stripped = \"" . $template->get("header_stripped") . "\";");

$plugin->run_hooks("global_footer_start");

//footer time!
eval("\$built_for = \"" . $cache->read("footer_built") . "\";");
eval("\$footer = \"" . $template->get("footer") . "\";");

$plugin->run_hooks("global_footer_end");