-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSessionInstance.php
More file actions
274 lines (217 loc) · 6.82 KB
/
Copy pathSessionInstance.php
File metadata and controls
274 lines (217 loc) · 6.82 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
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
<?php
namespace duncan3dc\Sessions;
use duncan3dc\Sessions\Exceptions\AlreadyActiveException;
use duncan3dc\Sessions\Exceptions\InvalidNameException;
use function array_key_exists;
use function is_array;
use function session_cache_limiter;
use function session_destroy;
use function session_id;
use function session_name;
use function session_set_cookie_params;
use function session_start;
use function session_write_close;
use function setcookie;
use function strlen;
use function time;
/**
* A non-blocking session manager.
*/
final class SessionInstance implements SessionInterface
{
use SessionTrait;
/**
* @var bool Whether the session has been started or not.
*/
private bool $init = false;
private string $name = "";
/**
* @var array<string, mixed> The cache of the session data.
*/
private array $data = [];
private string $id = "";
private CookieInterface $cookie;
/**
* Create a new instance.
*
* @param string $name The name of the session
* @param CookieInterface|null $cookie The cookie settings to use
* @param string $id The session ID to use
*/
public function __construct(string $name, ?CookieInterface $cookie = null, string $id = "")
{
if (strlen($name) < 1) {
throw new InvalidNameException("Cannot start session, no name has been specified");
}
if ($cookie === null) {
$cookie = Cookie::createFromIni();
}
$this->name = $name;
$this->cookie = $cookie;
$this->id = $id;
}
/**
* Ensure the session data is loaded into cache.
* @throws AlreadyActiveException
*/
private function init(): void
{
if ($this->init) {
return;
}
$this->init = true;
if (session_status() === \PHP_SESSION_ACTIVE) {
throw new AlreadyActiveException("A session has already been started");
}
session_cache_limiter("");
session_set_cookie_params($this->cookie->getLifetime(), $this->cookie->getPath(), $this->cookie->getDomain(), $this->cookie->isSecure(), $this->cookie->isHttpOnly());
session_name($this->name);
if ($this->id !== "") {
session_id($this->id);
}
session_start([
"read_and_close" => true,
]);
/**
* If the cookie has a specific lifetime (not unlimited)
* then ensure it is extended on each use of the session.
*/
if ($this->cookie->getLifetime() > 0) {
$expires = time() + $this->cookie->getLifetime();
setcookie($this->name, (string) session_id(), $expires, $this->cookie->getPath(), $this->cookie->getDomain(), $this->cookie->isSecure(), $this->cookie->isHttpOnly());
}
/** @var array<string, mixed> $_SESSION Grab the sessions data to respond to get() */
$this->data = $_SESSION;
# Grab session ID
$this->id = (string) session_id();
}
/**
* Get the session ID.
*
* @return string
* @throws AlreadyActiveException
*/
public function getId(): string
{
$this->init();
return $this->id;
}
/**
* Update the current session id with a newly generated one.
*
* @return string The new session ID
* @throws AlreadyActiveException
*/
public function regenerate(): string
{
$this->init();
# Generate a new session
session_start();
session_regenerate_id();
# Get the newly generated ID
$this->id = (string) session_id();
# Remove the lock from the session file
session_write_close();
return $this->id;
}
/**
* Create a new namespaced section of this session to avoid clashes.
*
* @param string $name The namespace of the session
*
* @return SessionInterface
*/
public function createNamespace(string $name): SessionInterface
{
return new SessionNamespace($name, $this);
}
/**
* Get a value from the session data cache.
*
* @param string $key The name of the name to retrieve
*
* @return mixed
* @throws AlreadyActiveException
*/
public function get(string $key)
{
$this->init();
if (!array_key_exists($key, $this->data)) {
return null;
}
return $this->data[$key];
}
/**
* Get all the current session data.
*
* @throws AlreadyActiveException
*/
public function getAll(): array
{
$this->init();
return $this->data;
}
/**
* Set a value within session data.
*
* @throws AlreadyActiveException
*/
public function set(string|array $data, $value = null): SessionInterface
{
$this->init();
# Check that at least one value has been changed before starting up the sesson
$changed = false;
if (is_array($data)) {
foreach ($data as $key => $val) {
if ($this->get($key) !== $val) {
$changed = true;
break;
}
}
} else {
if ($this->get($data) !== $value) {
$changed = true;
}
}
# If none of the values have changed then don't write to session data
if (!$changed) {
return $this;
}
/**
* Whenever a key is set, we need to start the session up again to store it
* When session_start is called it attempts to send the cookie to the browser with the session id in.
* However if some output has already been sent then this will fail, this is why we suppress errors on the call here
*/
@session_start();
if (is_array($data)) {
foreach ($data as $key => $val) {
$_SESSION[$key] = $val;
}
} else {
$_SESSION[$data] = $value;
}
/** @var array<string, mixed> $_SESSION Grab the sessions data to respond to get() */
$this->data = $_SESSION;
session_write_close();
return $this;
}
/**
* Tear down the session and wipe all its data.
*/
public function destroy(): void
{
try {
$this->init();
} catch (AlreadyActiveException $exception) {
}
# Start the session up, but ignore the error about headers already being sent
@session_start();
# Clear the session data from the server
session_destroy();
# Clear the cookie so the client knows the session is gone
setcookie($this->name, "", time() - 86400, $this->cookie->getPath(), $this->cookie->getDomain(), $this->cookie->isSecure(), $this->cookie->isHttpOnly());
# Reset the session data
$this->init = false;
$this->data = [];
}
}