You are here

class SessionProxy_Backend_Default in Session Proxy 7

Default implementation relies on a custom storage engine.

Hierarchy

Expanded class hierarchy of SessionProxy_Backend_Default

File

lib/SessionProxy/Backend/Default.php, line 6

View source
class SessionProxy_Backend_Default extends SessionProxy_Backend_Base {

  /**
   * @var SessionProxy_Storage_Interface
   */
  protected $storage;

  /**
   * Session write proxy that will allow us to disable session writing if
   * we are master of the storage.
   */
  public function writeProxy($sessionId, $serializedData) {
    if ($this
      ->isWriteEnabled()) {
      return $this->storage
        ->write($sessionId, $serializedData);
    }
  }

  /**
   * Session destroy will require us to update the current logged in user.
   */
  public function destroyProxy($sessionId) {
    $this->storage
      ->destroy($sessionId);
    $this
      ->deleteCurrentSessionCookie();
    $this
      ->refreshAfterSessionChange();
  }

  /**
   * @var int
   */
  protected $uid;
  protected function getSessionUid() {
    return $this->storage
      ->getSessionUid();
  }

  /**
   * @var bool
   */
  protected $doWrite = TRUE;
  public function writeDisable() {
    $this->doWrite = FALSE;
  }
  public function writeEnable() {
    $this->doWrite = TRUE;
  }
  public function isWriteEnabled() {
    return $this->doWrite;
  }
  public function handleHttps() {
    return variable_get('https', FALSE);
  }
  protected function sessionSetHandlers() {
    if (FALSE === session_set_save_handler(array(
      $this->storage,
      'open',
    ), array(
      $this->storage,
      'close',
    ), array(
      $this->storage,
      'read',
    ), array(
      $this,
      'writeProxy',
    ), array(
      $this,
      'destroyProxy',
    ), array(
      $this->storage,
      'gc',
    ))) {
      throw new Exception(__METHOD__ . ': unable to register the session handler');
    }
  }
  public function regenerate() {
    global $user;

    // FIXME: Default backend will erase current user at session read time.
    // We need to get it out of there for good and avoid this ugly hack.
    if ($user->uid) {
      $account = $user;
    }
    if (!$this
      ->sessionIsEmpty()) {
      $currentData = $_SESSION;
    }
    if ($this->started) {
      $this
        ->destroy();
    }
    $this
      ->generateSessionIdentifier();
    if (isset($currentData) && !empty($currentData)) {
      $_SESSION = $currentData;
    }
    $this
      ->start();

    // See comment above.
    if (isset($account)) {
      $user = $account;
    }
    if ($this->started) {

      // Some PHP versions won't reset correctly the cookie.
      $params = session_get_cookie_params();
      $expire = $params['lifetime'] ? REQUEST_TIME + $params['lifetime'] : 0;
      setcookie($this->sessionName, $this->sessionIdentifier, $expire, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
    }

    // On session regenerate, the storage UID is set to the one read during
    // the first session read attempt: we need to advertise the backend that
    // future session write will be linked to a new UID.
    $this->storage
      ->setSessionUid($user->uid);
    $this
      ->refreshAfterSessionChange();
  }
  public function destroy() {
    session_destroy();

    // PHP 5.2 bug: When session_destroy() is called, you need to reset
    // session handlers, else PHP loose them. See
    // http://php.net/manual/en/function.session-set-save-handler.php#22194
    $this
      ->sessionSetHandlers();
    $this
      ->deleteCurrentSessionCookie();
  }
  public function destroyAllForUser($uid) {
    $this->storage
      ->destroyFor(array(
      'uid',
      $uid,
    ));
  }
  public function __construct(SessionProxy_Storage_Interface $storage) {
    $this->storage = $storage;
    $this
      ->sessionSetHandlers();
    parent::__construct();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SessionProxy_Backend_Base::$httpsEnabled protected property
SessionProxy_Backend_Base::$sessionIdentifier protected property
SessionProxy_Backend_Base::$sessionName protected property
SessionProxy_Backend_Base::$sessionNameUnsecure protected property
SessionProxy_Backend_Base::$started protected property
SessionProxy_Backend_Base::$userAccessUpdated protected property
SessionProxy_Backend_Base::commit public function Called during shutdown hook time, this allows you to perform additional operations outside of the core PHP session handling at the end of request. Overrides SessionProxy_Backend_Interface::commit
SessionProxy_Backend_Base::deleteCurrentSessionCookie public function Delete current session cookie Overrides SessionProxy_Backend_Interface::deleteCurrentSessionCookie
SessionProxy_Backend_Base::generateSessionIdentifier protected function Generate new session identifier, set it as PHP session identifier and return it.
SessionProxy_Backend_Base::isStarted public function Is session started. Overrides SessionProxy_Backend_Interface::isStarted
SessionProxy_Backend_Base::refreshAfterSessionChange protected function Refresh various information of the object right after session state change.
SessionProxy_Backend_Base::sessionIsEmpty protected function Is the current session is empty.
SessionProxy_Backend_Base::start public function Start session. Overrides SessionProxy_Backend_Interface::start
SessionProxy_Backend_Base::updateUser protected function Refresh global user data following the actual session state.
SessionProxy_Backend_Default::$doWrite protected property
SessionProxy_Backend_Default::$storage protected property
SessionProxy_Backend_Default::$uid protected property
SessionProxy_Backend_Default::destroy public function Destroy session if any Overrides SessionProxy_Backend_Base::destroy
SessionProxy_Backend_Default::destroyAllForUser public function Native implementation is opaque, and cannot allow us to index session: it is impossible to proceed with this cleaning. Overrides SessionProxy_Backend_Base::destroyAllForUser
SessionProxy_Backend_Default::destroyProxy public function Session destroy will require us to update the current logged in user.
SessionProxy_Backend_Default::getSessionUid protected function Get the actual logged in user user identifier if any. Overrides SessionProxy_Backend_Base::getSessionUid
SessionProxy_Backend_Default::handleHttps public function Does this instance handle HTTPS diverging session name. Overrides SessionProxy_Backend_Base::handleHttps
SessionProxy_Backend_Default::isWriteEnabled public function Is session write enabled. Overrides SessionProxy_Backend_Interface::isWriteEnabled
SessionProxy_Backend_Default::regenerate public function Regenerate the current session. Overrides SessionProxy_Backend_Interface::regenerate
SessionProxy_Backend_Default::sessionSetHandlers protected function
SessionProxy_Backend_Default::writeDisable public function Enable session writing explicitely. Overrides SessionProxy_Backend_Interface::writeDisable
SessionProxy_Backend_Default::writeEnable public function Disable session writing explicitely. Overrides SessionProxy_Backend_Interface::writeEnable
SessionProxy_Backend_Default::writeProxy public function Session write proxy that will allow us to disable session writing if we are master of the storage.
SessionProxy_Backend_Default::__construct public function Default constructor. Overrides SessionProxy_Backend_Base::__construct