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