<?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_LIB . 'License.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('[WebView]' . $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('[WebView]' . $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_USERID] 		= "" ;
// 	$_SESSION[SE_USERGROUP] 	= "" ;
// 	$_SESSION[SE_MENU] 			= "" ;
	$_SESSION[SE_ORGID] 		= -1 ;
// 	$_SESSION[SE_USERKEY] 		= "" ;
	$_SESSION[SE_PORT_ORGNAME] 	= "" ;
	$_SESSION[SE_REMOTE_IP] 	= Util::getRemoteIP() ;
	$_SESSION[SE_URL] 			= $_SERVER["URL"];
}

//checking url
if ($_SESSION[SE_URL] != $_SERVER["URL"]) {
	session_regenerate_id();
	$_SESSION[SE_INIT] = true;
	$_SESSION[SE_USERID] = "";
//	$_SESSION[SE_USERGROUP] = "";
//	$_SESSION[SE_MENU] = "";
	$_SESSION[SE_ORGID] = -1;
//	$_SESSION[SE_USERKEY] = "";
	$_SESSION[SE_ORGNAME] = "";
	$_SESSION[SE_REMOTE_IP] = Util::getRemoteIP();
	$_SESSION[SE_URL] = $_SERVER["URL"];
	$_SESSION[SE_DB] = "";
}

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

$_SESSION[SE_ID] = session_id() ;

if (!isset($_SESSION[SE_DB]) || $_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(),$config->getPortNo()) ;
	else if ($config->getDbType() == DbType::MySqli)
		$_SESSION[SE_DB] = new MySqliDb($config->getHost(),$config->getDbName(),$config->getUserName(),$config->getPassword(),$config->getPortNo()) ;
	else
		$_SESSION[SE_DB] = new MsSqlDb($config->getHost(),$config->getDbName(),$config->getUserName(),$config->getPassword(),$config->getPortNo()) ;
	unset($config) ;
}

if (isset($_GET['c'])){
	$class = $_GET['c'];
} else {
	$class = "" ;
}

// log::write(print_r($_POST,true));
$page = "Signin";
if ($class != ""){
	if(isset($_POST['turn_off']) && $_POST['turn_off'] == 'y'){
		$page = $class;
		if(isset($_POST['org_id']) && $_POST['org_id'] != '')
			$_SESSION[SE_ORGID] = $_POST['org_id'];
	} else {
		if(isset($_GET['turn_off']) && $_GET['turn_off'] == 'y'){
			$page = $class;
			if(isset($_POST['org_id']) && $_POST['org_id'] != '')
				$_SESSION[SE_ORGID] = $_POST['org_id'];
		}
		else {
			if (isset($_SESSION[SE_USERID]) && $_SESSION[SE_USERID] != "" ){
				$page = Util::revertLink($class);
			}
		}
	}
}

$invalid = "";

if (!class_exists($page)) {
	$page = "Signin" ;
	$invalid = "C_INVALID_SESSION";
	Log::write("[WebView]class not found : " . $class . ", page : " . $page) ;
}

$params = array() ;

if (isset($_GET['t'])){
	$params['type'] = $_GET['t'] ;
} else {
	$params['type'] = 'v' ;
}

if ($_POST) {
	foreach ($_POST as $key => $value) {
		$params[$key] = $value;
	}
}

if ($_GET){
	if (isset($_GET['r'])){
		if (isset($_GET['fn']))
			$params['fn'] = $_GET['fn'];
		if (isset($_GET['tn']))
			$params['tn'] = $_GET['tn'];
		if (isset($_GET['select']))
			$params['select'] = $_GET['select'];
		if (isset($_GET['id']))
			$params['id'] = $_GET['id'];
		if (isset($_GET['t']))
			$params['type'] = $_GET['t'] ;
		if (isset($_GET['iv']))
			$params['invoice_detail'] = $_GET['iv'];
		if (isset($_GET['fa']))
			$params['fixed_assets'] = $_GET['fa'];
		if (isset($_GET['appr']))
			$params['appr'] = $_GET['appr'];
		if (isset($_GET['detail_id']))
			$params['detail_id'] = $_GET['detail_id'];
		if (isset($_GET['line_no']))
			$params['line_no'] = $_GET['line_no'];
	}
}

if($invalid == "C_INVALID_SESSION" && count($_POST) > 0){
	$params['type'] = Command::InvalidSession;
}

$called = call_user_func_array(array(new $page,"processRequest"),array($params)) ;
if ($called == FALSE) {
	echo "<h2>invalid request - call fail</h2>" ;
	Log::write("[WebView]Calling class failed : " . $class . " failed. Page : " . $page,$page) ;
}
