1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pimf |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Pimf\Session; |
10
|
|
|
|
11
|
|
|
use Pimf\Config; |
12
|
|
|
use Pimf\Session; |
13
|
|
|
use Pimf\Util\Character; |
14
|
|
|
use Pimf\Cookie; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Using the session payload |
18
|
|
|
* |
19
|
|
|
* <code> |
20
|
|
|
* |
21
|
|
|
* // Get an item from the session |
22
|
|
|
* $name = Session::get('name'); |
23
|
|
|
* |
24
|
|
|
* // Return a default value if the item doesn't exist |
25
|
|
|
* $name = Session::get('name', 'Robin'); |
26
|
|
|
* |
27
|
|
|
* // Write an item to the session payload |
28
|
|
|
* Session::put('name', 'Robin'); |
29
|
|
|
* |
30
|
|
|
* // Write an item to the session payload's flash data |
31
|
|
|
* Session::flash('name', 'Robin'); |
32
|
|
|
* |
33
|
|
|
* // Keep the "name" item from expiring from the flash data |
34
|
|
|
* Session::keep('name'); |
35
|
|
|
* |
36
|
|
|
* // Keep the "name" and "email" items from expiring from the flash data |
37
|
|
|
* Session::keep(array('name', 'email')); |
38
|
|
|
* |
39
|
|
|
* </code> |
40
|
|
|
* |
41
|
|
|
* @package Session |
42
|
|
|
* @author Gjero Krsteski <[email protected]> |
43
|
|
|
*/ |
44
|
|
|
class Payload |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* The session array that is stored by the storage. |
48
|
|
|
* |
49
|
|
|
* @var array|null |
50
|
|
|
*/ |
51
|
|
|
public $session; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The session storage used to retrieve and store the session payload. |
55
|
|
|
* |
56
|
|
|
* @var \Pimf\Session\Storages\Storage |
57
|
|
|
*/ |
58
|
|
|
public $storage; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Indicates if the session already exists in storage. |
62
|
|
|
* |
63
|
|
|
* @var bool |
64
|
|
|
*/ |
65
|
|
|
public $exists = true; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param Storages\Storage $storage |
69
|
|
|
*/ |
70
|
|
|
public function __construct(\Pimf\Session\Storages\Storage $storage) |
71
|
|
|
{ |
72
|
|
|
$this->storage = $storage; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Load the session for the current request. |
77
|
|
|
* |
78
|
|
|
* @param null|string $key |
79
|
|
|
*/ |
80
|
|
|
public function load($key) |
81
|
|
|
{ |
82
|
|
|
if ($key !== null) { |
83
|
|
|
$this->session = $this->storage->load($key); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// If the session doesn't exist or is invalid. |
87
|
|
|
if (is_null($this->session) || static::expired($this->session)) { |
88
|
|
|
$this->exists = false; |
89
|
|
|
$this->session = $this->storage->fresh(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// A CSRF token is stored in every session to protect |
93
|
|
|
// the application from cross-site request |
94
|
|
|
if (!$this->has(Session::CSRF)) { |
95
|
|
|
$this->put(Session::CSRF, Character::random(40)); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Determine if the session payload instance is valid. |
101
|
|
|
* |
102
|
|
|
* @param array $session |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
protected static function expired($session) |
107
|
|
|
{ |
108
|
|
|
if (array_key_exists('last_activity', $session)) { |
109
|
|
|
return (time() - $session['last_activity']) > (Config::get('session.lifetime') * 60); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Determine if the session or flash data contains an item. |
117
|
|
|
* |
118
|
|
|
* @param string $key |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function has($key) |
123
|
|
|
{ |
124
|
|
|
return ($this->get($key) !== null); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get an item from the session. |
129
|
|
|
* |
130
|
|
|
* @param string $key |
131
|
|
|
* @param null $default |
132
|
|
|
* |
133
|
|
|
* @return mixed|null |
134
|
|
|
*/ |
135
|
|
|
public function get($key, $default = null) |
136
|
|
|
{ |
137
|
|
|
$session = $this->session['data']; |
138
|
|
|
|
139
|
|
|
// check first for the item in the general session data. |
140
|
|
|
if (null !== ($value = $this->isIn($key, $session))) { |
141
|
|
|
return $value; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// or find it in the new session data. |
145
|
|
|
if (null !== ($value = $this->isIn($key, $session[':new:']))) { |
146
|
|
|
return $value; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// or finally return the default value. |
150
|
|
|
if (null !== ($value = $this->isIn($key, $session[':old:']))) { |
151
|
|
|
return $value; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $default; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Checks if key is in session. |
159
|
|
|
* |
160
|
|
|
* @param string $key |
161
|
|
|
* @param array $session |
162
|
|
|
* |
163
|
|
|
* @return mixed|null |
164
|
|
|
*/ |
165
|
|
|
protected function isIn($key, array $session) |
166
|
|
|
{ |
167
|
|
|
if (array_key_exists($key, $session) && $session[$key] !== null) { |
168
|
|
|
return $session[$key]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return null; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Write an item to the session. |
176
|
|
|
* |
177
|
|
|
* @param $key |
178
|
|
|
* @param string $value |
179
|
|
|
*/ |
180
|
|
|
public function put($key, $value) |
181
|
|
|
{ |
182
|
|
|
$this->session['data'][$key] = $value; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Write an item to the session flash data. |
187
|
|
|
* |
188
|
|
|
* @param $key |
189
|
|
|
* @param $value |
190
|
|
|
*/ |
191
|
|
|
public function flash($key, $value) |
192
|
|
|
{ |
193
|
|
|
$this->session['data'][':new:'][$key] = $value; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Keep all of the session flash data from expiring after the request. |
198
|
|
|
* |
199
|
|
|
* @return void |
200
|
|
|
*/ |
201
|
|
|
public function reflash() |
202
|
|
|
{ |
203
|
|
|
$this->session['data'][':new:'] = array_merge( |
204
|
|
|
$this->session['data'][':new:'], $this->session['data'][':old:'] |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Keep a session flash item from expiring at the end of the request. |
210
|
|
|
* |
211
|
|
|
* @param $keys |
212
|
|
|
*/ |
213
|
|
|
public function keep($keys) |
214
|
|
|
{ |
215
|
|
|
foreach ((array)$keys as $key) { |
216
|
|
|
$this->flash($key, $this->get($key)); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Remove an item from the session data. |
222
|
|
|
* |
223
|
|
|
* @param $key |
224
|
|
|
*/ |
225
|
|
|
public function forget($key) |
226
|
|
|
{ |
227
|
|
|
unset($this->session['data'][$key]); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Remove all of the items from the session (CSRF token will not be removed). |
232
|
|
|
*/ |
233
|
|
|
public function flush() |
234
|
|
|
{ |
235
|
|
|
$this->session['data'] = array(Session::CSRF => $this->token(), ':new:' => array(), ':old:' => array()); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Assign a new, random ID to the session. |
240
|
|
|
*/ |
241
|
|
|
public function regenerate() |
242
|
|
|
{ |
243
|
|
|
$this->session['id'] = $this->storage->id(); |
244
|
|
|
$this->exists = false; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Get the CSRF token that is stored in the session data. |
249
|
|
|
* |
250
|
|
|
* @return string |
251
|
|
|
*/ |
252
|
|
|
public function token() |
253
|
|
|
{ |
254
|
|
|
return $this->get(Session::CSRF); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Get the last activity for the session. |
259
|
|
|
* |
260
|
|
|
* @return int |
261
|
|
|
*/ |
262
|
|
|
public function activity() |
263
|
|
|
{ |
264
|
|
|
return $this->session['last_activity']; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Store the session payload in storage. |
269
|
|
|
* This method will be called automatically at the end of the request. |
270
|
|
|
* |
271
|
|
|
* @return void |
272
|
|
|
*/ |
273
|
|
|
public function save() |
274
|
|
|
{ |
275
|
|
|
$this->session['last_activity'] = time(); |
276
|
|
|
|
277
|
|
|
// age it that should expire at the end of the user's next request. |
278
|
|
|
$this->age(); |
279
|
|
|
|
280
|
|
|
$sessionConf = Config::get('session'); |
281
|
|
|
|
282
|
|
|
// save data into the specialized storage. |
283
|
|
|
$this->storage->save($this->session, $sessionConf, $this->exists); |
284
|
|
|
|
285
|
|
|
// determine the owner of the session |
286
|
|
|
// on the user's subsequent requests to the application. |
287
|
|
|
$this->cookie($sessionConf); |
288
|
|
|
|
289
|
|
|
// calculate and run garbage collection cleaning. |
290
|
|
|
$cleaning = $sessionConf['garbage_collection']; |
291
|
|
|
|
292
|
|
|
if (mt_rand(1, $cleaning[1]) <= $cleaning[0]) { |
293
|
|
|
$this->clean(); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Clean up expired sessions. |
299
|
|
|
* |
300
|
|
|
* @return void |
301
|
|
|
*/ |
302
|
|
|
public function clean() |
303
|
|
|
{ |
304
|
|
|
if ($this->storage instanceof \Pimf\Contracts\Cleanable) { |
305
|
|
|
$this->storage->clean(time() - (Config::get('session.lifetime') * 60)); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Age the session flash data. |
311
|
|
|
* |
312
|
|
|
* @return void |
313
|
|
|
*/ |
314
|
|
|
protected function age() |
315
|
|
|
{ |
316
|
|
|
$this->session['data'][':old:'] = $this->session['data'][':new:']; |
317
|
|
|
$this->session['data'][':new:'] = array(); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Send the session ID cookie to the browser. |
322
|
|
|
* |
323
|
|
|
* @param array $config |
324
|
|
|
* |
325
|
|
|
* @return void |
326
|
|
|
*/ |
327
|
|
|
protected function cookie($config) |
328
|
|
|
{ |
329
|
|
|
$minutes = (!$config['expire_on_close']) ? $config['lifetime'] : 0; |
330
|
|
|
|
331
|
|
|
Cookie::put($config['cookie'], $this->session['id'], $minutes, $config['path'], $config['domain'], |
332
|
|
|
$config['secure']); |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|