|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* \AppserverIo\Psr\Servlet\ServletSessionWrapper |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/appserver-io-psr/servlet |
|
18
|
|
|
* @link http://www.appserver.io |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace AppserverIo\Psr\Servlet; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* A servlet session implementation. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Tim Wagner <[email protected]> |
|
27
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
28
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
29
|
|
|
* @link https://github.com/appserver-io-psr/servlet |
|
30
|
|
|
* @link http://www.appserver.io |
|
31
|
|
|
*/ |
|
32
|
|
|
class ServletSessionWrapper |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The session instance. |
|
37
|
|
|
* |
|
38
|
|
|
* @var \AppserverIo\Psr\Servlet\ServletSessionInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $session; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Injects the passed session instance into this servlet session wrapper. |
|
44
|
|
|
* |
|
45
|
|
|
* @param \AppserverIo\Psr\Servlet\ServletSessionInterface $session The session instance used for initialization |
|
46
|
|
|
* |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
public function injectSession(ServletSessionInterface $session) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->session = $session; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns the servlet session instance. |
|
56
|
|
|
* |
|
57
|
|
|
* @return \AppserverIo\Psr\Servlet\ServletSessionInterface The session instance |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getSession() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->session; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Starts the session, if it has not been already started |
|
66
|
|
|
* |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
|
|
public function start() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->getSession()->start(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Tells if the session has been started already. |
|
76
|
|
|
* |
|
77
|
|
|
* @return boolean |
|
78
|
|
|
*/ |
|
79
|
|
|
public function isStarted() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->getSession()->isStarted(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Returns the current session identifier |
|
86
|
|
|
* |
|
87
|
|
|
* @return string The current session identifier |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getId() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->getSession()->getId(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Sets the current session identifier. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $id The current session identifier |
|
98
|
|
|
* |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
|
|
public function setId($id) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->getSession()->setId($id); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Returns the unix time stamp marking the last point in time this session has |
|
108
|
|
|
* been in use. |
|
109
|
|
|
* |
|
110
|
|
|
* For the current (local) session, this method will always return the current |
|
111
|
|
|
* time. For a remote session, the unix timestamp will be returned. |
|
112
|
|
|
* |
|
113
|
|
|
* @return integer UNIX timestamp |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getLastActivityTimestamp() |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->getSession()->getLastActivityTimestamp(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Returns the session name. |
|
122
|
|
|
* |
|
123
|
|
|
* @return string The session name |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getName() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->getSession()->getName(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Sets the session name. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $name The session name |
|
134
|
|
|
* |
|
135
|
|
|
* @return void |
|
136
|
|
|
*/ |
|
137
|
|
|
public function setName($name) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->getSession()->setName($name); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns date and time after the session expires. |
|
144
|
|
|
* |
|
145
|
|
|
* @return integer|\DateTime The date and time after the session expires |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getLifetime() |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->getSession()->getLifetime(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Sets date and time after the session expires. |
|
154
|
|
|
* |
|
155
|
|
|
* @param integer|\DateTime $lifetime The date and time after the session expires |
|
156
|
|
|
* |
|
157
|
|
|
* @return void |
|
158
|
|
|
*/ |
|
159
|
|
|
public function setLifetime($lifetime) |
|
160
|
|
|
{ |
|
161
|
|
|
$this->getSession()->setLifetime($lifetime); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Returns the number of seconds until the session expires. |
|
166
|
|
|
* |
|
167
|
|
|
* @return integer|null Number of seconds until the session expires |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getMaximumAge() |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->getSession()->getMaximumAge(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Sets the number of seconds until the session expires. |
|
176
|
|
|
* |
|
177
|
|
|
* @param integer $maximumAge Number of seconds until the session expires |
|
178
|
|
|
* |
|
179
|
|
|
* @return void |
|
180
|
|
|
*/ |
|
181
|
|
|
public function setMaximumAge($maximumAge) |
|
182
|
|
|
{ |
|
183
|
|
|
$this->getSession()->setMaximumAge($maximumAge); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Returns the host to which the user agent will send this cookie. |
|
188
|
|
|
* |
|
189
|
|
|
* @return string|null The host to which the user agent will send this cookie |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getDomain() |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->getSession()->getDomain(); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Sets the host to which the user agent will send this cookie. |
|
198
|
|
|
* |
|
199
|
|
|
* @param string $domain The host to which the user agent will send this cookie |
|
200
|
|
|
* |
|
201
|
|
|
* @return void |
|
202
|
|
|
*/ |
|
203
|
|
|
public function setDomain($domain) |
|
204
|
|
|
{ |
|
205
|
|
|
$this->getSession()->setDomain($domain); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Returns the path describing the scope of this cookie. |
|
210
|
|
|
* |
|
211
|
|
|
* @return string The path describing the scope of this cookie |
|
212
|
|
|
*/ |
|
213
|
|
|
public function getPath() |
|
214
|
|
|
{ |
|
215
|
|
|
return $this->getSession()->getPath(); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Sets the path describing the scope of this cookie. |
|
220
|
|
|
* |
|
221
|
|
|
* @param string $path The path describing the scope of this cookie |
|
222
|
|
|
* |
|
223
|
|
|
* @return void |
|
224
|
|
|
*/ |
|
225
|
|
|
public function setPath($path) |
|
226
|
|
|
{ |
|
227
|
|
|
$this->getSession()->setPath($path); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Returns if this session should only be sent through a "secure" channel by the user agent. |
|
232
|
|
|
* |
|
233
|
|
|
* @return boolean TRUE if the session should only be sent through a "secure" channel, else FALSE |
|
234
|
|
|
*/ |
|
235
|
|
|
public function isSecure() |
|
236
|
|
|
{ |
|
237
|
|
|
return $this->getSession()->isSecure(); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Sets the flag that this session should only be sent through a "secure" channel by the user agent. |
|
242
|
|
|
* |
|
243
|
|
|
* @param boolean $secure TRUE if the session should only be sent through a "secure" channel, else FALSE |
|
244
|
|
|
* |
|
245
|
|
|
* @return void |
|
246
|
|
|
*/ |
|
247
|
|
|
public function setSecure($secure = true) |
|
248
|
|
|
{ |
|
249
|
|
|
$this->getSession()->setSecure($secure); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Returns if this session should only be used through the HTTP protocol. |
|
254
|
|
|
* |
|
255
|
|
|
* @return boolean TRUE if the session should only be used through the HTTP protocol |
|
256
|
|
|
*/ |
|
257
|
|
|
public function isHttpOnly() |
|
258
|
|
|
{ |
|
259
|
|
|
return $this->getSession()->isHttpOnly(); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Sets the flag that this session should only be used through the HTTP protocol. |
|
264
|
|
|
* |
|
265
|
|
|
* @param boolean $httpOnly TRUE if the session should only be used through the HTTP protocol |
|
266
|
|
|
* |
|
267
|
|
|
* @return void |
|
268
|
|
|
*/ |
|
269
|
|
|
public function setHttpOnly($httpOnly = true) |
|
270
|
|
|
{ |
|
271
|
|
|
$this->getSession()->setHttpOnly($httpOnly); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Returns the data associated with the given key. |
|
276
|
|
|
* |
|
277
|
|
|
* @param string $key An identifier for the content stored in the session. |
|
278
|
|
|
* |
|
279
|
|
|
* @return mixed The contents associated with the given key |
|
280
|
|
|
*/ |
|
281
|
|
|
public function getData($key) |
|
282
|
|
|
{ |
|
283
|
|
|
return $this->getSession()->getData($key); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Returns TRUE if a session data entry $key is available. |
|
288
|
|
|
* |
|
289
|
|
|
* @param string $key Entry identifier of the session data |
|
290
|
|
|
* |
|
291
|
|
|
* @return boolean |
|
292
|
|
|
*/ |
|
293
|
|
|
public function hasKey($key) |
|
294
|
|
|
{ |
|
295
|
|
|
return $this->getSession()->hasKey($key); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* Stores the given data under the given key in the session |
|
300
|
|
|
* |
|
301
|
|
|
* @param string $key The key under which the data should be stored |
|
302
|
|
|
* @param mixed $data The data to be stored |
|
303
|
|
|
* |
|
304
|
|
|
* @return void |
|
305
|
|
|
*/ |
|
306
|
|
|
public function putData($key, $data) |
|
307
|
|
|
{ |
|
308
|
|
|
$this->getSession()->putData($key, $data); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* Tags this session with the given tag. |
|
313
|
|
|
* |
|
314
|
|
|
* Note that third-party libraries might also tag your session. Therefore it is |
|
315
|
|
|
* recommended to use namespaced tags such as "Acme-Demo-MySpecialTag". |
|
316
|
|
|
* |
|
317
|
|
|
* @param string $tag The tag – must match be a valid cache frontend tag |
|
318
|
|
|
* |
|
319
|
|
|
* @return void |
|
320
|
|
|
*/ |
|
321
|
|
|
public function addTag($tag) |
|
322
|
|
|
{ |
|
323
|
|
|
$this->getSession()->addTag($tag); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* Removes the specified tag from this session. |
|
328
|
|
|
* |
|
329
|
|
|
* @param string $tag The tag – must match be a valid cache frontend tag |
|
330
|
|
|
* |
|
331
|
|
|
* @return void |
|
332
|
|
|
*/ |
|
333
|
|
|
public function removeTag($tag) |
|
334
|
|
|
{ |
|
335
|
|
|
$this->getSession()->removeTag($tag); |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* Returns the tags this session has been tagged with. |
|
340
|
|
|
* |
|
341
|
|
|
* @return array The tags or an empty array if there aren't any |
|
342
|
|
|
*/ |
|
343
|
|
|
public function getTags() |
|
344
|
|
|
{ |
|
345
|
|
|
return $this->getSession()->getTags(); |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
/** |
|
349
|
|
|
* Returns TRUE if there is a session that can be resumed. |
|
350
|
|
|
* |
|
351
|
|
|
* If a to-be-resumed session was inactive for too long, this function will |
|
352
|
|
|
* trigger the expiration of that session. An expired session cannot be resumed. |
|
353
|
|
|
* |
|
354
|
|
|
* NOTE that this method does a bit more than the name implies: Because the |
|
355
|
|
|
* session info data needs to be loaded, this method stores this data already |
|
356
|
|
|
* so it doesn't have to be loaded again once the session is being used. |
|
357
|
|
|
* |
|
358
|
|
|
* @return boolean |
|
359
|
|
|
*/ |
|
360
|
|
|
public function canBeResumed() |
|
361
|
|
|
{ |
|
362
|
|
|
return $this->getSession()->canBeResumed(); |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* Resumes an existing session, if any. |
|
367
|
|
|
* |
|
368
|
|
|
* @return integer If a session was resumed, the inactivity of since the last request is returned |
|
369
|
|
|
*/ |
|
370
|
|
|
public function resume() |
|
371
|
|
|
{ |
|
372
|
|
|
return $this->getSession()->resume(); |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* Explicitly destroys all session data. |
|
377
|
|
|
* |
|
378
|
|
|
* @param string $reason The reason why the session has been destroyed |
|
379
|
|
|
* |
|
380
|
|
|
* @return void |
|
381
|
|
|
*/ |
|
382
|
|
|
public function destroy($reason) |
|
383
|
|
|
{ |
|
384
|
|
|
$this->getSession()->destroy($reason); |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
/** |
|
388
|
|
|
* Returns the checksum for this session instance. |
|
389
|
|
|
* |
|
390
|
|
|
* @return string The checksum |
|
391
|
|
|
*/ |
|
392
|
|
|
public function checksum() |
|
393
|
|
|
{ |
|
394
|
|
|
return $this->getSession()->checksum(); |
|
395
|
|
|
} |
|
396
|
|
|
} |
|
397
|
|
|
|