/
DatabaseSession.php
148 lines (131 loc) · 3.59 KB
/
DatabaseSession.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* Database Session save handler. Allows saving session information into a model.
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Network\Session;
use Cake\Core\Configure;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use SessionHandlerInterface;
/**
* DatabaseSession provides methods to be used with Session.
*
*/
class DatabaseSession implements SessionHandlerInterface {
/**
* Reference to the table handling the session data
*
* @var Model
*/
protected $_table;
/**
* Number of seconds to mark the session as expired
*
* @var integer
*/
protected $_timeout;
/**
* Constructor. Looks at Session configuration information and
* sets up the session model.
*
*/
public function __construct() {
$modelAlias = Configure::read('Session.handler.model');
if (empty($modelAlias)) {
$this->_table = TableRegistry::get('Sessions', [
'table' => 'cake_sessions',
]);
} else {
$this->_table = TableRegistry::get($modelAlias);
}
$this->_timeout = Configure::read('Session.timeout') * 60;
}
/**
* Method called on open of a database session.
*
* @param The path where to store/retrieve the session.
* @param The session name.
* @return boolean Success
*/
public function open($savePath, $name) {
return true;
}
/**
* Method called on close of a database session.
*
* @return boolean Success
*/
public function close() {
return true;
}
/**
* Method used to read from a database session.
*
* @param integer|string $id The key of the value to read
* @return mixed The value of the key or false if it does not exist
*/
public function read($id) {
$result = $this->_table
->find('all')
->select(['data'])
->where([$this->_table->primaryKey() => $id])
->hydrate(false)
->first();
if (empty($result)) {
return false;
}
return $result['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.
*/
public function write($id, $data) {
if (!$id) {
return false;
}
$expires = time() + $this->_timeout;
$record = compact('data', 'expires');
$record[$this->_table->primaryKey()] = $id;
$result = $this->_table->save(new Entity($record));
if ($result) {
return $result->toArray();
}
return false;
}
/**
* 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.
*/
public function destroy($id) {
return $this->_table->delete(new Entity([$this->_table->primaryKey() => $id]));
}
/**
* Helper function called on gc for database sessions.
*
* @param string $maxlifetime Sessions that have not updated for the last maxlifetime seconds will be removed.
* @return boolean True on success, false on failure.
*/
public function gc($maxlifetime) {
return $this->_table->deleteAll(['expires <' => time() - $maxlifetime]);
}
}