1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) Florian Krämer (https://florian-kraemer.net) |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
7
|
|
|
* Redistributions of files must retain the above copyright notice. |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net) |
10
|
|
|
* @author Florian Krämer |
11
|
|
|
* @link https://github.com/Phauthentic |
12
|
|
|
* @license https://opensource.org/licenses/MIT MIT License |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types=1); |
16
|
|
|
|
17
|
|
|
namespace Phauthentic\Session; |
18
|
|
|
|
19
|
|
|
use RuntimeException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Session Configuration Abstraction |
23
|
|
|
* |
24
|
|
|
* This class provides a way to configure most of the session settings |
25
|
|
|
* |
26
|
|
|
* @link https://www.php.net/manual/en/session.security.ini.php |
27
|
|
|
*/ |
28
|
|
|
class Config implements ConfigInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @param array $config |
32
|
|
|
* @return \Phauthentic\Session\Config |
33
|
|
|
*/ |
34
|
|
|
public static function fromArray(array $config): Config |
35
|
|
|
{ |
36
|
|
|
$that = new self(); |
37
|
|
|
|
38
|
|
|
foreach ($config as $key => $value) { |
39
|
|
|
$method = 'set' . $key; |
40
|
|
|
if (method_exists($that, $method)) { |
41
|
|
|
$that->$method($value); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $that; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritDoc |
50
|
|
|
*/ |
51
|
2 |
|
public function setUseTransSid(bool $useTransSid): ConfigInterface |
52
|
|
|
{ |
53
|
2 |
|
$this->iniSet('session.use_trans_sid', $useTransSid); |
54
|
|
|
|
55
|
2 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritDoc |
60
|
|
|
*/ |
61
|
|
|
public function setSerializeHandler(string $handler): ConfigInterface |
62
|
|
|
{ |
63
|
|
|
$this->iniSet('session.serialize_handler', $handler); |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc |
70
|
|
|
*/ |
71
|
|
|
public function setStrictMode(bool $useStrictMode): ConfigInterface |
72
|
|
|
{ |
73
|
|
|
$this->iniSet('session.use_strict_mode', $useStrictMode); |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
|
|
public function setCookiePath(string $path): ConfigInterface |
82
|
|
|
{ |
83
|
|
|
$this->iniSet('session.cookie_path', $path); |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritDoc |
90
|
|
|
*/ |
91
|
1 |
|
public function setCookieHttpOnly(bool $onlyHttp): ConfigInterface |
92
|
|
|
{ |
93
|
1 |
|
$this->iniSet('session.cookie_httponly', $onlyHttp); |
94
|
|
|
|
95
|
1 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritDoc |
100
|
|
|
*/ |
101
|
1 |
|
public function getCookieHttpOnly(): bool |
102
|
|
|
{ |
103
|
1 |
|
return (bool)ini_get('session.cookie_httponly'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritDoc |
108
|
|
|
*/ |
109
|
|
|
public function setCookieSecure(bool $secure): ConfigInterface |
110
|
|
|
{ |
111
|
|
|
$this->iniSet('session.cookie_secure', $secure); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @inheritDoc |
118
|
|
|
*/ |
119
|
|
|
public function setSessionName(string $name): ConfigInterface |
120
|
|
|
{ |
121
|
|
|
$this->iniSet('session.name', $name); |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @inheritDoc |
128
|
|
|
*/ |
129
|
|
|
public function setUseCookies(bool $useCookies): ConfigInterface |
130
|
|
|
{ |
131
|
|
|
$this->iniSet('session.use_cookies', $useCookies); |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @inheritDoc |
138
|
|
|
*/ |
139
|
1 |
|
public function setSavePath(string $path): ConfigInterface |
140
|
|
|
{ |
141
|
1 |
|
$this->iniSet('session.save_path', $path); |
142
|
|
|
|
143
|
1 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Checks an edge case for the handler |
148
|
|
|
* |
149
|
|
|
* @link https://github.com/php/php-src/commit/a93a51c3bf4ea1638ce0adc4a899cb93531b9f0d |
150
|
|
|
* @link http://php.net/manual/en/session.configuration.php |
151
|
|
|
* @param string $handler Handler |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
protected function checkHandler(string $handler): void |
155
|
|
|
{ |
156
|
|
|
if ( |
157
|
|
|
PHP_VERSION_ID >= 70200 |
158
|
|
|
&& $handler === 'user' |
159
|
|
|
) { |
160
|
|
|
throw new RuntimeException( |
161
|
|
|
'You can\'t set the `user` save handler any longer in php >= 7.2. ' |
162
|
|
|
. 'See https://github.com/php/php-src/commit/a93a51c3bf4ea1638ce0adc4a899cb93531b9f0d' |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @inheritDoc |
169
|
|
|
*/ |
170
|
|
|
public function setSaveHandler(string $handler): ConfigInterface |
171
|
|
|
{ |
172
|
|
|
$this->checkHandler($handler); |
173
|
|
|
$this->iniSet('session.save_handler', $handler); |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @inheritDoc |
180
|
|
|
*/ |
181
|
1 |
|
public function getUseTransSid(): bool |
182
|
|
|
{ |
183
|
1 |
|
return (bool)ini_get('session.use_trans_sid'); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @inheritDoc |
188
|
|
|
*/ |
189
|
|
|
public function getSerializeHandler(): string |
190
|
|
|
{ |
191
|
|
|
return ini_get('session.serialize_handler'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @inheritDoc |
196
|
|
|
*/ |
197
|
|
|
public function getUseCookies(): string |
198
|
|
|
{ |
199
|
|
|
return ini_get('session.use_cookies'); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @inheritDoc |
204
|
|
|
*/ |
205
|
1 |
|
public function getSavePath(): string |
206
|
|
|
{ |
207
|
1 |
|
return ini_get('session.save_path'); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @inheritDoc |
212
|
|
|
*/ |
213
|
|
|
public function getSaveHandler(): string |
214
|
|
|
{ |
215
|
|
|
return ini_get('session.save_handler'); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @inheritDoc |
220
|
|
|
*/ |
221
|
1 |
|
public function setGcLifeTime(int $minutes): ConfigInterface |
222
|
|
|
{ |
223
|
1 |
|
$this->iniSet('session.gc_maxlifetime', 60 * $minutes); |
224
|
|
|
|
225
|
1 |
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @inheritDoc |
230
|
|
|
*/ |
231
|
1 |
|
public function getGcLifeTime(): int |
232
|
|
|
{ |
233
|
1 |
|
return (int)ini_get('session.gc_maxlifetime') / 60; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @inheritDoc |
238
|
|
|
*/ |
239
|
2 |
|
public function iniSet(string $setting, $value) |
240
|
|
|
{ |
241
|
2 |
|
$result = ini_set($setting, (string)$value); |
242
|
2 |
|
if ($result === false) { |
243
|
|
|
throw new RuntimeException(sprintf('Could not set `%s`', $setting)); |
244
|
|
|
} |
245
|
|
|
|
246
|
2 |
|
return $result; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|