Completed
Push — 14811-fix-httpcache-freeze-unf... ( 300f13 )
by Alexander
14:45
created

Session::setCacheLimiter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\web;
9
10
use Yii;
11
use yii\base\Component;
12
use yii\base\InvalidConfigException;
13
use yii\base\InvalidParamException;
14
15
/**
16
 * Session provides session data management and the related configurations.
17
 *
18
 * Session is a Web application component that can be accessed via `Yii::$app->session`.
19
 *
20
 * To start the session, call [[open()]]; To complete and send out session data, call [[close()]];
21
 * To destroy the session, call [[destroy()]].
22
 *
23
 * Session can be used like an array to set and get session data. For example,
24
 *
25
 * ```php
26
 * $session = new Session;
27
 * $session->open();
28
 * $value1 = $session['name1'];  // get session variable 'name1'
29
 * $value2 = $session['name2'];  // get session variable 'name2'
30
 * foreach ($session as $name => $value) // traverse all session variables
31
 * $session['name3'] = $value3;  // set session variable 'name3'
32
 * ```
33
 *
34
 * Session can be extended to support customized session storage.
35
 * To do so, override [[useCustomStorage]] so that it returns true, and
36
 * override these methods with the actual logic about using custom storage:
37
 * [[openSession()]], [[closeSession()]], [[readSession()]], [[writeSession()]],
38
 * [[destroySession()]] and [[gcSession()]].
39
 *
40
 * Session also supports a special type of session data, called *flash messages*.
41
 * A flash message is available only in the current request and the next request.
42
 * After that, it will be deleted automatically. Flash messages are particularly
43
 * useful for displaying confirmation messages. To use flash messages, simply
44
 * call methods such as [[setFlash()]], [[getFlash()]].
45
 *
46
 * For more details and usage information on Session, see the [guide article on sessions](guide:runtime-sessions-cookies).
47
 *
48
 * @property array $allFlashes Flash messages (key => message or key => [message1, message2]). This property
49
 * is read-only.
50
 * @property array $cookieParams The session cookie parameters. This property is read-only.
51
 * @property int $count The number of session variables. This property is read-only.
52
 * @property string $flash The key identifying the flash message. Note that flash messages and normal session
53
 * variables share the same name space. If you have a normal session variable using the same name, its value will
54
 * be overwritten by this method. This property is write-only.
55
 * @property float $gCProbability The probability (percentage) that the GC (garbage collection) process is
56
 * started on every session initialization, defaults to 1 meaning 1% chance.
57
 * @property bool $hasSessionId Whether the current request has sent the session ID.
58
 * @property string $id The current session ID.
59
 * @property bool $isActive Whether the session has started. This property is read-only.
60
 * @property SessionIterator $iterator An iterator for traversing the session variables. This property is
61
 * read-only.
62
 * @property string $name The current session name.
63
 * @property string $savePath The current session save path, defaults to '/tmp'.
64
 * @property int $timeout The number of seconds after which data will be seen as 'garbage' and cleaned up. The
65
 * default value is 1440 seconds (or the value of "session.gc_maxlifetime" set in php.ini).
66
 * @property bool|null $useCookies The value indicating whether cookies should be used to store session IDs.
67
 * @property bool $useCustomStorage Whether to use custom storage. This property is read-only.
68
 * @property bool $useTransparentSessionID Whether transparent sid support is enabled or not, defaults to
69
 * false.
70
 *
71
 * @author Qiang Xue <[email protected]>
72
 * @since 2.0
73
 */
74
class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Countable
75
{
76
    /**
77
     * @var string the name of the session variable that stores the flash message data.
78
     */
79
    public $flashParam = '__flash';
80
    /**
81
     * @var \SessionHandlerInterface|array an object implementing the SessionHandlerInterface or a configuration array. If set, will be used to provide persistency instead of build-in methods.
82
     */
83
    public $handler;
84
85
    /**
86
     * @var array parameter-value pairs to override default session cookie parameters that are used for session_set_cookie_params() function
87
     * Array may have the following possible keys: 'lifetime', 'path', 'domain', 'secure', 'httponly'
88
     * @see http://www.php.net/manual/en/function.session-set-cookie-params.php
89
     */
90
    private $_cookieParams = ['httponly' => true];
91
92
    /**
93
     * @var $frozenSessionData array|null is used for saving session between recreations due to session parameters update.
94
     */
95
    private $frozenSessionData;
96
97
98
    /**
99
     * Initializes the application component.
100
     * This method is required by IApplicationComponent and is invoked by application.
101
     */
102 92
    public function init()
103
    {
104 92
        parent::init();
105 92
        register_shutdown_function([$this, 'close']);
106 92
        if ($this->getIsActive()) {
107 3
            Yii::warning('Session is already started', __METHOD__);
108 3
            $this->updateFlashCounters();
109
        }
110 92
    }
111
112
    /**
113
     * Returns a value indicating whether to use custom session storage.
114
     * This method should be overridden to return true by child classes that implement custom session storage.
115
     * To implement custom session storage, override these methods: [[openSession()]], [[closeSession()]],
116
     * [[readSession()]], [[writeSession()]], [[destroySession()]] and [[gcSession()]].
117
     * @return bool whether to use custom storage.
118
     */
119 50
    public function getUseCustomStorage()
120
    {
121 50
        return false;
122
    }
123
124
    /**
125
     * Starts the session.
126
     */
127 52
    public function open()
128
    {
129 52
        if ($this->getIsActive()) {
130 52
            return;
131
        }
132
133 51
        $this->registerSessionHandler();
134
135 51
        $this->setCookieParamsInternal();
136
137 51
        YII_DEBUG ? session_start() : @session_start();
138
139 51
        if ($this->getIsActive()) {
140 51
            Yii::info('Session started', __METHOD__);
141 51
            $this->updateFlashCounters();
142
        } else {
143
            $error = error_get_last();
144
            $message = isset($error['message']) ? $error['message'] : 'Failed to start session.';
145
            Yii::error($message, __METHOD__);
146
        }
147 51
    }
148
149
    /**
150
     * Registers session handler.
151
     * @throws \yii\base\InvalidConfigException
152
     */
153 51
    protected function registerSessionHandler()
154
    {
155 51
        if ($this->handler !== null) {
156
            if (!is_object($this->handler)) {
157
                $this->handler = Yii::createObject($this->handler);
158
            }
159
            if (!$this->handler instanceof \SessionHandlerInterface) {
160
                throw new InvalidConfigException('"' . get_class($this) . '::handler" must implement the SessionHandlerInterface.');
161
            }
162
            YII_DEBUG ? session_set_save_handler($this->handler, false) : @session_set_save_handler($this->handler, false);
163 51
        } elseif ($this->getUseCustomStorage()) {
164 1
            if (YII_DEBUG) {
165 1
                session_set_save_handler(
166 1
                    [$this, 'openSession'],
167 1
                    [$this, 'closeSession'],
168 1
                    [$this, 'readSession'],
169 1
                    [$this, 'writeSession'],
170 1
                    [$this, 'destroySession'],
171 1
                    [$this, 'gcSession']
172
                );
173
            } else {
174
                @session_set_save_handler(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
175
                    [$this, 'openSession'],
176
                    [$this, 'closeSession'],
177
                    [$this, 'readSession'],
178
                    [$this, 'writeSession'],
179
                    [$this, 'destroySession'],
180
                    [$this, 'gcSession']
181
                );
182
            }
183
        }
184 51
    }
185
186
    /**
187
     * Ends the current session and store session data.
188
     */
189 75
    public function close()
190
    {
191 75
        if ($this->getIsActive()) {
192 51
            YII_DEBUG ? session_write_close() : @session_write_close();
193
        }
194 75
    }
195
196
    /**
197
     * Frees all session variables and destroys all data registered to a session.
198
     *
199
     * This method has no effect when session is not [[getIsActive()|active]].
200
     * Make sure to call [[open()]] before calling it.
201
     * @see open()
202
     * @see isActive
203
     */
204 1
    public function destroy()
205
    {
206 1
        if ($this->getIsActive()) {
207 1
            $sessionId = session_id();
208 1
            $this->close();
209 1
            $this->setId($sessionId);
210 1
            $this->open();
211 1
            session_unset();
212 1
            session_destroy();
213 1
            $this->setId($sessionId);
214
        }
215 1
    }
216
217
    /**
218
     * @return bool whether the session has started
219
     */
220 92
    public function getIsActive()
221
    {
222 92
        return session_status() === PHP_SESSION_ACTIVE;
223
    }
224
225
    private $_hasSessionId;
226
227
    /**
228
     * Returns a value indicating whether the current request has sent the session ID.
229
     * The default implementation will check cookie and $_GET using the session name.
230
     * If you send session ID via other ways, you may need to override this method
231
     * or call [[setHasSessionId()]] to explicitly set whether the session ID is sent.
232
     * @return bool whether the current request has sent the session ID.
233
     */
234 22
    public function getHasSessionId()
235
    {
236 22
        if ($this->_hasSessionId === null) {
237 22
            $name = $this->getName();
238 22
            $request = Yii::$app->getRequest();
239 22
            if (!empty($_COOKIE[$name]) && ini_get('session.use_cookies')) {
240
                $this->_hasSessionId = true;
241 22
            } elseif (!ini_get('session.use_only_cookies') && ini_get('session.use_trans_sid')) {
242
                $this->_hasSessionId = $request->get($name) != '';
243
            } else {
244 22
                $this->_hasSessionId = false;
245
            }
246
        }
247
248 22
        return $this->_hasSessionId;
249
    }
250
251
    /**
252
     * Sets the value indicating whether the current request has sent the session ID.
253
     * This method is provided so that you can override the default way of determining
254
     * whether the session ID is sent.
255
     * @param bool $value whether the current request has sent the session ID.
256
     */
257
    public function setHasSessionId($value)
258
    {
259
        $this->_hasSessionId = $value;
260
    }
261
262
    /**
263
     * Gets the session ID.
264
     * This is a wrapper for [PHP session_id()](http://php.net/manual/en/function.session-id.php).
265
     * @return string the current session ID
266
     */
267 1
    public function getId()
268
    {
269 1
        return session_id();
270
    }
271
272
    /**
273
     * Sets the session ID.
274
     * This is a wrapper for [PHP session_id()](http://php.net/manual/en/function.session-id.php).
275
     * @param string $value the session ID for the current session
276
     */
277 1
    public function setId($value)
278
    {
279 1
        session_id($value);
280 1
    }
281
282
    /**
283
     * Updates the current session ID with a newly generated one.
284
     *
285
     * Please refer to <http://php.net/session_regenerate_id> for more details.
286
     *
287
     * This method has no effect when session is not [[getIsActive()|active]].
288
     * Make sure to call [[open()]] before calling it.
289
     *
290
     * @param bool $deleteOldSession Whether to delete the old associated session file or not.
291
     * @see open()
292
     * @see isActive
293
     */
294
    public function regenerateID($deleteOldSession = false)
295
    {
296
        if ($this->getIsActive()) {
297
            // add @ to inhibit possible warning due to race condition
298
            // https://github.com/yiisoft/yii2/pull/1812
299
            if (YII_DEBUG && !headers_sent()) {
300
                session_regenerate_id($deleteOldSession);
301
            } else {
302
                @session_regenerate_id($deleteOldSession);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
303
            }
304
        }
305
    }
306
307
    /**
308
     * Gets the name of the current session.
309
     * This is a wrapper for [PHP session_name()](http://php.net/manual/en/function.session-name.php).
310
     * @return string the current session name
311
     */
312 22
    public function getName()
313
    {
314 22
        return session_name();
315
    }
316
317
    /**
318
     * Sets the name for the current session.
319
     * This is a wrapper for [PHP session_name()](http://php.net/manual/en/function.session-name.php).
320
     * @param string $value the session name for the current session, must be an alphanumeric string.
321
     * It defaults to "PHPSESSID".
322
     */
323
    public function setName($value)
324
    {
325
        session_name($value);
326
    }
327
328
    /**
329
     * Gets the current session save path.
330
     * This is a wrapper for [PHP session_save_path()](http://php.net/manual/en/function.session-save-path.php).
331
     * @return string the current session save path, defaults to '/tmp'.
332
     */
333
    public function getSavePath()
334
    {
335
        return session_save_path();
336
    }
337
338
    /**
339
     * Sets the current session save path.
340
     * This is a wrapper for [PHP session_save_path()](http://php.net/manual/en/function.session-save-path.php).
341
     * @param string $value the current session save path. This can be either a directory name or a [path alias](guide:concept-aliases).
342
     * @throws InvalidParamException if the path is not a valid directory
343
     */
344
    public function setSavePath($value)
345
    {
346
        $path = Yii::getAlias($value);
347
        if (is_dir($path)) {
348
            session_save_path($path);
349
        } else {
350
            throw new InvalidParamException("Session save path is not a valid directory: $value");
351
        }
352
    }
353
354
    /**
355
     * @return array the session cookie parameters.
356
     * @see http://php.net/manual/en/function.session-get-cookie-params.php
357
     */
358 51
    public function getCookieParams()
359
    {
360 51
        return array_merge(session_get_cookie_params(), array_change_key_case($this->_cookieParams));
361
    }
362
363
    /**
364
     * Sets the session cookie parameters.
365
     * The cookie parameters passed to this method will be merged with the result
366
     * of `session_get_cookie_params()`.
367
     * @param array $value cookie parameters, valid keys include: `lifetime`, `path`, `domain`, `secure` and `httponly`.
368
     * @throws InvalidParamException if the parameters are incomplete.
369
     * @see http://us2.php.net/manual/en/function.session-set-cookie-params.php
370
     */
371
    public function setCookieParams(array $value)
372
    {
373
        $this->_cookieParams = $value;
374
    }
375
376
    /**
377
     * Sets the session cookie parameters.
378
     * This method is called by [[open()]] when it is about to open the session.
379
     * @throws InvalidParamException if the parameters are incomplete.
380
     * @see http://us2.php.net/manual/en/function.session-set-cookie-params.php
381
     */
382 51
    private function setCookieParamsInternal()
383
    {
384 51
        $data = $this->getCookieParams();
385 51
        if (isset($data['lifetime'], $data['path'], $data['domain'], $data['secure'], $data['httponly'])) {
386 51
            session_set_cookie_params($data['lifetime'], $data['path'], $data['domain'], $data['secure'], $data['httponly']);
387
        } else {
388
            throw new InvalidParamException('Please make sure cookieParams contains these elements: lifetime, path, domain, secure and httponly.');
389
        }
390 51
    }
391
392
    /**
393
     * Returns the value indicating whether cookies should be used to store session IDs.
394
     * @return bool|null the value indicating whether cookies should be used to store session IDs.
395
     * @see setUseCookies()
396
     */
397 1
    public function getUseCookies()
398
    {
399 1
        if (ini_get('session.use_cookies') === '0') {
400 1
            return false;
401 1
        } elseif (ini_get('session.use_only_cookies') === '1') {
402 1
            return true;
403
        }
404
405
        return null;
406
    }
407
408
    /**
409
     * Sets the value indicating whether cookies should be used to store session IDs.
410
     *
411
     * Three states are possible:
412
     *
413
     * - true: cookies and only cookies will be used to store session IDs.
414
     * - false: cookies will not be used to store session IDs.
415
     * - null: if possible, cookies will be used to store session IDs; if not, other mechanisms will be used (e.g. GET parameter)
416
     *
417
     * @param bool|null $value the value indicating whether cookies should be used to store session IDs.
418
     */
419 4
    public function setUseCookies($value)
420
    {
421 4
        $this->freeze();
422 4
        if ($value === false) {
423 1
            ini_set('session.use_cookies', '0');
424 1
            ini_set('session.use_only_cookies', '0');
425 3
        } elseif ($value === true) {
426 3
            ini_set('session.use_cookies', '1');
427 3
            ini_set('session.use_only_cookies', '1');
428
        } else {
429
            ini_set('session.use_cookies', '1');
430
            ini_set('session.use_only_cookies', '0');
431
        }
432 4
        $this->unfreeze();
433 4
    }
434
435
    /**
436
     * @return float the probability (percentage) that the GC (garbage collection) process is started on every session initialization, defaults to 1 meaning 1% chance.
437
     */
438 1
    public function getGCProbability()
439
    {
440 1
        return (float) (ini_get('session.gc_probability') / ini_get('session.gc_divisor') * 100);
441
    }
442
443
    /**
444
     * @param float $value the probability (percentage) that the GC (garbage collection) process is started on every session initialization.
445
     * @throws InvalidParamException if the value is not between 0 and 100.
446
     */
447 1
    public function setGCProbability($value)
448
    {
449 1
        $this->freeze();
450 1
        if ($value >= 0 && $value <= 100) {
451
            // percent * 21474837 / 2147483647 ≈ percent * 0.01
452 1
            ini_set('session.gc_probability', floor($value * 21474836.47));
453 1
            ini_set('session.gc_divisor', 2147483647);
454
        } else {
455
            throw new InvalidParamException('GCProbability must be a value between 0 and 100.');
456
        }
457 1
        $this->unfreeze();
458 1
    }
459
460
    /**
461
     * @return bool whether transparent sid support is enabled or not, defaults to false.
462
     */
463 1
    public function getUseTransparentSessionID()
464
    {
465 1
        return ini_get('session.use_trans_sid') == 1;
466
    }
467
468
    /**
469
     * @param bool $value whether transparent sid support is enabled or not.
470
     */
471 1
    public function setUseTransparentSessionID($value)
472
    {
473 1
        $this->freeze();
474 1
        ini_set('session.use_trans_sid', $value ? '1' : '0');
475 1
        $this->unfreeze();
476 1
    }
477
478
    /**
479
     * @return int the number of seconds after which data will be seen as 'garbage' and cleaned up.
480
     * The default value is 1440 seconds (or the value of "session.gc_maxlifetime" set in php.ini).
481
     */
482 21
    public function getTimeout()
483
    {
484 21
        return (int) ini_get('session.gc_maxlifetime');
485
    }
486
487
    /**
488
     * @param int $value the number of seconds after which data will be seen as 'garbage' and cleaned up
489
     */
490 4
    public function setTimeout($value)
491
    {
492 4
        $this->freeze();
493 4
        ini_set('session.gc_maxlifetime', $value);
494 4
        $this->unfreeze();
495 4
    }
496
497
    /**
498
     * Session open handler.
499
     * This method should be overridden if [[useCustomStorage]] returns true.
500
     * @internal Do not call this method directly.
501
     * @param string $savePath session save path
502
     * @param string $sessionName session name
503
     * @return bool whether session is opened successfully
504
     */
505 3
    public function openSession($savePath, $sessionName)
0 ignored issues
show
Unused Code introduced by
The parameter $savePath is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $sessionName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
506
    {
507 3
        return true;
508
    }
509
510
    /**
511
     * Session close handler.
512
     * This method should be overridden if [[useCustomStorage]] returns true.
513
     * @internal Do not call this method directly.
514
     * @return bool whether session is closed successfully
515
     */
516 3
    public function closeSession()
517
    {
518 3
        return true;
519
    }
520
521
    /**
522
     * Session read handler.
523
     * This method should be overridden if [[useCustomStorage]] returns true.
524
     * @internal Do not call this method directly.
525
     * @param string $id session ID
526
     * @return string the session data
527
     */
528
    public function readSession($id)
529
    {
530
        return '';
531
    }
532
533
    /**
534
     * Session write handler.
535
     * This method should be overridden if [[useCustomStorage]] returns true.
536
     * @internal Do not call this method directly.
537
     * @param string $id session ID
538
     * @param string $data session data
539
     * @return bool whether session write is successful
540
     */
541
    public function writeSession($id, $data)
542
    {
543
        return true;
544
    }
545
546
    /**
547
     * Session destroy handler.
548
     * This method should be overridden if [[useCustomStorage]] returns true.
549
     * @internal Do not call this method directly.
550
     * @param string $id session ID
551
     * @return bool whether session is destroyed successfully
552
     */
553
    public function destroySession($id)
554
    {
555
        return true;
556
    }
557
558
    /**
559
     * Session GC (garbage collection) handler.
560
     * This method should be overridden if [[useCustomStorage]] returns true.
561
     * @internal Do not call this method directly.
562
     * @param int $maxLifetime the number of seconds after which data will be seen as 'garbage' and cleaned up.
563
     * @return bool whether session is GCed successfully
564
     */
565 1
    public function gcSession($maxLifetime)
0 ignored issues
show
Unused Code introduced by
The parameter $maxLifetime is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
566
    {
567 1
        return true;
568
    }
569
570
    /**
571
     * Returns an iterator for traversing the session variables.
572
     * This method is required by the interface [[\IteratorAggregate]].
573
     * @return SessionIterator an iterator for traversing the session variables.
574
     */
575
    public function getIterator()
576
    {
577
        $this->open();
578
        return new SessionIterator();
579
    }
580
581
    /**
582
     * Returns the number of items in the session.
583
     * @return int the number of session variables
584
     */
585
    public function getCount()
586
    {
587
        $this->open();
588
        return count($_SESSION);
589
    }
590
591
    /**
592
     * Returns the number of items in the session.
593
     * This method is required by [[\Countable]] interface.
594
     * @return int number of items in the session.
595
     */
596
    public function count()
597
    {
598
        return $this->getCount();
599
    }
600
601
    /**
602
     * Returns the session variable value with the session variable name.
603
     * If the session variable does not exist, the `$defaultValue` will be returned.
604
     * @param string $key the session variable name
605
     * @param mixed $defaultValue the default value to be returned when the session variable does not exist.
606
     * @return mixed the session variable value, or $defaultValue if the session variable does not exist.
607
     */
608 52
    public function get($key, $defaultValue = null)
609
    {
610 52
        $this->open();
611 52
        return isset($_SESSION[$key]) ? $_SESSION[$key] : $defaultValue;
612
    }
613
614
    /**
615
     * Adds a session variable.
616
     * If the specified name already exists, the old value will be overwritten.
617
     * @param string $key session variable name
618
     * @param mixed $value session variable value
619
     */
620 37
    public function set($key, $value)
621
    {
622 37
        $this->open();
623 37
        $_SESSION[$key] = $value;
624 37
    }
625
626
    /**
627
     * Removes a session variable.
628
     * @param string $key the name of the session variable to be removed
629
     * @return mixed the removed value, null if no such session variable.
630
     */
631 33
    public function remove($key)
632
    {
633 33
        $this->open();
634 33
        if (isset($_SESSION[$key])) {
635 31
            $value = $_SESSION[$key];
636 31
            unset($_SESSION[$key]);
637
638 31
            return $value;
639
        }
640
641 33
        return null;
642
    }
643
644
    /**
645
     * Removes all session variables.
646
     */
647 14
    public function removeAll()
648
    {
649 14
        $this->open();
650 14
        foreach (array_keys($_SESSION) as $key) {
651 14
            unset($_SESSION[$key]);
652
        }
653 14
    }
654
655
    /**
656
     * @param mixed $key session variable name
657
     * @return bool whether there is the named session variable
658
     */
659
    public function has($key)
660
    {
661
        $this->open();
662
        return isset($_SESSION[$key]);
663
    }
664
665
    /**
666
     * Updates the counters for flash messages and removes outdated flash messages.
667
     * This method should only be called once in [[init()]].
668
     */
669 52
    protected function updateFlashCounters()
670
    {
671 52
        $counters = $this->get($this->flashParam, []);
672 52
        if (is_array($counters)) {
673 52
            foreach ($counters as $key => $count) {
674
                if ($count > 0) {
675
                    unset($counters[$key], $_SESSION[$key]);
676
                } elseif ($count == 0) {
677
                    $counters[$key]++;
678
                }
679
            }
680 52
            $_SESSION[$this->flashParam] = $counters;
681
        } else {
682
            // fix the unexpected problem that flashParam doesn't return an array
683
            unset($_SESSION[$this->flashParam]);
684
        }
685 52
    }
686
687
    /**
688
     * Returns a flash message.
689
     * @param string $key the key identifying the flash message
690
     * @param mixed $defaultValue value to be returned if the flash message does not exist.
691
     * @param bool $delete whether to delete this flash message right after this method is called.
692
     * If false, the flash message will be automatically deleted in the next request.
693
     * @return mixed the flash message or an array of messages if addFlash was used
694
     * @see setFlash()
695
     * @see addFlash()
696
     * @see hasFlash()
697
     * @see getAllFlashes()
698
     * @see removeFlash()
699
     */
700
    public function getFlash($key, $defaultValue = null, $delete = false)
701
    {
702
        $counters = $this->get($this->flashParam, []);
703
        if (isset($counters[$key])) {
704
            $value = $this->get($key, $defaultValue);
705
            if ($delete) {
706
                $this->removeFlash($key);
707
            } elseif ($counters[$key] < 0) {
708
                // mark for deletion in the next request
709
                $counters[$key] = 1;
710
                $_SESSION[$this->flashParam] = $counters;
711
            }
712
713
            return $value;
714
        }
715
716
        return $defaultValue;
717
    }
718
719
    /**
720
     * Returns all flash messages.
721
     *
722
     * You may use this method to display all the flash messages in a view file:
723
     *
724
     * ```php
725
     * <?php
726
     * foreach (Yii::$app->session->getAllFlashes() as $key => $message) {
727
     *     echo '<div class="alert alert-' . $key . '">' . $message . '</div>';
728
     * } ?>
729
     * ```
730
     *
731
     * With the above code you can use the [bootstrap alert][] classes such as `success`, `info`, `danger`
732
     * as the flash message key to influence the color of the div.
733
     *
734
     * Note that if you use [[addFlash()]], `$message` will be an array, and you will have to adjust the above code.
735
     *
736
     * [bootstrap alert]: http://getbootstrap.com/components/#alerts
737
     *
738
     * @param bool $delete whether to delete the flash messages right after this method is called.
739
     * If false, the flash messages will be automatically deleted in the next request.
740
     * @return array flash messages (key => message or key => [message1, message2]).
741
     * @see setFlash()
742
     * @see addFlash()
743
     * @see getFlash()
744
     * @see hasFlash()
745
     * @see removeFlash()
746
     */
747
    public function getAllFlashes($delete = false)
748
    {
749
        $counters = $this->get($this->flashParam, []);
750
        $flashes = [];
751
        foreach (array_keys($counters) as $key) {
752
            if (array_key_exists($key, $_SESSION)) {
753
                $flashes[$key] = $_SESSION[$key];
754
                if ($delete) {
755
                    unset($counters[$key], $_SESSION[$key]);
756
                } elseif ($counters[$key] < 0) {
757
                    // mark for deletion in the next request
758
                    $counters[$key] = 1;
759
                }
760
            } else {
761
                unset($counters[$key]);
762
            }
763
        }
764
765
        $_SESSION[$this->flashParam] = $counters;
766
767
        return $flashes;
768
    }
769
770
    /**
771
     * Sets a flash message.
772
     * A flash message will be automatically deleted after it is accessed in a request and the deletion will happen
773
     * in the next request.
774
     * If there is already an existing flash message with the same key, it will be overwritten by the new one.
775
     * @param string $key the key identifying the flash message. Note that flash messages
776
     * and normal session variables share the same name space. If you have a normal
777
     * session variable using the same name, its value will be overwritten by this method.
778
     * @param mixed $value flash message
779
     * @param bool $removeAfterAccess whether the flash message should be automatically removed only if
780
     * it is accessed. If false, the flash message will be automatically removed after the next request,
781
     * regardless if it is accessed or not. If true (default value), the flash message will remain until after
782
     * it is accessed.
783
     * @see getFlash()
784
     * @see addFlash()
785
     * @see removeFlash()
786
     */
787
    public function setFlash($key, $value = true, $removeAfterAccess = true)
788
    {
789
        $counters = $this->get($this->flashParam, []);
790
        $counters[$key] = $removeAfterAccess ? -1 : 0;
791
        $_SESSION[$key] = $value;
792
        $_SESSION[$this->flashParam] = $counters;
793
    }
794
795
    /**
796
     * Adds a flash message.
797
     * If there are existing flash messages with the same key, the new one will be appended to the existing message array.
798
     * @param string $key the key identifying the flash message.
799
     * @param mixed $value flash message
800
     * @param bool $removeAfterAccess whether the flash message should be automatically removed only if
801
     * it is accessed. If false, the flash message will be automatically removed after the next request,
802
     * regardless if it is accessed or not. If true (default value), the flash message will remain until after
803
     * it is accessed.
804
     * @see getFlash()
805
     * @see setFlash()
806
     * @see removeFlash()
807
     */
808
    public function addFlash($key, $value = true, $removeAfterAccess = true)
809
    {
810
        $counters = $this->get($this->flashParam, []);
811
        $counters[$key] = $removeAfterAccess ? -1 : 0;
812
        $_SESSION[$this->flashParam] = $counters;
813
        if (empty($_SESSION[$key])) {
814
            $_SESSION[$key] = [$value];
815
        } else {
816
            if (is_array($_SESSION[$key])) {
817
                $_SESSION[$key][] = $value;
818
            } else {
819
                $_SESSION[$key] = [$_SESSION[$key], $value];
820
            }
821
        }
822
    }
823
824
    /**
825
     * Removes a flash message.
826
     * @param string $key the key identifying the flash message. Note that flash messages
827
     * and normal session variables share the same name space.  If you have a normal
828
     * session variable using the same name, it will be removed by this method.
829
     * @return mixed the removed flash message. Null if the flash message does not exist.
830
     * @see getFlash()
831
     * @see setFlash()
832
     * @see addFlash()
833
     * @see removeAllFlashes()
834
     */
835
    public function removeFlash($key)
836
    {
837
        $counters = $this->get($this->flashParam, []);
838
        $value = isset($_SESSION[$key], $counters[$key]) ? $_SESSION[$key] : null;
839
        unset($counters[$key], $_SESSION[$key]);
840
        $_SESSION[$this->flashParam] = $counters;
841
842
        return $value;
843
    }
844
845
    /**
846
     * Removes all flash messages.
847
     * Note that flash messages and normal session variables share the same name space.
848
     * If you have a normal session variable using the same name, it will be removed
849
     * by this method.
850
     * @see getFlash()
851
     * @see setFlash()
852
     * @see addFlash()
853
     * @see removeFlash()
854
     */
855
    public function removeAllFlashes()
856
    {
857
        $counters = $this->get($this->flashParam, []);
858
        foreach (array_keys($counters) as $key) {
859
            unset($_SESSION[$key]);
860
        }
861
        unset($_SESSION[$this->flashParam]);
862
    }
863
864
    /**
865
     * Returns a value indicating whether there are flash messages associated with the specified key.
866
     * @param string $key key identifying the flash message type
867
     * @return bool whether any flash messages exist under specified key
868
     */
869
    public function hasFlash($key)
870
    {
871
        return $this->getFlash($key) !== null;
872
    }
873
874
    /**
875
     * This method is required by the interface [[\ArrayAccess]].
876
     * @param mixed $offset the offset to check on
877
     * @return bool
878
     */
879
    public function offsetExists($offset)
880
    {
881
        $this->open();
882
883
        return isset($_SESSION[$offset]);
884
    }
885
886
    /**
887
     * This method is required by the interface [[\ArrayAccess]].
888
     * @param int $offset the offset to retrieve element.
889
     * @return mixed the element at the offset, null if no element is found at the offset
890
     */
891
    public function offsetGet($offset)
892
    {
893
        $this->open();
894
895
        return isset($_SESSION[$offset]) ? $_SESSION[$offset] : null;
896
    }
897
898
    /**
899
     * This method is required by the interface [[\ArrayAccess]].
900
     * @param int $offset the offset to set element
901
     * @param mixed $item the element value
902
     */
903
    public function offsetSet($offset, $item)
904
    {
905
        $this->open();
906
        $_SESSION[$offset] = $item;
907
    }
908
909
    /**
910
     * This method is required by the interface [[\ArrayAccess]].
911
     * @param mixed $offset the offset to unset element
912
     */
913
    public function offsetUnset($offset)
914
    {
915
        $this->open();
916
        unset($_SESSION[$offset]);
917
    }
918
919
    /**
920
     * If session is started it's not possible to edit session ini settings. In PHP7.2+ it throws exception.
921
     * This function saves session data to temporary variable and stop session.
922
     * @since 2.0.14
923
     */
924 8
    protected function freeze()
925
    {
926 8
        if ($this->getIsActive()) {
927 2
            if (isset($_SESSION)) {
928 1
                $this->frozenSessionData = $_SESSION;
929
            }
930 2
            $this->close();
931 2
            Yii::info('Session frozen', __METHOD__);
932
        }
933 8
    }
934
935
    /**
936
     * Starts session and restores data from temporary variable
937
     * @since 2.0.14
938
     */
939 8
    protected function unfreeze()
940
    {
941 8
        if (null !== $this->frozenSessionData) {
942
943 1
            YII_DEBUG ? session_start() : @session_start();
944
945 1
            if ($this->getIsActive()) {
946 1
                Yii::info('Session unfrozen', __METHOD__);
947
            } else {
948
                $error = error_get_last();
949
                $message = isset($error['message']) ? $error['message'] : 'Failed to unfreeze session.';
950
                Yii::error($message, __METHOD__);
951
            }
952
953 1
            $_SESSION = $this->frozenSessionData;
954 1
            $this->frozenSessionData = null;
955
        }
956 8
    }
957
958
    /**
959
     * Set cache limiter
960
     *
961
     * @param string $cacheLimiter
962
     * @since 2.0.14
963
     */
964 1
    public function setCacheLimiter($cacheLimiter)
965
    {
966 1
        $this->freeze();
967 1
        session_cache_limiter($cacheLimiter);
968 1
        $this->unfreeze();
969 1
    }
970
971
    /**
972
     * Returns current cache limiter
973
     *
974
     * @return string current cache limiter
975
     * @since 2.0.14
976
     */
977
    public function getCacheLimiter()
978
    {
979
        return session_cache_limiter();
980
    }
981
}
982