2010-07-25 18:27:45 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Database Session save handler. Allows saving session information into a model.
|
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-07-25 18:27:45 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-07-25 18:27:45 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model.Datasource.Session
|
2010-07-25 18:27:45 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* DatabaseSession provides methods to be used with CakeSession.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model.Datasource.Session
|
2010-07-25 18:27:45 +00:00
|
|
|
*/
|
|
|
|
class DatabaseSession implements CakeSessionHandlerInterface {
|
2010-09-06 05:12:23 +00:00
|
|
|
|
|
|
|
/**
|
2011-05-28 20:38:46 +00:00
|
|
|
* Constructor. Looks at Session configuration information and
|
2010-09-06 05:12:23 +00:00
|
|
|
* sets up the session model.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function __construct() {
|
2010-09-06 05:12:23 +00:00
|
|
|
$modelName = Configure::read('Session.handler.model');
|
2011-07-10 22:16:41 +00:00
|
|
|
|
|
|
|
if (empty($modelName)) {
|
|
|
|
$settings = array(
|
|
|
|
'class' =>'Session',
|
|
|
|
'alias' => 'Session',
|
|
|
|
'table' => 'cake_sessions',
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$settings = array(
|
|
|
|
'class' =>$modelName,
|
|
|
|
'alias' => 'Session',
|
|
|
|
);
|
2010-09-06 05:12:23 +00:00
|
|
|
}
|
|
|
|
ClassRegistry::init($settings);
|
|
|
|
}
|
|
|
|
|
2010-07-25 18:27:45 +00:00
|
|
|
/**
|
|
|
|
* Method called on open of a database session.
|
|
|
|
*
|
|
|
|
* @return boolean Success
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-09-06 04:43:58 +00:00
|
|
|
public function open() {
|
2010-07-25 18:27:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method called on close of a database session.
|
|
|
|
*
|
|
|
|
* @return boolean Success
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-09-06 04:43:58 +00:00
|
|
|
public function close() {
|
2010-07-25 18:27:45 +00:00
|
|
|
$probability = mt_rand(1, 150);
|
|
|
|
if ($probability <= 3) {
|
2011-07-10 22:16:41 +00:00
|
|
|
$this->gc();
|
2010-07-25 18:27:45 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method used to read from a database session.
|
|
|
|
*
|
|
|
|
* @param mixed $id The key of the value to read
|
|
|
|
* @return mixed The value of the key or false if it does not exist
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-09-06 04:43:58 +00:00
|
|
|
public function read($id) {
|
2010-08-29 13:09:33 +00:00
|
|
|
$model = ClassRegistry::getObject('Session');
|
2010-07-25 18:27:45 +00:00
|
|
|
|
|
|
|
$row = $model->find('first', array(
|
|
|
|
'conditions' => array($model->primaryKey => $id)
|
|
|
|
));
|
|
|
|
|
|
|
|
if (empty($row[$model->alias]['data'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $row[$model->alias]['data'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function called on write for database sessions.
|
|
|
|
*
|
|
|
|
* @param integer $id ID that uniquely identifies session in database
|
|
|
|
* @param mixed $data The value of the data to be saved.
|
|
|
|
* @return boolean True for successful write, false otherwise.
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-09-06 04:43:58 +00:00
|
|
|
public function write($id, $data) {
|
2011-01-19 01:04:30 +00:00
|
|
|
if (!$id) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-07-25 21:06:03 +00:00
|
|
|
$expires = time() + (Configure::read('Session.timeout') * 60);
|
2011-06-15 20:56:55 +00:00
|
|
|
$Session = ClassRegistry::getObject('Session');
|
|
|
|
$record = compact('id', 'data', 'expires');
|
|
|
|
$record[$Session->primaryKey] = $id;
|
|
|
|
return $Session->save($record);
|
2010-07-25 18:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method called on the destruction of a database session.
|
|
|
|
*
|
|
|
|
* @param integer $id ID that uniquely identifies session in database
|
|
|
|
* @return boolean True for successful delete, false otherwise.
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-09-06 04:43:58 +00:00
|
|
|
public function destroy($id) {
|
2010-08-29 13:09:33 +00:00
|
|
|
return ClassRegistry::getObject('Session')->delete($id);
|
2010-07-25 18:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function called on gc for database sessions.
|
|
|
|
*
|
|
|
|
* @param integer $expires Timestamp (defaults to current time)
|
|
|
|
* @return boolean Success
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-09-06 04:43:58 +00:00
|
|
|
public function gc($expires = null) {
|
2010-07-25 18:27:45 +00:00
|
|
|
if (!$expires) {
|
|
|
|
$expires = time();
|
|
|
|
}
|
2010-08-29 13:09:33 +00:00
|
|
|
$model = ClassRegistry::getObject('Session');
|
|
|
|
return $model->deleteAll(array($model->alias . ".expires <" => $expires), false, false);
|
2010-07-25 18:27:45 +00:00
|
|
|
}
|
|
|
|
}
|