Classes

File phpboost/user/session/Session.class.php

File phpboost/user/session/Session.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 
<?php
/**
 * This class manages all sessions for the users.
 * Session::gc();
 * Session::start();
 * @package     PHPBoost
 * @subpackage  User\session
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Loic ROUCHON <horn@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2014 12 22
 * @since       PHPBoost 3.0 - 2010 11 04
*/

class Session
{
    const VISITOR_SESSION_ID = -1;

    public static $DATA_COOKIE_NAME;
    public static $AUTOCONNECT_COOKIE_NAME;

    /**
     * @var HTTPRequest
     */
    private static $request;
    /**
     * @var HTTPResponse
     */
    private static $response;

    public static function __static()
    {
        $config = SessionsConfig::load();
        self::$DATA_COOKIE_NAME = $config->get_cookie_name() . '_data';
        self::$AUTOCONNECT_COOKIE_NAME = $config->get_cookie_name() . '_autoconnect';
        self::$request = AppContext::get_request();
        self::$response = AppContext::get_response();
    }

    public static function gc()
    {
        SessionData::gc();
    }

    public static function start()
    {
        try
        {
            if (self::$request->has_cookieparameter(self::$DATA_COOKIE_NAME))
            {
                return self::connect();
            }
            if (self::$request->has_cookieparameter(self::$AUTOCONNECT_COOKIE_NAME))
            {
                return self::autoconnect();
            }
            return self::create_visitor();
        }
        catch (UnexpectedValueException $ex)
        {
            return self::create_visitor();
        }
    }

    public static function create($user_id, $autoconnect = false)
    {
        if ($user_id == Session::VISITOR_SESSION_ID)
        {
            return self::create_visitor();
        }
        else
        {
            $data = SessionData::create_from_user_id($user_id);
            if ($autoconnect)
            {
                AutoConnectData::create_cookie($user_id);
            }
            return $data;
        }
    }

    /**
     * Delete the session in database. The current session stays alive for the rest of the
     * request and a visitor session will be created at the next request.
     */
    public static function delete(SessionData $session)
    {
        $session->delete();
        self::$response->delete_cookie(self::$AUTOCONNECT_COOKIE_NAME);
    }

    private static function connect()
    {
        try
        {
            return SessionData::from_cookie(self::$request->get_cookie(self::$DATA_COOKIE_NAME));
        }
        catch (SessionNotFoundException $ex)
        {
            if (self::$request->has_cookieparameter(self::$AUTOCONNECT_COOKIE_NAME))
            {
                return self::autoconnect();
            }
            return self::create_visitor();
        }
    }

    private static function autoconnect()
    {
        $cookie = self::$request->get_cookie(self::$AUTOCONNECT_COOKIE_NAME);
        $user_id = AutoConnectData::get_user_id_from_cookie($cookie);
        if ($user_id != Session::VISITOR_SESSION_ID)
        {
            return SessionData::create_from_user_id($user_id);
        }
        else
        {
            self::$response->delete_cookie(self::$AUTOCONNECT_COOKIE_NAME);
            return self::create_visitor();
        }
    }

    private static function create_visitor()
    {
        return SessionData::create_visitor();
    }
}
?>
Annonces