This repository has been archived by the owner on May 14, 2019. It is now read-only.
forked from bermi/akelos
/
db_session.php
191 lines (173 loc) · 4.79 KB
/
db_session.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
# This file is part of the Akelos Framework
# (Copyright) 2004-2010 Bermi Ferrer bermi a t bermilabs com
# See LICENSE and CREDITS for details
/**
* Database based session.
*
* This class enables saving sessions into a database. This can
* be usefull for multiple server sites, and to have more
* control over sessions.
*
* You'll have to run AkDbSession::install() in order to create the default
* database scheme for strogin sessions.
*
* <code>
*
* $AkDbSession = new AkDbSession();
* $AkDbSession->sessionLife = AK_SESSION_EXPIRE;
* session_set_save_handler (
* array($AkDbSession, '_open'),
* array($AkDbSession, '_close'),
* array($AkDbSession, '_read'),
* array($AkDbSession, '_write'),
* array($AkDbSession, '_destroy'),
* array($AkDbSession, '_gc')
* );
*
* </code>
*
* @author Bermi Ferrer <bermi at akelos com>
* @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
* @since 0.1
* @deprecated use AkSession instead
* @version $Revision 0.1 $
*/
class AkDbSession
{
/**
* Secconds for the session to expire.
*
* @see setSessionLife
* @access public
* @var integer $sessionLife
*/
public $sessionLife = AK_SESSION_EXPIRE;
/**
* Database instance handler
*
* Stores a reference to an ADODB database instance.
*
* @access protected
* @var object $_db
*/
public $_db;
/**
* Original session value for avoiding hitting the database in case nothing has changed
*
* @access private
* @var string $_db
*/
public $_original_sess_value = '';
/**
* $this->sessionLife setter
*
* Use this method to set $this->sessionLife value
*
* @access public
* @see get$sessionLife
* @param integer $sessionLife Secconds for the session to expire.
* @return bool Returns true if $this->sessionLife has been set
* correctly.
*/
public function setSessionLife($sessionLife) {
$this->sessionLife = $sessionLife;
}
// ---- Protected methods ---- //
/**
* Session open handler
*
* @access protected
* @return boolean
*/
public function _open() {
$this->_db = Ak::db();
return true;
}
/**
* Session close handler
*
* @access protected
* @return boolean
*/
public function _close() {
/**
* @todo Get from cached vars last time garbage collection was made to avoid hitting db
* on every request
*/
$this->_gc();
return true;
}
/**
* Session read handler
*
* @access protected
* @param string $id Session Id
* @return string
*/
public function _read($id) {
$result = @$this->_db->selectValue("SELECT value FROM sessions WHERE id = ".$this->_db->quote_string($id));
return is_null($result) ? '' : (string)$result;
}
/**
* Session write handler
*
* @access protected
* @param string $id
* @param string $data
* @return boolean
*/
public function _write($id, $data) {
// We don't want to hit the db if nothing has changed
if($this->_original_sess_value != $data){
/**
* @todo replace with dbAdapter-method
*/
$ret = @$this->_db->connection->Replace('sessions', array('id'=>$this->_db->quote_string($id),'expire'=>$this->_db->quote_datetime(time()),'value'=>$this->_db->quote_string($data)), 'id');
if($ret == 0){
return false;
}else{
return true;
}
}else {
return true;
}
}
/**
* Session destroy handler
*
* @access protected
* @param string $id
* @return boolean
*/
public function _destroy($id) {
return (bool)@$this->_db->delete('DELETE FROM sessions WHERE id = '.$this->_db->quote_string($id));
}
/**
* Session garbage collection handler
*
* @access protected
* @return boolean
*/
public function _gc() {
return (bool)@$this->_db->delete('DELETE FROM sessions WHERE expire < '.$this->_db->quote_datetime(time()-$this->sessionLife));
}
static function install() {
$db = Ak::db();
if(!$db->tableExists('sessions')){
$Installer = new AkInstaller($db);
$Installer->createTable('sessions', '
id string(32) not null primary key,
expire datetime,
value text'
, array('timestamp'=>false));
}
}
static function uninstall() {
$db = Ak::db();
if($db->tableExists('sessions')){
$Installer = new AkInstaller($db);
$Installer->dropTable('sessions');
}
}
}