<?php
class users
{
	var $user_id;
	var $user_name;
	var $user_password;
	var $user_level;
	var $user_rights;
	var $user_normallevel;
	var $user_lang;
	var $user_tmppw;
	var $user_template;
	var $user_root_dir;
	var $editor_setting;
	
	function users($username)
	{
		$this->user_id = -1;
		$this->user_level = -1;
		
		$user = getuser($username);
			
		if(gettype($user) == "boolean")
			return false;

		$this->user_id = trim($user[0]);
		$this->user_name = trim($user[1]);
		$this->user_password = trim($user[2]);
		$this->user_normallevel = trim($user[3]);
		$this->user_rights = trim($user[4]);
		$this->user_lang = trim($user[5]);
		$this->user_tmppw = trim($user[6]);
		$this->user_template = trim($user[7]);
		$this->user_root_dir = trim($user[8]); 
		$this->editor_setting = trim($user[9]); 
		
		//validieren von usre_root_dir
		if($this->user_root_dir != "")
		{
			if(substr($this->user_root_dir, -1) != "/") 
				$this->user_root_dir .= "/";
			if($this->user_root_dir == "/")
				$this->user_root_dir = "";
			if(substr($this->user_root_dir, 0, 1) == "/") 
				$this->user_root_dir= substr($this->user_root_dir, 1);
		}
	}
	function login($password)
	{
		if(crypt($password,$this->user_password) == $this->user_password)
		{					
			$tmppw = md5(uniqid(rand(), true));
			$this->user_tmppw = $tmppw;		
			if(!$this->writeuser())
				message_die("Login Error","Couldn`t login User","Critical Error");
			
			setcookie("mfausername", $this->user_name, 0, "/",'', 0) or die("Konnte Cookie nicht setzen.");
			setcookie("mfatmppw", $tmppw, 0, "/",'', 0) or die("Konnte Cookie nicht setzen.");			
			return $tmppw;
		}
		else
			return false;
	}
	function logout()
	{
		$this->user_tmppw = 0;
		if(!$this->writeuser())
			message_die("Logout Problem", "Couldn`t logout user","Critical Error");
		
		$this->user_level = -1;
		$user_session = new file(SCRIPT_PATH."/work/temp/".$this->user_name.".usf");
		if($user_session->file_path != "NIL")
			$user_session->delete();
					
		setcookie("mfausername", '', 0, "/",'', 0);
		setcookie("mfatmppw", '', 0, "/",'', 0);
		unset($_SESSION['dir_list']);
	}
	function checkpassword($tmppw)
	{
		if(($this->user_tmppw != $tmppw))
			return false;
		else
		{
			$this->user_level = $this->user_normallevel;
			return true;
		}
	}
	
	function has_right($right)
	{
		switch($right)
		{
			case "upload":
				$right_position = 0;
				break;
			case "delete":
				$right_position = 1;
				break;
			case "view":
				$right_position = 2;
				break;
			case "edit":
				$right_position = 3;
				break;
			case "scripting":
				$right_position = 4;
				break;
		}		
		
		if(substr($this->user_rights, $right_position, 1) == 1)
			Return true;
		else
			Return false;
	}
	
	function writeuser()
	{
		//aktualisiert die User-Datei entsprechend der Instanz der Klasse users
		$user = array();
		$user[0] = $this->user_id;
		$user[1] = $this->user_name;
		$user[2] = $this->user_password;
		$user[3] = $this->user_normallevel;
		$user[4] = $this->user_rights;
		$user[5] = $this->user_lang;
		$user[6] = $this->user_tmppw;
		$user[7] = $this->user_template;
		$user[8] = $this->user_root_dir;
		$user[9] = $this->editor_setting;
		//schreiben der Daten
		if(!write_userdata($user))
			return false;
		else
			return true;
	}
	
	function changelang($newlang)
	{
		$this->user_lang = $newlang;
		$this->writeuser();
	}
	function changetemplate($newtemplate)
	{
		$this->user_template = $newtemplate;
		$this->writeuser();
	}
	function delete()
	{
		$user_file = new file(SCRIPT_PATH."/work/data/user_".$this->user_name.".php");
		return $user_file->delete();
	}
	//Zeig dein Gesicht
	function IsAdmin()
	{
		if($this->user_level == 1)
			Return true;
		else
			Return false;
	}
	function IsValid()
	{
		if($this->user_level != -1)
			Return true;
		else
			Return false;
	}
	//Stoppt, wenn User nicht entsprechenden Rand hat, das Skript
	function DieNotAdmin()
	{
		if($this->user_level != 1)
			message_die("Access denied","You havn`t got the required rights to enter this area","Message");
	}
	function DieNotValid()
	{
		if($this->user_level < 0)
			message_die("Access denied","You havn`t got the required rights to enter this area","Message");
	}
}
?>