/var/www/hkosl.com/imusiccircle/webadmin/libraies/aura/session/src/Session.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
<?php
/**
 *
 * This file is part of Aura for PHP.
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 *
 */
namespace Aura\Session;

/**
 *
 * A central control point for new session segments, PHP session management
 * values, and CSRF token checking.
 *
 * @package Aura.Session
 *
 */
class Session
{
    
/**
     *
     * Session key for the "next" flash values.
     *
     * @const string
     *
     */
    
const FLASH_NEXT 'Aura\Session\Flash\Next';

    
/**
     *
     * Session key for the "current" flash values.
     *
     * @const string
     *
     */
    
const FLASH_NOW 'Aura\Session\Flash\Now';

    
/**
     *
     * A session segment factory.
     *
     * @var SegmentFactory
     *
     */
    
protected $segment_factory;

    
/**
     *
     * The CSRF token for this session.
     *
     * @var CsrfToken
     *
     */
    
protected $csrf_token;

    
/**
     *
     * A CSRF token factory, for lazy-creating the CSRF token.
     *
     * @var CsrfTokenFactory
     *
     */
    
protected $csrf_token_factory;

    
/**
     *
     * Incoming cookies from the client, typically a copy of the $_COOKIE
     * superglobal.
     *
     * @var array
     *
     */
    
protected $cookies;

    
/**
     *
     * Session cookie parameters.
     *
     * @var array
     *
     */
    
protected $cookie_params = array();

    
/**
     *
     * An object to intercept PHP function calls; this makes testing easier.
     *
     * @var Phpfunc
     *
     */
    
protected $phpfunc;

    
/**
     *
     * A callable to invoke when deleting the session cookie. The callable
     * should have the signature ...
     *
     *      function ($cookie_name, $cookie_params)
     *
     * ... and return null.
     *
     * @var callable|null
     *
     * @see setDeleteCookie()
     *
     */
    
protected $delete_cookie;

    
/**
     *
     * Have the flash values been moved forward?
     *
     * @var bool
     *
     */
    
protected $flash_moved false;

    
/**
     *
     * Constructor
     *
     * @param SegmentFactory $segment_factory A session segment factory.
     *
     * @param CsrfTokenFactory $csrf_token_factory A CSRF token factory.
     *
     * @param Phpfunc $phpfunc An object to intercept PHP function calls;
     * this makes testing easier.
     *
     * @param array $cookies Optional: An array of cookies from the client, typically a
     * copy of $_COOKIE. Empty array by default.
     *
     * @param callable|null $delete_cookie Optional: An alternative callable
     * to invoke when deleting the session cookie. Defaults to `null`.
     *
     */
    
public function __construct(
        
SegmentFactory $segment_factory,
        
CsrfTokenFactory $csrf_token_factory,
        
Phpfunc $phpfunc,
        array 
$cookies = array(),
        
$delete_cookie null
    
) {
        
$this->segment_factory    $segment_factory;
        
$this->csrf_token_factory $csrf_token_factory;
        
$this->phpfunc            $phpfunc;
        
$this->cookies            $cookies;

        
$this->setDeleteCookie($delete_cookie);

        
$this->cookie_params $this->phpfunc->session_get_cookie_params();
    }

    
/**
     *
     * Sets the delete-cookie callable.
     *
     * If parameter is `null`, the session cookie will be deleted using the
     * traditional way, i.e. using an expiration date in the past.
     *
     * @param callable|null $delete_cookie The callable to invoke when deleting the
     * session cookie.
     *
     */
    
public function setDeleteCookie($delete_cookie)
    {
        
$this->delete_cookie $delete_cookie;
        if (! 
$this->delete_cookie) {
            
$phpfunc $this->phpfunc;
            
$this->delete_cookie = function (
                
$name,
                
$params
            
) use ($phpfunc) {
                
$phpfunc->setcookie(
                    
$name,
                    
'',
                    
time() - 42000,
                    
$params['path'],
                    
$params['domain']
                );
            };
        }
    }

    
/**
     *
     * Gets a new session segment instance by name. Segments with the same
     * name will be different objects but will reference the same $_SESSION
     * values, so it is possible to have two or more objects that share state.
     * For good or bad, this a function of how $_SESSION works.
     *
     * @param string $name The name of the session segment, typically a
     * fully-qualified class name.
     *
     * @return Segment New Segment instance.
     *
     */
    
public function getSegment($name)
    {
        return 
$this->segment_factory->newInstance($this$name);
    }

    
/**
     *
     * Is a session available to be resumed?
     *
     * @return bool
     *
     */
    
public function isResumable()
    {
        
$name $this->getName();
        return isset(
$this->cookies[$name]);
    }

    
/**
     *
     * Is the session already started?
     *
     * @return bool
     *
     */
    
public function isStarted()
    {
        if (
$this->phpfunc->function_exists('session_status')) {
            
$started $this->phpfunc->session_status() === PHP_SESSION_ACTIVE;
        } else {
            
$started $this->sessionStatus();
        }

        
// if the session was started externally, move the flash values forward
        
if ($started && ! $this->flash_moved) {
            
$this->moveFlash();
        }

        
// done
        
return $started;
    }

    
/**
     *
     * Returns the session status.
     *
     * Nota bene:
     *
     * PHP 5.3 implementation of session_status() for only active/none.
     * Relies on the fact that ini setting 'session.use_trans_sid' cannot be
     * changed when a session is active.
     *
     * PHP ini_set() raises a warning when we attempt to change this setting
     * and session is active. Note that the attempted change is to the
     * pre-existing value, so nothing will actually change on success.
     *
     */
    
protected function sessionStatus()
    {
        
$setting 'session.use_trans_sid';
        
$current $this->phpfunc->ini_get($setting);
        
$level   $this->phpfunc->error_reporting(0);
        
$result  $this->phpfunc->ini_set($setting$current);
        
$this->phpfunc->error_reporting($level);
        return 
$result !== $current;
    }

    
/**
     *
     * Starts a new or existing session.
     *
     * @return bool
     *
     */
    
public function start()
    {
        
$result $this->phpfunc->session_start();
        if (
$result) {
            
$this->moveFlash();
        }
        return 
$result;
    }

    
/**
     *
     * Moves the "next" flash values to the "now" values, thereby clearing the
     * "next" values.
     *
     * @return null
     *
     */
    
protected function moveFlash()
    {
        if (! isset(
$_SESSION[Session::FLASH_NEXT])) {
            
$_SESSION[Session::FLASH_NEXT] = array();
        }
        
$_SESSION[Session::FLASH_NOW] = $_SESSION[Session::FLASH_NEXT];
        
$_SESSION[Session::FLASH_NEXT] = array();
        
$this->flash_moved true;
    }

    
/**
     *
     * Resumes a session, but does not start a new one if there is no
     * existing one.
     *
     * @return bool
     *
     */
    
public function resume()
    {
        if (
$this->isStarted()) {
            return 
true;
        }

        if (
$this->isResumable()) {
            return 
$this->start();
        }

        return 
false;
    }

    
/**
     *
     * Clears all session variables across all segments.
     *
     * @return null
     *
     */
    
public function clear()
    {
        return 
$this->phpfunc->session_unset();
    }

    
/**
     *
     * Writes session data from all segments and ends the session.
     *
     * @return null
     *
     */
    
public function commit()
    {
        return 
$this->phpfunc->session_write_close();
    }

    
/**
     *
     * Destroys the session entirely.
     *
     * @return bool
     *
     * @see http://php.net/manual/en/function.session-destroy.php
     *
     */
    
public function destroy()
    {
        if (! 
$this->isStarted()) {
            
$this->start();
        }

        
$name $this->getName();
        
$params $this->getCookieParams();
        
$this->clear();

        
$destroyed $this->phpfunc->session_destroy();
        if (
$destroyed) {
            
call_user_func($this->delete_cookie$name$params);
        }

        return 
$destroyed;
    }

    
/**
     *
     * Returns the CSRF token, creating it if needed (and thereby starting a
     * session).
     *
     * @return CsrfToken
     *
     */
    
public function getCsrfToken()
    {
        if (! 
$this->csrf_token) {
            
$this->csrf_token $this->csrf_token_factory->newInstance($this);
        }

        return 
$this->csrf_token;
    }

    
// =======================================================================
    //
    // support and admin methods
    //

