<?php
date_default_timezone_set('Asia/Singapore');

require_once('Constants.php') ;
require_once(PATH_LIB . 'AutoLoader.php') ;
require_once(PATH_LIB . 'Util.php') ;
require_once(PATH_LIB . 'Log.php') ;
require_once(PATH_MESSAGE . 'Message.php') ;
require_once(PATH_CODE . "database/MsSqlDb.php") ;
require_once(PATH_CODE . "database/MySqlDb.php") ;
require_once(PATH_CODE . "database/MySqliDb.php") ;
require_once('ConfigDb.php') ;
function myException($e)
{
	Log::write('[WebService]' . $e->getMessage(),"") ;
	//die("Sorry, there is an error and the application can not continue and will be terminated.") ;
	die("") ;
}
set_exception_handler('myException');
register_shutdown_function('fatalErrorShutdownHandler');

function fatalErrorShutdownHandler()
{
	$last_error = error_get_last();
	if ($last_error['type'] > 0)
		Log::write('[WebService]' . $last_error['message'] . " - " . $last_error['file'] . " on line " . $last_error['line']);
}


if (session_id() == "") 
	session_start(); 

if (!isset($_SESSION[PORT_INIT]) || $_SESSION[PORT_INIT] == false ){
    session_regenerate_id();
    $_SESSION[PORT_INIT] 		= true;
	$_SESSION[SE_EMPID] 		= "" ;
// 	$_SESSION[SE_USERGROUP] 	= "" ;
// 	$_SESSION[SE_MENU] 			= "" ;
	$_SESSION[SE_ORGID] 		= -1 ;
// 	$_SESSION[SE_USERKEY] 		= "" ;
	$_SESSION[SE_PORT_ORGNAME] 	= "" ;
	$_SESSION[SE_REMOTE_IP] 	= Util::getRemoteIP() ;
}

if (isset($_SESSION[SE_ID]))
	$oldseid = $_SESSION[SE_ID] ;
else
	$oldseid = "" ;
	
$_SESSION[SE_ID] = session_id() ;

if (!isset($_SESSION[SE_DB])) {
	$config = new ConfigDb(PATH_CODE . 'db.xml') ;
	$config->loadConfig() ;
	if ($config->getDbType() == DbType::MySql)
		$_SESSION[SE_DB] = new MySqlDb($config->getHost(),$config->getDbName(),$config->getUserName(),$config->getPassword()) ;
	else if ($config->getDbType() == DbType::MySqli)
		$_SESSION[SE_DB] = new MySqliDb($config->getHost(),$config->getDbName(),$config->getUserName(),$config->getPassword()) ;
	else
		$_SESSION[SE_DB] = new MsSqlDb($config->getHost(),$config->getDbName(),$config->getUserName(),$config->getPassword()) ;
	unset($config) ;
}
if (!isset($_SESSION[SE_DB_SUN])) {
	$config = new ConfigDb(PATH_CODE . 'db.xml') ;
	$config->loadConfig() ;
	if ($config->getDbTypeSun() == DbType::MySql)
		$_SESSION[SE_DB_SUN] = new MySqlDb($config->getHostSun(),$config->getDbNameSun(),$config->getUserNameSun(),$config->getPasswordSun()) ;
	else if ($config->getDbTypeSun() == DbType::MySqli)
		$_SESSION[SE_DB_SUN] = new MySqliDb($config->getHostSun(),$config->getDbNameSun(),$config->getUserNameSun(),$config->getPasswordSun()) ;
	else
		$_SESSION[SE_DB_SUN] = new MsSqlDb($config->getHostSun(),$config->getDbNameSun(),$config->getUserNameSun(),$config->getPasswordSun()) ;
    $_SESSION[SE_DB_BU_SUN] = $config->getBusinessUnitSun();
	unset($config) ;
}
if (!isset($_SESSION[SE_DB_NOTIF])) {
	$config = new ConfigDb(PATH_CODE . 'db.xml') ;
	$config->loadConfig() ;
	if ($config->getDbTypeNotif() == DbType::MySql)
		$_SESSION[SE_DB_NOTIF] = new MySqlDb($config->getHostNotif(),$config->getDbNameNotif(),$config->getUserNameNotif(),$config->getPasswordNotif()) ;
	else if ($config->getDbTypeNotif() == DbType::MySqli)
		$_SESSION[SE_DB_NOTIF] = new MySqliDb($config->getHostNotif(),$config->getDbNameNotif(),$config->getUserNameNotif(),$config->getPasswordNotif()) ;
	else
		$_SESSION[SE_DB_NOTIF] = new MsSqlDb($config->getHostNotif(),$config->getDbNameNotif(),$config->getUserNameNotif(),$config->getPasswordNotif()) ;	
	unset($config) ;
}
if (!isset($_SESSION[SE_DB_RMS])) {
	$config = new ConfigDb(PATH_CODE . 'db.xml') ;
	$config->loadConfig() ;
	if ($config->getDbTypeRms() == DbType::MySql)
		$_SESSION[SE_DB_RMS] = new MySqlDb($config->getHostRms(),$config->getDbNameRms(),$config->getUserNameRms(),$config->getPasswordRms()) ;
	else if ($config->getDbTypeRms() == DbType::MySqli)
		$_SESSION[SE_DB_RMS] = new MySqliDb($config->getHostRms(),$config->getDbNameRms(),$config->getUserNameRms(),$config->getPasswordRms()) ;
	else
		$_SESSION[SE_DB_RMS] = new MsSqlDb($config->getHostRms(),$config->getDbNameRms(),$config->getUserNameRms(),$config->getPasswordRms()) ;	
	unset($config) ;
}

$fn = "";
if ($_GET){

	if (isset($_GET['fn'])){
		$params['fn'] = $_GET['fn'];
		$fn = $params['fn'];
	}
		

	if (isset($_GET['id']))
		$params['id'] = $_GET['id'];
	if (isset($_GET['t']))
		$params['type'] = $_GET['t'] ;
	
	if (isset($_GET["type"]) && $_GET["type"] == "DFILE"){
		$params["type"] = "DFILE";
		$params["filename"] = $_GET["filename"];
	}
	if (isset($_GET['c']))
		$class = $_GET['c'];
	
}else{

	$class = "" ;

}
//Log::write($_GET);
try {	
	$page = "Signin";
	$data = array() ;
	$called = false ;
	
	if ($class !=""){
		
		
		$page = Util::revertLink($class,$fn) ;
		$data = $params ;
		
		if (class_exists($page)) {
			$called = call_user_func_array(array(new $page,"processRequest"),array($data)) ;
		} 
	}
} catch (Exception $e) {
	Log::write('[WebService]' . $e->getMessage(),$page) ;
}
if (!$called) {
	header('Content-type: application/json');
	$arr = array(FIELD_STATUS =>Status::Invalid,FIELD_MESG =>"Invalid Request.",FIELD_DATA => "index.pzx");
	echo json_encode($arr) ;
}
?>