-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSession.php
More file actions
319 lines (282 loc) · 7.85 KB
/
Session.php
File metadata and controls
319 lines (282 loc) · 7.85 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
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
<?php
declare(strict_types=1);
/*
* UserFrosting Framework (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/framework
* @copyright Copyright (c) 2013-2024 Alexander Weissman, Louis Charette, Jordan Mele
* @license https://github.com/userfrosting/framework/blob/master/LICENSE.md (MIT License)
*/
namespace UserFrosting\Session;
use ArrayAccess;
use Illuminate\Session\ExistenceAwareInterface;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use SessionHandlerInterface;
/**
* A wrapper for $_SESSION that can be used with a variety of different session handlers, based on illuminate/session.
*
* @implements ArrayAccess<string, mixed>
*/
class Session implements ArrayAccess
{
/**
* The session handler implementation.
*
* @var SessionHandlerInterface
*/
protected SessionHandlerInterface $handler;
/**
* Create the session wrapper.
*
* @param SessionHandlerInterface $handler
* @param mixed[] $config
*
* @todo $config array should be replaced with a more strict alternative
*/
public function __construct(SessionHandlerInterface $handler, array $config = [])
{
$this->handler = $handler;
if ($this->status() == PHP_SESSION_NONE) {
session_set_save_handler($handler, true);
if (isset($config['cache_limiter'])) {
session_cache_limiter(strval($config['cache_limiter']));
}
if (isset($config['cache_expire'])) {
session_cache_expire(intval($config['cache_expire']));
}
if (isset($config['name'])) {
session_name(strval($config['name']));
}
if (isset($config['cookie_parameters'])) {
$param = $config['cookie_parameters'];
if (is_array($param)) {
session_set_cookie_params($param);
} else {
session_set_cookie_params(intval($param));
}
}
}
}
/**
* Returns the current session status.
*
* @return int PHP_SESSION_DISABLED | PHP_SESSION_NONE | PHP_SESSION_ACTIVE
*/
public function status(): int
{
return session_status();
}
/**
* Start the session.
*/
public function start(): void
{
if ($this->status() == PHP_SESSION_NONE) {
session_start();
}
}
/**
* Destroy the current session, and unset all values in memory. Destroy the session cookie as well to remove all traces client-side.
*
* @param bool $destroyCookie Destroy the cookie on the client side as well.
*/
public function destroy(bool $destroyCookie = true): void
{
if ($this->status() == PHP_SESSION_NONE) {
return;
}
session_unset();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if ($destroyCookie && ini_get('session.use_cookies') == true) {
$params = session_get_cookie_params();
$name = session_name();
setcookie(
($name !== false) ? $name : '',
'',
time() - 42000,
$params['path'],
$params['domain'],
$params['secure'],
$params['httponly']
);
}
session_destroy();
}
/**
* Regenerate the session id. For example, when logging someone in, you should regenerate the session to prevent session fixation attacks.
*
* @param bool $deleteOldSession Set to true when you are logging someone in.
*/
public function regenerateId(bool $deleteOldSession = false): void
{
session_regenerate_id($deleteOldSession);
$this->setExists(false);
}
/**
* Get the current session id.
*
* @see https://www.php.net/manual/en/function.session-id.php
*
* @return string|false Return false on error
*/
public function getId(): string|false
{
return session_id();
}
/**
* Set the existence of the session on the handler if applicable.
*
* @param bool $value
*/
public function setExists(bool $value): void
{
if ($this->handler instanceof ExistenceAwareInterface) {
$this->handler->setExists($value);
}
}
/**
* Determine if the given session value exists.
*
* @param string $key Dot notation is supported.
*
* @return bool
*/
public function has(string $key): bool
{
return Arr::has($_SESSION, $key);
}
/**
* Get the specified session value.
*
* @param string $key Dot notation is supported.
* @param mixed $default
*
* @return mixed
*/
public function get(string $key, mixed $default = null): mixed
{
return Arr::get($_SESSION, $key, $default);
}
/**
* Set a given session value.
*
* @param mixed[]|string|null $key Dot notation is supported.
* @param mixed $value
*/
public function set(array|string|null $key, mixed $value = null): void
{
if (is_array($key)) {
foreach ($key as $innerKey => $innerValue) {
Arr::set($_SESSION, $innerKey, $innerValue);
}
} else {
Arr::set($_SESSION, $key, $value);
}
}
/**
* Prepend a value onto an array session value.
*
* @param string $key Dot notation is supported.
* @param mixed $value
*/
public function prepend(string $key, mixed $value): void
{
$array = $this->get($key);
if (!is_array($array)) {
throw new InvalidArgumentException('Can prepend on non-array');
}
array_unshift($array, $value);
$this->set($key, $array);
}
/**
* Push a value onto an array session value.
*
* @param string $key Dot notation is supported.
* @param mixed $value
*/
public function push(string $key, mixed $value): void
{
$array = $this->get($key);
if (!is_array($array)) {
throw new InvalidArgumentException('Can push on non-array');
}
$array[] = $value;
$this->set($key, $array);
}
/**
* Unset a session value.
*
* @param string $key Dot notation is supported.
*/
public function forget(string $key): void
{
Arr::forget($_SESSION, $key);
}
/**
* Get all of the session items for the application.
*
* @return mixed[]
*/
public function all(): array
{
return $_SESSION;
}
/**
* Determine if the given session option exists.
*
* @internal
*
* @param string $key Dot notation is supported.
*
* @return bool
*/
public function offsetExists($key): bool
{
return $this->has($key);
}
/**
* Get a session option.
*
* @internal
*
* @param string $key Dot notation is supported.
*
* @return mixed
*/
public function offsetGet($key): mixed
{
return $this->get($key);
}
/**
* Set a session option.
*
* @internal
*
* @param string|null $key Dot notation is supported.
* @param mixed $value
*/
public function offsetSet($key, $value): void
{
$this->set($key, $value);
}
/**
* Unset a session option.
*
* @internal
*
* @param string $key Will be cast to string. Dot notation is supported.
*/
public function offsetUnset($key): void
{
$this->forget($key);
}
/**
* @return SessionHandlerInterface
*/
public function getHandler(): SessionHandlerInterface
{
return $this->handler;
}
}