4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Session\Storage;
14 use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
15 use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeSessionHandler;
16 use Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy;
17 use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
18 use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
21 * This provides a base class for session attribute storage.
23 * @author Drak <drak@zikula.org>
25 class NativeSessionStorage implements SessionStorageInterface
28 * Array of SessionBagInterface.
30 * @var SessionBagInterface[]
37 protected $started = false;
42 protected $closed = false;
47 protected $saveHandler;
52 protected $metadataBag;
57 * Depending on how you want the storage driver to behave you probably
58 * want to override this constructor entirely.
60 * List of options for $options array with their defaults.
62 * @see http://php.net/session.configuration for options
63 * but we omit 'session.' from the beginning of the keys for convenience.
65 * ("auto_start", is not supported as it tells PHP to start a session before
66 * PHP starts to execute user-land code. Setting during runtime has no effect).
68 * cache_limiter, "" (use "0" to prevent headers from being sent entirely).
71 * cookie_lifetime, "0"
77 * gc_maxlifetime, "1440"
79 * hash_bits_per_character, "4"
83 * serialize_handler, "php"
84 * use_strict_mode, "0"
86 * use_only_cookies, "1"
88 * upload_progress.enabled, "1"
89 * upload_progress.cleanup, "1"
90 * upload_progress.prefix, "upload_progress_"
91 * upload_progress.name, "PHP_SESSION_UPLOAD_PROGRESS"
92 * upload_progress.freq, "1%"
93 * upload_progress.min-freq, "1"
94 * url_rewriter.tags, "a=href,area=href,frame=src,form=,fieldset="
96 * @param array $options Session configuration options
97 * @param AbstractProxy|NativeSessionHandler|\SessionHandlerInterface|null $handler
98 * @param MetadataBag $metaBag MetadataBag
100 public function __construct(array $options = array(), $handler = null, MetadataBag $metaBag = null)
102 session_cache_limiter(''); // disable by default because it's managed by HeaderBag (if used)
103 ini_set('session.use_cookies', 1);
105 session_register_shutdown();
107 $this->setMetadataBag($metaBag);
108 $this->setOptions($options);
109 $this->setSaveHandler($handler);
113 * Gets the save handler instance.
115 * @return AbstractProxy
117 public function getSaveHandler()
119 return $this->saveHandler;
125 public function start()
127 if ($this->started) {
131 if (\PHP_VERSION_ID >= 50400 && \PHP_SESSION_ACTIVE === session_status()) {
132 throw new \RuntimeException('Failed to start the session: already started by PHP.');
135 if (\PHP_VERSION_ID < 50400 && !$this->closed && isset($_SESSION) && session_id()) {
136 // not 100% fool-proof, but is the most reliable way to determine if a session is active in PHP 5.3
137 throw new \RuntimeException('Failed to start the session: already started by PHP ($_SESSION is set).');
140 if (ini_get('session.use_cookies') && headers_sent($file, $line)) {
141 throw new \RuntimeException(sprintf('Failed to start the session because headers have already been sent by "%s" at line %d.', $file, $line));
144 // ok to try and start the session
145 if (!session_start()) {
146 throw new \RuntimeException('Failed to start the session');
149 $this->loadSession();
150 if (!$this->saveHandler->isWrapper() && !$this->saveHandler->isSessionHandlerInterface()) {
151 // This condition matches only PHP 5.3 with internal save handlers
152 $this->saveHandler->setActive(true);
161 public function getId()
163 return $this->saveHandler->getId();
169 public function setId($id)
171 $this->saveHandler->setId($id);
177 public function getName()
179 return $this->saveHandler->getName();
185 public function setName($name)
187 $this->saveHandler->setName($name);
193 public function regenerate($destroy = false, $lifetime = null)
195 // Cannot regenerate the session ID for non-active sessions.
196 if (\PHP_VERSION_ID >= 50400 && \PHP_SESSION_ACTIVE !== session_status()) {
200 // Check if session ID exists in PHP 5.3
201 if (\PHP_VERSION_ID < 50400 && '' === session_id()) {
205 if (null !== $lifetime) {
206 ini_set('session.cookie_lifetime', $lifetime);
210 $this->metadataBag->stampNew();
213 $isRegenerated = session_regenerate_id($destroy);
215 // The reference to $_SESSION in session bags is lost in PHP7 and we need to re-create it.
216 // @see https://bugs.php.net/bug.php?id=70013
217 $this->loadSession();
219 return $isRegenerated;
225 public function save()
227 session_write_close();
229 if (!$this->saveHandler->isWrapper() && !$this->saveHandler->isSessionHandlerInterface()) {
230 // This condition matches only PHP 5.3 with internal save handlers
231 $this->saveHandler->setActive(false);
234 $this->closed = true;
235 $this->started = false;
241 public function clear()
243 // clear out the bags
244 foreach ($this->bags as $bag) {
248 // clear out the session
251 // reconnect the bags to the session
252 $this->loadSession();
258 public function registerBag(SessionBagInterface $bag)
260 if ($this->started) {
261 throw new \LogicException('Cannot register a bag when the session is already started.');
264 $this->bags[$bag->getName()] = $bag;
270 public function getBag($name)
272 if (!isset($this->bags[$name])) {
273 throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
276 if ($this->saveHandler->isActive() && !$this->started) {
277 $this->loadSession();
278 } elseif (!$this->started) {
282 return $this->bags[$name];
286 * Sets the MetadataBag.
288 * @param MetadataBag $metaBag
290 public function setMetadataBag(MetadataBag $metaBag = null)
292 if (null === $metaBag) {
293 $metaBag = new MetadataBag();
296 $this->metadataBag = $metaBag;
300 * Gets the MetadataBag.
302 * @return MetadataBag
304 public function getMetadataBag()
306 return $this->metadataBag;
312 public function isStarted()
314 return $this->started;
318 * Sets session.* ini variables.
320 * For convenience we omit 'session.' from the beginning of the keys.
321 * Explicitly ignores other ini keys.
323 * @param array $options Session ini directives array(key => value)
325 * @see http://php.net/session.configuration
327 public function setOptions(array $options)
329 $validOptions = array_flip(array(
330 'cache_limiter', 'cookie_domain', 'cookie_httponly',
331 'cookie_lifetime', 'cookie_path', 'cookie_secure',
332 'entropy_file', 'entropy_length', 'gc_divisor',
333 'gc_maxlifetime', 'gc_probability', 'hash_bits_per_character',
334 'hash_function', 'name', 'referer_check',
335 'serialize_handler', 'use_strict_mode', 'use_cookies',
336 'use_only_cookies', 'use_trans_sid', 'upload_progress.enabled',
337 'upload_progress.cleanup', 'upload_progress.prefix', 'upload_progress.name',
338 'upload_progress.freq', 'upload_progress.min-freq', 'url_rewriter.tags',
341 foreach ($options as $key => $value) {
342 if (isset($validOptions[$key])) {
343 ini_set('session.'.$key, $value);
349 * Registers session save handler as a PHP session handler.
351 * To use internal PHP session save handlers, override this method using ini_set with
352 * session.save_handler and session.save_path e.g.
354 * ini_set('session.save_handler', 'files');
355 * ini_set('session.save_path', '/tmp');
357 * or pass in a NativeSessionHandler instance which configures session.save_handler in the
358 * constructor, for a template see NativeFileSessionHandler or use handlers in
359 * composer package drak/native-session
361 * @see http://php.net/session-set-save-handler
362 * @see http://php.net/sessionhandlerinterface
363 * @see http://php.net/sessionhandler
364 * @see http://github.com/drak/NativeSession
366 * @param AbstractProxy|NativeSessionHandler|\SessionHandlerInterface|null $saveHandler
368 * @throws \InvalidArgumentException
370 public function setSaveHandler($saveHandler = null)
372 if (!$saveHandler instanceof AbstractProxy &&
373 !$saveHandler instanceof NativeSessionHandler &&
374 !$saveHandler instanceof \SessionHandlerInterface &&
375 null !== $saveHandler) {
376 throw new \InvalidArgumentException('Must be instance of AbstractProxy or NativeSessionHandler; implement \SessionHandlerInterface; or be null.');
379 // Wrap $saveHandler in proxy and prevent double wrapping of proxy
380 if (!$saveHandler instanceof AbstractProxy && $saveHandler instanceof \SessionHandlerInterface) {
381 $saveHandler = new SessionHandlerProxy($saveHandler);
382 } elseif (!$saveHandler instanceof AbstractProxy) {
383 $saveHandler = \PHP_VERSION_ID >= 50400 ?
384 new SessionHandlerProxy(new \SessionHandler()) : new NativeProxy();
386 $this->saveHandler = $saveHandler;
388 if ($this->saveHandler instanceof \SessionHandlerInterface) {
389 if (\PHP_VERSION_ID >= 50400) {
390 session_set_save_handler($this->saveHandler, false);
392 session_set_save_handler(
393 array($this->saveHandler, 'open'),
394 array($this->saveHandler, 'close'),
395 array($this->saveHandler, 'read'),
396 array($this->saveHandler, 'write'),
397 array($this->saveHandler, 'destroy'),
398 array($this->saveHandler, 'gc')
405 * Load the session with attributes.
407 * After starting the session, PHP retrieves the session from whatever handlers
408 * are set to (either PHP's internal, or a custom save handler set with session_set_save_handler()).
409 * PHP takes the return value from the read() handler, unserializes it
410 * and populates $_SESSION with the result automatically.
412 * @param array|null $session
414 protected function loadSession(array &$session = null)
416 if (null === $session) {
417 $session = &$_SESSION;
420 $bags = array_merge($this->bags, array($this->metadataBag));
422 foreach ($bags as $bag) {
423 $key = $bag->getStorageKey();
424 $session[$key] = isset($session[$key]) ? $session[$key] : array();
425 $bag->initialize($session[$key]);
428 $this->started = true;
429 $this->closed = false;