    /**
     *
     * Sets the session cache expire time.
     *
     * @param int $expire The expiration time in seconds.
     *
     * @return int
     *
     * @see session_cache_expire()
     *
     */
    
public function setCacheExpire($expire)
    {
        return 
$this->phpfunc->session_cache_expire($expire);
    }

    
/**
     *
     * Gets the session cache expire time.
     *
     * @return int The cache expiration time in seconds.
     *
     * @see session_cache_expire()
     *
     */
    
public function getCacheExpire()
    {
        return 
$this->phpfunc->session_cache_expire();
    }

    
/**
     *
     * Sets the session cache limiter value.
     *
     * @param string $limiter The limiter value.
     *
     * @return string
     *
     * @see session_cache_limiter()
     *
     */
    
public function setCacheLimiter($limiter)
    {
        return 
$this->phpfunc->session_cache_limiter($limiter);
    }

    
/**
     *
     * Gets the session cache limiter value.
     *
     * @return string The limiter value.
     *
     * @see session_cache_limiter()
     *
     */
    
public function getCacheLimiter()
    {
        return 
$this->phpfunc->session_cache_limiter();
    }

    
/**
     *
     * Sets the session cookie params.  Param array keys are:
     *
     * - `lifetime` : Lifetime of the session cookie, defined in seconds.
     *
     * - `path` : Path on the domain where the cookie will work.
     *   Use a single slash ('/') for all paths on the domain.
     *
     * - `domain` : Cookie domain, for example 'www.php.net'.
     *   To make cookies visible on all subdomains then the domain must be
     *   prefixed with a dot like '.php.net'.
     *
     * - `secure` : If TRUE cookie will only be sent over secure connections.
     *
     * - `httponly` : If set to TRUE then PHP will attempt to send the httponly
     *   flag when setting the session cookie.
     *
     * @param array $params The array of session cookie param keys and values.
     *
     * @return null
     *
     * @see session_set_cookie_params()
     *
     */
    
public function setCookieParams(array $params)
    {
        
$this->cookie_params array_merge($this->cookie_params$params);
        
$this->phpfunc->session_set_cookie_params(
            
$this->cookie_params['lifetime'],
            
$this->cookie_params['path'],
            
$this->cookie_params['domain'],
            
$this->cookie_params['secure'],
            
$this->cookie_params['httponly']
        );
    }

    
/**
     *
     * Gets the session cookie params.
     *
     * @return array
     *
     */
    
public function getCookieParams()
    {
        return 
$this->cookie_params;
    }

    
/**
     *
     * Gets the current session id.
     *
     * @return string
     *
     */
    
public function getId()
    {
        return 
$this->phpfunc->session_id();
    }

    
/**
     *
     * Regenerates and replaces the current session id; also regenerates the
     * CSRF token value if one exists.
     *
     * @return bool True if regeneration worked, false if not.
     *
     */
    
public function regenerateId()
    {
        
$result $this->phpfunc->session_regenerate_id(true);
        if (
$result && $this->csrf_token) {
            
$this->csrf_token->regenerateValue();
        }
        return 
$result;
    }

    
/**
     *
     * Sets the current session name.
     *
     * @param string $name The session name to use.
     *
     * @return string
     *
     * @see session_name()
     *
     */
    
public function setName($name)
    {
        return 
$this->phpfunc->session_name($name);
    }

    
/**
     *
     * Returns the current session name.
     *
     * @return string
     *
     */
    
public function getName()
    {
        return 
$this->phpfunc->session_name();
    }

    
/**
     *
     * Sets the session save path.
     *
     * @param string $path The new save path.
     *
     * @return string
     *
     * @see session_save_path()
     *
     */
    
public function setSavePath($path)
    {
        return 
$this->phpfunc->session_save_path($path);
    }

    
/**
     *
     * Gets the session save path.
     *
     * @return string
     *
     * @see session_save_path()
     *
     */
    
public function getSavePath()
    {
        return 
$this->phpfunc->session_save_path();
    }
}