<?php
define('REMOTE_IP','REMOTE_ADDR');
define('USER_AGENT','HTTP_USER_AGENT');
/**
 * CtrlHttpSession
 *
 * @date 15.03.2007
 * @name CtrlHttpSession
 * @author Margo Poolak <margo 'dot' poolak 'at' mail 'dot' ee>
 * @copyright Copyright &copy;2006 Margo Poolak. All Rights Reserved.
 * @version 2.0
 * @package System.Web
 * @category 
 * 
 * Session class
 */
class CtrlHttpSession 
{
	private $_protected = true;/**Some parts of variables will be protected*/
	/**
	* The session will be started
	* Will be checked is the session user using the same id's
	*/	
	public function __construct()
	{
		/**
		* Stop the session if header sent
		*/
		if(headers_sent())
		{
			trigger_error("Cant start session because the header is allready sent");
			exit();
		}
		/**Start the session*/
		session_start();
		/**
		* Check is the remote ip reqistered
		*/
		$reqistered = $this->isRegistered(REMOTE_IP);
		/**
		* If the session is first time started then reqister it
		*/
		if($reqistered === false)
		{
			/**Make the protected variables writeable*/
			$this->_protected = false;
			$this->set(REMOTE_IP, $_SERVER[REMOTE_IP]);
			$this->set(USER_AGENT,$_SERVER[USER_AGENT]);
			/**Make the protected variables un-writeable*/
			$this->_protected = true;
		}
		else
		{
			$remote_ip = $this->get(REMOTE_IP);
			$user_agent = $this->get(USER_AGENT);
			
			if($remote_ip !== $_SERVER[REMOTE_IP] OR $user_agent !== $_SERVER[USER_AGENT])
			{
				trigger_error("Might be that this session is stolen! The session will be destroyed!");
				$this->destroy();
				exit();
			}
		}
	}
	/**
	* Is the session key registered
	* @param string key name
	* @return boolean
	*/
	public function isRegistered($key)
	{
		if(isset($_SESSION[$key]))
		{
			return true;
		}
		return false;
	}
	/** 
	* Removes the session key element
	* @param string key name
	* @return string deleted element value or if not set NULL
	*/	
	public function remove($key)
	{
		if(isset($_SESSION[$key]))
		{
			$value = $_SESSION[$key];
			unset($_SESSION[$key]);
			return $value;
		}
		return NULL;
	}
	/**
	* Returns the session key value
	* @param string key
	* @param string alternative value to return if key doesnt exist
	* @return string value
	*/	
	public function get($key,$alt='')
	{
		if(isset($_SESSION[$key]))
		{
			$value = $_SESSION[$key];
			return $value;
		}
		return $alt;
	}
	/**
	* Sets the session key a value
	* @param string key 
	* @param string value
	*/	
	public function set($key,$value)
	{
		if($this->_protected == true AND ($key == USER_AGENT OR $key == REMOTE_IP))
		{
			trigger_error("USER_AGENT and REMOTE_IP are protected session variables and not allowed to overwrite");
			exit();
		}
		$_SESSION[$key] = $value;
	}	
	/**
	* Counts all registered session
	* @return int count of sessions
	*/	
	public function count()
	{
		return count($_SESSION);
	}
	/**
	* Will return the current session id
	* @return string session id
	*/	
	public function getSessionId()
	{
		return session_id();
	}
	/**
	 * Destroys all data registered to a session.
	 */
	public function destroy()
	{
		session_destroy();
	}	
}
?>