2010-07-25 18:27:45 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Database Session save handler. Allows saving session information into a model.
|
2010-07-25 18:27:45 +00:00
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-07-25 18:27:45 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-07-25 18:27:45 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) 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
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2010-07-25 18:27:45 +00:00
|
|
|
*/
|
2012-01-07 22:50:16 +00:00
|
|
|
|
|
|
|
App::uses('CakeSessionHandlerInterface', 'Model/Datasource/Session');
|
2012-04-05 12:33:12 +00:00
|
|
|
App::uses('ClassRegistry', 'Utility');
|
2012-01-07 22:50:16 +00:00
|
|
|
|
2010-07-25 18:27:45 +00:00
|
|
|
/**
|
|
|
|
* 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-10-07 03:31:49 +00:00
|
|
|
/**
|
|
|
|
* Reference to the model handling the session data
|
|
|
|
*
|
|
|
|
* @var Model
|
|
|
|
*/
|
|
|
|
protected $_model;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of seconds to mark the session as expired
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var int
|
2011-10-07 03:31:49 +00:00
|
|
|
*/
|
|
|
|
protected $_timeout;
|
|
|
|
|
2010-09-06 05:12:23 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Constructor. Looks at Session configuration information and
|
2010-09-06 05:12:23 +00:00
|
|
|
* sets up the session model.
|
|
|
|
*/
|
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-08-16 03:55:08 +00:00
|
|
|
|
2011-07-10 22:16:41 +00:00
|
|
|
if (empty($modelName)) {
|
|
|
|
$settings = array(
|
2011-11-30 15:44:11 +00:00
|
|
|
'class' => 'Session',
|
2011-07-10 22:16:41 +00:00
|
|
|
'alias' => 'Session',
|
|
|
|
'table' => 'cake_sessions',
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$settings = array(
|
2011-11-30 15:44:11 +00:00
|
|
|
'class' => $modelName,
|
2011-07-10 22:16:41 +00:00
|
|
|
'alias' => 'Session',
|
|
|
|
);
|
2010-09-06 05:12:23 +00:00
|
|
|
}
|
2011-10-07 03:31:49 +00:00
|
|
|
$this->_model = ClassRegistry::init($settings);
|
|
|
|
$this->_timeout = Configure::read('Session.timeout') * 60;
|
2010-09-06 05:12:23 +00:00
|
|
|
}
|
|
|
|
|
2010-07-25 18:27:45 +00:00
|
|
|
/**
|
|
|
|
* Method called on open of a database session.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2010-07-25 18:27:45 +00:00
|
|
|
*/
|
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.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2010-07-25 18:27:45 +00:00
|
|
|
*/
|
2010-09-06 04:43:58 +00:00
|
|
|
public function close() {
|
2010-07-25 18:27:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method used to read from a database session.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int|string $id The key of the value to read
|
2010-07-25 18:27:45 +00:00
|
|
|
* @return mixed The value of the key or false if it does not exist
|
|
|
|
*/
|
2010-09-06 04:43:58 +00:00
|
|
|
public function read($id) {
|
2011-10-07 03:31:49 +00:00
|
|
|
$row = $this->_model->find('first', array(
|
2015-09-09 22:25:39 +00:00
|
|
|
'conditions' => array($this->_model->alias . '.' . $this->_model->primaryKey => $id)
|
2010-07-25 18:27:45 +00:00
|
|
|
));
|
|
|
|
|
2011-10-07 03:31:49 +00:00
|
|
|
if (empty($row[$this->_model->alias]['data'])) {
|
2010-07-25 18:27:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-07 03:31:49 +00:00
|
|
|
return $row[$this->_model->alias]['data'];
|
2010-07-25 18:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function called on write for database sessions.
|
|
|
|
*
|
2015-01-23 08:28:08 +00:00
|
|
|
* Will retry, once, if the save triggers a PDOException which
|
|
|
|
* can happen if a race condition is encountered
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $id ID that uniquely identifies session in database
|
2010-07-25 18:27:45 +00:00
|
|
|
* @param mixed $data The value of the data to be saved.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True for successful write, false otherwise.
|
2010-07-25 18:27:45 +00:00
|
|
|
*/
|
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;
|
|
|
|
}
|
2011-10-07 03:31:49 +00:00
|
|
|
$expires = time() + $this->_timeout;
|
2011-06-15 20:56:55 +00:00
|
|
|
$record = compact('id', 'data', 'expires');
|
2011-10-07 03:31:49 +00:00
|
|
|
$record[$this->_model->primaryKey] = $id;
|
2015-01-23 08:28:08 +00:00
|
|
|
|
2015-01-22 22:57:24 +00:00
|
|
|
$options = array(
|
|
|
|
'validate' => false,
|
|
|
|
'callbacks' => false,
|
|
|
|
'counterCache' => false
|
|
|
|
);
|
2015-01-23 08:28:08 +00:00
|
|
|
try {
|
2015-12-14 01:19:11 +00:00
|
|
|
return (bool)$this->_model->save($record, $options);
|
2015-01-23 08:28:08 +00:00
|
|
|
} catch (PDOException $e) {
|
2015-12-14 01:19:11 +00:00
|
|
|
return (bool)$this->_model->save($record, $options);
|
2015-01-23 08:28:08 +00:00
|
|
|
}
|
2010-07-25 18:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method called on the destruction of a database session.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $id ID that uniquely identifies session in database
|
|
|
|
* @return bool True for successful delete, false otherwise.
|
2010-07-25 18:27:45 +00:00
|
|
|
*/
|
2010-09-06 04:43:58 +00:00
|
|
|
public function destroy($id) {
|
2015-12-14 01:19:11 +00:00
|
|
|
return (bool)$this->_model->delete($id);
|
2010-07-25 18:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function called on gc for database sessions.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $expires Timestamp (defaults to current time)
|
|
|
|
* @return bool Success
|
2010-07-25 18:27:45 +00:00
|
|
|
*/
|
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();
|
2012-08-03 01:03:53 +00:00
|
|
|
} else {
|
|
|
|
$expires = time() - $expires;
|
2010-07-25 18:27:45 +00:00
|
|
|
}
|
2015-12-14 01:19:11 +00:00
|
|
|
return (bool)$this->_model->deleteAll(array($this->_model->alias . ".expires <" => $expires), false, false);
|
2011-10-07 03:31:49 +00:00
|
|
|
}
|
|
|
|
|
2010-07-25 18:27:45 +00:00
|
|
|
}
|