/**
  * Available options:
  *
  *  * session_name:            The cookie name (symfony by default)
  *  * session_id:              The session id (null by default)
  *  * auto_start:              Whether to start the session (true by default)
  *  * session_cookie_lifetime: Cookie lifetime
  *  * session_cookie_path:     Cookie path
  *  * session_cookie_domain:   Cookie domain
  *  * session_cookie_secure:   Cookie secure
  *  * session_cookie_httponly: Cookie http only (only for PHP >= 5.2)
  *
  * The default values for all 'session_cookie_*' options are those returned by the session_get_cookie_params() function
  *
  * @param array $options  An associative array of options
  *
  * @see sfStorage
  */
 public function initialize($options = null)
 {
     $cookieDefaults = session_get_cookie_params();
     $options = array_merge(array('session_name' => 'symfony', 'session_id' => null, 'auto_start' => true, 'session_cookie_lifetime' => $cookieDefaults['lifetime'], 'session_cookie_path' => $cookieDefaults['path'], 'session_cookie_domain' => $cookieDefaults['domain'], 'session_cookie_secure' => $cookieDefaults['secure'], 'session_cookie_httponly' => isset($cookieDefaults['httponly']) ? $cookieDefaults['httponly'] : false, 'session_cache_limiter' => 'none'), $options);
     // initialize parent
     parent::initialize($options);
     // set session name
     $sessionName = $this->options['session_name'];
     session_name($sessionName);
     if (!(bool) ini_get('session.use_cookies') && ($sessionId = $this->options['session_id'])) {
         session_id($sessionId);
     }
     $lifetime = $this->options['session_cookie_lifetime'];
     $path = $this->options['session_cookie_path'];
     $domain = $this->options['session_cookie_domain'];
     $secure = $this->options['session_cookie_secure'];
     $httpOnly = $this->options['session_cookie_httponly'];
     session_set_cookie_params($lifetime, $path, $domain, $secure, $httpOnly);
     if (!is_null($this->options['session_cache_limiter'])) {
         session_cache_limiter($this->options['session_cache_limiter']);
     }
     if ($this->options['auto_start'] && !self::$sessionStarted) {
         session_start();
         self::$sessionStarted = true;
     }
 }
 /**
  * Initializes this Storage instance.
  *
  * @param sfContext A sfContext instance
  * @param array   An associative array of initialization parameters
  *
  * @return boolean true, if initialization completes successfully, otherwise false
  *
  * @throws <b>sfInitializationException</b> If an error occurs while initializing this Storage
  */
 public function initialize($context, $parameters = null)
 {
     // initialize parent
     parent::initialize($context, $parameters);
     // set session name
     $sessionName = $this->getParameterHolder()->get('session_name', 'symfony');
     session_name($sessionName);
     $use_cookies = (bool) ini_get('session.use_cookies');
     if (!$use_cookies) {
         $sessionId = $context->getRequest()->getParameter($sessionName, '');
         if ($sessionId != '') {
             session_id($sessionId);
         }
     }
     $cookieDefaults = session_get_cookie_params();
     $lifetime = $this->getParameter('session_cookie_lifetime', sfConfig::get('sf_timeout'));
     $path = $this->getParameter('session_cookie_path', $cookieDefaults['path']);
     $domain = $this->getParameter('session_cookie_domain', $cookieDefaults['domain']);
     $secure = $this->getParameter('session_cookie_secure', $cookieDefaults['secure']);
     $httpOnly = $this->getParameter('session_cookie_httponly', isset($cookieDefaults['httponly']) ? $cookieDefaults['httponly'] : false);
     if (version_compare(phpversion(), '5.2', '>=')) {
         session_set_cookie_params($lifetime, $path, $domain, $secure, $httpOnly);
     } else {
         session_set_cookie_params($lifetime, $path, $domain, $secure);
     }
     if ($this->getParameter('auto_start', true)) {
         // start our session
         session_start();
     }
 }
