<?php
if(!defined('IN_BANDTOPIA')) {
	header("Location: http://www.band-topia.com/index.php");
	die('you should not be here');
}

session_start();

// __autoload function automatically loads the php file for the requested class
// php file must have same name as the class
// example (Class: LoginObj - Filename: LoginObj.php)
function __autoload($class_name) {
	include_once CLASS_PATH . $class_name . '.php';
}

// Try loading the database
// if it fails, kill the page and display specified error
// Database server info specified in include/config.php
try {
	$mydb = mysql_connect($db_server, $db_username, $db_password);
	$bt_dbconn = mysql_select_db($db_name, $mydb);
} catch (Exception $e) {
	die("Could not connect to database<br>" . $e);
}

/*// addslashes to all POST and GET variables
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
	foreach($_POST as $key => $value)
	{
		$_POST[$key] = mysql_real_escape_string(stripslashes($value));
	}
}
*/
// Start Template Object
$template = new Template('template');
// Set template file variable for overall layout
// This is the shell surrounding the whole sha-bang
if ($_GET['modal'] == 'true')
{
	$template -> set_filenames(array("layout" => SERVER_PATH.'template/layoutModal.htm'));
}
else
{
	$template -> set_filenames(array("layout" => SERVER_PATH.'template/layout.htm'));
}

if (!isset($templateStyle))
	$templateStyle = 'main';

$template -> assign_var('TEMPLATE_STYLE', $templateStyle);

// Assigns the template footer block for use
$template -> assign_block_vars("footer", array());

/***********************************************************/
/* Action Assignment
/***********************************************************/

$action = $_GET['action'];
$prev_action = $_GET['prev_action'];

if($action == '') {
	$action = 'main';
}
if($prev_action == '') {
	$prev_action = 'main';
}

// Implement Action stuff later
//include_once(MODULE_PATH."/core/action.php");

$allowAccess = true;

// Confirms the two security session variables are set
if(isset($_SESSION['HTTP_USER_AGENT']) && isset($_SESSION['lskejd'])) {
	
	// Creates concat string to verify against
	$string = $_SERVER['HTTP_USER_AGENT'];
	$string .= 'h8er89pkd3!';
	
	// if security session variables do not match what's expected, then redirect to login
	if($_SESSION['HTTP_USER_AGENT'] != md5($_SERVER['HTTP_USER_AGENT']) || $_SESSION['lskejd'] != md5($string)) {
		header("Location: index.php?action=login&cause=invalid&prev_action={$action}");
	}
}

// if access is not allowed for the current action, then redirect
if(!allowAccess) {
	header("Location: index.php?action=login&cause=nosession&prev_action={$action}");
	die("Error on page");
}

include(MODULE_PATH."core/login.php");

$template -> assign_vars(array('ACTION' => $action, 'PREV_ACTION' => $prev_action));

if($_SESSION['username'] != '') {
	define('LOGGEDIN', true);
	$template -> assign_block_vars("footer.profile", array());
	/**********************************************************/
	/* Register User Security Constants
	 /**********************************************************/
	define('SESSION_USER', $_SESSION['UserName']);
	define('SESSION_USERID', $_SESSION['UserId']);
	define('SESSION_USER_FULLNAME', $_SESSION['UserFullName']);
	define('SESSION_USERTYPEID', $_SESSION['UserTypeId']);
	define('SESSION_USER_TYPE_TITLE', $_SESSION['user_type_title']);
	if(SESSION_USER_TYPE_TITLE == 'artist') {
		define('ARTIST_ID', $_SESSION['user_info_id']);
	} elseif(SESSION_USER_TYPE_TITLE == 'staff') {
		define('STAFF_ID', $_SESSION['user_info_id']);
	}
	foreach($_SESSION['secy_cmpt'] AS $key => $value) {
		$admin = true;
	}

	define('ADMIN', $admin);
	if($admin == true) {
		$template -> assign_block_vars("footer.admin", array());
		define('ADMIN_FULL', $_SESSION['secy_cmpt'][1]);
		define('ADMIN_AGENT', $_SESSION['secy_cmpt'][2]);
		define('ADMIN_ARTIST', $_SESSION['secy_cmpt'][3]);
		define('ADMIN_SALES', $_SESSION['secy_cmpt'][12]);
		define('ADMIN_SECYCMPT', $_SESSION['secy_cmpt'][13]);
		define('ADMIN_RADIO', $_SESSION['secy_cmpt'][14]);
		define('ADMIN_VENUE', $_SESSION['secy_cmpt'][15]);
		define('ADMIN_STAFF', $_SESSION['secy_cmpt'][16]);
		define('ADMIN_USER', $_SESSION['secy_cmpt'][17]);
	}

	$template -> assign_vars(array(
		'SESSION_USER' => $_SESSION['username'],
		'SESSION_USERID' => $_SESSION['UserId'],
		'SESSION_USER_FULLNAME' => SESSION_USER_FULLNAME));
}
$template -> assign_var('SITE_PATH', $site_path);
$template -> assign_var('ACTION', $action);

function truncate($string, $length, $end_char = '.') {
	// Set the truncation tail
	//$truncTail = ' [<a href="' . $fullurl . '">Read More</a>]';
	$truncTail = ' . .';

	// If string exceeds the truncation length
	if(strlen($string) > $length) {

		// Output infoBox with output set
		$end = $length - strlen(strip_tags($truncTail));
		while($string[$end] != $end_char)
			$end--;
		return substr_replace($string, $truncTail, ++$end);

	} else {

		// Return string as is
		return $string;

	}
}

/*function truncate($string, $length, $fullurl, $end_char=' ') {

 // Set the truncation tail
 //$truncTail = ' [<a href="' . $fullurl . '">Read More</a>]';

 // If string exceeds the truncation length
 if (strlen($string) > $length) {

 // Output infoBox with output set
 $end = $length - strlen(strip_tags($truncTail));
 while($string[$end]!=$end_char) $end--;
 return substr_replace($string, $truncTail, ++$end);

 } else {

 // Return string as is
 return $string;

 }

 }*/
?>
