<?php

class Privilegies {

	static $privilegiesArray = array(
		/*array( 	"name" => "System configuration (database connections, email sender, etc.)", 
				"variable" => "system_configuration"
		),*/
		array( 	"name" => "User & groups privilegies managament", 
				"variable" => "user_and_privilegies"
		),
		array( 	"name" => "Logs - configuration and managament", 
				"variable" => "logs"
		),
		array( 	"name" => "Full access to server filesystem", 
				"variable" => "full_file_access"
		),
		array( 	"name" => "Files association (tab under Program managament)", 
				"variable" => "files_association"
		),
		array( 	"name" => "New software and upgrades installation  managament (for all users, for all programs)", 
				"variable" => "upgrade_managament"
		),
		/*array( 	"name" => "Language managament. (for all users)", 
				"variable" => "language_managament"
		),
		array( 	"name" => "Language selection", 
				"variable" => "language_selection"
		),*/
		array( 	"name" => "Fonts managament (for all users)", 
				"variable" => "fonts_managament"
		)
	);
	
	
	static $mustHaveApplicationsArray = array(
		"File Viewer", 
		"Image Viewer"
	);
	
	static $extensionsArray = array(
		array( 	"name" => "All files", 
				"variable" => "all_extensions",
				"extensions" => "*",
				"textfield" => false
		),
		array( 	"name" => "Safe files: jpg,jpeg,gif,png,bmp,fla,swf,as,txt,xml,html,avi,mpg,mp3,zip", 
				"variable" => "safe_extensions",
				"extensions" => "jpg,jpeg,gif,png,bmp,fla,swf,as,txt,xml,html,avi,mpg,mp3,zip",
				"textfield" => false
		),
		array( 	"name" => "Custom files", 
				"variable" => "custom_extensions",
				"extensions"  => "",
				"textfield" => true
		)
	);

	static $methodsArray = array(
						"all" => array(
							"User.signIn",
							"User.signOut",
							"User.isSignedIn",
							"User.getConfig",
	
							"Desktop.getShortCut",
							"Desktop.updateDesktopIconPosition",
							"Desktop.getDesktopFiles",						
							"Desktop.createShortcut",
							"Desktop.removeShortcut",
							
							"Application.getApplication",
							"Application.saveApplicaitonKey",
							"Application.saveApplicationState",
							"Application.getApplicationIcon",

							"File.getImage",
							"File.getFlashAsset",
							"File.getDirectory",
							"File.undoUpload",
							"File.redoUpload",
							"File.retriveFilesInfo",
							"File.upload",
							"File.uploadCheck",
							"File.fileProperties",
							"File.compressRedo",
							"File.compressUndo",
							"File.compress",
							"File.download",
							"File.copyMove",
							"File.copyMoveItem",
							"File.createFile",
							"File.createDirectory",
							"File.rename",
							"File.restoreCopyedOrMovedDirsAndFiles",
							"File.performCopyedOrMovedDirsAndFiles",
							"File.restoreDirsAndFiles",
							"File.deleteDirsAndFiles",
							"File.delete",
							"File.getFileContent",
							"File.saveFileContent",
							"File.saveRawPostData",
							
							"Fonts.getFont",
							
							"Themes.saveTheme",
							"Themes.getTheme",
							"Themes.getAllThemes",
							"Themes.getThemeImage",
							
							"Wallpapers.saveWallpaper",
							"Wallpapers.getAllWallpapersImages",
							"Wallpapers.getWallpaperImage",
							"Wallpapers.getWallpaperThumbnail",
							"Wallpapers.addWallpapers",
							"Wallpapers.deleteWallpaper",
							
							"Icons.getIconImage",
							"Icons.saveIconSet",
							"Icons.getIconSet",
							"Icons.getAllIcons",
							
							
						),
						"system_configuration" => array(
							"SystemConfiguration.setSystemVariable",
						
							"DatabaseConnection.add",
							"DatabaseConnection.edit",
							"DatabaseConnection.delete",
							"DatabaseConnection.load"
										
						),
						"user_and_privilegies" => array(
							"User.getAllGroupsAndUsersIndexes",
							"User.registerGroup",
							"User.editGroup",
							"User.getGroup",
							"User.removeGroups",
							"User.blockGroups",
							"User.setDeletedFlagToGroups",
							"User.getAllGroups",
							"User.registerUser",
							"User.editUser",
							"User.getUser",
							"User.removeUsers",
							"User.setDeletedFlagToUsers",
							"User.blockUsers",
							"User.getAllUsers",
							"User.getAllGroups",
							"User.getAccountsAndPrivilegiesArray",
							"User.getUsersPanelInfo"
						),
						"logs" => array(
							"Logger.getLogsView",
							"Logger.getLogs",
							"Logger.getLogsConfig",
							"Logger.saveLogsConfig",
							"Logger.deleteAllLogs",
							"Logger.getFullLog"
						),
						"files_association" => array(
							"Application.saveApplicationAssociation"
						),
						"upgrade_managament" => array(
							"InstallationManager.getLocalInstallations",
							"InstallationManager.getRemoteInstallations",
							"InstallationManager.installApplication"	
						/*
							"Installation.installUpgrade",
							"Installation.getLocalUpdateInformations",
							"Installation.getRemoteUpdateInformations",
							"Installation.installApplication",
							"Installation.installApplications",
							"Installation.getFirstTimeModules"*/
						),
						"language_managament" => array(
							"Languages.loadLanguages",
							"Languages.saveLanguage",
							"Languages.getLanguage",
							"Languages.deleteLanguage"
						),
						"language_selection" => array(
							"Languages.loadLanguages",
							"Languages.changeLanguage"
						),
						"fonts_managament" => array(
							"Fonts.getFonts",
							"Fonts.deleteFont",
							"Fonts.addFont",
							"Fonts.updateFont"
						)
);


