-
Notifications
You must be signed in to change notification settings - Fork 2
/
MongoDBSessionStorage.php
230 lines (198 loc) · 5.83 KB
/
MongoDBSessionStorage.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
use Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage;
/**
* MongoDBSessionStorage.
*
* @author Rich Sage <[email protected]>
*/
class MongoDBSessionStorage extends NativeSessionStorage
{
protected $db;
/**
* Class constructor.
*
* @throws InvalidArgumentException if the "collection" option is not provided
* @throws InvalidArgumentException if the "db_name" option is not provided
*/
public function __construct(\Mongo $con, array $options = array())
{
$options = array_merge(
array(
'id_field' => 'sess_id',
'data_field' => 'sess_data',
'time_field' => 'sess_time',
),
$options
);
if (!isset($options['db_name'])) {
throw new \InvalidArgumentException('You must provide the "db_name" option for the MongoDBSessionStorage.');
}
if (!isset($options['collection'])) {
throw new \InvalidArgumentException('You must provide the "collection" option for the MongoDBSessionStorage.');
}
parent::__construct($options);
$this->db = $con->selectDB($options['db_name']);
}
/**
* Starts the session.
*/
public function start()
{
if (self::$sessionStarted) {
return;
}
// use this object as the session handler
session_set_save_handler(
array($this, 'sessionOpen'),
array($this, 'sessionClose'),
array($this, 'sessionRead'),
array($this, 'sessionWrite'),
array($this, 'sessionDestroy'),
array($this, 'sessionGC')
);
parent::start();
}
/**
* Opens a session.
*
* @param string $path
* @param string $name
*
* @return bool
*/
public function sessionOpen($path = null, $name = null)
{
return true;
}
/**
* Closes a session.
*
* @return bool
*/
public function sessionClose()
{
return true;
}
/**
* Destroys a session.
*
* @param string $id The session ID to destroy
*
* @return bool
*/
public function sessionDestroy($id)
{
$collection = $this->options['collection'];
$id_field = $this->options['id_field'];
// Remove any records associated with this ID
return $this->db->selectCollection($collection)->remove(array(
$id_field => $id
));
}
/**
* Cleans up old sessions.
*
* @param int $lifetime The lifetime of a session (seconds)
*
* @return bool
*/
public function sessionGC($lifetime)
{
$collection = $this->options['collection'];
$time_field = $this->options['time_field'];
// delete any records that are past our expiry
$this->db->selectCollection($collection)->remove(array(
$time_field => array('$lt' => new \MongoDate(time() - $lifetime))
));
return true;
}
/**
* Reads a session.
*
* @param string $id A session ID
*
* @return string Session data
*/
public function sessionRead($id)
{
// get collection/fields
$collection = $this->options['collection'];
$data_field = $this->options['data_field'];
$id_field = $this->options['id_field'];
$time_field = $this->options['time_field'];
$collection = $this->db->selectCollection($collection);
$result = $collection->findOne(array($id_field => $id));
if ($result !== null) {
return $result[$data_field];
}
// session does not exist, create it
$collection->insert(array(
$id_field => $id,
$data_field => '',
$time_field => new \MongoDate(),
"created_at" => new \MongoDate(),
));
return '';
}
/**
* Writes session data.
*
* @param string $id A session ID
* @param string $data A serialised set of session data
*
* @return bool
*/
public function sessionWrite($id, $data)
{
$collection = $this->options['collection'];
$data_field = $this->options['data_field'];
$id_field = $this->options['id_field'];
$time_field = $this->options['time_field'];
$collection = $this->db->selectCollection($collection);
return $collection->update(
array(
$id_field => $id
),
array('$set' => array(
$data_field => $data,
$time_field => new \MongoDate()
))
);
}
/**
* Regenerates the session ID
* Also updates our Mongo record for the existing ID
*
* @param bool $destroy
* @return bool
*/
public function regenerate($destroy = false)
{
$existingID = $this->getId();
$collection = $this->options['collection'];
$id_field = $this->options['id_field'];
$time_field = $this->options['time_field'];
$collection = $this->db->selectCollection($collection);
$existingSession = $collection->findOne(array($id_field => $existingID));
parent::regenerate($destroy);
$newID = $this->getId();
if ($destroy)
{
// Wipe existing session
$collection->remove(
array($id_field => $existingID)
);
}
// Seems to be we can only update by specifying a non-updating
// field to search by - use the original Mongo ID to do this.
return $collection->update(
array(
"_id" => $existingSession["_id"]
),
array('$set' => array(
$id_field => $newID,
$time_field => new \MongoDate()
))
);
}
}