Cache.php in Session Proxy 7
File
lib/SessionProxy/Storage/Cache.php
View source
<?php
class SessionProxy_Storage_Cache extends SessionProxy_Storage_Base {
protected $cacheBackend;
protected $prefix = 'session_';
protected function getCid($sessionId) {
return $this->prefix . $sessionId;
}
public function open() {
return TRUE;
}
public function close() {
return TRUE;
}
public function read($sessionId) {
global $user;
if (!isset($_COOKIE[$this->sessionName])) {
$user = drupal_anonymous_user();
return '';
}
$cid = $this
->getCid($sessionId);
if ($cached = $this->cacheBackend
->get($cid)) {
$data = $cached->data;
if ($data && $data->uid > 0) {
$this->uid = $data->uid;
$serializedData = $data->session;
}
else {
$serializedData = '';
}
$this
->sessionDataSetHash($sessionId, $serializedData);
return $serializedData;
}
else {
return NULL;
}
}
public function write($sessionId, $serializedData) {
global $user;
try {
if ($this
->sessionDataHasChanged($sessionId, $serializedData)) {
$cid = $this
->getCid($sessionId);
$data = new stdClass();
$data->uid = $this->uid;
$data->session = $serializedData;
$this->cacheBackend
->set($cid, $data);
}
return TRUE;
} catch (Exception $exception) {
require_once DRUPAL_ROOT . '/includes/errors.inc';
if (error_displayable()) {
print '<h1>Uncaught exception thrown in session handler.</h1>';
print '<p>' . _drupal_render_exception_safe($exception) . '</p><hr />';
}
return FALSE;
}
}
public function destroy($sessionId) {
$cid = $this
->getCid($sessionId);
$this->cacheBackend
->clear($cid);
$_SESSION = array();
$this
->setSessionUid(NULL);
return TRUE;
}
public function gc($lifetime) {
$this->cacheBackend
->clear();
return TRUE;
}
public function handleHttps() {
return FALSE;
}
public function destroyFor($index, $value) {
}
public function __construct(array $options = array()) {
parent::__construct($options);
$bin = isset($this->options['cache_bin']) ? $this->options['cache_bin'] : 'cache_sessions';
if (!isset($this->options['cache_backend']) || !class_exists($this->options['cache_backend'])) {
$this->cacheBackend = new DrupalDatabaseCache($bin);
}
else {
$class = $this->options['cache_backend'];
$this->cacheBackend = new $class($bin);
}
}
}