<?php

// turn off NOTICE error reporting
error_reporting(E_ALL ^ E_NOTICE);

// Config class
if(is_file("config.php")){
	include_once("config.php");
}else{
 	include_once("classes/EmptyConfig.php");
}

if($_SERVER['REQUEST_METHOD']=="POST" || !empty($_GET["session"])){
	
	header("Content-type: text/plain");
	
	include_once("classes/Error.php");
	$error = new Error();

	include_once("classes/Constant.php");
	include_once("classes/Message.php");
	include_once("classes/Util.php");
	include_once("classes/Logger.php");
	include_once("classes/Privilegies.php");
	include_once("classes/UD.php");
	include_once("classes/Wddx.php");
	include_once("classes/WddxSerializer.php");
	include_once("classes/Response.php");
	include_once("classes/RecycleBin.php");
	include_once("classes/Json.php");
	include_once("classes/DataSerializer.php");
	include_once("classes/Request.php");
	include_once("classes/User.php");
	include_once("classes/UserFileSystem.php");
	include_once("classes/FileSecurity.php");
	include_once("classes/JSONUtil.php");
	include_once("classes/Languages.php");
	include_once("classes/File.php");
	include_once("classes/Indexes.php");
	include_once("classes/DynamicConfigStorage.php");


	Util::stripslashesGPC();
		 

	if($_SERVER["REQUEST_METHOD"]=="POST" && empty($_GET["session"]) && empty($_GET["action"])){
		$reqObj = DataSerializer::deserialize(($_POST["DATA"]));
		Request::setRequest("POST", $reqObj->session, $reqObj->action, $reqObj->appl, $reqObj->data);
	}else if(!empty($_GET[session])){
	 	Request::setRequest("GET", $_GET["session"], $_GET["action"], $_GET["appl"], $_GET["data"]);
	}else{
		die("Request is not recognized");
	}

	// lang
	if(Request::getClassName()=="Languages" && Request::getMethodName()=="getDefaultLanguage"){
	 	Languages::getInstance()->getDefaultLanguage();
	 	exit();
	}
	// --------------------
	// installation
	// --------------------
	
	if((Request::getClassName()=="FirstInstallation" || ($_GET[file_upload]==1 && $_GET[installation]==1)) && (!is_file("config.php") || User::getInstance()->getAllUsersCount()==0)){  
		include_once("classes/FirstInstallation.php");
		$installation = call_user_func(array("FirstInstallation", 'getInstance'));

	 	if($_GET[file_upload]==1 && $_GET[installation]==1){
	 		$installation->uploadFile();
			exit;
		}
		$className = Request::getClassName();
		$methodName = Request::getMethodName(); 
		if($className=="FirstInstallation"){
			$installation->$methodName(Request::getData());
		}
		exit();
	}


	// --------------------
	// sign in
	// --------------------
	if(Request::getClassName()=="User" && Request::getMethodName()=="signIn"){
		User::getInstance()->signIn(Request::getData());
	}
	
	
	// --------------------
	// get user information
	// --------------------
	User::getInstance()->isSignedIn(Request::getSession());
	
	
	// --------------------
	// log request
	// --------------------
	//if($config->logger)
	Logger::saveRequest(Request::getRequestObject());
		
	
	// --------------------
	// get user permissions
	// --------------------
	if(User::getInstance()->userInfo->masterAccount!==true)
		Privilegies::checkPermission(User::getInstance()->userInfo->username, Request::getClassName().".".Request::getMethodName(), Request::isModulClass());
	
	
	// --------------------
	// execute operation
	// --------------------
	$className = Request::getClassName();
	$methodName =  Request::getMethodName();
	$data = Request::getData();
	
	// Instance dynamic class
	if(Request::isModulClass()==true){
	 	include_once(Config::get("private_system_path")."/modules/".Request::getModulDir()."/classes/".$className.".php");
	}else{
		include_once("classes/".$className.".php");
	}
	
	if(!empty($data)){
	 	if (is_callable(array($className, "getInstance"))){
	 		$instance = call_user_func(array($className, 'getInstance'));
			$instance->$methodName(Request::getData());//execute
	 	}else{
	 	 	$instance = new $className();
			$instance->$methodName(Request::getData());//execute
		}
	}else{
	 	if (is_callable(array($className, 'getInstance'))){
			$instance = call_user_func(array($className, 'getInstance'));
			$instance->$methodName();//execute
	 	}else{
	 	 	$instance = new $className();
			$instance->$methodName();//execute
		}
	}
	
}

?>