You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					222 lines
				
				6.0 KiB
			
		
		
			
		
	
	
					222 lines
				
				6.0 KiB
			| 
								 
											13 years ago
										 
									 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @link http://www.yiiframework.com/
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * @copyright Copyright (c) 2008 Yii Software LLC
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * @license http://www.yiiframework.com/license/
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace yii\web;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								use Yii;
							 | 
						||
| 
								 | 
							
								use yii\db\Connection;
							 | 
						||
| 
								 | 
							
								use yii\db\Query;
							 | 
						||
| 
								 | 
							
								use yii\base\InvalidConfigException;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * DbSession extends [[Session]] by using database as session data storage.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 *
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * By default, DbSession stores session data in a DB table named 'tbl_session'. This table
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * must be pre-created. The table name can be changed by setting [[sessionTable]].
							 | 
						||
| 
								 | 
							
								 * 
							 | 
						||
| 
								 | 
							
								 * The following example shows how you can configure the application to use DbSession:
							 | 
						||
| 
								 | 
							
								 * 
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * ~~~
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * 'session' => array(
							 | 
						||
| 
								 | 
							
								 *     'class' => 'yii\web\DbSession',
							 | 
						||
| 
								 | 
							
								 *     // 'db' => 'mydb',
							 | 
						||
| 
								 | 
							
								 *     // 'sessionTable' => 'my_session',
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * )
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * ~~~
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @author Qiang Xue <qiang.xue@gmail.com>
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * @since 2.0
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 */
							 | 
						||
| 
								 | 
							
								class DbSession extends Session
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @var Connection|string the DB connection object or the application component ID of the DB connection.
							 | 
						||
| 
								 | 
							
									 * After the DbSession object is created, if you want to change this property, you should only assign it
							 | 
						||
| 
								 | 
							
									 * with a DB connection object.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									public $db = 'db';
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @var string the name of the DB table that stores the session data.
							 | 
						||
| 
								 | 
							
									 * The table should be pre-created as follows:
							 | 
						||
| 
								 | 
							
									 *
							 | 
						||
| 
								 | 
							
									 * ~~~
							 | 
						||
| 
								 | 
							
									 * CREATE TABLE tbl_session
							 | 
						||
| 
								 | 
							
									 * (
							 | 
						||
| 
								 | 
							
									 *     id CHAR(40) NOT NULL PRIMARY KEY,
							 | 
						||
| 
								 | 
							
									 *     expire INTEGER,
							 | 
						||
| 
								 | 
							
									 *     data BLOB
							 | 
						||
| 
								 | 
							
									 * )
							 | 
						||
| 
								 | 
							
									 * ~~~
							 | 
						||
| 
								 | 
							
									 *
							 | 
						||
| 
								 | 
							
									 * where 'BLOB' refers to the BLOB-type of your preferred DBMS. Below are the BLOB type
							 | 
						||
| 
								 | 
							
									 * that can be used for some popular DBMS:
							 | 
						||
| 
								 | 
							
									 *
							 | 
						||
| 
								 | 
							
									 * - MySQL: LONGBLOB
							 | 
						||
| 
								 | 
							
									 * - PostgreSQL: BYTEA
							 | 
						||
| 
								 | 
							
									 * - MSSQL: BLOB
							 | 
						||
| 
								 | 
							
									 *
							 | 
						||
| 
								 | 
							
									 * When using DbSession in a production server, we recommend you create a DB index for the 'expire'
							 | 
						||
| 
								 | 
							
									 * column in the session table to improve the performance.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									public $sessionTable = 'tbl_session';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * Initializes the DbSession component.
							 | 
						||
| 
								 | 
							
									 * This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
							 | 
						||
| 
								 | 
							
									 * @throws InvalidConfigException if [[db]] is invalid.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									public function init()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										if (is_string($this->db)) {
							 | 
						||
| 
								 | 
							
											$this->db = Yii::$app->getComponent($this->db);
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
										if (!$this->db instanceof Connection) {
							 | 
						||
| 
								 | 
							
											throw new InvalidConfigException("DbSession::db must be either a DB connection instance or the application component ID of a DB connection.");
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										}
							 | 
						||
| 
								 | 
							
										parent::init();
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * Returns a value indicating whether to use custom session storage.
							 | 
						||
| 
								 | 
							
									 * This method overrides the parent implementation and always returns true.
							 | 
						||
| 
								 | 
							
									 * @return boolean whether to use custom storage.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function getUseCustomStorage()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										return true;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * Updates the current session ID with a newly generated one .
							 | 
						||
| 
								 | 
							
									 * Please refer to [[http://php.net/session_regenerate_id]] for more details.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @param boolean $deleteOldSession Whether to delete the old associated session file or not.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function regenerateID($deleteOldSession = false)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										$oldID = session_id();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										// if no session is started, there is nothing to regenerate
							 | 
						||
| 
								 | 
							
										if (empty($oldID)) {
							 | 
						||
| 
								 | 
							
											return;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										parent::regenerateID(false);
							 | 
						||
| 
								 | 
							
										$newID = session_id();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										$query = new Query;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										$row = $query->from($this->sessionTable)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											->where(array('id' => $oldID))
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											->createCommand($this->db)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											->queryRow();
							 | 
						||
| 
								 | 
							
										if ($row !== false) {
							 | 
						||
| 
								 | 
							
											if ($deleteOldSession) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												$this->db->createCommand()
							 | 
						||
| 
								 | 
							
													->update($this->sessionTable, array('id' => $newID), array('id' => $oldID))
							 | 
						||
| 
								 | 
							
													->execute();
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											} else {
							 | 
						||
| 
								 | 
							
												$row['id'] = $newID;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												$this->db->createCommand()
							 | 
						||
| 
								 | 
							
													->insert($this->sessionTable, $row)
							 | 
						||
| 
								 | 
							
													->execute();
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											}
							 | 
						||
| 
								 | 
							
										} else {
							 | 
						||
| 
								 | 
							
											// shouldn't reach here normally
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											$this->db->createCommand()
							 | 
						||
| 
								 | 
							
												->insert($this->sessionTable, array(
							 | 
						||
| 
								 | 
							
													'id' => $newID,
							 | 
						||
| 
								 | 
							
													'expire' => time() + $this->getTimeout(),
							 | 
						||
| 
								 | 
							
												))->execute();
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										}
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * Session read handler.
							 | 
						||
| 
								 | 
							
									 * Do not call this method directly.
							 | 
						||
| 
								 | 
							
									 * @param string $id session ID
							 | 
						||
| 
								 | 
							
									 * @return string the session data
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function readSession($id)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										$query = new Query;
							 | 
						||
| 
								 | 
							
										$data = $query->select(array('data'))
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											->from($this->sessionTable)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											->where('[[expire]]>:expire AND [[id]]=:id', array(':expire' => time(), ':id' => $id))
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											->createCommand($this->db)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											->queryScalar();
							 | 
						||
| 
								 | 
							
										return $data === false ? '' : $data;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * Session write handler.
							 | 
						||
| 
								 | 
							
									 * Do not call this method directly.
							 | 
						||
| 
								 | 
							
									 * @param string $id session ID
							 | 
						||
| 
								 | 
							
									 * @param string $data session data
							 | 
						||
| 
								 | 
							
									 * @return boolean whether session write is successful
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function writeSession($id, $data)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										// exception must be caught in session write handler
							 | 
						||
| 
								 | 
							
										// http://us.php.net/manual/en/function.session-set-save-handler.php
							 | 
						||
| 
								 | 
							
										try {
							 | 
						||
| 
								 | 
							
											$expire = time() + $this->getTimeout();
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											$query = new Query;
							 | 
						||
| 
								 | 
							
											$exists = $query->select(array('id'))
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												->from($this->sessionTable)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												->where(array('id' => $id))
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												->createCommand($this->db)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												->queryScalar();
							 | 
						||
| 
								 | 
							
											if ($exists === false) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												$this->db->createCommand()
							 | 
						||
| 
								 | 
							
													->insert($this->sessionTable, array(
							 | 
						||
| 
								 | 
							
														'id' => $id,
							 | 
						||
| 
								 | 
							
														'data' => $data,
							 | 
						||
| 
								 | 
							
														'expire' => $expire,
							 | 
						||
| 
								 | 
							
													))->execute();
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											} else {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												$this->db->createCommand()
							 | 
						||
| 
								 | 
							
													->update($this->sessionTable, array('data' => $data, 'expire' => $expire), array('id' => $id))
							 | 
						||
| 
								 | 
							
													->execute();
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										} catch (\Exception $e) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											if (YII_DEBUG) {
							 | 
						||
| 
								 | 
							
												echo $e->getMessage();
							 | 
						||
| 
								 | 
							
											}
							 | 
						||
| 
								 | 
							
											// it is too late to log an error message here
							 | 
						||
| 
								 | 
							
											return false;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
										return true;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * Session destroy handler.
							 | 
						||
| 
								 | 
							
									 * Do not call this method directly.
							 | 
						||
| 
								 | 
							
									 * @param string $id session ID
							 | 
						||
| 
								 | 
							
									 * @return boolean whether session is destroyed successfully
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function destroySession($id)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										$this->db->createCommand()
							 | 
						||
| 
								 | 
							
											->delete($this->sessionTable, array('id' => $id))
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											->execute();
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										return true;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * Session GC (garbage collection) handler.
							 | 
						||
| 
								 | 
							
									 * Do not call this method directly.
							 | 
						||
| 
								 | 
							
									 * @param integer $maxLifetime the number of seconds after which data will be seen as 'garbage' and cleaned up.
							 | 
						||
| 
								 | 
							
									 * @return boolean whether session is GCed successfully
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function gcSession($maxLifetime)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										$this->db->createCommand()
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											->delete($this->sessionTable, '[[expire]]<:expire', array(':expire' => time()))
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											->execute();
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										return true;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								}
							 |