2011-08-18 19:56:29 -05:00
|
|
|
<?php namespace Laravel\Session;
|
2011-08-02 21:05:20 -05:00
|
|
|
|
2011-08-25 22:53:05 -05:00
|
|
|
use Laravel\Security\Crypter;
|
2011-08-02 21:05:20 -05:00
|
|
|
|
2011-08-19 20:12:39 -05:00
|
|
|
class Cookie extends Driver {
|
2011-08-02 21:05:20 -05:00
|
|
|
|
2011-08-25 22:53:05 -05:00
|
|
|
/**
|
|
|
|
|
* The cookie engine instance.
|
|
|
|
|
*
|
2011-08-30 22:09:47 -05:00
|
|
|
* @var Cookie
|
2011-08-25 22:53:05 -05:00
|
|
|
*/
|
|
|
|
|
private $cookie;
|
|
|
|
|
|
2011-08-15 20:11:21 -05:00
|
|
|
/**
|
|
|
|
|
* The Crypter instance.
|
|
|
|
|
*
|
|
|
|
|
* @var Crypter
|
|
|
|
|
*/
|
|
|
|
|
private $crypter;
|
|
|
|
|
|
2011-08-26 21:42:04 -05:00
|
|
|
/**
|
|
|
|
|
* The session configuration array.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
private $config;
|
|
|
|
|
|
2011-08-15 20:11:21 -05:00
|
|
|
/**
|
|
|
|
|
* Create a new Cookie session driver instance.
|
|
|
|
|
*
|
2011-08-31 00:07:45 -05:00
|
|
|
* @param Crypter $crypter
|
2011-08-26 00:01:08 -05:00
|
|
|
* @param Laravel\Cookie $cookie
|
2011-08-31 00:07:45 -05:00
|
|
|
* @param array $config
|
2011-08-15 20:11:21 -05:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2011-08-26 21:42:04 -05:00
|
|
|
public function __construct(Crypter $crypter, \Laravel\Cookie $cookie, $config)
|
2011-08-02 21:05:20 -05:00
|
|
|
{
|
2011-08-25 22:53:05 -05:00
|
|
|
$this->cookie = $cookie;
|
2011-08-26 21:42:04 -05:00
|
|
|
$this->config = $config;
|
2011-08-25 22:53:05 -05:00
|
|
|
$this->crypter = $crypter;
|
2011-08-02 21:05:20 -05:00
|
|
|
}
|
|
|
|
|
|
2011-08-25 22:53:05 -05:00
|
|
|
/**
|
|
|
|
|
* Load a session by ID.
|
|
|
|
|
*
|
|
|
|
|
* The session will be retrieved from persistant storage and returned as an array.
|
|
|
|
|
* The array contains the session ID, last activity UNIX timestamp, and session data.
|
|
|
|
|
*
|
|
|
|
|
* @param string $id
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2011-08-19 20:12:39 -05:00
|
|
|
protected function load($id)
|
2011-08-02 21:05:20 -05:00
|
|
|
{
|
2011-08-25 22:53:05 -05:00
|
|
|
if ($this->cookie->has('session_payload'))
|
2011-08-02 21:05:20 -05:00
|
|
|
{
|
2011-08-25 22:53:05 -05:00
|
|
|
return unserialize($this->crypter->decrypt($this->cookie->get('session_payload')));
|
2011-08-02 21:05:20 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-25 22:53:05 -05:00
|
|
|
/**
|
|
|
|
|
* Save the session to persistant storage.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2011-08-19 20:12:39 -05:00
|
|
|
protected function save()
|
2011-08-02 21:05:20 -05:00
|
|
|
{
|
2011-08-08 10:47:41 -05:00
|
|
|
if ( ! headers_sent())
|
|
|
|
|
{
|
2011-08-26 21:42:04 -05:00
|
|
|
extract($this->config);
|
2011-08-02 21:05:20 -05:00
|
|
|
|
2011-08-19 20:12:39 -05:00
|
|
|
$payload = $this->crypter->encrypt(serialize($this->session));
|
2011-08-15 20:11:21 -05:00
|
|
|
|
2011-08-25 22:53:05 -05:00
|
|
|
$this->cookie->put('session_payload', $payload, $lifetime, $path, $domain, $https, $http_only);
|
2011-08-08 10:47:41 -05:00
|
|
|
}
|
2011-08-02 21:05:20 -05:00
|
|
|
}
|
|
|
|
|
|
2011-08-25 22:53:05 -05:00
|
|
|
/**
|
|
|
|
|
* Delete the session from persistant storage.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2011-08-19 20:12:39 -05:00
|
|
|
protected function delete()
|
2011-08-02 21:05:20 -05:00
|
|
|
{
|
2011-08-25 22:53:05 -05:00
|
|
|
$this->cookie->forget('session_payload');
|
2011-08-02 21:05:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|