|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* sessionware (https://github.com/juliangut/sessionware). |
|
5
|
|
|
* PSR7 session management middleware. |
|
6
|
|
|
* |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @link https://github.com/juliangut/sessionware |
|
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Jgut\Sessionware; |
|
15
|
|
|
|
|
16
|
|
|
use Defuse\Crypto\Core; |
|
17
|
|
|
use Jgut\Sessionware\Traits\NativeSessionTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Session configuration. |
|
21
|
|
|
*/ |
|
22
|
|
|
class Configuration |
|
23
|
|
|
{ |
|
24
|
|
|
use NativeSessionTrait; |
|
25
|
|
|
|
|
26
|
|
|
const LIFETIME_FLASH = 300; // 5 minutes |
|
27
|
|
|
const LIFETIME_SHORT = 600; // 10 minutes |
|
28
|
|
|
const LIFETIME_NORMAL = 900; // 15 minutes |
|
29
|
|
|
const LIFETIME_DEFAULT = 1440; // 24 minutes |
|
30
|
|
|
const LIFETIME_EXTENDED = 3600; // 1 hour |
|
31
|
|
|
const LIFETIME_INFINITE = PHP_INT_MAX; // Around 1145 years (x86_64) |
|
32
|
|
|
|
|
33
|
|
|
const TIMEOUT_KEY_DEFAULT = '__SESSIONWARE_TIMEOUT__'; |
|
34
|
|
|
|
|
35
|
|
|
const SESSION_NAME_DEFAULT = 'PHPSESSID'; |
|
36
|
|
|
|
|
37
|
|
|
const SESSION_ID_LENGTH = 80; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $name; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $savePath; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var int |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $lifetime; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $cookiePath; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var string |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $cookieDomain; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var bool |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $cookieSecure; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var bool |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $cookieHttpOnly; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var string |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $encryptionKey; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var string |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $timeoutKey; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Configuration constructor. |
|
86
|
|
|
* |
|
87
|
|
|
* @param array $configurations |
|
88
|
|
|
*/ |
|
89
|
|
|
public function __construct(array $configurations = []) |
|
90
|
|
|
{ |
|
91
|
|
|
$configurations = array_merge( |
|
92
|
|
|
$this->getDefaultSessionSettings(), |
|
93
|
|
|
$configurations |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
$this->seedConfigurations($configurations); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Retrieve default session settings. |
|
101
|
|
|
* |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function getDefaultSessionSettings() : array |
|
105
|
|
|
{ |
|
106
|
|
|
$sessionLifetime = $this->getIntegerIniSetting('cookie_lifetime') === 0 |
|
107
|
|
|
? $this->getIntegerIniSetting('gc_maxlifetime') |
|
108
|
|
|
: min($this->getIntegerIniSetting('cookie_lifetime'), $this->getIntegerIniSetting('gc_maxlifetime')); |
|
109
|
|
|
$sessionName = session_name() !== static::SESSION_NAME_DEFAULT ? session_name() : static::SESSION_NAME_DEFAULT; |
|
110
|
|
|
|
|
111
|
|
|
return [ |
|
112
|
|
|
'name' => $this->getStringIniSetting('name', $sessionName), |
|
113
|
|
|
'savePath' => $this->getStringIniSetting('save_path', sys_get_temp_dir()), |
|
114
|
|
|
'lifetime' => $sessionLifetime > 0 ? $sessionLifetime : static::LIFETIME_DEFAULT, |
|
115
|
|
|
'cookiePath' => $this->getStringIniSetting('cookie_path', '/'), |
|
116
|
|
|
'cookieDomain' => $this->getStringIniSetting('cookie_domain'), |
|
117
|
|
|
'cookieSecure' => $this->hasBoolIniSetting('cookie_secure'), |
|
118
|
|
|
'cookieHttpOnly' => $this->hasBoolIniSetting('cookie_httponly'), |
|
119
|
|
|
'timeoutKey' => static::TIMEOUT_KEY_DEFAULT, |
|
120
|
|
|
]; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Seed configurations. |
|
125
|
|
|
* |
|
126
|
|
|
* @param array $configurations |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function seedConfigurations(array $configurations) |
|
129
|
|
|
{ |
|
130
|
|
|
$configs = [ |
|
131
|
|
|
'name', |
|
132
|
|
|
'cookiePath', |
|
133
|
|
|
'cookieDomain', |
|
134
|
|
|
'cookieSecure', |
|
135
|
|
|
'cookieHttpOnly', |
|
136
|
|
|
'savePath', |
|
137
|
|
|
'lifetime', |
|
138
|
|
|
'encryptionKey', |
|
139
|
|
|
'timeoutKey', |
|
140
|
|
|
]; |
|
141
|
|
|
|
|
142
|
|
|
foreach ($configs as $config) { |
|
143
|
|
|
if (isset($configurations[$config])) { |
|
144
|
|
|
$callback = [$this, 'set' . ucfirst($config)]; |
|
145
|
|
|
|
|
146
|
|
|
call_user_func($callback, $configurations[$config]); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get session name. |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getName() : string |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->name; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Set session name. |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $name |
|
165
|
|
|
* |
|
166
|
|
|
* @throws \InvalidArgumentException |
|
167
|
|
|
* |
|
168
|
|
|
* @return static |
|
169
|
|
|
*/ |
|
170
|
|
|
public function setName(string $name) |
|
171
|
|
|
{ |
|
172
|
|
|
if (trim($name) === '') { |
|
173
|
|
|
throw new \InvalidArgumentException('Session name must be a non empty string'); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
$this->name = $name; |
|
177
|
|
|
|
|
178
|
|
|
return $this; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Get session cookie path. |
|
183
|
|
|
* |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getCookiePath() : string |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->cookiePath; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Set session cookie path. |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $cookiePath |
|
195
|
|
|
* |
|
196
|
|
|
* @return static |
|
197
|
|
|
*/ |
|
198
|
|
|
public function setCookiePath(string $cookiePath) |
|
199
|
|
|
{ |
|
200
|
|
|
$this->cookiePath = $cookiePath; |
|
201
|
|
|
|
|
202
|
|
|
return $this; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Get session cookie domain. |
|
207
|
|
|
* |
|
208
|
|
|
* @return string |
|
209
|
|
|
*/ |
|
210
|
|
|
public function getCookieDomain() : string |
|
211
|
|
|
{ |
|
212
|
|
|
return $this->cookieDomain; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Set session cookie domain. |
|
217
|
|
|
* |
|
218
|
|
|
* @param string $cookieDomain |
|
219
|
|
|
* |
|
220
|
|
|
* @return static |
|
221
|
|
|
*/ |
|
222
|
|
|
public function setCookieDomain(string $cookieDomain) |
|
223
|
|
|
{ |
|
224
|
|
|
$this->cookieDomain = $cookieDomain; |
|
225
|
|
|
|
|
226
|
|
|
return $this; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Is session cookie HTTPS only. |
|
231
|
|
|
* |
|
232
|
|
|
* @return bool |
|
233
|
|
|
*/ |
|
234
|
|
|
public function isCookieSecure() : bool |
|
235
|
|
|
{ |
|
236
|
|
|
return $this->cookieSecure; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Set session cookie HTTPS only. |
|
241
|
|
|
* |
|
242
|
|
|
* @param bool $cookieSecure |
|
243
|
|
|
* |
|
244
|
|
|
* @return static |
|
245
|
|
|
*/ |
|
246
|
|
|
public function setCookieSecure(bool $cookieSecure) |
|
247
|
|
|
{ |
|
248
|
|
|
$this->cookieSecure = $cookieSecure; |
|
249
|
|
|
|
|
250
|
|
|
return $this; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Is session cookie HTTP only. |
|
255
|
|
|
* |
|
256
|
|
|
* @return bool |
|
257
|
|
|
*/ |
|
258
|
|
|
public function isCookieHttpOnly() : bool |
|
259
|
|
|
{ |
|
260
|
|
|
return $this->cookieHttpOnly; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Set session cookie HTTP only. |
|
265
|
|
|
* |
|
266
|
|
|
* @param bool $cookieHttpOnly |
|
267
|
|
|
* |
|
268
|
|
|
* @return static |
|
269
|
|
|
*/ |
|
270
|
|
|
public function setCookieHttpOnly(bool $cookieHttpOnly) |
|
271
|
|
|
{ |
|
272
|
|
|
$this->cookieHttpOnly = $cookieHttpOnly; |
|
273
|
|
|
|
|
274
|
|
|
return $this; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Get session save path. |
|
279
|
|
|
* |
|
280
|
|
|
* @return string |
|
281
|
|
|
*/ |
|
282
|
|
|
public function getSavePath() : string |
|
283
|
|
|
{ |
|
284
|
|
|
return $this->savePath; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Set session save path. |
|
289
|
|
|
* |
|
290
|
|
|
* @param string $savePath |
|
291
|
|
|
* |
|
292
|
|
|
* @throws \InvalidArgumentException |
|
293
|
|
|
* |
|
294
|
|
|
* @return static |
|
295
|
|
|
*/ |
|
296
|
|
|
public function setSavePath(string $savePath) |
|
297
|
|
|
{ |
|
298
|
|
|
if (trim($savePath) === '') { |
|
299
|
|
|
throw new \InvalidArgumentException('Session save path must be a non empty string'); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
$this->savePath = trim($savePath); |
|
303
|
|
|
|
|
304
|
|
|
return $this; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* Get session lifetime. |
|
309
|
|
|
* |
|
310
|
|
|
* @return int |
|
311
|
|
|
*/ |
|
312
|
|
|
public function getLifetime() : int |
|
313
|
|
|
{ |
|
314
|
|
|
return $this->lifetime; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Set session lifetime. |
|
319
|
|
|
* |
|
320
|
|
|
* @param int $lifetime |
|
321
|
|
|
* |
|
322
|
|
|
* @throws \InvalidArgumentException |
|
323
|
|
|
* |
|
324
|
|
|
* @return static |
|
325
|
|
|
*/ |
|
326
|
|
|
public function setLifetime(int $lifetime) |
|
327
|
|
|
{ |
|
328
|
|
|
if ((int) $lifetime < 1) { |
|
329
|
|
|
throw new \InvalidArgumentException('Session lifetime must be a positive integer'); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
$this->lifetime = (int) $lifetime; |
|
333
|
|
|
|
|
334
|
|
|
return $this; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* Set session encryption key. |
|
339
|
|
|
* |
|
340
|
|
|
* @return string|null |
|
341
|
|
|
*/ |
|
342
|
|
|
public function getEncryptionKey() |
|
343
|
|
|
{ |
|
344
|
|
|
return $this->encryptionKey; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* Set session encryption key. |
|
349
|
|
|
* |
|
350
|
|
|
* @param string $encryptionKey |
|
351
|
|
|
* |
|
352
|
|
|
* @throws \InvalidArgumentException |
|
353
|
|
|
* |
|
354
|
|
|
* @return static |
|
355
|
|
|
*/ |
|
356
|
|
|
public function setEncryptionKey(string $encryptionKey) : self |
|
357
|
|
|
{ |
|
358
|
|
|
if (trim($encryptionKey) === '') { |
|
359
|
|
|
throw new \InvalidArgumentException('Session encryption key must be a non empty string'); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
$this->encryptionKey = $encryptionKey; |
|
363
|
|
|
|
|
364
|
|
|
return $this; |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
/** |
|
368
|
|
|
* Get session timeout control key. |
|
369
|
|
|
* |
|
370
|
|
|
* @return string |
|
371
|
|
|
*/ |
|
372
|
|
|
public function getTimeoutKey() : string |
|
373
|
|
|
{ |
|
374
|
|
|
return $this->timeoutKey; |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
/** |
|
378
|
|
|
* Set session timeout control key. |
|
379
|
|
|
* |
|
380
|
|
|
* @param string $timeoutKey |
|
381
|
|
|
* |
|
382
|
|
|
* @throws \InvalidArgumentException |
|
383
|
|
|
* |
|
384
|
|
|
* @return static |
|
385
|
|
|
*/ |
|
386
|
|
|
public function setTimeoutKey(string $timeoutKey) |
|
387
|
|
|
{ |
|
388
|
|
|
if (trim($timeoutKey) === '') { |
|
389
|
|
|
throw new \InvalidArgumentException('Session timeout key must be a non empty string'); |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
$this->timeoutKey = $timeoutKey; |
|
393
|
|
|
|
|
394
|
|
|
return $this; |
|
395
|
|
|
} |
|
396
|
|
|
} |
|
397
|
|
|
|