-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathlmbSessionDbStorage.class.php
More file actions
153 lines (138 loc) · 3.92 KB
/
lmbSessionDbStorage.class.php
File metadata and controls
153 lines (138 loc) · 3.92 KB
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
<?php
/*
* Limb PHP Framework
*
* @link http://limb-project.com
* @copyright Copyright © 2004-2009 BIT(http://bit-creative.com)
* @license LGPL http://www.gnu.org/copyleft/lesser.html
*/
lmb_require('limb/session/src/lmbSessionStorage.interface.php');
lmb_require('limb/dbal/src/criteria/lmbSQLFieldCriteria.class.php');
lmb_require('limb/dbal/src/lmbSimpleDb.class.php');
/**
* lmbSessionDbStorage store session data in database.
* lmb_session db table used to store session data.
* The structure of lmb_session db table can be found in limb/session/init/ folder.
* @todo Check client ip while reading session.
* @todo Allow to set any db table name to store session data in.
* @see lmbSessionStartupFilter
* @version $Id: lmbSessionDbStorage.class.php 7486 2009-01-26 19:13:20Z pachanga $
* @package session
*/
class lmbSessionDbStorage implements lmbSessionStorage
{
/**
* @var lmbSimpleDb facade to work with database
*/
protected $db;
/**
* @var integer maximum session life time
*/
protected $max_life_time = null;
/**
* Constructor.
* @param lmbDbConnection database connection object
*/
function __construct($db_connection)
{
$max_life_time = lmb_env_get('LIMB_SESSION_DB_MAX_LIFE_TIME');
$this->max_life_time = $max_life_time;
$this->db = new lmbSimpleDb($db_connection);
}
/**
* @see lmbSessionStorage :: install()
* @return void
*/
function install()
{
session_set_save_handler(
array($this, 'storageOpen'),
array($this, 'storageClose'),
array($this, 'storageRead'),
array($this, 'storageWrite'),
array($this, 'storageDestroy'),
array($this, 'storageGc')
);
}
/**
* Opens session storage
* Does nothing and returns true
* @return boolean
*/
function storageOpen()
{
return true;
}
/**
* Closes session storage
* Does nothing and returns true
* @return boolean
*/
function storageClose()
{
return true;
}
/**
* Read a single row from <b>lmb_session</b> db table and returns <b>session_data</b> column
* @param string session ID
* @return mixed
*/
function storageRead($session_id)
{
$rs = $this->db->select('lmb_session', new lmbSQLFieldCriteria('session_id', $session_id));
$rs->rewind();
if($rs->valid())
return $rs->current()->get('session_data');
else
return false;
}
/**
* Creates new or updates existing row in <b>lmb_session</b> db table
* @param string session ID
* @param mixed session data
* @return void
*/
function storageWrite($session_id, $value)
{
$crit = new lmbSQLFieldCriteria('session_id', $session_id);
$rs = $this->db->select('lmb_session', $crit);
$data = array('last_activity_time' => time(),
'session_data' => $value);
$this->db->begin();
if($rs->count() > 0)
$this->db->update('lmb_session', $data, $crit);
else
{
$data['session_id'] = "{$session_id}";
$this->db->insert('lmb_session', $data, null);
}
$this->db->commit();
}
/**
* Removed a row from <b>lmb_session</b> db table
* @param string session ID
* @return void
*/
function storageDestroy($session_id)
{
$this->db->begin();
$this->db->delete('lmb_session',
new lmbSQLFieldCriteria('session_id', $session_id));
$this->db->commit();
}
/**
* Checks if storage is still valid. If session if not valid - removes it's row from <b>lmb_session</b> db table
* Prefers class attribute {@link $max_life_time} if it's not NULL.
* @param integer system session max life time
* @return void
*/
function storageGc($max_life_time)
{
if($this->max_life_time)
$max_life_time = $this->max_life_time;
$this->db->begin();
$this->db->delete('lmb_session',
new lmbSQLFieldCriteria('last_activity_time', time() - $max_life_time, lmbSQLFieldCriteria::LESS));
$this->db->commit();
}
}