This repository has been archived by the owner on Apr 9, 2021. It is now read-only.
forked from cballou/MongoSession
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MongoSession.php
298 lines (259 loc) · 8.74 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
<?php
/*
* This MongoDB session handler is intended to store any data you see fit.
*/
class MongoSession {
/**
* Whether session writes should be performed safely. If TRUE, the
* program will wait for a database response and throw a
* MongoCursorException if the update failed. Can also be set to an
* integer value for replication. For more information, see:
* http://www.php.net/manual/en/mongocollection.update.php
* Slower when on but minimizes any session errors when coupled with FSYNC.
*/
const SAFE = false;
/**
* If TRUE, forces the session write to be synced to disk before
* returning success.
*/
const FSYNC = false;
// example config with support for multiple servers
// (helpful for sharding and replication setups)
protected $_config = array(
// cookie related vars
'cookie_path' => '/',
'cookie_domain' => '.mofollow.com', // .mydomain.com
// session related vars
'lifetime' => 3600, // session lifetime in seconds
'database' => 'session', // name of MongoDB database
'collection' => 'session', // name of MongoDB collection
'persistent' => false, // persistent connection to DB?
'persistentId' => 'MongoSession', // name of persistent connection
// array of mongo db servers
'servers' => array(
array(
'host' => Mongo::DEFAULT_HOST,
'port' => Mongo::DEFAULT_PORT,
'username' => null,
'password' => null
)
)
);
// stores the database connection
protected $connection;
// stores the mongo collection
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']);
// name the session
session_name('mongo_sess');
// start it up
session_start();
}
/**
* Checks supplied configuration and initializes
* connection to database.
*
* @access protected
* @param array $config
*/
protected 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 = $config;
// generate server connection strings
$connections = array();
if (!empty($this->_config['servers'])) {
foreach ($this->_config['servers'] as $server) {
$str = '';
if (!empty($server['username']) && !empty($server['password'])) {
$str .= $server['username'] . ':' . $server['password'] . '@';
}
$str .= $server['host'] . ':' . $server['port'];
array_push($connections, $str);
}
} else {
// use default connection settings
array_push($connections, Mongo::DEFAULT_HOST . ':' . Mongo::DEFAULT_PORT);
}
$options = array(
'connect' => true, // Immediately connect to MongoDB
);
// Add persist option if persistent connection requested
if($this->_config['persistent']) {
$option[] = array(
'persist' => $this->_config['persistentId']
);
}
// load mongo servers
$this->connection = new Mongo('mongodb://' . implode(',', $connections), $options);
// load db
try {
$database = $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 = $database->selectCollection($this->_config['collection']);
} catch (Exception $e) {
throw new Exception('The MongoDB collection specified in the config does not exist.');
}
// ensure we have proper indexing on the expiration
$this->mongo->ensureIndex('expiry', array('expiry' => 1));
}
/**
* 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) {
// retrieve valid session data
$now = time();
// exclude results that are inactive or expired
$result = $this->mongo->findOne(
array(
'session_id' => $id,
'expiry' => array('$gte' => $now),
'active' => 1
)
);
if ($result) {
$this->session = $result;
return $result['data'];
}
return '';
}
/**
* Atomically write data to the session.
*
* @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(
'session_id' => $id,
'data' => $data,
'active' => 1,
'expiry' => $expiry
);
// atomic update
$query = array('session_id' => $id);
// update options
$options = array(
'upsert' => true,
'safe' => MongoSession::SAFE,
'fsync' => MongoSession::FSYNC
);
// perform the update or insert
try {
$this->mongo->update($query, array('$set' => $new_obj), $options);
} catch (Exception $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), true);
return true;
}
/**
* Garbage collection. We currently don't remove the session data.
* Sessions are set to inactive.
*
* @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' => MongoSession::SAFE,
'fsync' => MongoSession::FSYNC
);
// update expired elements and set to inactive
$this->mongo->update($query, $update, $options);
return true;
}
}