<?php

/**
 *--------------------------------------
 * Session类
 *--------------------------------------
 * @project		: pfa
 * @author		: cblee
 * @created		: 2012-9-23
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

$session_id = ARequest::get(session_name());
if(!empty($session_id))
{
	session_id($session_id);
}
if(!isset($_SESSION))
{
	session_start();
}
class ASession
{
	/* 设置session数据 */
	public static function set($name, $value = '')
	{
		$prefix = C('SESSION.PREFIX');

		if(C('SESSION.CILENT_CHECK'))
		{
			$_SESSION[$prefix.'cliend_id'] = AServer::get_clientId();
		}

		$_SESSION[$prefix.$name] = $value;
	}

	/* 获取session数据 */
	public static function get($name = null)
	{
		$prefix = C('SESSION.PREFIX');

		$is_checked = 1;
		if(C('SESSION.CILENT_CHECK'))
		{
			$is_checked = self::check_clientId();
		}

		if(1 == $is_checked)
		{
			if(is_null($name))
			{
				return $_SESSION;
			}
			return isset($_SESSION[$prefix.$name]) ? $_SESSION[$prefix.$name] : '';
		}
		elseif(0 == $is_checked)
		{
			self::del($prefix.'cliend_id');
		}
		return '';
	}

	/* 删除 */
	public static function del($name)
	{
		$prefix = C('SESSION.PREFIX');
		unset($_SESSION[$prefix.$name]);
	}

	/* 清空所有Session */
	public static function clear()
	{
		return session_destroy();
	}

	/* Session的安全验证 */
	private static function check_clientId()
	{
		$prefix = C('SESSION.PREFIX');
		$key = C('SESSION.KEY');
		if(isset($_SESSION[self::$prefix.'cliend_id']))
		{
			if(AServer::get_clientId() == $_SESSION[$prefix.'client_id'])
			{
				return 1;
			}
			return 0;
		}
		return -1;
	}

	/* 构造函数私有化 */
	private function __construct(){}
}

?>