<?php

class Application{

	// TODO:: security 
	
	function Application(){
	}

	// -----------------------------------------------------
	// GET APLICATION KEY 
	// -----------------------------------------------------
	/*public function getApplicaitonKey($d){
		
		global $config, $file, $auth;
	
		$fileObj = $file->checkPathAndGetPathElements(Config::get("private_system_path")."/keys/".basepath($d->name));
		
		$retObj = (object) array();
		if(is_file($fileObj->realPath)){
		 	$retObj->success = true;
			$retObj->key = DataSerializer::deserialize(Util::readFile($fileObj->realPath));
			Response::sendOKResponse($retObj);
		}else{
		 	$retObj->success = false;
			Response::sendOKResponse($retObj);
		}
		
	}*/
	
	// -----------------------------------------------------
	// SAVE APLICATION KEY 
	// -----------------------------------------------------
	
	public function saveApplicaitonKey($d){
		
		$filePath = Config::get("private_system_path")."/modules/".basename($d->applicationDirectory)."/key.txt";
			
		// security
		$is_secure = FileSecurity::getInstance()->systemPathCheck($filePath , array("txt"), Config::get("private_system_path")."/modules/");
		if(!$is_secure){
			Response::sendResponse(false, null, "system", "terminated_by_system_path_check");
		}
		
		$retObj = (object) array();
		
		$retObj->success = Util::writeFile($filePath,$d->key);
		
		Response::sendOKResponse($retObj);

	}

	// -----------------------------------------------------
	// APPLICATION ASSOCIATION 
	// -----------------------------------------------------
	public function saveApplicationAssociation($d){

		$retObj = (object) array();
		$retObj->success = Util::writeFile(Config::get("private_system_path")."/config/users/".User::getInstance()->userInfo->username."_aa.txt", DataSerializer::serialize($d));
		
		Response::sendOKResponse($retObj);
		
	}
	
	// -----------------------------------------------------
	// GET THE APPLICATION SWF FILE
	// -----------------------------------------------------
	
	function getApplication($a){

		$notUnderPath = Config::get("private_system_path")."/modules/";
		$realPath = Config::get("private_system_path")."/modules/".$a[0];
		
		if(FileSecurity::getInstance()->systemPathCheck($realPath, array("swf"), $notUnderPath)){
			header("Content-type: application/x-shockwave-flash");
			header("Content-Disposition: filename=". Util::getFileNameFromPath($realPath));
			readfile($realPath);
		}

		exit;
		
	} 
	
	// -----------------------------------------------------
	// GET THE APPLICATION SWF FILE
	// -----------------------------------------------------
	
	public function getApplicationIcon($a){
	 	  
		$notUnderPath = Config::get("private_system_path")."/modules/";
		$realPath = Config::get("private_system_path")."/modules/".$a[0];
		
		if(FileSecurity::getInstance()->systemPathCheck($realPath, array("png", "gif", "bmp","jpg"), $notUnderPath)){
			header("Content-type: image/".Util::getExtension($realPath));
			header("Content-Disposition: filename=". Util::getFileNameFromPath($realPath));
			readfile($realPath);
		}

		exit;
		
	}
		
	// -----------------------------------------------------
	// APPLICATION STATES  
	// -----------------------------------------------------
	
	function saveApplicationState($d){
		
		$applicationStates = (object) array();
		
		$asPath = Config::get("private_system_path")."/config/users/".User::getInstance()->userInfo->username."_as.txt";
		if(is_file($asPath)){
			$asData = Util::readFile($asPath);
		 	if($asData!==false){
		 		$applicationStates = DataSerializer::deserialize($asData);
		 	}
		}
		$name = (string) $d->applicationName;
		
		$applicationStates->$name = $d->applicationState;

		Util::writeFile($asPath,  DataSerializer::serialize($applicationStates));

		Response::sendOKResponse($applicationStates);
	}
}