1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Session; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Session provides session data management and the related configurations. |
7
|
|
|
*/ |
8
|
|
|
class Session implements SessionInterface |
9
|
|
|
{ |
10
|
|
|
private const DEFAULT_OPTIONS = [ |
11
|
|
|
'use_cookies' => 1, |
12
|
|
|
'cookie_secure' => 1, |
13
|
|
|
'use_only_cookies' => 1, |
14
|
|
|
'cookie_httponly' => 1, |
15
|
|
|
'use_strict_mode' => 1, |
16
|
|
|
'sid_bits_per_character' => 5, |
17
|
|
|
'sid_length' => 48, |
18
|
|
|
'cache_limiter' => 'nocache', |
19
|
|
|
'cookie_samesite' => 'Lax', |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
private ?string $sessionId = null; |
23
|
|
|
private array $options; |
24
|
|
|
|
25
|
12 |
|
public function __construct(array $options = [], \SessionHandlerInterface $handler = null) |
26
|
|
|
{ |
27
|
12 |
|
if ($handler !== null) { |
28
|
|
|
session_set_save_handler($handler); |
29
|
|
|
} |
30
|
|
|
|
31
|
12 |
|
$defaultOptions = self::DEFAULT_OPTIONS; |
32
|
12 |
|
$this->options = array_merge($defaultOptions, $options); |
33
|
12 |
|
} |
34
|
|
|
|
35
|
3 |
|
public function get(string $key, $default = null) |
36
|
|
|
{ |
37
|
3 |
|
$this->open(); |
38
|
3 |
|
return $_SESSION[$key] ?? $default; |
39
|
|
|
} |
40
|
|
|
|
41
|
7 |
|
public function set(string $key, $value): void |
42
|
|
|
{ |
43
|
7 |
|
$this->open(); |
44
|
7 |
|
$_SESSION[$key] = $value; |
45
|
7 |
|
} |
46
|
|
|
|
47
|
1 |
|
public function close(): void |
48
|
|
|
{ |
49
|
1 |
|
if ($this->isActive()) { |
50
|
|
|
try { |
51
|
1 |
|
session_write_close(); |
52
|
|
|
} catch (\Throwable $e) { |
53
|
|
|
throw new SessionException('Unable to close session', $e->getCode(), $e); |
54
|
|
|
} |
55
|
|
|
} |
56
|
1 |
|
} |
57
|
|
|
|
58
|
9 |
|
public function open(): void |
59
|
|
|
{ |
60
|
9 |
|
if ($this->isActive()) { |
61
|
5 |
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
9 |
|
if ($this->sessionId !== null) { |
65
|
3 |
|
session_id($this->sessionId); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
try { |
69
|
9 |
|
session_start($this->options); |
70
|
9 |
|
$this->sessionId = session_id(); |
71
|
|
|
} catch (\Throwable $e) { |
72
|
|
|
throw new SessionException('Failed to start session', $e->getCode(), $e); |
73
|
|
|
} |
74
|
9 |
|
} |
75
|
|
|
|
76
|
10 |
|
public function isActive(): bool |
77
|
|
|
{ |
78
|
10 |
|
return session_status() === PHP_SESSION_ACTIVE; |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
public function getId(): ?string |
82
|
|
|
{ |
83
|
2 |
|
return $this->sessionId === '' ? null : $this->sessionId; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
public function regenerateId(): void |
87
|
|
|
{ |
88
|
1 |
|
if ($this->isActive()) { |
89
|
|
|
try { |
90
|
1 |
|
if (session_regenerate_id(true)) { |
91
|
1 |
|
$this->sessionId = session_id(); |
92
|
|
|
} |
93
|
|
|
} catch (\Throwable $e) { |
94
|
|
|
throw new SessionException('Failed to regenerate ID', $e->getCode(), $e); |
95
|
|
|
} |
96
|
|
|
} |
97
|
1 |
|
} |
98
|
|
|
|
99
|
1 |
|
public function discard(): void |
100
|
|
|
{ |
101
|
1 |
|
if ($this->isActive()) { |
102
|
1 |
|
session_abort(); |
103
|
|
|
} |
104
|
1 |
|
} |
105
|
|
|
|
106
|
1 |
|
public function getName(): string |
107
|
|
|
{ |
108
|
1 |
|
return session_name(); |
109
|
|
|
} |
110
|
|
|
|
111
|
2 |
|
public function all(): array |
112
|
|
|
{ |
113
|
2 |
|
$this->open(); |
114
|
2 |
|
return $_SESSION; |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
public function remove(string $key): void |
118
|
|
|
{ |
119
|
1 |
|
$this->open(); |
120
|
1 |
|
unset($_SESSION[$key]); |
121
|
1 |
|
} |
122
|
|
|
|
123
|
1 |
|
public function has(string $key): bool |
124
|
|
|
{ |
125
|
1 |
|
$this->open(); |
126
|
1 |
|
return isset($_SESSION[$key]); |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
public function pull(string $key) |
130
|
|
|
{ |
131
|
1 |
|
$value = $this->get($key); |
132
|
1 |
|
$this->remove($key); |
133
|
1 |
|
return $value; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
public function clear(): void |
137
|
|
|
{ |
138
|
1 |
|
$this->open(); |
139
|
1 |
|
$_SESSION = []; |
140
|
1 |
|
} |
141
|
|
|
|
142
|
10 |
|
public function destroy(): void |
143
|
|
|
{ |
144
|
10 |
|
if ($this->isActive()) { |
145
|
9 |
|
session_destroy(); |
146
|
9 |
|
$this->sessionId = null; |
147
|
|
|
} |
148
|
10 |
|
} |
149
|
|
|
|
150
|
1 |
|
public function getCookieParameters(): array |
151
|
|
|
{ |
152
|
1 |
|
return session_get_cookie_params(); |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
public function setId(string $sessionId): void |
156
|
|
|
{ |
157
|
1 |
|
$this->sessionId = $sessionId; |
158
|
1 |
|
} |
159
|
|
|
} |
160
|
|
|
|