	function getPrivArray(){
		return self::$p;
	}
	function getPublicUserPrivArray(){
		return self::$ppu;
	}
	function getAccountArray(){
		return self::$accounts;
	}
 	function getPrivObj($b){
	 
	 	$p = self::$p;
	 	
 	 	$privObj = (object) array();
 	 	for ($i=0; $i< count($p);$i++){
			$privName = $p[$i]["name"];
 	 		$privObj->$privName = $b;
 	 	}
 	
 	 	return $privObj;
 	 	
	}
 	function getPublicUserPrivObj($b){
	 
	 	$ppu = self::$ppu;
	 	
 	 	$privObj = (object) array();
 	 	for ($i=0; $i< count($ppu);$i++){
			$privName = $ppu[$i]["name"];
 	 		$privObj->$privName = $b;
 	 	}
 	
 	 	return $privObj;
 	 	
	}
	
 	// PUBLIC
 	function checkPermission($username, $action, $isModuleClass){
		
		if($isModuleClass===true){
			return true; 
		}
		 
 	 	$variable="";
 	 	foreach(self::$methodsArray as $key=>$actionsArray){
 	 		for($i=0; $i<count($actionsArray); $i++){
 	 			if($actionsArray[$i]==$action){
 	 			 	$variable=$key;
 	 			}
 	 		}
		}
 	 	
 	 	if($variable==""){
 	 	 	Response::sendNotice("priv", "no_permission");
			exit;
		}else if($variable=="all"){
 	 	 	return;
		}
		
		if(User::GetInstance()->userInfo->privilegies->$variable!==true){
 	 	 	Response::sendNotice("priv", "no_permission");
			exit;
		}
		return;
 	}
 
 	function getUserPermission($username){
 		
	 	$privFile = Config::get("private_system_privilegies_path")."/".$username.".xml";
 	 	if(!is_file($privFile)){
 	 		Response::sendNotice("priv", "no_definition");
		}
		
 	 	//retrive user privilegies data	
 	 	$privInfo = DataSerializer::deserialize(Util::readFile($privFile));
 	 		
		return $privInfo;
 	}
 	
 	function setPriv($username, $privData){

 	 	self::setPrivObj($username, $privData);
		
 	}
	function removePriv($username){
	 	
		return Util::deleteFile(Config::get("private_system_privilegies_path")."/".$username.".xml");
		
	}
 	function setPrivObj($username, $privData){
 	  	
		$privObj = self::getPrivObj(false);
 	  	foreach ($privObj as $key=>$value){
 	  		$privObj->$key = ($privData->$key==true);
 	  	}
 	  
	   	$privFile = Config::get("private_system_privilegies_path")."/".$username.".xml";
	  	if(!Util::writeFile($privFile, DataSerializer::serialize($privObj))){
			return false;
		}
		
		return true;
		
 	}
 	
 	function setMasterAccountPriv($username){
 		
	 	$p = self::$p;
 	 	
 	 	$privObj = self::getPrivObj(true);
 	 	
 	 	$privFile = Config::get("private_system_privilegies_path")."/".$username.".xml";
	  	if(!Util::writeFile($privFile, DataSerializer::serialize($privObj))){
			return false;
		}
		
		return true;
		
 	}

 	function setLimitedAccountPriv($username){

 	 	$privObj = self::getPrivObj(false);
 	 	$privObj->auth_signin = true;
 	 	$privObj->auth_signout = true;
 	 	
 	 	$privFile = Config::get("private_system_privilegies_path")."/".$username.".xml";
	  	if(!Util::writeFile($privFile, DataSerializer::serialize($privObj))){
			return false;
		}
		
		return true;
		
	}
}