123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <?php
- class sfCacheSessionStorage extends sfStorage
- {
- protected
- $context = null,
- $request = null,
- $response = null,
- $cache = null,
- $data = array(),
- $dataChanged = false;
-
- public function initialize($options = array())
- {
-
- parent::initialize(array_merge(array('session_name' => 'sfproject',
- 'session_cookie_lifetime' => '+30 days',
- 'session_cookie_path' => '/',
- 'session_cookie_domain' => null,
- 'session_cookie_secure' => false,
- 'session_cookie_http_only' => true,
- 'session_cookie_secret' => 'sf$ecret'), $options));
-
- if (isset($this->options['cache']) && $this->options['cache']['class'])
- {
- $this->cache = new $this->options['cache']['class'](is_array($this->options['cache']['param']) ? $this->options['cache']['param'] : array());
- }
- else
- {
- throw new InvalidArgumentException('sfCacheSessionStorage requires cache option.');
- }
- $this->context = sfContext::getInstance();
- $this->dispatcher = $this->context->getEventDispatcher();
- $this->request = $this->context->getRequest();
- $this->response = $this->context->getResponse();
- $cookie = $this->request->getCookie($this->options['session_name']);
- if(strpos($cookie, ':') !== false)
- {
-
- list($id, $signature) = explode(':', $cookie, 2);
- if($signature == sha1($id.':'.$this->options['session_cookie_secret']))
- {
-
- $this->id = $id;
- }
- else
- {
-
- $this->id = null;
- }
- }
- else
- {
-
- $this->id = null;
- }
- if(empty($this->id))
- {
- $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'localhost';
- $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'ua';
-
- $this->id = md5(rand(0, 999999).$ip.$ua.$this->options['session_cookie_secret']);
- if(sfConfig::get('sf_logging_enabled'))
- {
- $this->dispatcher->notify(new sfEvent($this, 'application.log', array('New session created')));
- }
-
- $this->response->setCookie($this->options['session_name'],
- $this->id.':'.sha1($this->id.':'.$this->options['session_cookie_secret']),
- $this->options['session_cookie_lifetime'],
- $this->options['session_cookie_path'],
- $this->options['session_cookie_domain'],
- $this->options['session_cookie_secure'],
- $this->options['session_cookie_http_only']);
- $this->data = array();
- }
- else
- {
-
- $this->data = $this->cache->get($this->id, array());
- if(sfConfig::get('sf_logging_enabled'))
- {
- $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Restored previous session')));
- }
- }
- return true;
- }
-
- public function write($key, $data)
- {
- $this->dataChanged = true;
- $this->data[$key] =& $data;
- }
-
- public function read($key)
- {
- $retval = null;
- if (isset($this->data[$key]))
- {
- $retval =& $this->data[$key];
- }
- return $retval;
- }
-
- public function remove($key)
- {
- $retval = null;
- if (isset($this->data[$key]))
- {
- $this->dataChanged = true;
- $retval =& $this->data[$key];
- unset($this->data[$key]);
- }
- return $retval;
- }
-
- public function regenerate($destroy = false)
- {
- if($destroy)
- {
- $this->data = array();
- $this->cache->remove($this->id);
- }
-
- $this->id = md5(rand(0, 999999).$_SERVER['REMOTE_ADDR'].$_SERVER['HTTP_USER_AGENT'].$this->options['session_cookie_secret']);
-
- $this->cache->set($this->id, $this->data);
-
- $this->response->setCookie($this->options['session_name'],
- $this->id.':'.sha1($this->id.':'.$this->options['session_cookie_secret']),
- $this->options['session_cookie_lifetime'],
- $this->options['session_cookie_path'],
- $this->options['session_cookie_domain'],
- $this->options['session_cookie_secure'],
- $this->options['session_cookie_http_only']);
- return true;
- }
-
- public function expire()
- {
-
- $this->regenerate(true);
- if(sfConfig::get('sf_logging_enabled'))
- {
- $this->dispatcher->notify(new sfEvent($this, 'application.log', array('new session created due to expiraton')));
- }
- }
-
- public function shutdown()
- {
-
- if($this->dataChanged === true)
- {
- $this->cache->set($this->id, $this->data);
- if(sfConfig::get('sf_logging_enabled'))
- {
- $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Storing session to cache')));
-
- }
- }
- }
- }
|