<?php
/*
EasyGUI XP is free software released under the terms of the following BSD license.

Copyright (c) 2005,2006 Gaetan Lauff (glauff@plansoft.de)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, 
this list of conditions and the following disclaimer. 

- Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation 
and/or other materials provided with the distribution.

- Neither the name of the developer nor the names of its contributors may 
be used to endorse or promote products derived from this software without 
specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
 * Provides basic functionality for session handling
 *
 * @author Gaetan Lauff <glauff@plansoft.de>
 * @package session
 * @version $Id: class.ek_session.php 5 2006-02-01 17:32:21Z glauff $
 */
class EK_Session
{
	/**
	 * Comnstructor
	 *
	 * Starts a new Session
	 *
	 * @return void
	 *
	 * @access public
	 */
	function EK_Session()
	{
		//@session_start();
	}
	
	/**
	 * Starts a Session
	 * @return void
	 */
	function start()
	{
		@session_start();
	}

	/**
	 * Sets a session variable
	 *
	 * @param string	$var	Variable
	 * @param string	$val	Value
	 * @return void
	 *
	 * @access public
	 */
	function set( $idx, $val, $section = null )
	{
		if (!is_null( $section ) ) {
			$_SESSION[$section][$idx] = $val;
		} else {
			$_SESSION[$idx] = $val;
		}
	}
	
	/**
	 * Get Value from Session
	 * 
	 * @param 	string	$idx		Variable Name
	 * @param 	string	$section	Section to get Value from
	 * @return 	string
	 *
	 * @access 	public
	 */
	function get( $idx, $section = null )
	{
		if (!is_null( $section ) ) {
			if ( isset ( $_SESSION[$section][$idx] ) ) {
				return $_SESSION[$section][$idx];
			} else {
				trigger_error( 'Session IDX: ' . $idx . ' in Section ' . $section . ' is not set !', E_USER_NOTICE );
			}
		} else {
			if (isset($_SESSION[$idx])) {
				return $_SESSION[$idx];
			} else {
				trigger_error( 'Session IDX: ' . $idx . ' is not set !', E_USER_NOTICE );
			}
		}
		
		return false;
	}
	
	/**
	 * Deletes a Session Variable
	 *
	 * @param 	string	$idx		Variable
	 * @param 	string	$section	Section to delete from
	 * @return 	void
	 *
	 * @access 	public
	 */
	function del( $idx, $section = null )
	{
		if ( !is_null( $section ) ) {
			unset( $_SESSION[$section][$idx] );
		} else {
			unset ( $_SESSION[$idx] );
		}
	}
	
	/**
	 * Destroys the created Session
	 *
	 * @return void
	 *
	 * @access public
	 */
	function destroy()
	{
		$_SESSION = array();
		@session_destroy();
	}
	
	/**
	 * Checks if an index exists in the Session
	 *
	 * @param string $idx
	 * @param string $section
	 * @return TRUE if index exists, FALSE otherwise
	 */
	function idxExists( $idx, $section = null )
	{
		if ( !is_null( $section ) ) {
			if ( array_key_exists( $idx, $_SESSION[$section] ) ) {
				return true;
			} else {
				return false;
			}
		}
		
		if ( isset( $_SESSION[$idx] ) ) {
			return true;
		}
		
		return false;
	} 
}
?>