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-2011, 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-2011, 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: * DatabaseSession provides methods to be used with CakeSession.
21: *
22: * @package Cake.Model.Datasource.Session
23: */
24: class DatabaseSession implements CakeSessionHandlerInterface {
25:
26: /**
27: * Reference to the model handling the session data
28: *
29: * @var Model
30: */
31: protected $_model;
32:
33: /**
34: * Number of seconds to mark the session as expired
35: *
36: * @var int
37: */
38: protected $_timeout;
39:
40: /**
41: * Constructor. Looks at Session configuration information and
42: * sets up the session model.
43: *
44: */
45: public function __construct() {
46: $modelName = Configure::read('Session.handler.model');
47:
48: if (empty($modelName)) {
49: $settings = array(
50: 'class' => 'Session',
51: 'alias' => 'Session',
52: 'table' => 'cake_sessions',
53: );
54: } else {
55: $settings = array(
56: 'class' => $modelName,
57: 'alias' => 'Session',
58: );
59: }
60: $this->_model = ClassRegistry::init($settings);
61: $this->_timeout = Configure::read('Session.timeout') * 60;
62: }
63:
64: /**
65: * Method called on open of a database session.
66: *
67: * @return boolean Success
68: */
69: public function open() {
70: return true;
71: }
72:
73: /**
74: * Method called on close of a database session.
75: *
76: * @return boolean Success
77: */
78: public function close() {
79: $probability = mt_rand(1, 150);
80: if ($probability <= 3) {
81: $this->gc();
82: }
83: return true;
84: }
85:
86: /**
87: * Method used to read from a database session.
88: *
89: * @param mixed $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: }
141: return $this->_model->deleteAll(array($this->_model->alias . ".expires <" => $expires), false, false);
142: }
143:
144: /**
145: * Closes the session before the objects handling it become unavailable
146: *
147: * @return void
148: */
149: public function __destruct() {
150: session_write_close();
151: }
152: }
153: