-
Notifications
You must be signed in to change notification settings - Fork 0
/
Session.php
executable file
·393 lines (303 loc) · 8.02 KB
/
Session.php
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 <[email protected]>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*
*/
namespace Koded\Session;
use Koded\Exceptions\KodedException;
use Koded\Stdlib\{Immutable, UUID};
use Koded\Stdlib\Interfaces\Data;
interface Session
{
/**
* The timestamp when session started.
*/
const STAMP = '_stamp';
/**
* The web browser signature.
*/
const AGENT = '_agent';
/**
* Session token (useful for CSRF)
*/
const TOKEN = '_token';
/**
* Flash data
*/
const FLASH = '_flash';
public function id(): string;
public function get(string $name, $default = null);
public function set(string $name, $value): Session;
public function pull(string $name, $default = null);
public function add(string $name, $value): void;
public function import(array $data): Session;
public function remove(string $name): void;
public function all(): array;
public function toArray(): array;
public function toData(): Data;
public function clear(): 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
*/
public function destroy(): 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
*/
public function regenerate(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
*/
public function replace(array $data): array;
public function has(string $name): bool;
public function accessed(): bool;
public function modified(): bool;
public function token(): string;
public function starttime(): float;
public function agent(): string;
public function isEmpty(): bool;
public function count(): int;
public function flash(string $name, $value = null);
}
/**
* Class PhpSession
*/
final class 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 = '';
public function __construct()
{
// @codeCoverageIgnoreStart
if (false === isset($_SESSION)) {
global $_SESSION;
$_SESSION = [];
}
// @codeCoverageIgnoreEnd
$this->loadMetadata();
$this->accessed = false;
$this->modified = false;
session($this);
}
public function get(string $name, $default = null)
{
$this->accessed = true;
return $_SESSION[$name] ?? $default;
}
public function __get(string $name)
{
return $this->get($name);
}
public function all(): array
{
$this->accessed = true;
return (array)$_SESSION + $this->getMetadata();
}
public function __set(string $name, $value)
{
return $this->set($name, $value);
}
public function toArray(): array
{
$this->accessed = true;
return $_SESSION ?? [];
}
public function toData(): Data
{
$this->accessed = true;
return new Immutable($_SESSION ?? []);
}
/*
*
* (mutator methods)
*
*/
public function set(string $name, $value): Session
{
$this->modified = true;
$_SESSION[$name] = $value;
return $this;
}
public function add(string $name, $value): void
{
$this->has($name) || $this->set($name, $value);
}
public function import(array $data): Session
{
$data = array_filter($data, 'is_string', ARRAY_FILTER_USE_KEY);
$_SESSION = array_replace($_SESSION, $data);
$this->modified = true;
return $this;
}
public function pull(string $name, $default = null)
{
$value = $_SESSION[$name] ?? $default;
unset($_SESSION[$name]);
return $value;
}
public function remove(string $name): void
{
$this->modified = true;
unset($_SESSION[$name]);
}
public function flash(string $name, $value = null)
{
if (null === $value) {
$value = $this->pull(self::FLASH);
} else {
$_SESSION[self::FLASH][$name] = $value;
}
$this->modified = true;
return $value;
}
public function has(string $name): bool
{
return array_key_exists($name, $_SESSION ?? []);
}
/*
*
* (support methods)
*
*/
public function replace(array $data): array
{
$oldSession = $_SESSION;
$_SESSION = [];
$this->import($data);
return $oldSession;
}
public function clear(): bool
{
$_SESSION = [];
$this->modified = true;
return empty($_SESSION);
}
public function regenerate(bool $deleteOldSession = false): bool
{
$_SESSION[self::TOKEN] = $this->token = UUID::v4();
return session_regenerate_id($deleteOldSession);
}
public function destroy(): bool
{
session_write_close();
// @codeCoverageIgnoreStart
if (false === session_start()) {
return false;
}
// @codeCoverageIgnoreEnd
$updated = session_regenerate_id(true);
$this->replace([]);
$this->resetMetadata();
return $updated;
}
public function id(): string
{
return session_id();
}
public function accessed(): bool
{
return $this->accessed;
}
public function modified(): bool
{
return $this->modified;
}
public function token(): string
{
return $this->token;
}
public function starttime(): float
{
return $this->stamp;
}
public function agent(): string
{
return $this->agent;
}
public function isEmpty(): bool
{
return 0 === $this->count();
}
public function count(): int
{
return count($_SESSION);
}
/*
*
* Session metadata
*
*/
/**
* move CSRF token, start time and user agent into object properties.
*/
private function loadMetadata(): 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());
}
private function resetMetadata(): void
{
$_SESSION[self::STAMP] = $this->stamp = microtime(true);
$_SESSION[self::AGENT] = $this->agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
$_SESSION[self::TOKEN] = $this->token = UUID::v4();
}
private function getMetadata(): array
{
return [
self::STAMP => $this->stamp,
self::AGENT => $this->agent,
self::TOKEN => $this->token,
];
}
}
/**
* Class SessionException
*/
class SessionException extends KodedException
{
private const E_HANDLER_NOT_FOUND = 0;
protected $messages = [
self::E_HANDLER_NOT_FOUND => 'Failed to load the session handler class. Requested :handler',
];
public static function forNotFoundHandler(string $handler): SessionException
{
return new self(self::E_HANDLER_NOT_FOUND, [':handler' => $handler]);
}
}