-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPhpSession.php
More file actions
210 lines (174 loc) · 5.31 KB
/
Copy pathPhpSession.php
File metadata and controls
210 lines (174 loc) · 5.31 KB
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
<?php
namespace Odan\Session;
use Odan\Session\Exception\SessionException;
/**
* A PHP Session handler adapter.
*/
final class PhpSession implements SessionInterface, SessionManagerInterface
{
/**
* @var array<string, mixed>
*/
private array $storage;
private FlashInterface $flash;
/**
* @var array<string, mixed>
*/
private array $options = [
'id' => null,
'name' => 'app',
'lifetime' => 7200,
'path' => null,
'domain' => null,
'secure' => false,
'httponly' => true,
// public, private_no_expire, private, nocache
// Setting the cache limiter to '' will turn off automatic sending of cache headers entirely.
'cache_limiter' => 'nocache',
];
/**
* @param array<string, mixed> $options
*/
public function __construct(array $options = [])
{
// Prevent uninitialized state
$empty = [];
$this->storage = &$empty;
$this->flash = new Flash($empty);
$keys = array_keys($this->options);
foreach ($keys as $key) {
if (array_key_exists($key, $options)) {
$this->options[$key] = $options[$key];
unset($options[$key]);
}
}
foreach ($options as $key => $value) {
ini_set('session.' . $key, $value);
}
}
public function start(): void
{
if ($this->isStarted()) {
throw new SessionException('Failed to start the session: Already started.');
}
if (headers_sent($file, $line) && filter_var(ini_get('session.use_cookies'), FILTER_VALIDATE_BOOLEAN)) {
throw new SessionException(
sprintf(
'Failed to start the session because headers have already been sent by "%s" at line %d.',
$file,
$line
)
);
}
$current = session_get_cookie_params();
$lifetime = (int)($this->options['lifetime'] ?: $current['lifetime']);
$path = $this->options['path'] ?: $current['path'];
$domain = $this->options['domain'] ?: $current['domain'];
$secure = (bool)$this->options['secure'];
$httponly = (bool)$this->options['httponly'];
session_set_cookie_params($lifetime, $path, $domain, $secure, $httponly);
session_name($this->options['name']);
session_cache_limiter($this->options['cache_limiter']);
$sessionId = $this->options['id'] ?: null;
if ($sessionId) {
session_id($sessionId);
}
// Try and start the session
if (!session_start()) {
throw new SessionException('Failed to start the session.');
}
// Load the session
$this->storage = &$_SESSION;
$this->flash = new Flash($_SESSION);
}
public function isStarted(): bool
{
return session_status() === PHP_SESSION_ACTIVE;
}
public function regenerateId(): void
{
if (!$this->isStarted()) {
throw new SessionException('Cannot regenerate the session ID for non-active sessions.');
}
if (headers_sent()) {
throw new SessionException('Headers have already been sent.');
}
if (!session_regenerate_id(true)) {
throw new SessionException('The session ID could not be regenerated.');
}
}
public function destroy(): void
{
if (!$this->isStarted()) {
return;
}
$this->clear();
if (ini_get('session.use_cookies')) {
$params = session_get_cookie_params();
setcookie(
$this->getName(),
'',
time() - 42000,
$params['path'],
$params['domain'],
$params['secure'],
$params['httponly']
);
}
if (session_unset() === false) {
throw new SessionException('The session could not be unset.');
}
if (session_destroy() === false) {
throw new SessionException('The session could not be destroyed.');
}
}
public function getId(): string
{
return (string)session_id();
}
public function getName(): string
{
return (string)session_name();
}
public function get(string $key, mixed $default = null): mixed
{
return $this->storage[$key] ?? $default;
}
public function all(): array
{
return (array)$this->storage;
}
public function set(string $key, mixed $value): void
{
$this->storage[$key] = $value;
}
public function setValues(array $values): void
{
foreach ($values as $key => $value) {
$this->storage[$key] = $value;
}
}
public function has(string $key): bool
{
return array_key_exists($key, $this->storage);
}
public function delete(string $key): void
{
unset($this->storage[$key]);
}
public function clear(): void
{
$keys = array_keys($this->storage);
foreach ($keys as $key) {
unset($this->storage[$key]);
}
}
public function save(): void
{
session_write_close();
}
public function getFlash(): FlashInterface
{
return $this->flash;
}
}