Location: PHPKode > scripts > OO Auth > ooauth_class.php
<?PHP
/**
* ooauth - Klasse zur Validierung von Sessions unter PHP
* ooauth - Class to track time collected session
* @author Heiko Dillemuth <hide@address.com>
* @copyright Heiko Dillemuth (c) 01/2004
* @version 1.01rc1 / 30.03.2004, 06.08.2004
* @todo testing, rc1
* @access public
*/
class ooauth
{

	/**
	* 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 ooauth($ssave="ooauth", $exitfunc="")
	{
		session_start();

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

		if(!$_SESSION[ooauth_version])		#Init, if not set already
		{
			$_SESSION[ooauth_version]    =$this->ooversion;
			$_SESSION[ooauth_prg]        =$ssave;
			$_SESSION[ooauth_errmsg][0]  ="";   #no Error
			$_SESSION[ooauth_errmsg][-1] ="Session Timeout";
			$_SESSION[ooauth_errmsg][-2] ="Password wrong";
			$_SESSION[ooauth_errmsg][-3] ="Your IP has changed?!!!";
			$_SESSION[ooauth_errmsg][-4] ="Session: session has wrong ooauth version, updated?";
			$_SESSION[ooauth_errmsg][-5] ="Can't create Session, already text send to output!!!";
			$_SESSION[ooauth_errmsg][-6] ="Your try to use more than one session";
			$_SESSION[ooauth_errmsg][-10] ="Session Closed";
			$_SESSION[ooauth_errmsg][-11] ="Session not started";
			$_SESSION[ooauth_errmsg][-99]="undefined Error";
			$_SESSION[ooauth_exitfunc]    =$exitfunc;		#exit function
		}

	}#endfunc


	/**
	* Alias for ooauth() Constructor for PHP5, not tested yet
	*
	* @see ooauth();
	*/
	function __construct($ssave="ooauth", $exitfunc="")
	{
		ooauth($ssave, $exitfunc);
	}


	/**
	* 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[ooauth_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[ooauth_uservar][$vn])
		return "";
		else
		if($ap)
		return $_SESSION[ooauth_uservar][$vn][$ap];
		else
		return $_SESSION[ooauth_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;
		#if !bild leer, dann fehlermeldung rausgeben!
		#aus dem buch raussuchen

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

		$_SESSION[ooauth_logintime]	=date("d.M.Y H:M:i");
		$_SESSION[ooauth_time]		=time()+$timeout;
		$_SESSION[ooauth_hostvar]	=$hostvar;
		$_SESSION[ooauth_host]		=$$hostvar;
		$_SESSION[ooauth_lasterr]   =false;

		#if($this->cfunc)
		#call_user_func_array($this->cfunc[func], $this->cfunc[param]);

		return $_SESSION[ooauth_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[ooauth_hostvar];

		if($_SESSION[ooauth_lasterr]==false and !$_SESSION[ooauth_logintime])
		ooauth::set_error(-11);

		if($_SESSION[ooauth_lasterr]==false and $_SESSION[ooauth_version]!=$this->ooversion)
		ooauth::set_error(-4);

		if($_SESSION[ooauth_lasterr]==false and $_SESSION[ooauth_prg]!=$this->prg)
		ooauth::set_error(-6);

		if($_SESSION[ooauth_lasterr]==false and $_SESSION[ooauth_host]!=$$_SESSION[ooauth_hostvar])
		ooauth::set_error(-3);

		if($_SESSION[ooauth_lasterr]==false and $_SESSION[ooauth_time]<=time())             #Timeout
		ooauth::set_error(-1);

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

		ooauth::print_debug("\nooauth:  -- Update Session new Time is: {$_SESSION[ooauth_time]}\n");

		return $_SESSION[ooauth_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[ooauth_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[ooauth_time]=false;					#Destroy Timestamp
		$_SESSION[ooauth_lasterr]=$enr;
		#$_SESSION[ooauth_version]="";					#Force init next session-init

		return $ern;
	}


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

		if(empty($e))									#get current error
		return false;

		if(!$_SESSION[ooauth_errmsg][$e]) 								#if !Msg, set default msg
		$_SESSION[ooauth_errmsg][$e]=$_SESSION[ooauth_errmsg][-99];

		return $_SESSION[ooauth_errmsg][$e];
	}


	/**
	* Kill the session
	*
	* @access public
	* @see new_session(), is_session()
	*/
	function kill_session()
	{
		#Set Timeout
		ooauth::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="")
	{
		if($this->is_session($e)<1000)
		return $this->get_error_msg();

		return false;
	}


}#endclasse ooauth

?>
Return current item: OO Auth