<?php
	ob_start();
	
	$cupath = realpath(dirname(__FILE__).'/../');
	define('ROOT_PATH',$cupath);

	if(!is_file(ROOT_PATH . '/includes/include/config.php')) {
		
		include_once(ROOT_PATH . '/installer/productconfig/product.config.php');
		header('Location: '.$CONFIG['wampurl'].'/installer/install.php');
		exit ();
	}
	else{

		function getmicrotime(){ 
			list($usec, $sec) = explode(" ",microtime()); 
		return ((float)$usec + (float)$sec); 
		}
		$PageStartTime = getmicrotime();
		$Version = 3.0;
		$VerDate = "2009-01-01";
		ini_set ("session.gc_maxlifetime", "360");//180 mean 3min
		//ini_set('session.cookie_lifetime', "60");
		
		define("BACKEND","../");
		require_once(BACKEND . "includes/include/config.inc.php");
		session_name (APPNAME . "_backend");
		session_start();
		ob_start("ob_gzhandler");
		define("APPLICATIONINFO", "etamine.com");
		
		require_once(CLASSPATH . "dropdown.inc.php");
		require_once(CLASSPATH . "functions.inc.php");		
		require_once(CLASSPATH . "etamine.com/class.etamine.com.php");
		
		
		//Include display related files.
		if (!$_SESSION['ETAMINEFRAMEWORK']['DISPLAY']['BACKEND_SKINS']) {$_SESSION['ETAMINEFRAMEWORK']['DISPLAY'] = getTheme($_SESSION['userid'],$_SESSION['groupid']);}
		define("SKINS","../skins/" . $_SESSION['ETAMINEFRAMEWORK']['DISPLAY']['BACKEND_SKINS'] . "/");
		define("LANGUAGE",$_SESSION['ETAMINEFRAMEWORK']['DISPLAY']['BACKEND_LANGUAGE']);			//Default Language
		define("PAGELENGTH",$_SESSION['ETAMINEFRAMEWORK']['DISPLAY']['BACKEND_PAGELENGTH']);		
		
		
		if (!@include_once($GLOBALS['skins'] . "files/colorscheme.php")) {exit ("Error - Theme Files not Found");}
		require_once(CLASSPATH . "language/". LANGUAGE . ".php");
		
		//echo SKINS;
		//Global definition for Themes for backward compatability
		$GLOBALS['skins'] = SKINS;
		
		$GLOBALS['javascript'] = "../js/";
		
		//Initialising the classes -- Moved from switch from previous version
		//Following classes would be intialised so that initialiation is not required.
		$etamineParser = new etamineParser();
		$listbox = new ListBox();
		$db = new etamineDb();
		$class_etamine_com->UseClass("settings");
		$etamineSettings = new etamineSettings();
		
		//Check whether mail settings data available in the session otherwise get from database
		if (! $_SESSION['ETAMINEFRAMEWORK']['MAILHOST']) {		
			$db->SetResultType(MYSQL_NUM);
			$MailValues = $db->DbGetMultipleRecords("select parameter, settingvalue from tblsetting left join tblsettingvalue on tblsetting.settingid = tblsettingvalue.settingid where tblsetting.smodule='Mail' and (parameter='mailhost' or parameter='mailprotocol' or parameter='smtpusername' or parameter='smtppassword')","FirstVal");
			$_SESSION['ETAMINEFRAMEWORK'] = array_change_key_case($MailValues, CASE_UPPER);
		}
		
		$GLOBALS['mailhost']		=	$_SESSION['ETAMINEFRAMEWORK']['MAILHOST']; // Multiple host can be entered seperated by comma
		$GLOBALS['mailprotocol']	=	$_SESSION['ETAMINEFRAMEWORK']['MAILPROTOCOL']; 
		$GLOBALS['smtpusername']	=	$_SESSION['ETAMINEFRAMEWORK']['SMTPUSERNAME'];
		$GLOBALS['smtppassword']	=	$_SESSION['ETAMINEFRAMEWORK']['SMTPPASSWORD'];
		
		if (empty($p_nomenu)) {$p_nomenu = 0;}
		
		//Default values for the include files
		$topFile	= $GLOBALS['common'] . "top.html"; // add the file location also
		$leftFile	= $GLOBALS['common'] . "left.html";
		$middleFile = $GLOBALS['common'] . "home.html";
		$rightFile	= $GLOBALS['common'] . "right.html";
		$bottomFile = $GLOBALS['common'] . "bottom.html";
		$menuFile 	= $GLOBALS['common'] . "menus.html";
		

		//Check for sessions
		
		if ($p_module != "cronjobs") { //If cronjobs dont check for login authentication
			if (($p_module <> "main")  and ($_SESSION['sessionid'] <> session_id())) {
				$middleValues[] = array(
					"ERRORMESSAGE"=>"Session Time out."
				);
				$middleFile	= $GLOBALS['common'] . "errorfile.html";
				$p_module = "main"; //Ignore the module parameters
				$p_action = "login";
			}
			else {
				
				// added by sithes for login user identification 23/10/09
				$sqlupdate = "UPDATE etaminev2.tbluser SET expirytime = ADDTIME(NOW(),'00:05:00'), islogin = 'Yes' where userid = '".$_SESSION['userid']."'";
				$db->DbExecute($sqlupdate);	
				//end

				//Creating the create listbox in the left
				$db->SetResultType(MYSQL_NUM);
				$createValues = $db->DbGetMultipleRecords("select menuid, menuname from tblmenu where mcreate='Yes' and menuactive='Yes' order by menuname","FirstVal");
				$createCount = 0;
			
				if ($_SESSION['GROUPID'] != 1) {
					foreach ($createValues as $key => $value) {
					if ($_SESSION['ACCESSINFO'][$value]['Create']){
						$createArray[$createCount++] = $value;
					}
					}
					$createValues = $createArray;
				}
				
				if (is_array($createValues)){
					$createList = $listbox->ListBoxArray($createValues, $p_module);
					$templateValues[] = array(
					"PHP_SELF"=>$_SERVER['PHP_SELF'] . "?module=". $p_module . "&action=$p_action",
					"NEWENTRYOPTIONS"=>$createList
					);
				}
				
				//Create users list
				$listbox->ValueOnly = false;
				$templateValues[] = array(
					"USEROPTIONS"=>$listbox->ListBoxDb("select userid, firstname from etaminev2.tbluser where active='Yes' and groupid !='" . CLIENTUSERGROUP . "'", $p_changeuserid),
				);
			}
		}

		if (!empty($p_createsubmit)) {$p_action="create";}

		$p_module = strtolower($p_module);
		include_once("switch.php");
		
		//Moved from switch file
		//Get menu only after sucessfull login
		if ($_SESSION['sessionid']) {
			if (empty($_SESSION['ETAMINEFRAMEWORK']['MENU'])) {$_SESSION['ETAMINEFRAMEWORK']['MENU'] = getMenu();}
			$buildMenu = new etamineUIAppsuite($cfg["UIAPPSUITETHEME"]);
			$menucontent=$buildMenu->BuildMenu("innerDiv",$_SESSION['ETAMINEFRAMEWORK']['MENU']);			
			// Language Option
			$languageOption = getLanguage();
		}

		$templateValues[] = array(
			"MENUS"=>$menucontent,
			"TOPCONTENTS"=>$topContents,
			"LEFTCONTENTS"=>$leftContents,
			"MIDDLECONTENTS"=>$middleContents,
			"RIGHTCONTENTS"=>$rightContents,
			"BOTTOMCONTENTS"=>$bottomContents,
			"LANGUAGEOPTION"=>$languageOption
			//"PHP_SELF"=>$_SERVER['PHP_SELF'] ,// added by sithes on 09/11/09
		);

		$etamineParser->Parse($templateFile,$templateValues,True);
		
	/*	if (DEBUG && empty($p_nomenu)){
			$processedTime = getmicrotime() - $PageStartTime;
			echo "<span class='m8'>Exection Time: " . sprintf("%01.4f", $processedTime) ." Sec</span>";
		}*/
		ob_end_flush();
		 
	}
?>
