/** * Initializes this sfUser. * * Available options: * * * auto_shutdown: Whether to automatically save the changes to the session (true by default) * * culture: The user culture * * default_culture: The default user culture (en by default) * * use_flash: Whether to enable flash usage (false by default) * * logging: Whether to enable logging (false by default) * * @param sfEventDispatcher $dispatcher An sfEventDispatcher instance. * @param sfStorage $storage An sfStorage instance. * @param array $options An associative array of options. * * @return Boolean true, if initialization completes successfully, otherwise false. */ public function initialize(sfEventDispatcher $dispatcher, sfStorage $storage, $options = array()) { $this->dispatcher = $dispatcher; $this->storage = $storage; $this->options = array_merge(array('auto_shutdown' => true, 'culture' => null, 'default_culture' => 'en', 'use_flash' => false, 'logging' => false), $options); $this->attributeHolder = new sfNamespacedParameterHolder(self::ATTRIBUTE_NAMESPACE); // read attributes from storage $attributes = $storage->read(self::ATTRIBUTE_NAMESPACE); if (is_array($attributes)) { foreach ($attributes as $namespace => $values) { $this->attributeHolder->add($values, $namespace); } } // set the user culture to sf_culture parameter if present in the request // otherwise // - use the culture defined in the user session // - use the default culture set in settings.yml $currentCulture = $storage->read(self::CULTURE_NAMESPACE); $this->setCulture(null !== $this->options['culture'] ? $this->options['culture'] : (null !== $currentCulture ? $currentCulture : $this->options['default_culture'])); // flag current flash to be removed at shutdown if ($this->options['use_flash'] && ($names = $this->attributeHolder->getNames('symfony/user/sfUser/flash'))) { if ($this->options['logging']) { $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Flag old flash messages ("%s")', implode('", "', $names))))); } foreach ($names as $name) { $this->attributeHolder->set($name, true, 'symfony/user/sfUser/flash/remove'); } } }
/** * 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(); } }
/** * 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(); } }
/** * 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(); } }
<?php // auto-generated by sfFactoryConfigHandler // date: 2009/08/15 10:45:03 $this->controller = sfController::newInstance(sfConfig::get('sf_factory_controller', 'sfFrontWebController')); $this->request = sfRequest::newInstance(sfConfig::get('sf_factory_request', 'sfWebRequest')); $this->response = sfResponse::newInstance(sfConfig::get('sf_factory_response', 'sfWebResponse')); $this->storage = sfStorage::newInstance(sfConfig::get('sf_factory_storage', 'sfSessionStorage')); $this->user = sfUser::newInstance(sfConfig::get('sf_factory_user', 'myUser')); $this->controller->initialize($this); $this->request->initialize($this, sfConfig::get('sf_factory_request_parameters', NULL), sfConfig::get('sf_factory_request_attributes', array())); $this->response->initialize($this, sfConfig::get('sf_factory_response_parameters', NULL)); $this->storage->initialize($this, sfConfig::get('sf_factory_storage_parameters', array('session_name' => 'symfony'))); $this->user->initialize($this, sfConfig::get('sf_factory_user_parameters', NULL)); if (sfConfig::get('sf_cache')) { $this->viewCacheManager = new sfViewCacheManager(); $this->viewCacheManager->initialize($this, sfConfig::get('sf_factory_view_cache', 'sfFileCache'), sfConfig::get('sf_factory_view_cache_parameters', array('automaticCleaningFactor' => 0, 'cacheDir' => '/Users/takizo/Sites/coscupdemo/cache/frontend/dev/template'))); }
{ return $this->user; } public function getStorage() { return $this->storage; } } $t = new lime_test(33, new lime_output_color()); $_SERVER['session_id'] = 'test'; sfConfig::set('sf_test_cache_dir', sfToolkit::getTmpDir()); $context = new sfContext(); $request = new sfWebRequest(); $request->initialize($context); $context->request = $request; $storage = sfStorage::newInstance('sfSessionTestStorage'); $storage->initialize($context); $storage->clear(); $context->storage = $storage; $user = new sfUser(); $user->initialize($context); $context->user = $user; // ->initialize() $t->diag('->initialize()'); $t->is($user->getCulture(), 'en', '->initialize() sets the culture to "en" by default'); sfConfig::set('sf_i18n_default_culture', 'de'); $user->setCulture(null); user_flush($context); $t->is($user->getCulture(), 'de', '->initialize() sets the culture to the value of sf_i18n_default_culture if available'); sfConfig::set('sf_i18n_default_culture', 'fr'); user_flush($context);
/** * 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; }
public function getStorage() { $storage = sfStorage::newInstance('sfSessionTestStorage'); $storage->initialize($this); return $storage; }
{ } $context = new sfContext(); $storage = new myStorage(); $storage->initialize($context); // ::newInstance() $t->diag('::newInstance()'); $t->isa_ok(sfStorage::newInstance('myStorage'), 'myStorage', '::newInstance() takes a storage class as its first parameter'); $t->isa_ok(sfStorage::newInstance('myStorage'), 'myStorage', '::newInstance() returns an instance of myStorage'); try { sfStorage::newInstance('fakeStorage'); $t->fail('::newInstance() throws a sfFactoryException if the class does not extends sfStorage'); } catch (sfFactoryException $e) { $t->pass('::newInstance() throws a sfFactoryException if the class does not extends sfStorage'); } // ->initialize() $t->diag('->initialize()'); $storage = sfStorage::newInstance('myStorage'); $t->is($storage->getContext(), null, '->initialize() takes a sfContext object as its first argument'); $storage->initialize($context, array('foo' => 'bar')); $t->is($storage->getParameter('foo'), 'bar', '->initialize() takes an array of parameters as its second argument'); $storage = new myStorage(); $storage->initialize($context); // ->getContext() $t->diag('->getContext()'); $storage->initialize($context); $t->is($storage->getContext(), $context, '->getContext() returns the current context'); // parameter holder proxy require_once $_test_dir . '/unit/sfParameterHolderTest.class.php'; $pht = new sfParameterHolderProxyTest($t); $pht->launchTests($storage, 'parameter');