-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbHttpSession.php
278 lines (239 loc) · 7.98 KB
/
DbHttpSession.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
namespace Intersvyaz\HttpSession;
use CDbHttpSession;
use Exception;
use PDO;
use Intersvyaz\Pdo\Oci8;
class DbHttpSession extends CDbHttpSession
{
/**
* Initial state of the session (when it was open).
* @var string
*/
protected $initSessionData;
/**
* Exclusive access to the session.
* @var bool
*/
protected $exclusive;
/**
* @var CDbTransaction
*/
private $transaction;
/**
* @inheritdoc
*/
public function init()
{
// Opening db connection first
$this->getDbConnection()->setActive(true);
parent::init();
}
/**
* @param bool $status
*/
public function setExclusive($status)
{
$this->exclusive = (bool)$status;
}
/**
* @inheritdoc
*/
public function regenerateID($deleteOldSession = false)
{
$oldID = session_id();
// if no session is started, there is nothing to regenerate
if (empty($oldID)) {
return;
}
if ($this->getIsStarted()) {
session_regenerate_id($deleteOldSession);
}
$newID = session_id();
$db = $this->getDbConnection();
$row = $db->createCommand("SELECT ID,EXPIRE,DATA FROM {$this->sessionTableName} WHERE id=:id")
->bindValue(':id', $oldID)->queryRow();
if ($row !== false) {
if ($deleteOldSession) {
$db->createCommand("UPDATE {$this->sessionTableName} SET id=:newID WHERE id=:oldID")
->bindValue(':newID', $newID)
->bindValue(':oldID', $oldID)
->execute();
} else {
if ($this->isOci8Driver()) {
$fp = $row['DATA'];
} else {
$fp = fopen('php://memory', "rwb");
fwrite($fp, stream_get_contents($row['DATA']));
fseek($fp, 0);
$transaction = $db->beginTransaction();
}
$db->createCommand("INSERT INTO {$this->sessionTableName} (id, expire,data)
VALUES (:id, :expire, empty_blob()) returning data into :data")
->bindValue(':id', $newID)
->bindValue(':expire', $row['EXPIRE'])
->bindParam(':data', $fp, PDO::PARAM_LOB)
->execute();
if (!$this->isOci8Driver()) {
$transaction->commit();
fclose($fp);
}
}
} else {
// shouldn't reach here normally
$db->createCommand("INSERT INTO {$this->sessionTableName} (id, expire) values (:ID, :EXPIRE)")
->execute(array(
'ID' => $newID,
'EXPIRE' => time() + $this->getTimeout(),
));
}
}
/**
* @inheritdoc
*/
public function openSession($savePath, $sessionName)
{
$this->initSessionData = null;
return parent::openSession($savePath, $sessionName);
}
/**
* @inheritdoc
*/
public function readSession($id)
{
$data = $this->getSessionData($id, $this->exclusive);
if ($this->initSessionData === null) {
$this->initSessionData = $data;
}
return $data;
}
/**
* @inheritdoc
*/
public function writeSession($id, $data)
{
// exception must be caught in session write handler
// http://us.php.net/manual/en/function.session-set-save-handler.php
try {
$expire = time() + $this->getTimeout();
$db = $this->getDbConnection();
$sql = "SELECT id FROM {$this->sessionTableName} WHERE id=:id";
if ($db->createCommand($sql)->bindValue(':id', $id)->queryScalar() === false) {
$sql = "INSERT INTO {$this->sessionTableName} (id, expire,data)
VALUES (:id, :expire, empty_blob()) returning data into :data";
} else {
$data = $this->getDataForSave($id, $data);
$sql = "UPDATE {$this->sessionTableName} SET data=empty_blob(), expire=:expire
WHERE id=:id returning data into :data";
}
if ($this->isOci8Driver()) {
$fp = $data;
} else {
$fp = fopen('php://memory', "rwb");
fwrite($fp, $data);
fseek($fp, 0);
if (!$this->transaction) {
$this->transaction = $this->getDbConnection()->beginTransaction();
}
}
$db->createCommand($sql)
->bindValue(':id', $id)
->bindValue(':expire', $expire)
->bindParam(':data', $fp, PDO::PARAM_LOB)
->execute();
if (!$this->isOci8Driver()) {
fclose($fp);
}
if ($this->transaction) {
$this->transaction->commit();
$this->transaction = null;
}
} catch (Exception $e) {
if (YII_DEBUG) {
echo $e->getMessage();
}
if ($this->transaction) {
$this->transaction->rollback();
$this->transaction = null;
}
// it is too late to log an error message here
return false;
}
return true;
}
/**
* @param string $id
* @param bool $forUpdate
* @return bool|string
* @throws \CDbException
* @throws \CException
*/
protected function getSessionData($id, $forUpdate = false)
{
if ($forUpdate && !$this->transaction) {
$this->transaction = $this->getDbConnection()->beginTransaction();
}
$data = $this->getDbConnection()
->createCommand("SELECT data FROM {$this->sessionTableName} WHERE expire>:expire AND id=:id" .
($forUpdate ? ' FOR UPDATE' : '')
)
->bindValue(':id', $id)
->bindValue(':expire', time())
->queryRow();
if ($this->isOci8Driver()) {
return (string)$data['DATA'];
}
return $data === false ? '' : stream_get_contents($data['DATA']);
}
/**
* Get the current state of the session from the database,
* replacing only the fields which were modified during execution.
* @param string $id
* @param string $data
* @throws \CDbException
* @throws \CException
*/
protected function getDataForSave($id, $data)
{
// Get the initial state of the session (when it was open).
$_SESSION = [];
@session_decode($this->initSessionData);
$initSessionData = $_SESSION;
// Get the final state of the session (when writeSession() was called).
$_SESSION = [];
@session_decode($data);
$finalSessionData = $_SESSION;
// Get the session state from the database with the lock (SELECT FOR UPDATE).
$_SESSION = [];
@session_decode($this->getSessionData($id, true));
$dbSessionData = $_SESSION;
$sessionChanges = ArrayHelper::arrayRecursiveDiff($finalSessionData, $initSessionData);
$merged = ArrayHelper::merge($dbSessionData, $sessionChanges);
$_SESSION = ArrayHelper::arrayRemovedRecursiveDiff($merged, $initSessionData, $finalSessionData);
return @session_encode();
}
/**
* @inheritdoc
*/
protected function createSessionTable($db, $tableName)
{
$sql = 'CREATE
TABLE ' . $this->sessionTableName . '
(
"ID" VARCHAR2(32 BYTE),
"EXPIRE" NUMBER(10,0) NOT NULL,
"DATA" BLOB,
CONSTRAINT YiiSession_PK PRIMARY KEY(ID)
);';
$db->createCommand($sql)
->execute();
}
/**
* @return bool
* @throws \CDbException
*/
protected function isOci8Driver()
{
return $this->getDbConnection()->pdoClass == Oci8::class;
}
}