Commit f610527a by Qiang Xue

finished session components.

parent a8a3e1a9
<?php <?php
/** /**
* DbSession class file. * CacheSession class file.
* *
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC * @copyright Copyright &copy; 2008 Yii Software LLC
...@@ -9,61 +9,69 @@ ...@@ -9,61 +9,69 @@
namespace yii\web; namespace yii\web;
use Yii;
use yii\caching\Cache;
use yii\base\InvalidConfigException;
/** /**
* CacheSession implements a session component using cache as storage medium. * CacheSession implements a session component using cache as storage medium.
* *
* The cache being used can be any cache application component implementing {@link ICache} interface. * The cache being used can be any cache application component.
* The ID of the cache application component is specified via {@link cacheID}, which defaults to 'cache'. * The ID of the cache application component is specified via [[cacheID]], which defaults to 'cache'.
* *
* Beware, by definition cache storage are volatile, which means the data stored on them * Beware, by definition cache storage are volatile, which means the data stored on them
* may be swapped out and get lost. Therefore, you must make sure the cache used by this component * may be swapped out and get lost. Therefore, you must make sure the cache used by this component
* is NOT volatile. If you want to use {@link CDbCache} as storage medium, use {@link CDbHttpSession} * is NOT volatile. If you want to use database as storage medium, use [[DbSession]] is a better choice.
* is a better choice.
*
* @property boolean $useCustomStorage Whether to use custom storage.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @package system.web * @since 2.0
* @since 1.0
*/ */
class CacheSession extends Session class CacheSession extends Session
{ {
/** /**
* Prefix to the keys for storing cached data
*/
const CACHE_KEY_PREFIX = 'Yii.CacheSession.';
/**
* @var string the ID of the cache application component. Defaults to 'cache' (the primary cache application component.) * @var string the ID of the cache application component. Defaults to 'cache' (the primary cache application component.)
*/ */
public $cacheID = 'cache'; public $cacheID = 'cache';
/** /**
* @var ICache the cache component * @var Cache the cache component
*/ */
private $_cache; private $_cache;
/** /**
* Initializes the application component. * Returns a value indicating whether to use custom session storage.
* This method overrides the parent implementation by checking if cache is available. * This method overrides the parent implementation and always returns true.
* @return boolean whether to use custom storage.
*/ */
public function init() public function getUseCustomStorage()
{ {
$this->_cache = Yii::app()->getComponent($this->cacheID); return true;
if (!($this->_cache instanceof ICache)) { }
throw new CException(Yii::t('yii', 'CacheSession.cacheID is invalid. Please make sure "{id}" refers to a valid cache application component.', /**
array('{id}' => $this->cacheID))); * Returns the cache instance used for storing session data.
* @return Cache the cache instance
* @throws InvalidConfigException if [[cacheID]] does not point to a valid application component.
*/
public function getCache()
{
if ($this->_cache === null) {
$cache = Yii::$app->getComponent($this->cacheID);
if ($cache instanceof Cache) {
$this->_cache = $cache;
} else {
throw new InvalidConfigException('CacheSession::cacheID must refer to the ID of a cache application component.');
} }
parent::init(); }
return $this->_cache;
} }
/** /**
* Returns a value indicating whether to use custom session storage. * Sets the cache instance used by the session component.
* This method overrides the parent implementation and always returns true. * @param Cache $value the cache instance
* @return boolean whether to use custom storage.
*/ */
public function getUseCustomStorage() public function setCache($value)
{ {
return true; $this->_cache = $value;
} }
/** /**
...@@ -74,7 +82,7 @@ class CacheSession extends Session ...@@ -74,7 +82,7 @@ class CacheSession extends Session
*/ */
public function readSession($id) public function readSession($id)
{ {
$data = $this->_cache->get($this->calculateKey($id)); $data = $this->getCache()->get($this->calculateKey($id));
return $data === false ? '' : $data; return $data === false ? '' : $data;
} }
...@@ -87,7 +95,7 @@ class CacheSession extends Session ...@@ -87,7 +95,7 @@ class CacheSession extends Session
*/ */
public function writeSession($id, $data) public function writeSession($id, $data)
{ {
return $this->_cache->set($this->calculateKey($id), $data, $this->getTimeout()); return $this->getCache()->set($this->calculateKey($id), $data, $this->getTimeout());
} }
/** /**
...@@ -98,7 +106,7 @@ class CacheSession extends Session ...@@ -98,7 +106,7 @@ class CacheSession extends Session
*/ */
public function destroySession($id) public function destroySession($id)
{ {
return $this->_cache->delete($this->calculateKey($id)); return $this->getCache()->delete($this->calculateKey($id));
} }
/** /**
...@@ -108,6 +116,6 @@ class CacheSession extends Session ...@@ -108,6 +116,6 @@ class CacheSession extends Session
*/ */
protected function calculateKey($id) protected function calculateKey($id)
{ {
return self::CACHE_KEY_PREFIX . $id; return $this->getCache()->buildKey(__CLASS__, $id);
} }
} }
...@@ -47,7 +47,7 @@ class Cookie extends \yii\base\Object ...@@ -47,7 +47,7 @@ class Cookie extends \yii\base\Object
* By setting this property to true, the cookie will not be accessible by scripting languages, * By setting this property to true, the cookie will not be accessible by scripting languages,
* such as JavaScript, which can effectively help to reduce identity theft through XSS attacks. * such as JavaScript, which can effectively help to reduce identity theft through XSS attacks.
*/ */
public $httpOnly = false; public $httponly = false;
/** /**
* Magic method to turn a cookie object into a string without having to explicitly access [[value]]. * Magic method to turn a cookie object into a string without having to explicitly access [[value]].
......
...@@ -111,7 +111,7 @@ class CookieCollection extends \yii\base\Object implements \IteratorAggregate, \ ...@@ -111,7 +111,7 @@ class CookieCollection extends \yii\base\Object implements \IteratorAggregate, \
{ {
if (isset($this->_cookies[$cookie->name])) { if (isset($this->_cookies[$cookie->name])) {
$c = $this->_cookies[$cookie->name]; $c = $this->_cookies[$cookie->name];
setcookie($c->name, '', 0, $c->path, $c->domain, $c->secure, $c->httpOnly); setcookie($c->name, '', 0, $c->path, $c->domain, $c->secure, $c->httponly);
} }
$value = $cookie->value; $value = $cookie->value;
...@@ -124,7 +124,7 @@ class CookieCollection extends \yii\base\Object implements \IteratorAggregate, \ ...@@ -124,7 +124,7 @@ class CookieCollection extends \yii\base\Object implements \IteratorAggregate, \
$value = SecurityHelper::hashData(serialize($value), $key); $value = SecurityHelper::hashData(serialize($value), $key);
} }
setcookie($cookie->name, $value, $cookie->expire, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httpOnly); setcookie($cookie->name, $value, $cookie->expire, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httponly);
$this->_cookies[$cookie->name] = $cookie; $this->_cookies[$cookie->name] = $cookie;
} }
...@@ -138,7 +138,7 @@ class CookieCollection extends \yii\base\Object implements \IteratorAggregate, \ ...@@ -138,7 +138,7 @@ class CookieCollection extends \yii\base\Object implements \IteratorAggregate, \
$cookie = $this->_cookies[$cookie]; $cookie = $this->_cookies[$cookie];
} }
if ($cookie instanceof Cookie) { if ($cookie instanceof Cookie) {
setcookie($cookie->name, '', 0, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httpOnly); setcookie($cookie->name, '', 0, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httponly);
unset($this->_cookies[$cookie->name]); unset($this->_cookies[$cookie->name]);
} }
} }
...@@ -149,7 +149,7 @@ class CookieCollection extends \yii\base\Object implements \IteratorAggregate, \ ...@@ -149,7 +149,7 @@ class CookieCollection extends \yii\base\Object implements \IteratorAggregate, \
public function removeAll() public function removeAll()
{ {
foreach ($this->_cookies as $cookie) { foreach ($this->_cookies as $cookie) {
setcookie($cookie->name, '', 0, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httpOnly); setcookie($cookie->name, '', 0, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httponly);
} }
$this->_cookies = array(); $this->_cookies = array();
} }
......
...@@ -9,39 +9,42 @@ ...@@ -9,39 +9,42 @@
namespace yii\web; namespace yii\web;
use Yii;
use yii\db\Connection;
use yii\db\Query;
use yii\base\InvalidConfigException;
/** /**
* DbSession extends {@link CHttpSession} by using database as session data storage. * DbSession extends [[Session]] by using database as session data storage.
* *
* DbSession stores session data in a DB table named 'YiiSession'. The table name * DbSession uses a DB application component to perform DB operations. The ID of the DB application
* can be changed by setting {@link sessionTableName}. If the table does not exist, * component is specified via [[connectionID]] which defaults to 'db'.
* it will be automatically created if {@link autoCreateSessionTable} is set true.
* *
* The following is the table structure: * By default, DbSession stores session data in a DB table named 'tbl_session'. This table
* must be pre-created. The table name can be changed by setting [[sessionTableName]].
* The table should have the following structure:
* *
* <pre> * ~~~
* CREATE TABLE YiiSession * CREATE TABLE tbl_session
* ( * (
* id CHAR(32) PRIMARY KEY, * id CHAR(32) PRIMARY KEY,
* expire INTEGER, * expire INTEGER,
* data BLOB * data BLOB
* ) * )
* </pre> * ~~~
* Where 'BLOB' refers to the BLOB-type of your preffered database.
* *
* DbSession relies on {@link http://www.php.net/manual/en/ref.pdo.php PDO} to access database. * where 'BLOB' refers to the BLOB-type of your preferred database. Below are the BLOB type
* that can be used for some popular databases:
* *
* By default, it will use an SQLite3 database named 'session-YiiVersion.db' under the application runtime directory. * - MySQL: LONGBLOB
* You can also specify {@link connectionID} so that it makes use of a DB application component to access database. * - PostgreSQL: BYTEA
* - MSSQL: BLOB
* *
* When using DbSession in a production server, we recommend you pre-create the session DB table * When using DbSession in a production server, we recommend you create a DB index for the 'expire'
* and set {@link autoCreateSessionTable} to be false. This will greatly improve the performance. * column in the session table to improve the performance.
* You may also create a DB index for the 'expire' column in the session table to further improve the performance.
*
* @property boolean $useCustomStorage Whether to use custom storage.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @package system.web * @since 2.0
* @since 1.0
*/ */
class DbSession extends Session class DbSession extends Session
{ {
...@@ -60,14 +63,9 @@ class DbSession extends Session ...@@ -60,14 +63,9 @@ class DbSession extends Session
* </pre> * </pre>
* @see autoCreateSessionTable * @see autoCreateSessionTable
*/ */
public $sessionTableName = 'YiiSession'; public $sessionTableName = 'tbl_session';
/**
* @var boolean whether the session DB table should be automatically created if not exists. Defaults to true.
* @see sessionTableName
*/
public $autoCreateSessionTable = true;
/** /**
* @var CDbConnection the DB connection instance * @var Connection the DB connection instance
*/ */
private $_db; private $_db;
...@@ -83,10 +81,9 @@ class DbSession extends Session ...@@ -83,10 +81,9 @@ class DbSession extends Session
} }
/** /**
* Updates the current session id with a newly generated one. * Updates the current session ID with a newly generated one .
* Please refer to {@link http://php.net/session_regenerate_id} for more details. * Please refer to [[http://php.net/session_regenerate_id]] for more details.
* @param boolean $deleteOldSession Whether to delete the old associated session file or not. * @param boolean $deleteOldSession Whether to delete the old associated session file or not.
* @since 1.1.8
*/ */
public function regenerateID($deleteOldSession = false) public function regenerateID($deleteOldSession = false)
{ {
...@@ -99,93 +96,56 @@ class DbSession extends Session ...@@ -99,93 +96,56 @@ class DbSession extends Session
parent::regenerateID(false); parent::regenerateID(false);
$newID = session_id(); $newID = session_id();
$db = $this->getDbConnection(); $db = $this->getDb();
$row = $db->createCommand() $query = new Query;
->select() $row = $query->from($this->sessionTableName)
->from($this->sessionTableName) ->where(array('id' => $oldID))
->where('id=:id', array(':id' => $oldID)) ->createCommand($db)
->queryRow(); ->queryRow();
if ($row !== false) { if ($row !== false) {
if ($deleteOldSession) { if ($deleteOldSession) {
$db->createCommand()->update($this->sessionTableName, array( $db->createCommand()->update($this->sessionTableName, array(
'id' => $newID 'id' => $newID
), 'id=:oldID', array(':oldID' => $oldID)); ), array('id' => $oldID))->execute();
} else { } else {
$row['id'] = $newID; $row['id'] = $newID;
$db->createCommand()->insert($this->sessionTableName, $row); $db->createCommand()->insert($this->sessionTableName, $row)->execute();
} }
} else { } else {
// shouldn't reach here normally // shouldn't reach here normally
$db->createCommand()->insert($this->sessionTableName, array( $db->createCommand()->insert($this->sessionTableName, array(
'id' => $newID, 'id' => $newID,
'expire' => time() + $this->getTimeout(), 'expire' => time() + $this->getTimeout(),
)); ))->execute();
} }
} }
/** /**
* Creates the session DB table. * Returns the DB connection instance used for storing session data.
* @param CDbConnection $db the database connection * @return Connection the DB connection instance
* @param string $tableName the name of the table to be created * @throws InvalidConfigException if [[connectionID]] does not point to a valid application component.
*/ */
protected function createSessionTable($db, $tableName) public function getDb()
{ {
$driver = $db->getDriverName(); if ($this->_db === null) {
if ($driver === 'mysql') { $db = Yii::$app->getComponent($this->connectionID);
$blob = 'LONGBLOB'; if ($db instanceof Connection) {
} elseif ($driver === 'pgsql') { $this->_db = $db;
$blob = 'BYTEA';
} else { } else {
$blob = 'BLOB'; throw new InvalidConfigException("DbSession::connectionID must refer to the ID of a DB application component.");
} }
$db->createCommand()->createTable($tableName, array(
'id' => 'CHAR(32) PRIMARY KEY',
'expire' => 'integer',
'data' => $blob,
));
} }
/**
* @return CDbConnection the DB connection instance
* @throws CException if {@link connectionID} does not point to a valid application component.
*/
protected function getDbConnection()
{
if ($this->_db !== null) {
return $this->_db;
} elseif (($id = $this->connectionID) !== null) {
if (($this->_db = Yii::app()->getComponent($id)) instanceof CDbConnection) {
return $this->_db; return $this->_db;
} else {
throw new CException(Yii::t('yii', 'DbSession.connectionID "{id}" is invalid. Please make sure it refers to the ID of a CDbConnection application component.',
array('{id}' => $id)));
}
} else {
$dbFile = Yii::app()->getRuntimePath() . DIRECTORY_SEPARATOR . 'session-' . Yii::getVersion() . '.db';
return $this->_db = new CDbConnection('sqlite:' . $dbFile);
}
} }
/** /**
* Session open handler. * Sets the DB connection used by the session component.
* Do not call this method directly. * @param Connection $value the DB connection instance
* @param string $savePath session save path
* @param string $sessionName session name
* @return boolean whether session is opened successfully
*/ */
public function openSession($savePath, $sessionName) public function setDb($value)
{ {
if ($this->autoCreateSessionTable) { $this->_db = $value;
$db = $this->getDbConnection();
$db->setActive(true);
try {
$db->createCommand()->delete($this->sessionTableName, 'expire<:expire', array(':expire' => time()));
} catch (Exception $e) {
$this->createSessionTable($db, $this->sessionTableName);
}
}
return true;
} }
/** /**
...@@ -196,10 +156,11 @@ class DbSession extends Session ...@@ -196,10 +156,11 @@ class DbSession extends Session
*/ */
public function readSession($id) public function readSession($id)
{ {
$data = $this->getDbConnection()->createCommand() $query = new Query;
->select('data') $data = $query->select(array('data'))
->from($this->sessionTableName) ->from($this->sessionTableName)
->where('expire>:expire AND id=:id', array(':expire' => time(), ':id' => $id)) ->where('expire>:expire AND id=:id', array(':expire' => time(), ':id' => $id))
->createCommand($this->getDb())
->queryScalar(); ->queryScalar();
return $data === false ? '' : $data; return $data === false ? '' : $data;
} }
...@@ -217,20 +178,26 @@ class DbSession extends Session ...@@ -217,20 +178,26 @@ class DbSession extends Session
// http://us.php.net/manual/en/function.session-set-save-handler.php // http://us.php.net/manual/en/function.session-set-save-handler.php
try { try {
$expire = time() + $this->getTimeout(); $expire = time() + $this->getTimeout();
$db = $this->getDbConnection(); $db = $this->getDb();
if ($db->createCommand()->select('id')->from($this->sessionTableName)->where('id=:id', array(':id' => $id))->queryScalar() === false) { $query = new Query;
$exists = $query->select(array('id'))
->from($this->sessionTableName)
->where(array('id' => $id))
->createCommand($db)
->queryScalar();
if ($exists === false) {
$db->createCommand()->insert($this->sessionTableName, array( $db->createCommand()->insert($this->sessionTableName, array(
'id' => $id, 'id' => $id,
'data' => $data, 'data' => $data,
'expire' => $expire, 'expire' => $expire,
)); ))->execute();
} else { } else {
$db->createCommand()->update($this->sessionTableName, array( $db->createCommand()->update($this->sessionTableName, array(
'data' => $data, 'data' => $data,
'expire' => $expire 'expire' => $expire
), 'id=:id', array(':id' => $id)); ), array('id' => $id))->execute();
} }
} catch (Exception $e) { } catch (\Exception $e) {
if (YII_DEBUG) { if (YII_DEBUG) {
echo $e->getMessage(); echo $e->getMessage();
} }
...@@ -248,8 +215,9 @@ class DbSession extends Session ...@@ -248,8 +215,9 @@ class DbSession extends Session
*/ */
public function destroySession($id) public function destroySession($id)
{ {
$this->getDbConnection()->createCommand() $this->getDb()->createCommand()
->delete($this->sessionTableName, 'id=:id', array(':id' => $id)); ->delete($this->sessionTableName, array('id' => $id))
->execute();
return true; return true;
} }
...@@ -261,8 +229,9 @@ class DbSession extends Session ...@@ -261,8 +229,9 @@ class DbSession extends Session
*/ */
public function gcSession($maxLifetime) public function gcSession($maxLifetime)
{ {
$this->getDbConnection()->createCommand() $this->getDb()->createCommand()
->delete($this->sessionTableName, 'expire<:expire', array(':expire' => time())); ->delete($this->sessionTableName, 'expire<:expire', array(':expire' => time()))
->execute();
return true; return true;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment