<?PHP

class auth
{

	/**
	* Class init, Setting default parameter
	*
	* @param string x Your Application short name
	* @param string x URL for Loggin Screnn if error occures (Ex. timeout)
	* @access public
	*/
	function __construct($ssave="auth", $exitfunc="")
	//function auth($ssave="auth", $exitfunc="")
	{
		session_start();

		$this->DEBUG     = false;
		$this->ooversion = "auth:1.01rc1";
		$this->prg			 = $ssave;                      #application name

		//if(!isset($_SESSION['auth_version']))		#Init, if not set already
		{
			$_SESSION['auth_version']     = $this->ooversion;
			$_SESSION['auth_prg']         = $ssave;
			$_SESSION['auth_errmsg'][0]   = "";   #no Error
			$_SESSION['auth_errmsg'][-1]  = "Session Timeout";
			$_SESSION['auth_errmsg'][-2]  = "Password wrong";
			$_SESSION['auth_errmsg'][-3]  = "Your IP has changed?!!!";
			$_SESSION['auth_errmsg'][-4]  = "Session: session has wrong auth version, updated?";
			$_SESSION['auth_errmsg'][-5]  = "Can't create Session, already text send to output!!!";
			$_SESSION['auth_errmsg'][-6]  = "Your try to use more than one session";
			$_SESSION['auth_errmsg'][-10] = "Session Closed";
			$_SESSION['auth_errmsg'][-11] = "Session not started";
			$_SESSION['auth_errmsg'][-99] = "undefined Error";
			$_SESSION['auth_exitfunc']    = $exitfunc;		#exit function
		}
		/*
		echo '<pre>';
		var_dump($_SESSION);
		echo '</pre>';
		var_dump("done init");
		*/
	}#endfunc


	/**
	* Enable/disable DEBUG Messages. DEBUG Flag is not be stored in the session!
	*
	* @param boolean x on/off [true|false*]
	* @access public
	* @see print_debug();
	*/
	function set_debug($d=true)
	{
		$this->DEBUG=$d;
	}

	/**
	* Print Out Debug Msg, if switched on with set_debug(true);
	*
	* @param string x on/off [true|false*]
	* @see set_debug();
	* @access private
	*/
	function print_debug($d)
	{
		if($this->DEBUG!=false)
		print $d;
	}

	/**
	* Stores a variable into the session
	*
	* @param string x Variable Name
	* @param any x Variable Value
	* @access public
	* @see set_formname();
	*/
	function set_uvar($vn, $vv)
	{
		$_SESSION['auth_uservar'][$vn]=$vv;
	}

	/**
	* Restores a session variable. If the variable is an array and you will need only one element you can give the keyname as second parameter. Ex. $s->get_uvar("abc", "p1") is $abc[p1]="value"
	*
	* @param string x Variable Name
	* @param string x Array Keyname
	* @return any Variable value
	* @access public
	* @see set_uvar();
	*
	*/
	function get_uvar($vn, $ap="")
	{
		if(!$_SESSION['auth_uservar'][$vn])
		return "";
		else
		if($ap)
		return $_SESSION['auth_uservar'][$vn][$ap];
		else
		return $_SESSION['auth_uservar'][$vn];
	}

	/**
	* Creates (if not) a new session and sets a lot of default values. then it calls is_session()
	*
	* @param string x Host String [0.0.0.0] Ex. you shold set it to $HTTP_HOST
	* @param string x Timeout in Sec. [20]
	* @return string $host String mit IP Adresse
	* @access public
	* @see is_session();
	*
	*/
	function new_session($timeout=600, $hostvar="_SERVER['REMOTE_ADDR']")
	{
		global $$hostvar;

		auth::print_debug("Create new Session: Timeout=$timeout, Host=$bk\n");

		$_SESSION['auth_logintime']	= date("d.M.Y H:M:i");
		$_SESSION['auth_time']		  = time()+$timeout;
		$_SESSION['auth_hostvar']	  = $hostvar;
		$_SESSION['auth_host']		  = $$hostvar;
		$_SESSION['auth_lasterr']   = false;
		
		return $_SESSION['auth_host'];
	}#endfunc new_session

	/**
	* Checks the session
	*
	* @param string x Host String [0.0.0.0] Ex. you shold set it to $HTTP_HOST
	* @param string x opt: Timeout in Sec. [default is 600]
	* @see new_session();
	* @access public
	*/
	function is_session($timeout=600)
	{
		global $_SESSION;
		/*
		echo '<pre>';
		var_dump($_SESSION);
		echo '</pre>';
		var_dump("auth_time1 : ".$_SESSION['auth_time']."<br>");
		*/
		if($_SESSION['auth_lasterr']==false and !$_SESSION['auth_logintime'])
		$this->set_error(-11);

		if($_SESSION['auth_lasterr']==false and $_SESSION['auth_version']!=$this->ooversion)
		$this->set_error(-4);

		if($_SESSION['auth_lasterr']==false and $_SESSION['auth_prg']!=$this->prg)
		$this->set_error(-6);

		if($_SESSION['auth_lasterr']==false and $_SESSION['auth_host']!=$$_SESSION['auth_hostvar'])
		$this->set_error(-3);

		if($_SESSION['auth_lasterr']==false and $_SESSION['auth_time']<=time()) #Timeout
		$this->set_error(-1);

		if($_SESSION['auth_lasterr']==false and $_SESSION['auth_time']>10000 and $timeout>0) #No Error
		$_SESSION['auth_time']=time()+$timeout;

		$this->print_debug("\nauth:  -- Update Session new Time is: {$_SESSION['auth_time']}\n");

		//var_dump("auth_lasterr : ".$_SESSION['auth_lasterr']."<br>");
		//var_dump("auth_time2 : ".$_SESSION['auth_time']."<br>");
		
		return $_SESSION['auth_time'];
	}#endfunc new_session

	/**
	* If you don't want engl. Error messages you can replace it by your own your own. The new values are stored in the session so you don't nedd to setup them up every time!
	*
	* @param int x ErrorNo.
	* @param string x new Error Message
	* @access public
	* @see get_error_msg();
	*/
	function set_error_msg($e, $em)
	{
		$_SESSION['auth_errmsg'][$e]=$em;
	}

	/**
	* Merkt sich den letzten Fehler
	*
	* @param string x Optionaler Parameter für das Form-Name TAG
	* @access public
	* @see set_formname();
	*
	*/
	function set_error($enr)
	{
		$_SESSION['auth_time']=0;					#Destroy Timestamp
		$_SESSION['auth_lasterr']=$enr;
		return $enr;
	}

	/**
	* Returns an error message
	*
	* @param int x ErrorNo.
	* @return string Error message text
	* @access public
	* @see set_error_msg();
	*/
	function get_error_msg($e="")
	{
		#get current error
		if(empty($e)) $e= $_SESSION['auth_lasterr'];

		if(!$_SESSION['auth_errmsg'][$e]) #if !Msg, set default msg
		  $_SESSION['auth_errmsg'][$e]=$_SESSION[auth_errmsg][-99];
		return $_SESSION['auth_errmsg'][$e];
	}

	/**
	* Kill the session
	*
	* @access public
	* @see new_session(), is_session()
	*/
	function kill_session()
	{
		#Set Timeout
		auth::set_error(-10);

		#Call if defined a UserDefined Function before session will be destroyed
		if($this->dfunc)
		call_user_func_array($this->dfunc[func], $this->dfunc[param]);
	}

	/**
	* Tests Session and return ErrorMsg if failed
	*
	* @param int x ErrorNo.
	* @param string x new Error Message
	* @access public
	* @see get_error_msg();
	*/
	function test_session($e="")
	{
	  $tmpval = $this->is_session($e);
		if($tmpval<1000 || $tmpval=="") {
		  return true;
		} else {
		  return false;
		}
	}

}#endclasse auth

?>