<?php

class Themes {

	function Themes(){
	}

	// -----------------------------------------------------
	// SAVE THEME
	// -----------------------------------------------------
	
	function saveTheme($d){
 	 
		$themeFile = Config::get("private_system_path")."/config/users/".User::getInstance()->userInfo->username."_dp.txt";
		$dpXmlSrt = Util::readFile($themeFile);
	 	if($dpXmlSrt===false){
	 		Response::sendResponse(false, null, "desktop", "user_theme_doesnot_exist");
	 	}
	 	
		$dp = DataSerializer::deserialize($dpXmlSrt);
	 	$dp->theme = $d->themeName;
	 	
	 	if(!Util::writeFile($themeFile,DataSerializer::serialize($dp))){
	 	 	Response::sendResponse(false, null, "desktop", "user_theme_cannot_write");
	 	}
 	 	
		Response::sendOKResponse((object) array("success"=>true));
		
	}	 
	
	// -----------------------------------------------------
	// GET THEME INFO
	// -----------------------------------------------------
	
	function getTheme($d){

		//security

		$themeFile = "resources/themes/".basename($d->fileName);
		
		//security
		$is_secure = FileSecurity::getInstance()->systemPathCheck($themeFile , array("txt"));
		if(!$is_secure){
			Response::sendResponse(false, null, "system", "terminated_by_system_path_check");
		}
		
 	 	if(!is_file($themeFile)){
 	 		Response::sendResponse(false, null, "desktop", "theme_doesnot_exist");
		}
		
		$theme = WddxSerializer::deserialize(Util::readFile($themeFile));
 	 	

		Response::sendOKResponse($theme);
	}	 
	
	// -----------------------------------------------------
	// GET ALL THEMES ARRAY
	// -----------------------------------------------------
	
	function getAllThemes(){
	 	
	 	$ud = new UD();
	 	$ud->readOnlyFiles = true;
	 	
	 	$FaF = $ud->getFoldersAndFiles("resources/themes");
	 	if($FaF<0){
	 		Response::sendErrorResponse($ud->getErrorID($FaF), $ud->getErrorMsg($FaF));
		}
		Response::sendOKResponse($FaF);
	}

	// -----------------------------------------------------
	// GET  THEME IMAGE
	// -----------------------------------------------------
	
	function getThemeImage($a){

		$relativePath = "resources/themes/image/".basename($a[0]);
		
		if(FileSecurity::getInstance()->systemPathCheck($relativePath, array("png", "gif", "bmp","jpg"))){
			header("Content-type: image/".Util::getExtension($relativePath));
			header("Content-Disposition: filename=". Util::getFileNameFromPath($relativePath));
			readfile($relativePath);
		}
		
		exit;
		
	}
	
}
?>