1: <?php
2: /**
3: * Database Session save handler. Allows saving session information into a model.
4: *
5: * PHP 5
6: *
7: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8: * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
9: *
10: * Licensed under The MIT License
11: * Redistributions of files must retain the above copyright notice.
12: *
13: * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
14: * @link http://cakephp.org CakePHP(tm) Project
15: * @package Cake.Model.Datasource.Session
16: * @since CakePHP(tm) v 2.0
17: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
18: */
19:
20: App::uses('CakeSessionHandlerInterface', 'Model/Datasource/Session');
21: App::uses('ClassRegistry', 'Utility');
22:
23: /**
24: * DatabaseSession provides methods to be used with CakeSession.
25: *
26: * @package Cake.Model.Datasource.Session
27: */
28: class DatabaseSession implements CakeSessionHandlerInterface {
29:
30: /**
31: * Reference to the model handling the session data
32: *
33: * @var Model
34: */
35: protected $_model;
36:
37: /**
38: * Number of seconds to mark the session as expired
39: *
40: * @var int
41: */
42: protected $_timeout;
43:
44: /**
45: * Constructor. Looks at Session configuration information and
46: * sets up the session model.
47: *
48: */
49: public function __construct() {
50: $modelName = Configure::read('Session.handler.model');
51:
52: if (empty($modelName)) {
53: $settings = array(
54: 'class' => 'Session',
55: 'alias' => 'Session',
56: 'table' => 'cake_sessions',
57: );
58: } else {
59: $settings = array(
60: 'class' => $modelName,
61: 'alias' => 'Session',
62: );
63: }
64: $this->_model = ClassRegistry::init($settings);
65: $this->_timeout = Configure::read('Session.timeout') * 60;
66: }
67:
68: /**
69: * Method called on open of a database session.
70: *
71: * @return boolean Success
72: */
73: public function open() {
74: return true;
75: }
76:
77: /**
78: * Method called on close of a database session.
79: *
80: * @return boolean Success
81: */
82: public function close() {
83: return true;
84: }
85:
86: /**
87: * Method used to read from a database session.
88: *
89: * @param integer|string $id The key of the value to read
90: * @return mixed The value of the key or false if it does not exist
91: */
92: public function read($id) {
93: $row = $this->_model->find('first', array(
94: 'conditions' => array($this->_model->primaryKey => $id)
95: ));
96:
97: if (empty($row[$this->_model->alias]['data'])) {
98: return false;
99: }
100:
101: return $row[$this->_model->alias]['data'];
102: }
103:
104: /**
105: * Helper function called on write for database sessions.
106: *
107: * @param integer $id ID that uniquely identifies session in database
108: * @param mixed $data The value of the data to be saved.
109: * @return boolean True for successful write, false otherwise.
110: */
111: public function write($id, $data) {
112: if (!$id) {
113: return false;
114: }
115: $expires = time() + $this->_timeout;
116: $record = compact('id', 'data', 'expires');
117: $record[$this->_model->primaryKey] = $id;
118: return $this->_model->save($record);
119: }
120:
121: /**
122: * Method called on the destruction of a database session.
123: *
124: * @param integer $id ID that uniquely identifies session in database
125: * @return boolean True for successful delete, false otherwise.
126: */
127: public function destroy($id) {
128: return $this->_model->delete($id);
129: }
130:
131: /**
132: * Helper function called on gc for database sessions.
133: *
134: * @param integer $expires Timestamp (defaults to current time)
135: * @return boolean Success
136: */
137: public function gc($expires = null) {
138: if (!$expires) {
139: $expires = time();
140: } else {
141: $expires = time() - $expires;
142: }
143: return $this->_model->deleteAll(array($this->_model->alias . ".expires <" => $expires), false, false);
144: }
145:
146: }
147: