2011-11-29 04:35:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2012-02-08 23:15:47 +00:00
|
|
|
namespace Symfony\Component\HttpFoundation\Session\Storage;
|
2011-11-29 04:35:09 +00:00
|
|
|
|
2012-02-08 23:15:47 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
|
2012-03-03 02:54:20 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
|
2011-11-29 04:35:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This provides a base class for session attribute storage.
|
|
|
|
*
|
|
|
|
* @author Drak <drak@zikula.org>
|
|
|
|
*/
|
2012-03-03 02:54:20 +00:00
|
|
|
class SessionStorage implements SessionStorageInterface
|
2011-11-29 04:35:09 +00:00
|
|
|
{
|
|
|
|
/**
|
2012-02-08 09:46:38 +00:00
|
|
|
* Array of SessionBagInterface
|
|
|
|
*
|
|
|
|
* @var array
|
2011-11-29 04:35:09 +00:00
|
|
|
*/
|
2012-02-08 09:46:38 +00:00
|
|
|
protected $bags;
|
2011-11-29 04:35:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2012-01-02 15:32:41 +00:00
|
|
|
protected $options = array();
|
2011-11-29 04:35:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $started = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $closed = false;
|
|
|
|
|
2012-03-03 02:54:20 +00:00
|
|
|
/**
|
|
|
|
* @var AbstractProxy
|
|
|
|
*/
|
|
|
|
protected $saveHandler;
|
|
|
|
|
2011-11-29 04:35:09 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* Depending on how you want the storage driver to behave you probably
|
|
|
|
* want top override this constructor entirely.
|
|
|
|
*
|
|
|
|
* List of options for $options array with their defaults.
|
2012-02-22 18:05:55 +00:00
|
|
|
* @see http://php.net/session.configuration for options
|
|
|
|
* but we omit 'session.' from the beginning of the keys for convenience.
|
2011-11-29 04:35:09 +00:00
|
|
|
*
|
|
|
|
* auto_start, "0"
|
2012-02-22 18:05:55 +00:00
|
|
|
* cache_limiter, "nocache" (use "0" to prevent headers from being sent entirely).
|
2011-11-29 04:35:09 +00:00
|
|
|
* cookie_domain, ""
|
|
|
|
* cookie_httponly, ""
|
|
|
|
* cookie_lifetime, "0"
|
|
|
|
* cookie_path, "/"
|
|
|
|
* cookie_secure, ""
|
|
|
|
* entropy_file, ""
|
|
|
|
* entropy_length, "0"
|
|
|
|
* gc_divisor, "100"
|
|
|
|
* gc_maxlifetime, "1440"
|
|
|
|
* gc_probability, "1"
|
|
|
|
* hash_bits_per_character, "4"
|
|
|
|
* hash_function, "0"
|
|
|
|
* name, "PHPSESSID"
|
|
|
|
* referer_check, ""
|
|
|
|
* serialize_handler, "php"
|
|
|
|
* use_cookies, "1"
|
|
|
|
* use_only_cookies, "1"
|
|
|
|
* use_trans_sid, "0"
|
|
|
|
* upload_progress.enabled, "1"
|
|
|
|
* upload_progress.cleanup, "1"
|
|
|
|
* upload_progress.prefix, "upload_progress_"
|
|
|
|
* upload_progress.name, "PHP_SESSION_UPLOAD_PROGRESS"
|
|
|
|
* upload_progress.freq, "1%"
|
|
|
|
* upload_progress.min-freq, "1"
|
|
|
|
* url_rewriter.tags, "a=href,area=href,frame=src,form=,fieldset="
|
|
|
|
*
|
2012-02-22 18:05:55 +00:00
|
|
|
* @param array $options Session configuration options.
|
2012-03-03 02:54:20 +00:00
|
|
|
* @param $handler SessionHandlerInterface.
|
2011-11-29 04:35:09 +00:00
|
|
|
*/
|
2012-03-03 02:54:20 +00:00
|
|
|
public function __construct(array $options = array(), $handler = null)
|
2011-11-29 04:35:09 +00:00
|
|
|
{
|
|
|
|
$this->setOptions($options);
|
2012-03-03 02:54:20 +00:00
|
|
|
|
|
|
|
$this->setSaveHandler($handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the save handler instance.
|
|
|
|
*
|
|
|
|
* @return AbstractProxy
|
|
|
|
*/
|
|
|
|
public function getSaveHandler()
|
|
|
|
{
|
|
|
|
return $this->saveHandler;
|
2011-11-29 04:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function start()
|
|
|
|
{
|
|
|
|
if ($this->started && !$this->closed) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-02 15:32:41 +00:00
|
|
|
if ($this->options['use_cookies'] && headers_sent()) {
|
|
|
|
throw new \RuntimeException('Failed to start the session because header have already been sent.');
|
|
|
|
}
|
|
|
|
|
2011-11-29 04:35:09 +00:00
|
|
|
// start the session
|
|
|
|
if (!session_start()) {
|
|
|
|
throw new \RuntimeException('Failed to start the session');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->loadSession();
|
|
|
|
|
2012-03-03 02:54:20 +00:00
|
|
|
if (!$this->saveHandler->isWrapper() && !$this->saveHandler->isSessionHandlerInterface()) {
|
|
|
|
$this->saveHandler->setActive(false);
|
|
|
|
}
|
|
|
|
|
2011-11-29 04:35:09 +00:00
|
|
|
$this->started = true;
|
|
|
|
$this->closed = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getId()
|
|
|
|
{
|
|
|
|
if (!$this->started) {
|
|
|
|
return ''; // returning empty is consistent with session_id() behaviour
|
|
|
|
}
|
|
|
|
|
|
|
|
return session_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* {@inheritdoc}
|
2011-11-29 04:35:09 +00:00
|
|
|
*/
|
|
|
|
public function regenerate($destroy = false)
|
|
|
|
{
|
|
|
|
return session_regenerate_id($destroy);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function save()
|
|
|
|
{
|
|
|
|
session_write_close();
|
2012-03-03 02:54:20 +00:00
|
|
|
|
|
|
|
if (!$this->saveHandler->isWrapper() && !$this->getSaveHandler()->isSessionHandlerInterface()) {
|
|
|
|
$this->saveHandler->setActive(false);
|
|
|
|
}
|
|
|
|
|
2011-11-29 04:35:09 +00:00
|
|
|
$this->closed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function clear()
|
|
|
|
{
|
|
|
|
// clear out the bags
|
2012-02-08 09:46:38 +00:00
|
|
|
foreach ($this->bags as $bag) {
|
|
|
|
$bag->clear();
|
|
|
|
}
|
2011-11-29 04:35:09 +00:00
|
|
|
|
|
|
|
// clear out the session
|
|
|
|
$_SESSION = array();
|
|
|
|
|
|
|
|
// reconnect the bags to the session
|
|
|
|
$this->loadSession();
|
|
|
|
}
|
|
|
|
|
2012-02-08 09:46:38 +00:00
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* {@inheritdoc}
|
2012-02-08 09:46:38 +00:00
|
|
|
*/
|
|
|
|
public function registerBag(SessionBagInterface $bag)
|
|
|
|
{
|
|
|
|
$this->bags[$bag->getName()] = $bag;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* {@inheritdoc}
|
2012-02-08 09:46:38 +00:00
|
|
|
*/
|
|
|
|
public function getBag($name)
|
|
|
|
{
|
|
|
|
if (!isset($this->bags[$name])) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->options['auto_start'] && !$this->started) {
|
|
|
|
$this->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->bags[$name];
|
|
|
|
}
|
|
|
|
|
2011-11-29 04:35:09 +00:00
|
|
|
/**
|
|
|
|
* Sets session.* ini variables.
|
|
|
|
*
|
|
|
|
* For convenience we omit 'session.' from the beginning of the keys.
|
|
|
|
* Explicitly ignores other ini keys.
|
|
|
|
*
|
|
|
|
* session_get_cookie_params() overrides values.
|
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
*
|
2012-02-22 18:05:55 +00:00
|
|
|
* @see http://php.net/session.configuration
|
2011-11-29 04:35:09 +00:00
|
|
|
*/
|
|
|
|
protected function setOptions(array $options)
|
|
|
|
{
|
2012-02-23 10:53:14 +00:00
|
|
|
$this->options = $options;
|
|
|
|
|
|
|
|
// set defaults for certain values
|
|
|
|
$defaults = array(
|
|
|
|
'cache_limiter' => '', // disable by default because it's managed by HeaderBag (if used)
|
2012-02-29 14:34:20 +00:00
|
|
|
'auto_start' => false,
|
2012-02-23 10:53:14 +00:00
|
|
|
'use_cookies' => true,
|
|
|
|
'cookie_httponly' => true,
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($defaults as $key => $value) {
|
|
|
|
if (!isset($this->options[$key])) {
|
|
|
|
$this->options[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
2012-01-02 15:32:41 +00:00
|
|
|
|
2011-11-29 04:35:09 +00:00
|
|
|
foreach ($this->options as $key => $value) {
|
|
|
|
if (in_array($key, array(
|
2012-02-19 15:45:25 +00:00
|
|
|
'auto_start', 'cache_limiter', 'cookie_domain', 'cookie_httponly',
|
2011-11-29 04:35:09 +00:00
|
|
|
'cookie_lifetime', 'cookie_path', 'cookie_secure',
|
|
|
|
'entropy_file', 'entropy_length', 'gc_divisor',
|
|
|
|
'gc_maxlifetime', 'gc_probability', 'hash_bits_per_character',
|
|
|
|
'hash_function', 'name', 'referer_check',
|
2012-03-03 02:54:20 +00:00
|
|
|
'serialize_handler', 'use_cookies',
|
2011-11-29 04:35:09 +00:00
|
|
|
'use_only_cookies', 'use_trans_sid', 'upload_progress.enabled',
|
|
|
|
'upload_progress.cleanup', 'upload_progress.prefix', 'upload_progress.name',
|
|
|
|
'upload_progress.freq', 'upload_progress.min-freq', 'url_rewriter.tags'))) {
|
|
|
|
ini_set('session.'.$key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-03 02:54:20 +00:00
|
|
|
* Registers save handler as a PHP session handler.
|
2011-11-29 04:35:09 +00:00
|
|
|
*
|
2012-02-22 18:05:55 +00:00
|
|
|
* To use internal PHP session save handlers, override this method using ini_set with
|
2011-11-29 04:35:09 +00:00
|
|
|
* session.save_handlers and session.save_path e.g.
|
|
|
|
*
|
|
|
|
* ini_set('session.save_handlers', 'files');
|
|
|
|
* ini_set('session.save_path', /tmp');
|
|
|
|
*
|
2012-02-22 18:05:55 +00:00
|
|
|
* @see http://php.net/session-set-save-handler
|
|
|
|
* @see http://php.net/sessionhandlerinterface
|
2012-03-03 02:54:20 +00:00
|
|
|
* @see http://php.net/sessionhandler
|
|
|
|
*
|
|
|
|
* @param object $saveHandler
|
2011-11-29 04:35:09 +00:00
|
|
|
*/
|
2012-03-03 02:54:20 +00:00
|
|
|
public function setSaveHandler($saveHandler)
|
2011-11-29 04:35:09 +00:00
|
|
|
{
|
2012-03-03 02:54:20 +00:00
|
|
|
// Wrap $saveHandler in proxy
|
|
|
|
if (!$saveHandler instanceof AbstractProxy && $saveHandler instanceof \SessionHandlerInterface) {
|
|
|
|
$saveHandler = new SessionHandlerProxy($saveHandler);
|
|
|
|
} else {
|
|
|
|
$saveHandler = new NativeProxy($saveHandler);
|
2011-11-29 04:35:09 +00:00
|
|
|
}
|
|
|
|
|
2012-03-03 02:54:20 +00:00
|
|
|
$this->saveHandler = $saveHandler;
|
|
|
|
|
|
|
|
if ($this->saveHandler instanceof \SessionHandlerInterface) {
|
|
|
|
if (version_compare(phpversion(), '5.4.0', '>=')) {
|
|
|
|
session_set_save_handler($this->saveHandler, true);
|
|
|
|
} else {
|
|
|
|
session_set_save_handler(
|
|
|
|
array($this->saveHandler, 'open'),
|
|
|
|
array($this->saveHandler, 'close'),
|
|
|
|
array($this->saveHandler, 'read'),
|
|
|
|
array($this->saveHandler, 'write'),
|
|
|
|
array($this->saveHandler, 'destroy'),
|
|
|
|
array($this->saveHandler, 'gc')
|
|
|
|
);
|
|
|
|
|
|
|
|
register_shutdown_function('session_write_close');
|
|
|
|
}
|
|
|
|
}
|
2011-11-29 04:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the session with attributes.
|
|
|
|
*
|
|
|
|
* After starting the session, PHP retrieves the session from whatever handlers
|
2012-02-22 18:05:55 +00:00
|
|
|
* are set to (either PHP's internal, or a custom save handler set with session_set_save_handler()).
|
|
|
|
* PHP takes the return value from the read() handler, unserializes it
|
2011-11-29 04:35:09 +00:00
|
|
|
* and populates $_SESSION with the result automatically.
|
2012-02-12 00:54:16 +00:00
|
|
|
*
|
|
|
|
* @param array|null $session
|
2011-11-29 04:35:09 +00:00
|
|
|
*/
|
2012-02-08 09:46:38 +00:00
|
|
|
protected function loadSession(array &$session = null)
|
2011-11-29 04:35:09 +00:00
|
|
|
{
|
2012-02-08 09:46:38 +00:00
|
|
|
if (null === $session) {
|
|
|
|
$session = &$_SESSION;
|
|
|
|
}
|
2011-11-29 04:35:09 +00:00
|
|
|
|
2012-02-08 09:46:38 +00:00
|
|
|
foreach ($this->bags as $bag) {
|
|
|
|
$key = $bag->getStorageKey();
|
|
|
|
$session[$key] = isset($session[$key]) ? $session[$key] : array();
|
|
|
|
$bag->initialize($session[$key]);
|
|
|
|
}
|
2011-11-29 04:35:09 +00:00
|
|
|
}
|
|
|
|
}
|