1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Appserver\ServletEngine\DefaultSessionSettings |
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/appserver |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Appserver\ServletEngine; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Http\HttpCookie; |
24
|
|
|
use AppserverIo\Psr\Servlet\ServletSessionInterface; |
25
|
|
|
use AppserverIo\Psr\Servlet\ServletContextInterface; |
26
|
|
|
use AppserverIo\Storage\GenericStackable; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Interface for all session storage implementation. |
30
|
|
|
* |
31
|
|
|
* @author Tim Wagner <[email protected]> |
32
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
33
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
34
|
|
|
* @link https://github.com/appserver-io/appserver |
35
|
|
|
* @link http://www.appserver.io |
36
|
|
|
* @see http://php.net/session |
37
|
|
|
* @see http://php.net/setcookie |
38
|
|
|
* |
39
|
|
|
* @property string $sessionName The session name |
40
|
|
|
* @property string $sessionFilePrefix The session file prefix |
41
|
|
|
* @property string $sessionSavePath The default path to persist sessions |
42
|
|
|
* @property integer $sessionCookieLifetime The session cookie lifetime |
43
|
|
|
* @property integer $sessionMaximumAge The maximum age in seconds, or NULL if none has been defined |
44
|
|
|
* @property string $sessionCookieDomain The cookie domain set for the session |
45
|
|
|
* @property string $sessionCookiePath The cookie path set for the session |
46
|
|
|
* @property boolean $sessionCookieSecure TRUE if a secure cookie should be set, else FALSE |
47
|
|
|
* @property boolean $sessionCookieHttpOnly TRUE if a Http only cookie should be used |
48
|
|
|
* @property float $garbageCollectionProbability The garbage collector probability |
49
|
|
|
* @property integer $inactivityTimeout The inactivity timeout in seconds |
50
|
|
|
*/ |
51
|
|
|
class DefaultSessionSettings extends GenericStackable implements SessionSettingsInterface |
52
|
|
|
{ |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The default servlet session name. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
const DEFAULT_SESSION_NAME = 'SESSID'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The default session prefix. |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
const DEFAULT_SESSION_FILE_PREFIX = 'sess_'; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The default session cookie path. |
70
|
|
|
* |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
const DEFAULT_SESSION_COOKIE_PATH = '/'; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* The default inactivity timeout. |
77
|
|
|
* |
78
|
|
|
* @var string |
79
|
|
|
*/ |
80
|
|
|
const DEFAULT_INACTIVITY_TIMEOUT = 1440; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* The default probaility the garbage collection will be invoked. |
84
|
|
|
* |
85
|
|
|
* @var string |
86
|
|
|
*/ |
87
|
|
|
const DEFAULT_GARBAGE_COLLECTION_PROBABILITY = 0.1; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Initialize the default session settings. |
91
|
|
|
*/ |
92
|
1 |
|
public function __construct() |
93
|
|
|
{ |
94
|
|
|
// initialize the default values |
95
|
1 |
|
$this->setSessionCookieLifetime(86400); |
96
|
1 |
|
$this->setSessionName(DefaultSessionSettings::DEFAULT_SESSION_NAME); |
97
|
1 |
|
$this->setSessionFilePrefix(DefaultSessionSettings::DEFAULT_SESSION_FILE_PREFIX); |
98
|
1 |
|
$this->setSessionMaximumAge(0); |
99
|
1 |
|
$this->setSessionCookieDomain(HttpCookie::LOCALHOST); |
100
|
1 |
|
$this->setSessionCookiePath(DefaultSessionSettings::DEFAULT_SESSION_COOKIE_PATH); |
101
|
1 |
|
$this->setSessionCookieSecure(false); |
102
|
1 |
|
$this->setSessionCookieHttpOnly(false); |
103
|
1 |
|
$this->setGarbageCollectionProbability(DefaultSessionSettings::DEFAULT_GARBAGE_COLLECTION_PROBABILITY); |
104
|
1 |
|
$this->setInactivityTimeout(DefaultSessionSettings::DEFAULT_INACTIVITY_TIMEOUT); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the session name |
109
|
|
|
* |
110
|
|
|
* @param string $sessionName The session name |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
1 |
|
public function setSessionName($sessionName) |
115
|
|
|
{ |
116
|
1 |
|
$this->sessionName = $sessionName; |
117
|
1 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns the session name to use. |
121
|
|
|
* |
122
|
|
|
* @return string The session name |
123
|
|
|
*/ |
124
|
1 |
|
public function getSessionName() |
125
|
|
|
{ |
126
|
1 |
|
return $this->sessionName; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Set the session file prefix we use. |
131
|
|
|
* |
132
|
|
|
* @param string $sessionFilePrefix The session file prefix |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
1 |
|
public function setSessionFilePrefix($sessionFilePrefix) |
137
|
|
|
{ |
138
|
1 |
|
$this->sessionFilePrefix = $sessionFilePrefix; |
139
|
1 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns the session file prefix to use. |
143
|
|
|
* |
144
|
|
|
* @return string The session file prefix |
145
|
|
|
*/ |
146
|
|
|
public function getSessionFilePrefix() |
147
|
|
|
{ |
148
|
|
|
return $this->sessionFilePrefix; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Set the default path to persist sessions. |
153
|
|
|
* |
154
|
|
|
* @param string $sessionSavePath The default path to persist sessions |
155
|
|
|
* |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
|
|
public function setSessionSavePath($sessionSavePath) |
159
|
|
|
{ |
160
|
|
|
$this->sessionSavePath = $sessionSavePath; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Returns the default path to persist sessions. |
165
|
|
|
* |
166
|
|
|
* @return string The default path to persist session |
167
|
|
|
*/ |
168
|
|
|
public function getSessionSavePath() |
169
|
|
|
{ |
170
|
|
|
return $this->sessionSavePath; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Sets the session cookie lifetime. |
175
|
|
|
* |
176
|
|
|
* @param integer $sessionCookieLifetime The session cookie lifetime |
177
|
|
|
* |
178
|
|
|
* @return void |
179
|
|
|
*/ |
180
|
1 |
|
public function setSessionCookieLifetime($sessionCookieLifetime) |
181
|
|
|
{ |
182
|
1 |
|
$this->sessionCookieLifetime = $sessionCookieLifetime; |
183
|
1 |
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Returns the session cookie lifetime. |
187
|
|
|
* |
188
|
|
|
* @return integer The session cookie lifetime |
189
|
|
|
*/ |
190
|
|
|
public function getSessionCookieLifetime() |
191
|
|
|
{ |
192
|
|
|
return $this->sessionCookieLifetime; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Sets the number of seconds until the session expires, if defined. |
197
|
|
|
* |
198
|
|
|
* @param integer $sessionMaximumAge The maximum age in seconds, or NULL if none has been defined. |
199
|
|
|
* |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
1 |
|
public function setSessionMaximumAge($sessionMaximumAge) |
203
|
|
|
{ |
204
|
1 |
|
$this->sessionMaximumAge = $sessionMaximumAge; |
205
|
1 |
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Returns the number of seconds until the session expires, if defined. |
209
|
|
|
* |
210
|
|
|
* @return integer The maximum age in seconds, or NULL if none has been defined. |
211
|
|
|
*/ |
212
|
|
|
public function getSessionMaximumAge() |
213
|
|
|
{ |
214
|
|
|
return $this->sessionMaximumAge; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Sets the cookie domain set for the session. |
219
|
|
|
* |
220
|
|
|
* @param string $sessionCookieDomain The cookie domain set for the session |
221
|
|
|
* |
222
|
|
|
* @return void |
223
|
|
|
*/ |
224
|
1 |
|
public function setSessionCookieDomain($sessionCookieDomain) |
225
|
|
|
{ |
226
|
1 |
|
$this->sessionCookieDomain = $sessionCookieDomain; |
227
|
1 |
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Returns the cookie domain set for the session. |
231
|
|
|
* |
232
|
|
|
* @return string The cookie domain set for the session |
233
|
|
|
*/ |
234
|
|
|
public function getSessionCookieDomain() |
235
|
|
|
{ |
236
|
|
|
return $this->sessionCookieDomain; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Sets the cookie path set for the session. |
241
|
|
|
* |
242
|
|
|
* @param string $sessionCookiePath The cookie path set for the session |
243
|
|
|
* |
244
|
|
|
* @return void |
245
|
|
|
*/ |
246
|
1 |
|
public function setSessionCookiePath($sessionCookiePath) |
247
|
|
|
{ |
248
|
1 |
|
$this->sessionCookiePath = $sessionCookiePath; |
249
|
1 |
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Returns the cookie path set for the session. |
253
|
|
|
* |
254
|
|
|
* @return string The cookie path set for the session |
255
|
|
|
*/ |
256
|
|
|
public function getSessionCookiePath() |
257
|
|
|
{ |
258
|
|
|
return $this->sessionCookiePath; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Sets the flag that the session cookie should only be set in a secure connection. |
263
|
|
|
* |
264
|
|
|
* @param boolean $sessionCookieSecure TRUE if a secure cookie should be set, else FALSE |
265
|
|
|
* |
266
|
|
|
* @return void |
267
|
|
|
*/ |
268
|
1 |
|
public function setSessionCookieSecure($sessionCookieSecure) |
269
|
|
|
{ |
270
|
1 |
|
$this->sessionCookieSecure = $sessionCookieSecure; |
271
|
1 |
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Returns the flag that the session cookie should only be set in a secure connection. |
275
|
|
|
* |
276
|
|
|
* @return boolean TRUE if a secure cookie should be set, else FALSE |
277
|
|
|
*/ |
278
|
|
|
public function getSessionCookieSecure() |
279
|
|
|
{ |
280
|
|
|
return $this->sessionCookieSecure; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Sets the flag if the session should set a Http only cookie. |
285
|
|
|
* |
286
|
|
|
* @param boolean $sessionCookieHttpOnly TRUE if a Http only cookie should be used |
287
|
|
|
* |
288
|
|
|
* @return void |
289
|
|
|
*/ |
290
|
1 |
|
public function setSessionCookieHttpOnly($sessionCookieHttpOnly) |
291
|
|
|
{ |
292
|
1 |
|
$this->sessionCookieHttpOnly = $sessionCookieHttpOnly; |
293
|
1 |
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Returns the flag if the session should set a Http only cookie. |
297
|
|
|
* |
298
|
|
|
* @return boolean TRUE if a Http only cookie should be used |
299
|
|
|
*/ |
300
|
|
|
public function getSessionCookieHttpOnly() |
301
|
|
|
{ |
302
|
|
|
return $this->sessionCookieHttpOnly; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Sets the probability the garbage collector will be invoked on the session. |
307
|
|
|
* |
308
|
|
|
* @param float $garbageCollectionProbability The garbage collector probability |
309
|
|
|
* |
310
|
|
|
* @return void |
311
|
|
|
*/ |
312
|
1 |
|
public function setGarbageCollectionProbability($garbageCollectionProbability) |
313
|
|
|
{ |
314
|
1 |
|
$this->garbageCollectionProbability = $garbageCollectionProbability; |
315
|
1 |
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Returns the probability the garbage collector will be invoked on the session. |
319
|
|
|
* |
320
|
|
|
* @return float The garbage collector probability |
321
|
|
|
*/ |
322
|
|
|
public function getGarbageCollectionProbability() |
323
|
|
|
{ |
324
|
|
|
return $this->garbageCollectionProbability; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Sets the inactivity timeout until the session will be invalidated. |
329
|
|
|
* |
330
|
|
|
* @param integer $inactivityTimeout The inactivity timeout in seconds |
331
|
|
|
* |
332
|
|
|
* @return void |
333
|
|
|
*/ |
334
|
1 |
|
public function setInactivityTimeout($inactivityTimeout) |
335
|
|
|
{ |
336
|
1 |
|
$this->inactivityTimeout = $inactivityTimeout; |
337
|
1 |
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Returns the inactivity timeout until the session will be invalidated. |
341
|
|
|
* |
342
|
|
|
* @return integer The inactivity timeout in seconds |
343
|
|
|
*/ |
344
|
|
|
public function getInactivityTimeout() |
345
|
|
|
{ |
346
|
|
|
return $this->inactivityTimeout; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Merges the values of the passed settings into this instance and overwrites the one of this instance. |
351
|
|
|
* |
352
|
|
|
* @param \AppserverIo\Psr\Servlet\ServletContextInterface $context The context we want to merge the session settings from |
353
|
|
|
* |
354
|
|
|
* @return void |
355
|
|
|
*/ |
356
|
|
|
public function mergeServletContext(ServletContextInterface $context) |
357
|
|
|
{ |
358
|
|
|
|
359
|
|
|
// check if the context has his own session parameters |
360
|
|
|
if ($context->hasSessionParameters() === true) { |
361
|
|
|
if (($garbageCollectionProbability = $context->getSessionParameter(ServletSessionInterface::GARBAGE_COLLECTION_PROBABILITY)) !== null) { |
362
|
|
|
$this->setGarbageCollectionProbability((float) $garbageCollectionProbability); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
if (($sessionName = $context->getSessionParameter(ServletSessionInterface::SESSION_NAME)) !== null) { |
366
|
|
|
$this->setSessionName($sessionName); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
if (($sessionFilePrefix = $context->getSessionParameter(ServletSessionInterface::SESSION_FILE_PREFIX)) !== null) { |
370
|
|
|
$this->setSessionFilePrefix($sessionFilePrefix); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
if (($sessionSavePath = $context->getSessionParameter(ServletSessionInterface::SESSION_SAVE_PATH)) !== null) { |
374
|
|
|
$this->setSessionSavePath($sessionSavePath); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
if (($sessionMaximumAge = $context->getSessionParameter(ServletSessionInterface::SESSION_MAXIMUM_AGE)) !== null) { |
378
|
|
|
$this->setSessionMaximumAge((integer) $sessionMaximumAge); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
if (($sessionInactivityTimeout = $context->getSessionParameter(ServletSessionInterface::SESSION_INACTIVITY_TIMEOUT)) !== null) { |
382
|
|
|
$this->setInactivityTimeout((integer) $sessionInactivityTimeout); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
if (($sessionCookieLifetime = $context->getSessionParameter(ServletSessionInterface::SESSION_COOKIE_LIFETIME)) !== null) { |
386
|
|
|
$this->setSessionCookieLifetime((integer) $sessionCookieLifetime); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
if (($sessionCookieDomain = $context->getSessionParameter(ServletSessionInterface::SESSION_COOKIE_DOMAIN)) !== null) { |
390
|
|
|
$this->setSessionCookieDomain($sessionCookieDomain); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
if (($sessionCookiePath = $context->getSessionParameter(ServletSessionInterface::SESSION_COOKIE_PATH)) !== null) { |
394
|
|
|
$this->setSessionCookiePath($sessionCookiePath); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
if (($sessionCookieSecure = $context->getSessionParameter(ServletSessionInterface::SESSION_COOKIE_SECURE)) !== null) { |
398
|
|
|
$this->setSessionCookieSecure((boolean) $sessionCookieSecure); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
if (($sessionCookieHttpOnly = $context->getSessionParameter(ServletSessionInterface::SESSION_COOKIE_HTTP_ONLY)) !== null) { |
402
|
|
|
$this->setSessionCookieHttpOnly((boolean) $sessionCookieHttpOnly); |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
|