<?

class class_user {
	
	var $db;

	var $page;

	var $config;
	
	var $userdata;

	var $userprivileges;

	function __construct($config,$db,$page) {

		$this->config = $config;
		$this->db = $db;
		$this->page = $page;

		session_start();

		if ($_GET["logout"]){
		  session_destroy();
		  session_unset();
		  unset($_REQUEST);
		  unset($_GET);
		  unset($_POST);
		  die('<script language="JavaScript">top.location.href=\''.$this->config->getValue($this->page->modultype,"index").'\';</script>');
		}

		if (isset($_SESSION["user"])){
		  $user=$_SESSION["user"];
		  $md5_password = $_SESSION["md5_password"];
		}else{
		  $md5_password = md5(trim($_REQUEST["PASSWORD_ID"]));
		  $user=$_REQUEST["USER_ID"];		
		}

		if ($user!= "" AND $md5_password!= ""){
			$this->login($user,$md5_password);
		} else {			
			$this->page->setTag("title", $this->config->getValue($this->page->modultype,"login_title"));
			$this->page->AppendTag("content", $this->page->getAppropriateTemplate($this->config->getValue("templates_path","")."/login.tmpl.php"));
			$this->page->out();
		}
	}
	
	
	function login($user,$md5_password) {;

	  if ($user=="")
	    die('<script language="JavaScript">alert("'.$this->config->getValue("no_userid","").'");top.location.href=\''.$this->config->getValue($this->page->modultype,"index").'\';</script>');
	  if ($md5_password=="")
	    die('<script language="JavaScript">alert("'.$this->config->getValue("no_password","").'");top.location.href=\''.$this->config->getValue($this->page->modultype,"index").'\';</script>');

	  $sql=sprintf("SELECT id_pg_dolgozok, nev, id_pg_jogcsoporttipusok as jogcsoport,pg_dolgozoktipus.megnevezes as dolgozotipus, pg_szolghelyek.megnevezes as szolgalatihely
	      FROM pg_dolgozok 
	      JOIN pg_dolgozoktipus ON pg_dolgozoktipus.id_pg_dolgozoktipus=pg_dolgozok.id_pg_dolgozoktipus
	      JOIN pg_szolghelyek ON pg_szolghelyek.id_pg_szolghelyek=pg_dolgozok.id_pg_szolghelyek
	      WHERE kod='%s' and jelszo='%s' and jelszo is not null",
	      mysql_real_escape_string($user),
	      mysql_real_escape_string($md5_password));	      

	  $this->userdata=$this->db->getOne($sql);

	  if ($this->userdata){	    

	    $_SESSION["user"]=$user;
	    $_SESSION["md5_password"]=$md5_password;

//Menü létrehozás a jogosultságok alapján és jog ellenőrzése az aktuális funkcióhoz $is_privileg


	    $modul_menu=menuCreate($this->db,$this->userdata["jogcsoport"],'dropdown_modul','modul',$this->page->modultype,$this->config->getValue("modul","title"),$this->config);
	    $karbantartas_menu=menuCreate($this->db,$this->userdata["jogcsoport"],'dropdown_karbantartas','karbant',$this->page->modultype,$this->config->getValue("edit","title"),$this->config);
	    $listak_menu=menuListaCreate($this->db,$this->userdata["jogcsoport"],'dropdown_listak','lista',$this->config->getValue("list","title"),$this->config);
	    
	    if ($modul_menu[0][1]){	     

	      $this->page->setTag("dolgid",$this->userdata["id_pg_dolgozok"]);
	      $this->page->setTag("modultype",$this->page->modultype);
	      $this->page->setTag("modul_menu",$modul_menu[0]);
	      $this->page->setTag("karbantartas_menu",$karbantartas_menu[0]);
	      $this->page->setTag("listak_menu",$listak_menu);

//-Vége- Menü létrehozás a jogosultságok alapján és jog ellenőrzése az aktuális funkcióhoz $is_privileg  top.location.href='".$this->config->getValue($value["funkcio"],"index")."';

//Logout
	      //$content_logout='<input type="button" value="'.$this->config->getValue("logout_button","").'" class="button" onClick="top.location.href=\''.$this->config->getValue($this->page->modultype,"index").'?logout=true\'">';
	      $content_logout='<a href="javascript:void(0);" class="button_item" onclick="top.location.href=\''.$this->config->getValue($this->page->modultype,"index").'?logout=true\';">'.$this->config->getValue("logout_button","").'</a>';
	      $this->page->setTag("content_logout",$content_logout);

	      require_once($this->config->getValue("includes_path","")."/".$this->page->modultype.".php");

	      $this->page->setTag("content_userdata",$this->userdata["nev"]." - ".$this->userdata["dolgozotipus"]);

	      $this->page->setTag("title", $this->config->getValue($this->page->modultype,"title"));

	      $this->page->AppendTag("content","<table border=\"0\" style=\"margin-bottom:0px;\"><tr><td>");
//TOP
	      $this->page->AppendTag("content", $this->page->getAppropriateTemplate($this->config->getValue("templates_path","")."/top.tmpl.php"));
//CONTENT
	      $this->page->AppendTag("content", $this->page->getAppropriateTemplate($this->config->getValue("templates_path","")."/".$this->page->modultype."_default.tmpl.php"));

	      $this->page->AppendTag("content","</td></tr></table>");

	      $this->page->out();

	    }else{
	      die('<script language="JavaScript">alert("'.$this->config->getValue("no_privileges","").'");top.location.href=\''.$this->config->getValue($this->page->modultype,"index").'?logout=true\';</script>');
	    }
	  }else{
	    die('<script language="JavaScript">alert("'.$this->config->getValue("no_login","").'");top.location.href=\''.$this->config->getValue($this->page->modultype,"index").'?logout=true\';</script>');
	  }

	}

}



?>