1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Session |
5
|
|
|
* |
6
|
|
|
* Platine Session is the lightweight implementation of php native |
7
|
|
|
* session handler interface |
8
|
|
|
* |
9
|
|
|
* This content is released under the MIT License (MIT) |
10
|
|
|
* |
11
|
|
|
* Copyright (c) 2020 Platine Session |
12
|
|
|
* |
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
15
|
|
|
* in the Software without restriction, including without limitation the rights |
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
18
|
|
|
* furnished to do so, subject to the following conditions: |
19
|
|
|
* |
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
21
|
|
|
* copies or substantial portions of the Software. |
22
|
|
|
* |
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
29
|
|
|
* SOFTWARE. |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @file Session.php |
34
|
|
|
* |
35
|
|
|
* The Session class used to manage the session |
36
|
|
|
* |
37
|
|
|
* @package Platine\Session |
38
|
|
|
* @author Platine Developers Team |
39
|
|
|
* @copyright Copyright (c) 2020 |
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
41
|
|
|
* @link https://www.platine-php.com |
42
|
|
|
* @version 1.0.0 |
43
|
|
|
* @filesource |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
declare(strict_types=1); |
47
|
|
|
|
48
|
|
|
namespace Platine\Session; |
49
|
|
|
|
50
|
|
|
use Platine\Session\Configuration; |
51
|
|
|
use Platine\Session\Storage\NullStorage; |
52
|
|
|
use Platine\Stdlib\Helper\Arr; |
53
|
|
|
use SessionHandlerInterface; |
54
|
|
|
|
55
|
|
|
class Session |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* The session driver to use |
59
|
|
|
* @var SessionHandlerInterface |
60
|
|
|
*/ |
61
|
|
|
protected SessionHandlerInterface $handler; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The configuration instance |
65
|
|
|
* @var Configuration |
66
|
|
|
*/ |
67
|
|
|
protected Configuration $config; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Create new Session instance |
71
|
|
|
* @param SessionHandlerInterface|null $handler the handler to use |
72
|
|
|
* @param Configuration|null $config the configuration to use |
73
|
|
|
*/ |
74
|
|
|
public function __construct( |
75
|
|
|
?SessionHandlerInterface $handler = null, |
76
|
|
|
?Configuration $config = null |
77
|
|
|
) { |
78
|
|
|
$this->config = $config ?? new Configuration([]); |
79
|
|
|
$this->handler = $handler ?? new NullStorage($config); |
80
|
|
|
|
81
|
|
|
if ((session_status() !== PHP_SESSION_ACTIVE)) { |
82
|
|
|
$this->init(); |
83
|
|
|
session_set_save_handler($this->handler, true); |
84
|
|
|
|
85
|
|
|
//now start the session |
86
|
|
|
session_start(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Return the instance of session handler |
92
|
|
|
* @return SessionHandlerInterface |
93
|
|
|
*/ |
94
|
|
|
public function getHandler(): SessionHandlerInterface |
95
|
|
|
{ |
96
|
|
|
return $this->handler; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Check whether the session data for given key exists |
101
|
|
|
* @param string $key |
102
|
|
|
* @return boolean |
103
|
|
|
*/ |
104
|
|
|
public function has(string $key): bool |
105
|
|
|
{ |
106
|
|
|
return Arr::has($_SESSION, $key); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set the session data |
111
|
|
|
* @param string $key the key name |
112
|
|
|
* @param mixed $value the session data value |
113
|
|
|
*/ |
114
|
|
|
public function set(string $key, mixed $value): void |
115
|
|
|
{ |
116
|
|
|
Arr::set($_SESSION, $key, $value); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the session data |
121
|
|
|
* @param string $key the key name |
122
|
|
|
* @param mixed $default the default value to return if can |
123
|
|
|
* not find session data |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
|
|
public function get(string $key, mixed $default = null): mixed |
127
|
|
|
{ |
128
|
|
|
return Arr::get($_SESSION, $key, $default); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Return all session data |
133
|
|
|
* @param bool $includeFlash whether to include flash data |
134
|
|
|
* @return array<string, mixed> |
135
|
|
|
*/ |
136
|
|
|
public function all(bool $includeFlash = false): array |
137
|
|
|
{ |
138
|
|
|
$session = $_SESSION; |
139
|
|
|
$flashKey = $this->config->get('flash_key'); |
140
|
|
|
if (!$includeFlash) { |
141
|
|
|
if (array_key_exists($flashKey, $session)) { |
142
|
|
|
unset($session[$flashKey]); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $session; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Remove the session data for the given key |
151
|
|
|
* @param string $key the key name |
152
|
|
|
* |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
|
public function remove(string $key): bool |
156
|
|
|
{ |
157
|
|
|
Arr::forget($_SESSION, $key); |
158
|
|
|
|
159
|
|
|
return true; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Check whether the session flash data for given key exists |
164
|
|
|
* @param string $key |
165
|
|
|
* @return boolean |
166
|
|
|
*/ |
167
|
|
|
public function hasFlash(string $key): bool |
168
|
|
|
{ |
169
|
|
|
$flashKey = $this->config->get('flash_key'); |
170
|
|
|
$name = sprintf('%s.%s', $flashKey, $key); |
171
|
|
|
|
172
|
|
|
return $this->has($name); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get the session flash data |
177
|
|
|
* @param string $key the key name |
178
|
|
|
* @param mixed $default the default value to return if can |
179
|
|
|
* not find session data |
180
|
|
|
* @return mixed |
181
|
|
|
*/ |
182
|
|
|
public function getFlash(string $key, mixed $default = null): mixed |
183
|
|
|
{ |
184
|
|
|
$flashKey = $this->config->get('flash_key'); |
185
|
|
|
$name = sprintf('%s.%s', $flashKey, $key); |
186
|
|
|
|
187
|
|
|
$value = $default; |
188
|
|
|
if ($this->has($name)) { |
189
|
|
|
$value = $this->get($name); |
190
|
|
|
$this->removeFlash($key); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $value; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Set the session flash data |
198
|
|
|
* @param string $key the key name |
199
|
|
|
* @param mixed $value the session data value |
200
|
|
|
*/ |
201
|
|
|
public function setFlash(string $key, mixed $value): void |
202
|
|
|
{ |
203
|
|
|
$flashKey = $this->config->get('flash_key'); |
204
|
|
|
$name = sprintf('%s.%s', $flashKey, $key); |
205
|
|
|
|
206
|
|
|
$this->set($name, $value); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Remove the session data for the given key |
211
|
|
|
* @param string $key the key name |
212
|
|
|
* |
213
|
|
|
* @return bool |
214
|
|
|
*/ |
215
|
|
|
public function removeFlash(string $key): bool |
216
|
|
|
{ |
217
|
|
|
$flashKey = $this->config->get('flash_key'); |
218
|
|
|
$name = sprintf('%s.%s', $flashKey, $key); |
219
|
|
|
$this->remove($name); |
220
|
|
|
|
221
|
|
|
return true; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Set the session information |
226
|
|
|
* @return void |
227
|
|
|
*/ |
228
|
|
|
protected function init(): void |
229
|
|
|
{ |
230
|
|
|
$sessionName = $this->config->get('name'); |
231
|
|
|
if ($sessionName) { |
232
|
|
|
session_name($sessionName); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$ttl = (int)$this->config->get('ttl'); |
236
|
|
|
$lifetime = (int)$this->config->get('cookie.lifetime'); |
237
|
|
|
$path = $this->config->get('cookie.path'); |
238
|
|
|
$domain = $this->config->get('cookie.domain'); |
239
|
|
|
$secure = $this->config->get('cookie.secure'); |
240
|
|
|
|
241
|
|
|
session_set_cookie_params([ |
242
|
|
|
'lifetime' => $lifetime, |
243
|
|
|
'path' => $path, |
244
|
|
|
'domain' => $domain, |
245
|
|
|
'secure' => $secure, |
246
|
|
|
'httponly' => true, // for security to prevent access to cookie via javascript or XSS attack |
247
|
|
|
'samesite' => 'Lax' |
248
|
|
|
]); |
249
|
|
|
|
250
|
|
|
//to prevent attack of Session Fixation |
251
|
|
|
//thank to https://www.phparch.com/2018/01/php-sessions-in-depth/ |
252
|
|
|
ini_set('session.use_strict_mode', '1'); |
253
|
|
|
ini_set('session.use_only_cookies', '1'); |
254
|
|
|
ini_set('session.use_trans_sid', '0'); |
255
|
|
|
ini_set('session.gc_maxlifetime', (string) $ttl); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|