-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionManager.php
More file actions
277 lines (242 loc) · 6 KB
/
SessionManager.php
File metadata and controls
277 lines (242 loc) · 6 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
<?php
namespace Altair\Session;
use Altair\Security\Support\Salt;
use Altair\Session\Contracts\CsrfTokenInterface;
use Altair\Session\Contracts\SessionBlockInterface;
use Altair\Session\Contracts\SessionManagerInterface;
use Altair\Session\Exception\InvalidArgumentException;
use Altair\Session\Factory\SessionBlockFactory;
use Psr\Http\Message\ServerRequestInterface;
use SessionHandlerInterface;
class SessionManager implements SessionManagerInterface
{
/**
* Incoming cookies from the client, typically a copy of the $_COOKIE
* superglobal.
*
* @var array
*/
protected $cookies;
/**
* Session cookie parameters.
*
* @var array
*/
protected $cookieParams = [];
/**
* A callable to invoke when deleting the session cookie. The callable
* should have the following signature: `function ($cookie_name, $cookie_params)` and must return null;
*
* @var callable
*
* @see setDeleteCookie()
*/
protected $deleteCookieCallable;
/**
* @var SessionHandlerInterface
*/
protected $sessionHandler;
/**
* @var CsrfToken
*/
protected $csrfToken;
/**
* SessionManager constructor.
*
* @param ServerRequestInterface $request
* @param SessionHandlerInterface|null $sessionHandler
* @param callable|null $deleteCookieCallable
*/
public function __construct(
ServerRequestInterface $request,
SessionHandlerInterface $sessionHandler = null,
callable $deleteCookieCallable = null
) {
$this->cookies = $request->getCookieParams();
$this->cookieParams = session_get_cookie_params();
$this->sessionHandler = $sessionHandler;
$this->setDeleteCookieCallable($deleteCookieCallable);
register_shutdown_function([$this, 'close']);
}
/**
* @inheritdoc
*/
public function getId(): string
{
return session_id();
}
/**
* @inheritdoc
*/
public function setId(string $id)
{
session_id($id);
}
/**
* @inheritdoc
*/
public function setDeleteCookieCallable(callable $callable = null)
{
$this->deleteCookieCallable = $callable?? function ($name, $params) {
$path = $params['path']?? null;
$domain = $params['domain']?? null;
setcookie($name, '', time() - 42000, $path, $domain);
};
}
/**
* @inheritdoc
*/
public function getSessionBlock(string $name): SessionBlockInterface
{
return SessionBlockFactory::create($name, $this);
}
/**
* @inheritdoc
*/
public function getName(): string
{
return session_name();
}
/**
* @inheritdoc
*/
public function setName(string $name)
{
session_name($name);
}
/**
* @inheritdoc
*/
public function getSavePath(): string
{
return session_save_path();
}
/**
* @inheritdoc
*/
public function setSavePath(string $path)
{
if (!is_dir($path)) {
throw new InvalidArgumentException(sprintf('Session save path is not a valid directory: %s', $path));
}
session_save_path($path);
}
/**
* @inheritdoc
*/
public function getCookieParams(): array
{
return $this->getCookieParams();
}
/**
* @inheritdoc
*/
public function setCookieParams(array $params)
{
$this->cookieParams = array_merge($this->cookieParams, $params);
session_set_cookie_params(
$this->cookieParams['lifetime'],
$this->cookieParams['path'],
$this->cookieParams['domain'],
$this->cookieParams['secure'],
$this->cookieParams['httponly']
);
}
/**
* @inheritdoc
*/
public function getIsActive(): bool
{
return session_status() === PHP_SESSION_ACTIVE;
}
/**
* @inheritdoc
*/
public function exists(): bool
{
return isset($this->cookies[$this->getName()]);
}
/**
* @inheritdoc
*/
public function start(): bool
{
if (!$this->getIsActive()) {
if ($this->sessionHandler !== null) {
session_set_save_handler($this->sessionHandler, false);
}
return session_start();
}
return true;
}
/**
* @inheritdoc
*/
public function clear()
{
session_unset();
}
/**
* @inheritdoc
*/
public function resume(): bool
{
if ($this->getIsActive()) {
return true;
}
if ($this->exists()) {
return $this->start();
}
return false;
}
/**
* @inheritdoc
*/
public function close()
{
if ($this->getIsActive()) {
session_write_close();
}
}
/**
* @inheritdoc
*/
public function destroy(): bool
{
if (!$this->getIsActive()) {
$this->start();
}
$this->clear();
if (session_destroy()) {
call_user_func($this->deleteCookieCallable, $this->getName(), $this->getCookieParams());
return true;
}
return false;
}
/**
* @inheritdoc
*/
public function regenerateId(bool $deletePrevious = true): bool
{
$result = false;
if ($this->getIsActive()) {
$result = session_regenerate_id($deletePrevious);
if ($result && $this->csrfToken instanceof CsrfTokenInterface) {
$this->csrfToken->generateValue();
}
}
return $result;
}
/**
* @inheritdoc
*/
public function getCsrfToken(): CsrfTokenInterface
{
if ($this->csrfToken === null) {
$sessionBlock = $this->getSessionBlock(SessionBlockInterface::CSRF_KEY);
$this->csrfToken = new CsrfToken($sessionBlock, new Salt());
$this->csrfToken->generateValue();
}
return $this->csrfToken;
}
}