<?php
	class cChangePasswordPage extends cStudentPage
	{
    function Process()
    {
      if ( !$this->bLoggedIn ) 
			{
        return true;
      }
      else
			{ 
	      if (isset($this->postParams["start"]) && isset($this->postParams["passwdOld"]) && isset($this->postParams["passwdNew"]) && isset($this->postParams["passwdRetype"]))
				{
		      if ( $this->postParams["passwdNew"] == "" )
		      {
		        $this->smarty->assign( "error", "Nové heslo nesmie byť prázdne!" );        
		      }
		      else if ( $this->postParams["passwdNew"] != $this->postParams["passwdRetype"] )
		      {
		        $this->smarty->assign( "error", "Zadané heslá sa nezhodujú!" );        
		      }
		      else
		      { 
		        $passwdOld=mysql_real_escape_string($this->postParams["passwdOld"]);
		        $passwdNew=mysql_real_escape_string($this->postParams["passwdNew"]);
		        $passwdRetype=mysql_real_escape_string($this->postParams["passwdRetype"]);
		        
						$query = "SELECT * FROM tUser WHERE id='".$_SESSION["userId"]."' AND password=MD5(CONCAT(passPrefix,'".$this->postParams["passwdOld"]."'));";
						$result = $this->db->Query($query); 
		        if ( $result != 0) 
		        {
		        	if ( $this->db->NumRows($result)>0 )
		        	{
			          $query = "UPDATE tUser SET password=MD5(CONCAT(passPrefix,'".$this->postParams["passwdNew"]."')) ".
			                   "WHERE id=".$_SESSION["userId"].";";       
			          $result = $this->db->Query($query);
			          if ( $result != 0 )
			          {
			            $this->smarty->assign( "message", "Heslo bolo úspešne zmenené." );
			          }
								else
								{
			            $this->smarty->assign( "error", "Chyba pri komunikácii s databázou." );
			          }
			        }
			        else
			        {
		          $this->smarty->assign( "error", "Nesprávne zadané heslo!" );
			        }
		        }
		        else
		        {              
			        $this->smarty->assign( "error", "Chyba pri komunikácii s databázou." );
		        }
		      }
		    }
	      return true;
	    }
    }
    
    function Draw()
    {
      $this->smarty->assign("content", "changePassword.tpl");
      parent::Draw();
    }
	}
?>
