Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSession.php
More file actions
Latest commit
executable file
·393 lines (303 loc) · 8.02 KB
/
Copy pathSession.php
File metadata and controls
executable file
·393 lines (303 loc) · 8.02 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <mihail@kodeart.com>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*
*/
namespaceKoded\Session;
useKoded\Exceptions\KodedException;
useKoded\Stdlib\{Immutable, UUID};
useKoded\Stdlib\Interfaces\Data;
interface Session
{
/**
* The timestamp when session started.
*/
constSTAMP = '_stamp';
/**
* The web browser signature.
*/
constAGENT = '_agent';
/**
* Session token (useful for CSRF)
*/
constTOKEN = '_token';
/**
* Flash data
*/
constFLASH = '_flash';
publicfunctionid(): string;
publicfunctionget(string$name, $default = null);
publicfunctionset(string$name, $value): Session;
publicfunctionpull(string$name, $default = null);
publicfunctionadd(string$name, $value): void;
publicfunctionimport(array$data): Session;
publicfunctionremove(string$name): void;
publicfunctionall(): array;
publicfunctiontoArray(): array;
publicfunctiontoData(): Data;
publicfunctionclear(): bool;
/**
* Destroys all of the data associated with the current session,
* by closing the session and re-opening it again.
* Also the object state is cleaned and internal metadata is recreated.
*
* @return bool TRUE on success, FALSE otherwise
*/
publicfunctiondestroy(): bool;
/**
* Update the current session id with a newly generated one
* and session token with a new v4 UUID.
*
* @param bool $deleteOldSession [optional] Whether to delete the old associated session or not
*
* @return bool TRUE on success, FALSE otherwise
* @link https://php.net/manual/en/function.session-regenerate-id.php
*/
publicfunctionregenerate(bool$deleteOldSession = false): bool;
/**
* Exchange the session data for another one.
*
* @param array $data The new array or object to exchange with the current session data
*
* @return array The old session data
*/
publicfunctionreplace(array$data): array;
publicfunctionhas(string$name): bool;
publicfunctionaccessed(): bool;
publicfunctionmodified(): bool;
publicfunctiontoken(): string;
publicfunctionstarttime(): float;
publicfunctionagent(): string;
publicfunctionisEmpty(): bool;
publicfunctioncount(): int;
publicfunctionflash(string$name, $value = null);
}
/**
* Class PhpSession
*/
finalclass PhpSession implements Session
{
/**
* @var bool
*/
private$accessed = false;
/**
* @var bool
*/
private$modified = false;
/**
* @var float
*/
private$stamp = 0;
/**
* @var string
*/
private$agent = '';
/**
* @var string
*/
private$token = '';
publicfunction__construct()
{
// @codeCoverageIgnoreStart
if (false === isset($_SESSION)) {
global$_SESSION;
$_SESSION = [];
}
// @codeCoverageIgnoreEnd
$this->loadMetadata();
$this->accessed = false;
$this->modified = false;
session($this);
}
publicfunctionget(string$name, $default = null)
{
$this->accessed = true;
return$_SESSION[$name] ?? $default;
}
publicfunction__get(string$name)
{
return$this->get($name);
}
publicfunctionall(): array
{
$this->accessed = true;
return (array)$_SESSION + $this->getMetadata();
}
publicfunction__set(string$name, $value)
{
return$this->set($name, $value);
}
publicfunctiontoArray(): array
{
$this->accessed = true;
return$_SESSION ?? [];
}
publicfunctiontoData(): Data
{
$this->accessed = true;
returnnewImmutable($_SESSION ?? []);
}
/*
*
* (mutator methods)
*
*/
publicfunctionset(string$name, $value): Session
{
$this->modified = true;
$_SESSION[$name] = $value;
return$this;
}
publicfunctionadd(string$name, $value): void
{
$this->has($name) || $this->set($name, $value);
}
publicfunctionimport(array$data): Session
{
$data = array_filter($data, 'is_string', ARRAY_FILTER_USE_KEY);
$_SESSION = array_replace($_SESSION, $data);
$this->modified = true;
return$this;
}
publicfunctionpull(string$name, $default = null)
{
$value = $_SESSION[$name] ?? $default;
unset($_SESSION[$name]);
return$value;
}
publicfunctionremove(string$name): void
{
$this->modified = true;
unset($_SESSION[$name]);
}
publicfunctionflash(string$name, $value = null)
{
if (null === $value) {
$value = $this->pull(self::FLASH);
} else {
$_SESSION[self::FLASH][$name] = $value;
}
$this->modified = true;
return$value;
}
publicfunctionhas(string$name): bool
{
returnarray_key_exists($name, $_SESSION ?? []);
}
/*
*
* (support methods)
*
*/
publicfunctionreplace(array$data): array
{
$oldSession = $_SESSION;
$_SESSION = [];
$this->import($data);
return$oldSession;
}
publicfunctionclear(): bool
{
$_SESSION = [];
$this->modified = true;
returnempty($_SESSION);
}
publicfunctionregenerate(bool$deleteOldSession = false): bool
{
$_SESSION[self::TOKEN] = $this->token = UUID::v4();
returnsession_regenerate_id($deleteOldSession);
}
publicfunctiondestroy(): bool
{
session_write_close();
// @codeCoverageIgnoreStart
if (false === session_start()) {
returnfalse;
}
// @codeCoverageIgnoreEnd
$updated = session_regenerate_id(true);
$this->replace([]);
$this->resetMetadata();
return$updated;
}
publicfunctionid(): string
{
returnsession_id();
}
publicfunctionaccessed(): bool
{
return$this->accessed;
}
publicfunctionmodified(): bool
{
return$this->modified;
}
publicfunctiontoken(): string
{
return$this->token;
}
publicfunctionstarttime(): float
{
return$this->stamp;
}
publicfunctionagent(): string
{
return$this->agent;
}
publicfunctionisEmpty(): bool
{
return0 === $this->count();
}
publicfunctioncount(): int
{
returncount($_SESSION);
}
/*
*
* Session metadata
*
*/
/**
* move CSRF token, start time and user agent into object properties.
*/
privatefunctionloadMetadata(): void
{
$this->stamp = $this->pull(self::STAMP, microtime(true));
$this->agent = $this->pull(self::AGENT, $_SERVER['HTTP_USER_AGENT'] ?? '');
$this->token = $this->pull(self::TOKEN, UUID::v4());
}
privatefunctionresetMetadata(): void
{
$_SESSION[self::STAMP] = $this->stamp = microtime(true);
$_SESSION[self::AGENT] = $this->agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
$_SESSION[self::TOKEN] = $this->token = UUID::v4();
}
privatefunctiongetMetadata(): array
{
return [
self::STAMP => $this->stamp,
self::AGENT => $this->agent,
self::TOKEN => $this->token,
];
}
}
/**
* Class SessionException
*/
class SessionException extends KodedException
{
privateconstE_HANDLER_NOT_FOUND = 0;
protected$messages = [
self::E_HANDLER_NOT_FOUND => 'Failed to load the session handler class. Requested :handler',
];
publicstaticfunctionforNotFoundHandler(string$handler): SessionException
{
returnnewself(self::E_HANDLER_NOT_FOUND, [':handler' => $handler]);
}
}