Session.class.php 7 KB
<?php
/**
 * Session service
 * @name Service_Memory_Session
 * @version 1.0 (2013-01-11)
 * @package Service/Memory/Session
 * @author zhouqq
 * @since 1.0
 */
class Service_Session extends Lib_Service
{
	 const ROUTER = 'auth.yh_uuc.session';
	 const EXIPRE = 3600;
	 const PERSIST_LIFE = 14400; //记住自己的session的最大生存周期,暂定4个小时,考虑数据安全。
     const BROWSER_LIFE = 3600; //浏览器生存周期的session得最大数据库生存时间,暂定1小时。
	 //tbl_session表缓存
	 private static $_tag_tbl_session = 'tag_yh_uuc_tbl_session_';
	 private static $_key_tbl_session = 'key_yh_uuc_tbl_session_';
	 
	 
     /**
     * 创建一个系统session
     *
     * @param $uid integer
     * @param $persist boolean
     * @return array false
     */
    public function createSession ($uid, $persist = true)
    {
        $time = time();
        $params = array();
        $params['uid'] = $uid;
        $params['sessionkey'] = Lib_Utils::getSessionKey($uid);
        $params['lasttime'] = $time;
        $params['createtime'] = $time;
        $params['createip'] = Util_Utils_Ip::GetRealRemoteIp();
        $params['persist'] = $persist ? 'y' : 'n';
        $params['postid'] = Lib_Utils::getUniqId();
        $params['logoutid'] = Lib_Utils::getUniqId();
        $params['src'] = $this->getAgentTxt(true);
        $status = self::service(self::ROUTER)->cache(false)->insert('createSession', $params)->status();
        if($status)
        {
            self::service(self::ROUTER)->delTags(self::$_tag_tbl_session);
            return $params;
        }
        else
        {
            return false;
        }
    }

    /**
     * 删除系统session
     *
     * @param $session_key string
     * @return boolean
     */
    public function deleteSession ($sessionkey)
    {
        $status =  self::service(self::ROUTER)->delete('deleteSession', array('sessionkey' => $sessionkey))->status();
        if($status)
        {
            self::service(self::ROUTER)->delTags(self::$_tag_tbl_session);
        }
        return $status;
    }
    
    /**
     * 根据用户来删除session
     *
     * @param $session_key string
     * @return boolean
     */
    public function deleteSessionByUid ($uid)
    {
    	$status =  self::service(self::ROUTER)->delete('deleteSessionByUid', array('uid' => $uid))->status();
    	if($status)
    	{
    		self::service(self::ROUTER)->delTags(self::$_tag_tbl_session);
    	}
    	return $status;
    }

    /**
     * 得到session全部信息
     *
     * @param $session_key string
     * @return array false
     */
    public function getSession ($sessionkey)
    {
        $key  = self::$_key_tbl_session.$sessionkey;
        return self::service(self::ROUTER)->tag(self::$_tag_tbl_session)->key($key)->expire(self::EXIPRE)->fetchRow('getSession', array('sessionkey' => $sessionkey));
    }

    /**
     * 更新sessoin信息
     *
     * @param $sessionkey string
     * @param $params array
     * @return boolean
     */
    public function updateSession ($sessionkey, $params)
    {
        $replaces = array('upstring' => Util_Utils_SqlString::mergeSqlUpstring($params));
        $params['sessionkey'] = $sessionkey;
        $status =  self::service(self::ROUTER)->update('updateSession', $params, $replaces)->status();
        if($status)
        {
            self::service(self::ROUTER)->delTags(self::$_tag_tbl_session);
        }
        return $status;
    }

    /**
     * 得到应该垃圾收集的session
     *
     * @return array false
     */
    public function getGcSessions ()
    {
        $time = time();
        $params = array();
        $params['templife'] = $time - self::PERSIST_LIFE;
        $params['persistlife'] = $time - self::BROWSER_LIFE;
        return self::service(self::ROUTER)->cache(false)->fetchCol('getGcSessions', $params);
    }

    /**
     * 得到需要垃圾收集的session数量
     *
     * @return integer
     */
    public function getGcSessionsCounter ()
    {
        $time = time();
        $params = array();
        $params['templife'] = $time - self::PERSIST_LIFE;
        $params['persistlife'] = $time - self::BROWSER_LIFE;
        return (int) self::service(self::ROUTER)->cache(false)->fetchOne('getGcSessionsCounter', $params);
    }

    /**
     * 获取多个sessions
     * 
     * @param string $uids
     * @return array
     */
    public function getUserSessions ($uids)
    {
        $rs = substr(str_repeat('?,', count($uids)), 0, - 1);
        $key = self::$_key_tbl_session.$uids;
        return self::service(self::ROUTER)->tag(self::$_tag_tbl_session)->key($key)->expire(self::EXIPRE)->fetchCol('getUserSessions', $uids, array('rs' => $rs));
    }

    /**
     * 获取用户最新一条session
     * 
     * @param string $uid
     * @return string
     */
    public function getUserLastSession($uid)
    {
    	$key = self::$_key_tbl_session.'lastSession_'.$uid;
    	return self::service(self::ROUTER)->tag(self::$_tag_tbl_session)->key($key)->expire(self::EXIPRE)->fetchOne('getUserLastSession', array('uid' => $uid));
    }
    
    /**
     * 垃圾收集
     *
     * 暂时没有使用
     *
     * @return boolean
     */
    public function gcSessions ()
    {
        $time = time();
        $params = array();
        $params['templife'] = $time - self::PERSIST_LIFE;
        $params['persistlife'] = $time - self::BROWSER_LIFE;
        $keys = $this->getGcSessions();
        if ($keys)
        {
            $keys = $this->_getRealKeys($keys);
            self::service(self::ROUTER)->delTags(self::$_tag_tbl_session);
            return self::service(self::ROUTER)->delete('gcSessions', $params)->status();
        }
        else
        {
            return true;
        }
    }

    /**
     * 得到实际的键
     *
     * @param $keys array
     * @return array
     */
    private function _getRealKeys ($keys)
    {
        $return = array();
        foreach ($keys as $v)
        {
            $return[] = self::$key_session . $v;
        }
        return $return;
    }

    /**
     * 获取访问终端信息
     *
     * @return string
     */
    public function getAgentTxt ($int = false)
    {
    	$agent = isset($_SERVER['HTTP_USER_AGENT']) ?$_SERVER['HTTP_USER_AGENT'] : '' ;
        $agent = Util_StringHelper::toUtf8($agent);
        $ret = 'web';
        if (is_string($agent))
        {
            if (preg_match('/(Android .+)\;/sU', $agent))
            {
                $ret = 'android';
            }
            else if (stripos($agent, 'iPad') !== false)
            {
                $ret = 'ios';
            }
            else if (stripos($agent, 'iPod touch') !== false)
            {
                $ret = 'ios';
            }
            else if (stripos($agent, 'iPhone') !== false)
            {
                $ret = 'ios';
            }
            else
            {
                $ret = 'web';
            }
        }
        if ($int) 
        {
        	$list = array('web' => 1, 'android' => 2, 'ios' => 3);
        	return isset($list[$ret])?$list[$ret]:1 ;
        }
        return $ret ;
    }
}

?>