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
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Session\SaveHandler;
use Zend\Db\TableGateway\TableGateway;
/**
* DB Table Gateway session save handler
*/
class DbTableGateway implements SaveHandlerInterface
{
/**
* Session Save Path
*
* @var string
*/
protected $sessionSavePath;
/**
* Session Name
*
* @var string
*/
protected $sessionName;
/**
* Lifetime
* @var int
*/
protected $lifetime;
/**
* Zend Db Table Gateway
* @var TableGateway
*/
protected $tableGateway;
/**
* DbTableGateway Options
* @var DbTableGatewayOptions
*/
protected $options;
/**
* Constructor
*
* @param TableGateway $tableGateway
* @param DbTableGatewayOptions $options
*/
public function __construct(TableGateway $tableGateway, DbTableGatewayOptions $options)
{
$this->tableGateway = $tableGateway;
$this->options = $options;
}
/**
* Open Session
*
* @param string $savePath
* @param string $name
* @return bool
*/
public function open($savePath, $name)
{
$this->sessionSavePath = $savePath;
$this->sessionName = $name;
$this->lifetime = ini_get('session.gc_maxlifetime');
return true;
}
/**
* Close session
*
* @return bool
*/
public function close()
{
return true;
}
/**
* Read session data
*
* @param string $id
* @param bool $destroyExpired Optional; true by default
* @return string
*/
public function read($id, $destroyExpired = true)
{
$row = $this->tableGateway->select([
$this->options->getIdColumn() => $id,
$this->options->getNameColumn() => $this->sessionName,
])->current();
if ($row) {
if ($row->{$this->options->getModifiedColumn()} +
$row->{$this->options->getLifetimeColumn()} > time()) {
return (string) $row->{$this->options->getDataColumn()};
}
if ($destroyExpired) {
$this->destroy($id);
}
}
return '';
}
/**
* Write session data
*
* @param string $id
* @param string $data
* @return bool
*/
public function write($id, $data)
{
$data = [
$this->options->getModifiedColumn() => time(),
$this->options->getDataColumn() => (string) $data,
];
$rows = $this->tableGateway->select([
$this->options->getIdColumn() => $id,
$this->options->getNameColumn() => $this->sessionName,
])->current();
if ($rows) {
return (bool) $this->tableGateway->update($data, [
$this->options->getIdColumn() => $id,
$this->options->getNameColumn() => $this->sessionName,
]);
}
$data[$this->options->getLifetimeColumn()] = $this->lifetime;
$data[$this->options->getIdColumn()] = $id;
$data[$this->options->getNameColumn()] = $this->sessionName;
return (bool) $this->tableGateway->insert($data);
}
/**
* Destroy session
*
* @param string $id
* @return bool
*/
public function destroy($id)
{
if (! (bool) $this->read($id, false)) {
return true;
}
return (bool) $this->tableGateway->delete([
$this->options->getIdColumn() => $id,
$this->options->getNameColumn() => $this->sessionName,
]);
}
/**
* Garbage Collection
*
* @param int $maxlifetime
* @return true
*/
public function gc($maxlifetime)
{
$platform = $this->tableGateway->getAdapter()->getPlatform();
return (bool) $this->tableGateway->delete(
sprintf(
'%s < %d',
$platform->quoteIdentifier($this->options->getModifiedColumn()),
(time() - $this->lifetime)
)
);
}
}