1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\Site; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use Helix\Site; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The session. |
10
|
|
|
*/ |
11
|
|
|
class Session implements ArrayAccess |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var Site |
16
|
|
|
*/ |
17
|
|
|
protected $site; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Starts the session and initializes the CSRF token. |
21
|
|
|
* |
22
|
|
|
* @param Site $site |
23
|
|
|
*/ |
24
|
|
|
public function __construct(Site $site) |
25
|
|
|
{ |
26
|
|
|
$this->site = $site; |
27
|
|
|
session_set_cookie_params(0, '/', null, !$site->isDev(), true); |
28
|
|
|
session_start(); |
29
|
|
|
$_SESSION['__csrf'] ??= bin2hex(random_bytes(8)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public function getToken(): string |
36
|
|
|
{ |
37
|
|
|
return $_SESSION['__csrf']; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns the stored user, or `null`. |
42
|
|
|
* |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
public function getUser() |
46
|
|
|
{ |
47
|
|
|
return $_SESSION['__user'] ?? null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Wipes the session. |
52
|
|
|
*/ |
53
|
|
|
public function logout(): void |
54
|
|
|
{ |
55
|
|
|
setcookie(session_name(), null, 1); |
56
|
|
|
session_destroy(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param mixed $offset |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function offsetExists($offset): bool |
64
|
|
|
{ |
65
|
|
|
return isset($_SESSION[$offset]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param mixed $offset |
70
|
|
|
* @return mixed Coalesces to `null` |
71
|
|
|
*/ |
72
|
|
|
public function offsetGet($offset) |
73
|
|
|
{ |
74
|
|
|
return $_SESSION[$offset] ?? null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param mixed $offset |
79
|
|
|
* @param mixed $value |
80
|
|
|
*/ |
81
|
|
|
public function offsetSet($offset, $value): void |
82
|
|
|
{ |
83
|
|
|
$_SESSION[$offset] = $value; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param mixed $offset |
88
|
|
|
*/ |
89
|
|
|
public function offsetUnset($offset): void |
90
|
|
|
{ |
91
|
|
|
unset($_SESSION[$offset]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Sets the user (logs them in). |
96
|
|
|
* |
97
|
|
|
* @param mixed $user |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function setUser($user) |
101
|
|
|
{ |
102
|
|
|
$_SESSION['__user'] = $user; |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Checks the given CSRF token against what we expect. |
108
|
|
|
* |
109
|
|
|
* If they don't match, a `403` is logged and thrown. |
110
|
|
|
* |
111
|
|
|
* @param $token |
112
|
|
|
* @return $this |
113
|
|
|
* @throws HttpError |
114
|
|
|
*/ |
115
|
|
|
public function verify($token) |
116
|
|
|
{ |
117
|
|
|
if ($token !== $this->getToken()) { |
118
|
|
|
$this->site->log(403, 'Invalid CSRF token.'); |
119
|
|
|
throw new HttpError(403, 'Invalid CSRF token.'); |
120
|
|
|
} |
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|