-
Notifications
You must be signed in to change notification settings - Fork 24
/
MongoSession.php
395 lines (337 loc) · 10.5 KB
/
MongoSession.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
/*
* This MongoDB session handler is intended to store any data you see fit.
* One interesting optimization to note is the setting of the active flag
* to 0 when a session has expired. The intended purpose of this garbage
* collection is to allow you to create a batch process for removal of
* all expired sessions. This should most likely be implemented as a cronjob
* script.
*
* @author Corey Ballou
* @copyright Corey Ballou (2010)
*
*/
class MongoSession {
// default config with support for multiple servers
// (helpful for sharding and replication setups)
protected $_config = array(
// cookie related vars
'cookie_path' => '/',
'cookie_domain' => '.mydomain.com',
'cookie_secure' => false,
'cookie_httponly' => false,
// session related vars
'lifetime' => 3600, // session lifetime in seconds
'database' => 'session', // name of MongoDB database
'collection' => 'session', // name of MongoDB collection
// persistent related vars
'persistent' => false, // persistent connection to DB?
'persistentId' => 'MongoSession', // name of persistent connection
// whether we're supporting replicaSet
'replicaSet' => false,
// array of mongo db servers
'connectionString' => ''
);
// stores the connection
protected $_connection;
// stores the mongo db
protected $_mongo;
// stores session data results
protected $_session;
/**
* Default constructor.
*
* @access public
* @param array $config
*/
public function __construct($config = array())
{
// initialize the database
$this->_init(empty($config) ? $this->_config : $config);
// set object as the save handler
session_set_save_handler(
array($this, 'open'),
array($this, 'close'),
array($this, 'read'),
array($this, 'write'),
array($this, 'destroy'),
array($this, 'gc')
);
// set some important session vars
ini_set('session.auto_start', 0);
ini_set('session.gc_probability', 1);
ini_set('session.gc_divisor', 100);
ini_set('session.gc_maxlifetime', $this->_config['lifetime']);
ini_set('session.referer_check', '');
ini_set('session.entropy_file', '/dev/urandom');
ini_set('session.entropy_length', 16);
ini_set('session.use_cookies', 1);
ini_set('session.use_only_cookies', 1);
ini_set('session.use_trans_sid', 0);
ini_set('session.hash_function', 1);
ini_set('session.hash_bits_per_character', 5);
// disable client/proxy caching
session_cache_limiter('nocache');
// set the cookie parameters
session_set_cookie_params(
$this->_config['lifetime'],
$this->_config['cookie_path'],
$this->_config['cookie_domain'],
$this->_config['cookie_secure'],
$this->_config['cookie_httponly']
);
// name the session
session_name('mongo_sess');
register_shutdown_function('session_write_close');
// start it up
session_start();
}
/**
* Initialize MongoDB. There is currently no support for persistent
* connections. It would be very easy to implement, I just didn't need it.
*
* @access private
* @param array $config
*/
private function _init($config)
{
// ensure they supplied a database
if (empty($config['database'])) {
throw new Exception('You must specify a MongoDB database to use for session storage.');
}
if (empty($config['collection'])) {
throw new Exception('You must specify a MongoDB collection to use for session storage.');
}
// update config
$this->_config = array_merge($this->_config, $config);
// add immediate connection
$opts = array('connect' => true);
// support persistent connections
if ($this->_config['persistent'] && !empty($this->_config['persistentId'])) {
$opts['persist'] = $this->_config['persistentId'];
}
// support replica sets
if ($this->_config['replicaSet']) {
$opts['replicaSet'] = $this->_config['replicaSet'];
}
// load mongo server connection
try {
$this->_connection = new Mongo($this->_config['connectionString'], $opts);
} catch (Exception $e) {
throw new Exception('Can\'t connect to the MongoDB server.');
}
// load the db
try {
$mongo = $this->_connection->selectDB($this->_config['database']);
} catch (InvalidArgumentException $e) {
throw new Exception('The MongoDB database specified in the config does not exist.');
}
// load collection
try {
$this->_mongo = $mongo->selectCollection($this->_config['collection']);
} catch(Exception $e) {
throw new Exception('The MongoDB collection specified in the config does not exist.');
}
// proper indexing on the expiration
$this->_mongo->ensureIndex(
array('expiry' => 1),
array('name' => 'expiry',
'unique' => false,
'dropDups' => true,
'safe' => true,
'sparse' => true,
)
);
// proper indexing of session id and lock
$this->_mongo->ensureIndex(
array('session_id' => 1, 'lock' => 1),
array('name' => 'session_id',
'unique' => true,
'dropDups' => true,
'safe' => true
)
);
}
/**
* Open does absolutely nothing as we already have an open connection.
*
* @access public
* @return bool
*/
public function open($save_path, $session_name)
{
return true;
}
/**
* Close does absolutely nothing as we can assume __destruct handles
* things just fine.
*
* @access public
* @return bool
*/
public function close()
{
return true;
}
/**
* Read the session data.
*
* @access public
* @param string $id
* @return string
*/
public function read($id)
{
// obtain a read lock on the data, or subsequently wait for
// the lock to be released
$this->_lock($id);
// exclude results that are inactive or expired
$result = $this->_mongo->findOne(
array(
'session_id' => $id,
'expiry' => array('$gte' => time()),
'active' => 1
)
);
if (isset($result['data'])) {
$this->_session = $result;
return $result['data'];
}
return '';
}
/**
* Atomically write data to the session, ensuring we remove any
* read locks.
*
* @access public
* @param string $id
* @param mixed $data
* @return bool
*/
public function write($id, $data)
{
// create expires
$expiry = time() + $this->_config['lifetime'];
// create new session data
$new_obj = array(
'data' => $data,
'lock' => 0,
'active' => 1,
'expiry' => $expiry
);
// check for existing session for merge
if (!empty($this->_session)) {
$obj = (array) $this->_session;
$new_obj = array_merge($obj, $new_obj);
}
unset($new_obj['_id']);
// atomic update
$query = array('session_id' => $id);
// update options
$options = array(
'upsert' => TRUE,
'safe' => TRUE,
'fsync' => FALSE
);
// perform the update or insert
try {
$result = $this->_mongo->update($query, array('$set' => $new_obj), $options);
return $result['ok'] == 1;
} catch (Exception $e) {
throw $e;
return false;
}
return true;
}
/**
* Destroys the session by removing the document with
* matching session_id.
*
* @access public
* @param string $id
* @return bool
*/
public function destroy($id)
{
$this->_mongo->remove(array('session_id' => $id), array('w' => true));
return true;
}
/**
* Garbage collection. Remove all expired entries atomically.
*
* @access public
* @return bool
*/
public function gc()
{
// define the query
$query = array('expiry' => array('$lt' => time()));
// specify the update vars
$update = array('$set' => array('active' => 0));
// update options
$options = array(
'multiple' => TRUE,
'safe' => TRUE,
'fsync' => FALSE
);
// update expired elements and set to inactive
$this->_mongo->update($query, $update, $options);
return true;
}
/**
* Solves issues with write() and close() throwing exceptions.
*
* @access public
* @return void
*/
public function __destruct()
{
session_write_close();
}
/**
* Create a global lock for the specified document.
*
* @author Benson Wong ([email protected])
* @access private
* @param string $id
*/
private function _lock($id)
{
$remaining = 30000000;
$timeout = 5000;
// Check for a row.
$result = $this->_mongo->findOne(
array(
'session_id' => $id,
'expiry' => array('$gte' => time()),
'active' => 1
)
);
// If we have a row, attempt to get a read lock.
if (!empty($result)) {
do {
try {
$query = array('session_id' => $id, 'lock' => 0);
$update = array('$set' => array('lock' => 1));
$options = array('safe' => true);
$result = $this->_mongo->update($query, $update, $options);
if ($result['n'] == 1) {
return true;
}
} catch (MongoCursorException $e) {
throw $e;
if (substr($e->getMessage(), 0, 26) != 'E11000 duplicate key error') {
throw $e; // not a dup key?
}
}
// force delay in microseconds
usleep($timeout);
$remaining -= $timeout;
// backoff on timeout, save a tree. max wait 1 second
$timeout = ($timeout < 1000000) ? $timeout * 2 : 1000000;
} while ($remaining > 0);
// aww shit.
throw new Exception('Could not obtain a session lock.');
}
}
}