1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of Aura for PHP. |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
namespace Aura\Session; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* A central control point for new session segments, PHP session management |
14
|
|
|
* values, and CSRF token checking. |
15
|
|
|
* |
16
|
|
|
* @package Aura.Session |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
class Session |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* |
23
|
|
|
* Session key for the "next" flash values. |
24
|
|
|
* |
25
|
|
|
* @const string |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
const FLASH_NEXT = 'Aura\Session\Flash\Next'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* Session key for the "current" flash values. |
33
|
|
|
* |
34
|
|
|
* @const string |
35
|
|
|
* |
36
|
|
|
*/ |
37
|
|
|
const FLASH_NOW = 'Aura\Session\Flash\Now'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
* A session segment factory. |
42
|
|
|
* |
43
|
|
|
* @var SegmentFactory |
44
|
|
|
* |
45
|
|
|
*/ |
46
|
|
|
protected $segment_factory; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
* The CSRF token for this session. |
51
|
|
|
* |
52
|
|
|
* @var CsrfToken |
53
|
|
|
* |
54
|
|
|
*/ |
55
|
|
|
protected $csrf_token; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* |
59
|
|
|
* A CSRF token factory, for lazy-creating the CSRF token. |
60
|
|
|
* |
61
|
|
|
* @var CsrfTokenFactory |
62
|
|
|
* |
63
|
|
|
*/ |
64
|
|
|
protected $csrf_token_factory; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* |
68
|
|
|
* Incoming cookies from the client, typically a copy of the $_COOKIE |
69
|
|
|
* superglobal. |
70
|
|
|
* |
71
|
|
|
* @var array |
72
|
|
|
* |
73
|
|
|
*/ |
74
|
|
|
protected $cookies; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* |
78
|
|
|
* Session cookie parameters. |
79
|
|
|
* |
80
|
|
|
* @var array |
81
|
|
|
* |
82
|
|
|
*/ |
83
|
|
|
protected $cookie_params = array(); |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
* An object to intercept PHP function calls; this makes testing easier. |
88
|
|
|
* |
89
|
|
|
* @var Phpfunc |
90
|
|
|
* |
91
|
|
|
*/ |
92
|
|
|
protected $phpfunc; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* |
96
|
|
|
* A callable to invoke when deleting the session cookie. The callable |
97
|
|
|
* should have the signature ... |
98
|
|
|
* |
99
|
|
|
* function ($cookie_name, $cookie_params) |
100
|
|
|
* |
101
|
|
|
* ... and return null. |
102
|
|
|
* |
103
|
|
|
* @var callable|null |
104
|
|
|
* |
105
|
|
|
* @see setDeleteCookie() |
106
|
|
|
* |
107
|
|
|
*/ |
108
|
|
|
protected $delete_cookie; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* |
112
|
|
|
* Have the flash values been moved forward? |
113
|
|
|
* |
114
|
|
|
* @var bool |
115
|
|
|
* |
116
|
|
|
*/ |
117
|
|
|
protected $flash_moved = false; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* |
121
|
|
|
* Constructor |
122
|
|
|
* |
123
|
|
|
* @param SegmentFactory $segment_factory A session segment factory. |
124
|
|
|
* |
125
|
|
|
* @param CsrfTokenFactory $csrf_token_factory A CSRF token factory. |
126
|
|
|
* |
127
|
|
|
* @param Phpfunc $phpfunc An object to intercept PHP function calls; |
128
|
|
|
* this makes testing easier. |
129
|
|
|
* |
130
|
|
|
* @param array $cookies Optional: An array of cookies from the client, typically a |
131
|
|
|
* copy of $_COOKIE. Empty array by default. |
132
|
|
|
* |
133
|
|
|
* @param callable|null $delete_cookie Optional: An alternative callable |
134
|
|
|
* to invoke when deleting the session cookie. Defaults to `null`. |
135
|
|
|
* |
136
|
|
|
*/ |
137
|
33 |
|
public function __construct( |
138
|
|
|
SegmentFactory $segment_factory, |
139
|
|
|
CsrfTokenFactory $csrf_token_factory, |
140
|
|
|
Phpfunc $phpfunc, |
141
|
|
|
array $cookies = array(), |
142
|
|
|
$delete_cookie = null |
143
|
|
|
) { |
144
|
33 |
|
$this->segment_factory = $segment_factory; |
145
|
33 |
|
$this->csrf_token_factory = $csrf_token_factory; |
146
|
33 |
|
$this->phpfunc = $phpfunc; |
147
|
33 |
|
$this->cookies = $cookies; |
148
|
|
|
|
149
|
33 |
|
$this->setDeleteCookie($delete_cookie); |
150
|
|
|
|
151
|
33 |
|
$this->cookie_params = $this->phpfunc->session_get_cookie_params(); |
|
|
|
|
152
|
33 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* |
156
|
|
|
* Sets the delete-cookie callable. |
157
|
|
|
* |
158
|
|
|
* If parameter is `null`, the session cookie will be deleted using the |
159
|
|
|
* traditional way, i.e. using an expiration date in the past. |
160
|
|
|
* |
161
|
|
|
* @param callable|null $delete_cookie The callable to invoke when deleting the |
162
|
|
|
* session cookie. |
163
|
|
|
* |
164
|
|
|
*/ |
165
|
33 |
|
public function setDeleteCookie($delete_cookie) |
166
|
|
|
{ |
167
|
33 |
|
$this->delete_cookie = $delete_cookie; |
168
|
33 |
|
if (! $this->delete_cookie) { |
169
|
33 |
|
$phpfunc = $this->phpfunc; |
170
|
2 |
|
$this->delete_cookie = function ( |
171
|
|
|
$name, |
172
|
|
|
$params |
173
|
|
|
) use ($phpfunc) { |
174
|
2 |
|
$phpfunc->setcookie( |
|
|
|
|
175
|
2 |
|
$name, |
176
|
2 |
|
'', |
177
|
2 |
|
time() - 42000, |
178
|
2 |
|
$params['path'], |
179
|
2 |
|
$params['domain'] |
180
|
2 |
|
); |
181
|
2 |
|
}; |
182
|
33 |
|
} |
183
|
33 |
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* |
187
|
|
|
* Gets a new session segment instance by name. Segments with the same |
188
|
|
|
* name will be different objects but will reference the same $_SESSION |
189
|
|
|
* values, so it is possible to have two or more objects that share state. |
190
|
|
|
* For good or bad, this a function of how $_SESSION works. |
191
|
|
|
* |
192
|
|
|
* @param string $name The name of the session segment, typically a |
193
|
|
|
* fully-qualified class name. |
194
|
|
|
* |
195
|
|
|
* @return Segment New Segment instance. |
196
|
|
|
* |
197
|
|
|
*/ |
198
|
19 |
|
public function getSegment($name) |
199
|
|
|
{ |
200
|
19 |
|
return $this->segment_factory->newInstance($this, $name); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* |
205
|
|
|
* Is a session available to be resumed? |
206
|
|
|
* |
207
|
|
|
* @return bool |
208
|
|
|
* |
209
|
|
|
*/ |
210
|
18 |
|
public function isResumable() |
211
|
|
|
{ |
212
|
18 |
|
$name = $this->getName(); |
213
|
18 |
|
return isset($this->cookies[$name]); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* |
218
|
|
|
* Is the session already started? |
219
|
|
|
* |
220
|
|
|
* @return bool |
221
|
|
|
* |
222
|
|
|
*/ |
223
|
22 |
|
public function isStarted() |
224
|
|
|
{ |
225
|
22 |
|
if ($this->phpfunc->function_exists('session_status')) { |
|
|
|
|
226
|
21 |
|
$started = $this->phpfunc->session_status() === PHP_SESSION_ACTIVE; |
|
|
|
|
227
|
21 |
|
} else { |
228
|
1 |
|
$started = $this->sessionStatus(); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
// if the session was started externally, move the flash values forward |
232
|
22 |
|
if ($started && ! $this->flash_moved) { |
233
|
1 |
|
$this->moveFlash(); |
234
|
1 |
|
} |
235
|
|
|
|
236
|
|
|
// done |
237
|
22 |
|
return $started; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* |
242
|
|
|
* Returns the session status. |
243
|
|
|
* |
244
|
|
|
* Nota bene: |
245
|
|
|
* |
246
|
|
|
* PHP 5.3 implementation of session_status() for only active/none. |
247
|
|
|
* Relies on the fact that ini setting 'session.use_trans_sid' cannot be |
248
|
|
|
* changed when a session is active. |
249
|
|
|
* |
250
|
|
|
* PHP ini_set() raises a warning when we attempt to change this setting |
251
|
|
|
* and session is active. Note that the attempted change is to the |
252
|
|
|
* pre-existing value, so nothing will actually change on success. |
253
|
|
|
* |
254
|
|
|
*/ |
255
|
1 |
|
protected function sessionStatus() |
256
|
|
|
{ |
257
|
1 |
|
$setting = 'session.use_trans_sid'; |
258
|
1 |
|
$current = $this->phpfunc->ini_get($setting); |
|
|
|
|
259
|
1 |
|
$level = $this->phpfunc->error_reporting(0); |
|
|
|
|
260
|
1 |
|
$result = $this->phpfunc->ini_set($setting, $current); |
|
|
|
|
261
|
1 |
|
$this->phpfunc->error_reporting($level); |
|
|
|
|
262
|
1 |
|
return $result !== $current; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* |
267
|
|
|
* Starts a new or existing session. |
268
|
|
|
* |
269
|
|
|
* @return bool |
270
|
|
|
* |
271
|
|
|
*/ |
272
|
17 |
|
public function start() |
273
|
|
|
{ |
274
|
17 |
|
$result = $this->phpfunc->session_start(); |
|
|
|
|
275
|
17 |
|
if ($result) { |
276
|
17 |
|
$this->moveFlash(); |
277
|
17 |
|
} |
278
|
17 |
|
return $result; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* |
283
|
|
|
* Moves the "next" flash values to the "now" values, thereby clearing the |
284
|
|
|
* "next" values. |
285
|
|
|
* |
286
|
|
|
* @return null |
287
|
|
|
* |
288
|
|
|
*/ |
289
|
18 |
|
protected function moveFlash() |
|
|
|
|
290
|
|
|
{ |
291
|
18 |
|
if (! isset($_SESSION[Session::FLASH_NEXT])) { |
292
|
18 |
|
$_SESSION[Session::FLASH_NEXT] = array(); |
293
|
18 |
|
} |
294
|
18 |
|
$_SESSION[Session::FLASH_NOW] = $_SESSION[Session::FLASH_NEXT]; |
295
|
18 |
|
$_SESSION[Session::FLASH_NEXT] = array(); |
296
|
18 |
|
$this->flash_moved = true; |
297
|
18 |
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* |
301
|
|
|
* Resumes a session, but does not start a new one if there is no |
302
|
|
|
* existing one. |
303
|
|
|
* |
304
|
|
|
* @return bool |
305
|
|
|
* |
306
|
|
|
*/ |
307
|
17 |
|
public function resume() |
308
|
|
|
{ |
309
|
17 |
|
if ($this->isStarted()) { |
310
|
1 |
|
return true; |
311
|
|
|
} |
312
|
|
|
|
313
|
17 |
|
if ($this->isResumable()) { |
314
|
2 |
|
return $this->start(); |
315
|
|
|
} |
316
|
|
|
|
317
|
16 |
|
return false; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* |
322
|
|
|
* Clears all session variables across all segments. |
323
|
|
|
* |
324
|
|
|
* @return null |
325
|
|
|
* |
326
|
|
|
*/ |
327
|
3 |
|
public function clear() |
328
|
|
|
{ |
329
|
3 |
|
return $this->phpfunc->session_unset(); |
|
|
|
|
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* |
334
|
|
|
* Writes session data from all segments and ends the session. |
335
|
|
|
* |
336
|
|
|
* @return null |
337
|
|
|
* |
338
|
|
|
*/ |
339
|
2 |
|
public function commit() |
340
|
|
|
{ |
341
|
2 |
|
return $this->phpfunc->session_write_close(); |
|
|
|
|
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* |
346
|
|
|
* Destroys the session entirely. |
347
|
|
|
* |
348
|
|
|
* @return bool |
349
|
|
|
* |
350
|
|
|
* @see http://php.net/manual/en/function.session-destroy.php |
351
|
|
|
* |
352
|
|
|
*/ |
353
|
2 |
|
public function destroy() |
354
|
|
|
{ |
355
|
2 |
|
if (! $this->isStarted()) { |
356
|
1 |
|
$this->start(); |
357
|
1 |
|
} |
358
|
|
|
|
359
|
2 |
|
$name = $this->getName(); |
360
|
2 |
|
$params = $this->getCookieParams(); |
361
|
2 |
|
$this->clear(); |
362
|
|
|
|
363
|
2 |
|
$destroyed = $this->phpfunc->session_destroy(); |
|
|
|
|
364
|
2 |
|
if ($destroyed) { |
365
|
2 |
|
call_user_func($this->delete_cookie, $name, $params); |
366
|
2 |
|
} |
367
|
|
|
|
368
|
2 |
|
return $destroyed; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* |
373
|
|
|
* Returns the CSRF token, creating it if needed (and thereby starting a |
374
|
|
|
* session). |
375
|
|
|
* |
376
|
|
|
* @return CsrfToken |
377
|
|
|
* |
378
|
|
|
*/ |
379
|
5 |
|
public function getCsrfToken() |
380
|
|
|
{ |
381
|
5 |
|
if (! $this->csrf_token) { |
382
|
5 |
|
$this->csrf_token = $this->csrf_token_factory->newInstance($this); |
383
|
5 |
|
} |
384
|
|
|
|
385
|
5 |
|
return $this->csrf_token; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
// ======================================================================= |
389
|
|
|
// |
390
|
|
|
// support and admin methods |
391
|
|
|
// |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* |
395
|
|
|
* Sets the session cache expire time. |
396
|
|
|
* |
397
|
|
|
* @param int $expire The expiration time in seconds. |
398
|
|
|
* |
399
|
|
|
* @return int |
400
|
|
|
* |
401
|
|
|
* @see session_cache_expire() |
402
|
|
|
* |
403
|
|
|
*/ |
404
|
1 |
|
public function setCacheExpire($expire) |
405
|
|
|
{ |
406
|
1 |
|
return $this->phpfunc->session_cache_expire($expire); |
|
|
|
|
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* |
411
|
|
|
* Gets the session cache expire time. |
412
|
|
|
* |
413
|
|
|
* @return int The cache expiration time in seconds. |
414
|
|
|
* |
415
|
|
|
* @see session_cache_expire() |
416
|
|
|
* |
417
|
|
|
*/ |
418
|
1 |
|
public function getCacheExpire() |
419
|
|
|
{ |
420
|
1 |
|
return $this->phpfunc->session_cache_expire(); |
|
|
|
|
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* |
425
|
|
|
* Sets the session cache limiter value. |
426
|
|
|
* |
427
|
|
|
* @param string $limiter The limiter value. |
428
|
|
|
* |
429
|
|
|
* @return string |
430
|
|
|
* |
431
|
|
|
* @see session_cache_limiter() |
432
|
|
|
* |
433
|
|
|
*/ |
434
|
1 |
|
public function setCacheLimiter($limiter) |
435
|
|
|
{ |
436
|
1 |
|
return $this->phpfunc->session_cache_limiter($limiter); |
|
|
|
|
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* |
441
|
|
|
* Gets the session cache limiter value. |
442
|
|
|
* |
443
|
|
|
* @return string The limiter value. |
444
|
|
|
* |
445
|
|
|
* @see session_cache_limiter() |
446
|
|
|
* |
447
|
|
|
*/ |
448
|
1 |
|
public function getCacheLimiter() |
449
|
|
|
{ |
450
|
1 |
|
return $this->phpfunc->session_cache_limiter(); |
|
|
|
|
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* |
455
|
|
|
* Sets the session cookie params. Param array keys are: |
456
|
|
|
* |
457
|
|
|
* - `lifetime` : Lifetime of the session cookie, defined in seconds. |
458
|
|
|
* |
459
|
|
|
* - `path` : Path on the domain where the cookie will work. |
460
|
|
|
* Use a single slash ('/') for all paths on the domain. |
461
|
|
|
* |
462
|
|
|
* - `domain` : Cookie domain, for example 'www.php.net'. |
463
|
|
|
* To make cookies visible on all subdomains then the domain must be |
464
|
|
|
* prefixed with a dot like '.php.net'. |
465
|
|
|
* |
466
|
|
|
* - `secure` : If TRUE cookie will only be sent over secure connections. |
467
|
|
|
* |
468
|
|
|
* - `httponly` : If set to TRUE then PHP will attempt to send the httponly |
469
|
|
|
* flag when setting the session cookie. |
470
|
|
|
* |
471
|
|
|
* @param array $params The array of session cookie param keys and values. |
472
|
|
|
* |
473
|
|
|
* @return null |
474
|
|
|
* |
475
|
|
|
* @see session_set_cookie_params() |
476
|
|
|
* |
477
|
|
|
*/ |
478
|
1 |
|
public function setCookieParams(array $params) |
479
|
|
|
{ |
480
|
1 |
|
$this->cookie_params = array_merge($this->cookie_params, $params); |
481
|
1 |
|
$this->phpfunc->session_set_cookie_params( |
|
|
|
|
482
|
1 |
|
$this->cookie_params['lifetime'], |
483
|
1 |
|
$this->cookie_params['path'], |
484
|
1 |
|
$this->cookie_params['domain'], |
485
|
1 |
|
$this->cookie_params['secure'], |
486
|
1 |
|
$this->cookie_params['httponly'] |
487
|
1 |
|
); |
488
|
1 |
|
} |
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* |
492
|
|
|
* Gets the session cookie params. |
493
|
|
|
* |
494
|
|
|
* @return array |
495
|
|
|
* |
496
|
|
|
*/ |
497
|
3 |
|
public function getCookieParams() |
498
|
|
|
{ |
499
|
3 |
|
return $this->cookie_params; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* |
504
|
|
|
* Gets the current session id. |
505
|
|
|
* |
506
|
|
|
* @return string |
507
|
|
|
* |
508
|
|
|
*/ |
509
|
1 |
|
public function getId() |
510
|
|
|
{ |
511
|
1 |
|
return $this->phpfunc->session_id(); |
|
|
|
|
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
/** |
515
|
|
|
* |
516
|
|
|
* Regenerates and replaces the current session id; also regenerates the |
517
|
|
|
* CSRF token value if one exists. |
518
|
|
|
* |
519
|
|
|
* @return bool True if regeneration worked, false if not. |
520
|
|
|
* |
521
|
|
|
*/ |
522
|
1 |
|
public function regenerateId() |
523
|
|
|
{ |
524
|
1 |
|
$result = $this->phpfunc->session_regenerate_id(true); |
|
|
|
|
525
|
1 |
|
if ($result && $this->csrf_token) { |
526
|
1 |
|
$this->csrf_token->regenerateValue(); |
527
|
1 |
|
} |
528
|
1 |
|
return $result; |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* |
533
|
|
|
* Sets the current session name. |
534
|
|
|
* |
535
|
|
|
* @param string $name The session name to use. |
536
|
|
|
* |
537
|
|
|
* @return string |
538
|
|
|
* |
539
|
|
|
* @see session_name() |
540
|
|
|
* |
541
|
|
|
*/ |
542
|
1 |
|
public function setName($name) |
543
|
|
|
{ |
544
|
1 |
|
return $this->phpfunc->session_name($name); |
|
|
|
|
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
/** |
548
|
|
|
* |
549
|
|
|
* Returns the current session name. |
550
|
|
|
* |
551
|
|
|
* @return string |
552
|
|
|
* |
553
|
|
|
*/ |
554
|
19 |
|
public function getName() |
555
|
|
|
{ |
556
|
19 |
|
return $this->phpfunc->session_name(); |
|
|
|
|
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* |
561
|
|
|
* Sets the session save path. |
562
|
|
|
* |
563
|
|
|
* @param string $path The new save path. |
564
|
|
|
* |
565
|
|
|
* @return string |
566
|
|
|
* |
567
|
|
|
* @see session_save_path() |
568
|
|
|
* |
569
|
|
|
*/ |
570
|
1 |
|
public function setSavePath($path) |
571
|
|
|
{ |
572
|
1 |
|
return $this->phpfunc->session_save_path($path); |
|
|
|
|
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
/** |
576
|
|
|
* |
577
|
|
|
* Gets the session save path. |
578
|
|
|
* |
579
|
|
|
* @return string |
580
|
|
|
* |
581
|
|
|
* @see session_save_path() |
582
|
|
|
* |
583
|
|
|
*/ |
584
|
1 |
|
public function getSavePath() |
585
|
|
|
{ |
586
|
1 |
|
return $this->phpfunc->session_save_path(); |
|
|
|
|
587
|
|
|
} |
588
|
|
|
} |
589
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: