forked from phalcon/incubator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.php
230 lines (206 loc) · 7.25 KB
/
Database.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (https://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <[email protected]> |
| Eduar Carvajal <[email protected]> |
| Nikita Vershinin <[email protected]> |
+------------------------------------------------------------------------+
*/
namespace Phalcon\Session\Adapter;
use Phalcon\Db;
use Phalcon\Session\Adapter;
use Phalcon\Session\AdapterInterface;
use Phalcon\Session\Exception;
use Phalcon\Db\AdapterInterface as DbAdapter;
/**
* Phalcon\Session\Adapter\Database
* Database adapter for Phalcon\Session
*/
class Database extends Adapter implements AdapterInterface
{
/**
* @var DbAdapter
*/
protected $connection;
/**
* {@inheritdoc}
*
* @param array $options
* @throws Exception
*/
public function __construct($options = null)
{
if (!isset($options['db']) || !$options['db'] instanceof DbAdapter) {
throw new Exception(
'Parameter "db" is required and it must be an instance of Phalcon\Db\AdapterInterface'
);
}
$this->connection = $options['db'];
unset($options['db']);
if (!isset($options['table']) || empty($options['table']) || !is_string($options['table'])) {
throw new Exception("Parameter 'table' is required and it must be a non empty string");
}
$columns = ['session_id', 'data', 'created_at', 'modified_at'];
foreach ($columns as $column) {
$oColumn = "column_$column";
if (!isset($options[$oColumn]) || !is_string($options[$oColumn]) || empty($options[$oColumn])) {
$options[$oColumn] = $column;
}
}
parent::__construct($options);
session_set_save_handler(
[$this, 'open'],
[$this, 'close'],
[$this, 'read'],
[$this, 'write'],
[$this, 'destroy'],
[$this, 'gc']
);
}
/**
* {@inheritdoc}
*
* @return boolean
*/
public function open()
{
return true;
}
/**
* {@inheritdoc}
*
* @return boolean
*/
public function close()
{
return false;
}
/**
* {@inheritdoc}
*
* @param string $sessionId
* @return string
*/
public function read($sessionId)
{
$maxLifetime = (int) ini_get('session.gc_maxlifetime');
$options = $this->getOptions();
$row = $this->connection->fetchOne(
sprintf(
'SELECT %s FROM %s WHERE %s = ? AND COALESCE(%s, %s) + %d >= ?',
$this->connection->escapeIdentifier($options['column_data']),
$this->connection->escapeIdentifier($options['table']),
$this->connection->escapeIdentifier($options['column_session_id']),
$this->connection->escapeIdentifier($options['column_modified_at']),
$this->connection->escapeIdentifier($options['column_created_at']),
$maxLifetime
),
Db::FETCH_NUM,
[$sessionId, time()]
);
if (empty($row)) {
return '';
}
return $row[0];
}
/**
* {@inheritdoc}
*
* @param string $sessionId
* @param string $data
* @return boolean
*/
public function write($sessionId, $data)
{
$options = $this->getOptions();
$row = $this->connection->fetchOne(
sprintf(
'SELECT COUNT(*) FROM %s WHERE %s = ?',
$this->connection->escapeIdentifier($options['table']),
$this->connection->escapeIdentifier($options['column_session_id'])
),
Db::FETCH_NUM,
[$sessionId]
);
if (!empty($row) && intval($row[0]) > 0) {
return $this->connection->execute(
sprintf(
'UPDATE %s SET %s = ?, %s = ? WHERE %s = ?',
$this->connection->escapeIdentifier($options['table']),
$this->connection->escapeIdentifier($options['column_data']),
$this->connection->escapeIdentifier($options['column_modified_at']),
$this->connection->escapeIdentifier($options['column_session_id'])
),
[$data, time(), $sessionId]
);
} else {
return $this->connection->execute(
sprintf(
'INSERT INTO %s (%s, %s, %s, %s) VALUES (?, ?, ?, NULL)',
$this->connection->escapeIdentifier($options['table']),
$this->connection->escapeIdentifier($options['column_session_id']),
$this->connection->escapeIdentifier($options['column_data']),
$this->connection->escapeIdentifier($options['column_created_at']),
$this->connection->escapeIdentifier($options['column_modified_at'])
),
[$sessionId, $data, time()]
);
}
}
/**
* {@inheritdoc}
*
* @return boolean
*/
public function destroy($session_id = null)
{
if (!$this->isStarted()) {
return true;
}
if (is_null($session_id)) {
$session_id = $this->getId();
}
$this->_started = false;
$options = $this->getOptions();
$result = $this->connection->execute(
sprintf(
'DELETE FROM %s WHERE %s = ?',
$this->connection->escapeIdentifier($options['table']),
$this->connection->escapeIdentifier($options['column_session_id'])
),
[$session_id]
);
return $result && session_destroy();
}
/**
* {@inheritdoc}
* @param integer $maxlifetime
*
* @return boolean
*/
public function gc($maxlifetime)
{
$options = $this->getOptions();
return $this->connection->execute(
sprintf(
'DELETE FROM %s WHERE COALESCE(%s, %s) + %d < ?',
$this->connection->escapeIdentifier($options['table']),
$this->connection->escapeIdentifier($options['column_modified_at']),
$this->connection->escapeIdentifier($options['column_created_at']),
$maxlifetime
),
[time()]
);
}
}