<?php
/**
 * @version $Id: Session.class.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

class Session
{
    /**
     * 构造函数
     *
     * @return void
     */
    function Session()
    {
    }

	/**
	 * 开启session
	 */
    function startSession()
    {
        session_start();
        if (@function_exists('session_cache_limiter'))
        {
            session_cache_limiter('private, must-revalidate');
        }
    }
	/**
	 * 设置session存放目录
	 */
    function setSessionSavePath($path)
    {
        if (is_dir($path) && is_writable($path))
        {
            session_save_path($path);
        }
        else
        {
            exit('Session save path is not a available dir!');
        }
    }

    function setSessionLifeTime($lifeTime)
    {
        if ($lifeTime)
        {
            session_set_cookie_params($lifeTime);
        }
    }

    function getSessionID()
    {
        return session_id();
    }

    /**
     * 取得类的一个实例
     *
     * @return object
     */
    function & getInstance()
    {
        static $instance;

        if (empty($instance))
        {
            $instance = new Session();
        }
        return $instance;
    }

    function isSessionExists($key)
    {
        return Session::getValue($key) ? TRUE : FALSE;
    }

    function setArrayValue($arrID, $key, $value)
    {
        $_SESSION[$arrID][$key] = $value;
    }

    function getArrayValue($arrID, $key)
    {
        if (isset($_SESSION[$arrID][$key]))
        {
            return $_SESSION[$arrID][$key];
        }
        return NULL;
    }

    /**
     * 设置Session值
     *
     * @param String $key
     * @param mixed $value
     */
    function setValue($key, $value)
    {
        $_SESSION[$key] = $value;
    }

    /**
     * 取得指定Session名的值
     *
     * @param string $name
     * @return mixed
     */
    function getValue($key)
    {
        if (isset($_SESSION[$key]))
        {
            return $_SESSION[$key];
        }
        return NULL;
    }

    /**
     * 删除指定Session名的值
     *
     * @param String $name
     * @return Boolean
     */
	function delValue($key)
	{
	    if (isset($_SESSION[$key]))
	    {
	        $_SESSION[$key] = '';
	        unset($_SESSION[$key]);
	    }
        return TRUE;
	}

    /**
     * 清除Session
     */
    function clean()
    {
        foreach ($_SESSION as $key => $value)
        {
            unset($_SESSION[$key]);
        }
        session_destroy();
    }
}