Пример #3
0
 /**
  * Initializes this Storage instance.
  *
  * @param sfContext A sfContext instance
  * @param array   An associative array of initialization parameters
  *
  * @return boolean true, if initialization completes successfully, otherwise false
  *
  * @throws <b>sfInitializationException</b> If an error occurs while initializing this Storage
  */
 public function initialize($context, $parameters = null)
 {
     // initialize parent
     parent::initialize($context, $parameters);
     // maintain compatiblity with sfSessionStorage which always sent expiry headers
     header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
     header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
     header('Pragma: no-cache');
 }
 /**
  * Initialize this Storage.
  *
  * @param array $options  An associative array of initialization parameters.
  *                        session_name [required] name of session to use
  *                        session_cookie_path [required] cookie path
  *                        session_cookie_domain [required] cookie domain
  *                        session_cookie_lifetime [required] liftime of cookie
  *                        session_cookie_secure [required] send only if secure connection
  *                        session_cookie_http_only [required] accessible only via http protocol
  *
  * @return bool true, when initialization completes successfully.
  *
  * @throws <b>sfInitializationException</b> If an error occurs while initializing this Storage.
  */
 public function initialize($options = array())
 {
     // initialize parent
     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));
     // create cache instance
     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) {
         // split cookie data id:signature(id+secret)
         list($id, $signature) = explode(':', $cookie, 2);
         if ($signature == sha1($id . ':' . $this->options['session_cookie_secret'])) {
             // cookie is valid
             $this->id = $id;
         } else {
             // cookie signature broken
             $this->id = null;
         }
     } else {
         // cookie format wrong
         $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';
         // generate new id based on random # / ip / user agent / secret
         $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')));
         }
         // only send cookie when id is issued
         $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 {
         // load data from cache
         $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')));
         }
     }
     session_id($this->id);
     $this->response->addCacheControlHttpHeader('private');
     return true;
 }
 /**
  * Initializes this Storage instance.
  *
  * @param sfContext A sfContext instance
  * @param array   An associative array of initialization parameters
  *
  * @return boolean true, if initialization completes successfully, otherwise false
  *
  * @throws <b>sfInitializationException</b> If an error occurs while initializing this Storage
  */
 public function initialize($context, $parameters = null)
 {
     // initialize parent
     parent::initialize($context, $parameters);
     $this->sessionPath = sfConfig::get('sf_test_cache_dir') . DIRECTORY_SEPARATOR . 'sessions';
     if (array_key_exists('session_id', $_SERVER)) {
         $this->sessionId = $_SERVER['session_id'];
         // we read session data from temp file
         $file = $this->sessionPath . DIRECTORY_SEPARATOR . $this->sessionId . '.session';
         $this->sessionData = file_exists($file) ? unserialize(file_get_contents($file)) : array();
     } else {
         $this->sessionId = md5(uniqid(rand(), true));
         $this->sessionData = array();
     }
 }
 /**
  * Available options:
  *
  *  * session_path: The path to store the session files (%SF_TEST_CACHE_DIR%/sessions by default)
  *  * session_id:   The session identifier
  *
  * @param array $options  An associative array of options
  *
  * @see sfStorage
  */
 public function initialize($options = null)
 {
     $options = array_merge(array('session_path' => sfConfig::get('sf_test_cache_dir') . '/sessions', 'session_id' => null), $options);
     // initialize parent
     parent::initialize($options);
     $this->sessionId = !is_null($this->options['session_id']) ? $this->options['session_id'] : (array_key_exists('session_id', $_SERVER) ? $_SERVER['session_id'] : null);
     if ($this->sessionId) {
         // we read session data from temp file
         $file = $this->options['session_path'] . DIRECTORY_SEPARATOR . $this->sessionId . '.session';
         $this->sessionData = file_exists($file) ? unserialize(file_get_contents($file)) : array();
     } else {
         $this->sessionId = md5(uniqid(rand(), true));
         $this->sessionData = array();
     }
 }
Пример #7
0
 /**
  * Available options:
  *
  *  * session_path: The path to store the session files
  *  * session_id:   The session identifier
  *
  * @param array $options  An associative array of options
  *
  * @see sfStorage
  */
 public function initialize($options = null)
 {
     if (!isset($options['session_path'])) {
         throw new InvalidArgumentException('The "session_path" option is mandatory for the sfSessionTestStorage class.');
     }
     $options = array_merge(array('session_id' => null), $options);
     // initialize parent
     parent::initialize($options);
     $this->sessionId = null !== $this->options['session_id'] ? $this->options['session_id'] : (array_key_exists('session_id', $_SERVER) ? $_SERVER['session_id'] : null);
     if ($this->sessionId) {
         // we read session data from temp file
         $file = $this->options['session_path'] . DIRECTORY_SEPARATOR . $this->sessionId . '.session';
         $this->sessionData = file_exists($file) ? unserialize(file_get_contents($file)) : array();
     } else {
         $this->sessionId = md5(uniqid(rand(), true));
         $this->sessionData = array();
     }
 }
 /**
  * Initialize this Storage.
  *
  * @param array $options  An associative array of initialization parameters.
  *                        session_name [required] name of session to use
  *                        session_cookie_path [required] cookie path
  *                        session_cookie_domain [required] cookie domain
  *                        session_cookie_lifetime [required] liftime of cookie
  *                        session_cookie_secure [required] send only if secure connection
  *                        session_cookie_http_only [required] accessible only via http protocol
  *
  * @return bool true, when initialization completes successfully.
  *
  * @throws <b>sfInitializationException</b> If an error occurs while initializing this Storage.
  */
 public function initialize($options = array())
 {
     // initialize parent
     // bc with a slightly different name formerly used here, let's be
     // compatible with the base class name for it from here on out
     if (isset($options['session_cookie_http_only'])) {
         $options['session_cookie_httponly'] = $options['session_cookie_http_only'];
     }
     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_httponly' => true, 'session_cookie_secret' => 'sf$ecret'), $options));
     // create cache instance
     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) {
         // split cookie data id:signature(id+secret)
         list($id, $signature) = explode(':', $cookie, 2);
         if ($signature == sha1($id . ':' . $this->options['session_cookie_secret'])) {
             // cookie is valid
             $this->id = $id;
         } else {
             // cookie signature broken
             $this->id = null;
         }
     } else {
         // cookie format wrong
         $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';
         // generate new id based on random # / ip / user agent / secret
         $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')));
         }
         // only send cookie when id is issued
         $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_httponly']);
         $this->data = array();
     } else {
         // load data from cache. Watch out for the default case. We could
         // serialize(array()) as the default to the call but that would be a performance hit
         $raw = $this->cache->get($this->id, null);
         if (is_null($raw)) {
             $this->data = array();
         } else {
             $data = @unserialize($raw);
             // We test 'b:0' special case, because such a string would result
             // in $data being === false, while raw is serialized
             // see http://stackoverflow.com/questions/1369936/check-to-see-if-a-string-is-serialized
             if ($raw === 'b:0;' || $data !== false) {
                 $this->data = $data;
             } else {
                 // Probably an old cached value (BC)
                 $this->data = $raw;
             }
         }
         if (sfConfig::get('sf_logging_enabled')) {
             $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Restored previous session')));
         }
     }
     session_id($this->id);
     $this->response->addCacheControlHttpHeader('private');
     return true;
 }