1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\utils\http; |
4
|
|
|
|
5
|
|
|
use Ubiquity\utils\base\UString; |
6
|
|
|
use Ubiquity\utils\http\session\SessionObject; |
7
|
|
|
use Ubiquity\controllers\Startup; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Http Session utilities |
11
|
|
|
* This class is part of Ubiquity |
12
|
|
|
* |
13
|
|
|
* @author jcheron <[email protected]> |
14
|
|
|
* @version 1.1.4 |
15
|
|
|
*/ |
16
|
|
|
class USession { |
17
|
|
|
protected static $sessionInstance; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Returns an array stored in session variable as $arrayKey |
21
|
|
|
* |
22
|
|
|
* @param string $arrayKey the key of the array to return |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
6 |
|
public static function getArray(string $arrayKey): array { |
26
|
6 |
|
if (self::$sessionInstance->exists ( $arrayKey )) { |
27
|
1 |
|
$array = self::$sessionInstance->get ( $arrayKey ); |
28
|
1 |
|
if (! \is_array ( $array )) |
29
|
1 |
|
$array = [ ]; |
30
|
|
|
} else |
31
|
5 |
|
$array = [ ]; |
32
|
6 |
|
return $array; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Adds or removes a value from an array in session |
37
|
|
|
* |
38
|
|
|
* @param string $arrayKey the key of the array to add or remove in |
39
|
|
|
* @param mixed $value the value to add |
40
|
|
|
* @param boolean|null $add If true, adds otherwise removes |
41
|
|
|
* @return boolean|null |
42
|
|
|
*/ |
43
|
6 |
|
public static function addOrRemoveValueFromArray(string $arrayKey, $value, $add = null): ?bool { |
44
|
6 |
|
$array = self::getArray ( $arrayKey ); |
45
|
6 |
|
$_SESSION [$arrayKey] = $array; |
46
|
6 |
|
$search = \array_search ( $value, $array ); |
47
|
6 |
|
if ($search === false && $add) { |
48
|
6 |
|
$array [] = $value; |
49
|
6 |
|
self::$sessionInstance->set ( $arrayKey, $array ); |
50
|
6 |
|
return true; |
51
|
|
|
} else if ($add !== true) { |
52
|
|
|
unset ( $array [$search] ); |
53
|
|
|
self::$sessionInstance->set ( $arrayKey, $array ); |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
return null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Removes a value from an array in session |
61
|
|
|
* |
62
|
|
|
* @param string $arrayKey the key of the array to remove in |
63
|
|
|
* @param mixed $value the value to remove |
64
|
|
|
* @return boolean|null |
65
|
|
|
*/ |
66
|
|
|
public static function removeValueFromArray(string $arrayKey, $value): ?bool { |
67
|
|
|
return self::addOrRemoveValueFromArray ( $arrayKey, $value, false ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Adds a value from an array in session |
72
|
|
|
* |
73
|
|
|
* @param string $arrayKey the key of the array to add in |
74
|
|
|
* @param mixed $value the value to add |
75
|
|
|
* @return boolean|null |
76
|
|
|
*/ |
77
|
|
|
public static function addValueToArray(string $arrayKey, $value): ?bool { |
78
|
|
|
return self::addOrRemoveValueFromArray ( $arrayKey, $value, true ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Sets a boolean value at key position in session |
83
|
|
|
* |
84
|
|
|
* @param string $key the key to add or set in |
85
|
|
|
* @param mixed $value the value to set |
86
|
|
|
* @return boolean |
87
|
|
|
*/ |
88
|
|
|
public static function setBoolean(string $key, $value) { |
89
|
|
|
return self::$sessionInstance->set ( $key, UString::isBooleanTrue ( $value ) ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns a boolean stored at the key position in session |
94
|
|
|
* |
95
|
|
|
* @param string $key the key to add or set |
96
|
|
|
* @return boolean |
97
|
|
|
*/ |
98
|
|
|
public static function getBoolean(string $key): bool { |
99
|
|
|
$v = self::$sessionInstance->get ( $key, false ); |
100
|
|
|
return UString::isBooleanTrue ( $v ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns the value stored at the key position in session |
105
|
|
|
* |
106
|
|
|
* @param string $key the key to retreive |
107
|
|
|
* @param mixed $default the default value to return if the key does not exists in session |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
1 |
|
public static function session(string $key, $default = NULL) { |
111
|
1 |
|
return self::$sessionInstance->get ( $key, $default ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns the value stored at the key position in session |
116
|
|
|
* |
117
|
|
|
* @param string $key the key to retreive |
118
|
|
|
* @param mixed $default the default value to return if the key does not exists in session |
119
|
|
|
* @return mixed |
120
|
|
|
*/ |
121
|
12 |
|
public static function get(string $key, $default = NULL) { |
122
|
12 |
|
return self::$sessionInstance->get ( $key, $default ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Adds or sets a value to the Session at position $key |
127
|
|
|
* |
128
|
|
|
* @param string $key the key to add or set |
129
|
|
|
* @param mixed $value |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
11 |
|
public static function set(string $key, $value) { |
133
|
11 |
|
return self::$sessionInstance->set ( $key, $value ); |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
public static function setTmp(string $key, $value, $duration) { |
137
|
1 |
|
if (self::$sessionInstance->exists ( $key )) { |
138
|
|
|
$object = self::$sessionInstance->get ( $key ); |
139
|
|
|
if ($object instanceof SessionObject) { |
140
|
|
|
return $object->setValue ( $value ); |
141
|
|
|
} |
142
|
|
|
} |
143
|
1 |
|
$object = new SessionObject ( $value, $duration ); |
144
|
1 |
|
return self::$sessionInstance->set ( $key, $object ); |
145
|
|
|
} |
146
|
|
|
|
147
|
1 |
|
public static function getTmp(string $key, $default = null) { |
148
|
1 |
|
if (self::$sessionInstance->exists ( $key )) { |
149
|
1 |
|
$object = self::$sessionInstance->get ( $key ); |
150
|
1 |
|
if ($object instanceof SessionObject) { |
151
|
1 |
|
$value = $object->getValue (); |
152
|
1 |
|
if (isset ( $value )) { |
153
|
1 |
|
return $object->getValue(); |
154
|
|
|
} |
155
|
|
|
else { |
156
|
1 |
|
self::delete ( $key ); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
1 |
|
return $default; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public static function getTimeout(string $key) { |
164
|
|
|
if (self::$sessionInstance->exists ( $key )) { |
165
|
|
|
$object = self::$sessionInstance->get ( $key ); |
166
|
|
|
if ($object instanceof SessionObject) { |
167
|
|
|
$value = $object->getTimeout (); |
168
|
|
|
if ($value < 0) { |
169
|
|
|
return 0; |
170
|
|
|
} else { |
171
|
|
|
return $value; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
return; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Deletes the key in Session |
180
|
|
|
* |
181
|
|
|
* @param string $key the key to delete |
182
|
|
|
*/ |
183
|
3 |
|
public static function delete(string $key): void { |
184
|
3 |
|
self::$sessionInstance->delete ( $key ); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Increment the value at the key index in session |
189
|
|
|
* |
190
|
|
|
* @param string $key |
191
|
|
|
* @param int $inc |
192
|
|
|
* @return number |
193
|
|
|
*/ |
194
|
1 |
|
public static function inc(string $key, int $inc = 1) { |
195
|
1 |
|
return self::set ( $key, self::get ( $key, 0 ) + $inc ); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Decrement the value at the key index in session |
200
|
|
|
* |
201
|
|
|
* @param string $key |
202
|
|
|
* @param int $dec |
203
|
|
|
* @return number |
204
|
|
|
*/ |
205
|
1 |
|
public static function dec(string $key, int $dec = 1) { |
206
|
1 |
|
return self::set ( $key, self::get ( $key, 0 ) - $dec ); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Adds a string at the end of the value at the key index in session |
211
|
|
|
* |
212
|
|
|
* @param string $key |
213
|
|
|
* @param string $str |
214
|
|
|
* @param ?string $default |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
public static function concat(string $key, string $str, string $default = NULL): string { |
218
|
|
|
return self::set ( $key, self::get ( $key, $default ) . $str ); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Applies a callback function to the value at the key index in session |
223
|
|
|
* |
224
|
|
|
* @param string $key |
225
|
|
|
* @param string|callable $callback |
226
|
|
|
* @param null $default |
|
|
|
|
227
|
|
|
* @return mixed |
228
|
|
|
*/ |
229
|
1 |
|
public static function apply(string $key, $callback, $default = NULL) { |
230
|
1 |
|
$value = self::get ( $key, $default ); |
231
|
1 |
|
if (\is_string ( $callback ) && \function_exists ( $callback )) { |
232
|
1 |
|
$value = \call_user_func ( $callback, $value ); |
233
|
1 |
|
} elseif ($callback instanceof \Closure) { |
234
|
1 |
|
$value = $callback ( $value ); |
235
|
|
|
} else { |
236
|
|
|
return $value; |
237
|
|
|
} |
238
|
1 |
|
return self::set ( $key, $value ); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Apply a user supplied function to every member of Session array |
243
|
|
|
* |
244
|
|
|
* @param callable $callback |
245
|
|
|
* @param mixed $userData |
246
|
|
|
* @return array |
247
|
|
|
*/ |
248
|
|
|
public static function Walk($callback, $userData = null): array { |
249
|
|
|
$all = self::$sessionInstance->getAll (); |
250
|
|
|
foreach ( $all as $k => $v ) { |
251
|
|
|
self::$sessionInstance->set ( $k, $callback ( $k, $v, $userData ) ); |
252
|
|
|
} |
253
|
|
|
return self::$sessionInstance->getAll (); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Replaces elements from Session array with $keyAndValues |
258
|
|
|
* |
259
|
|
|
* @param array $keyAndValues |
260
|
|
|
* @return array |
261
|
|
|
*/ |
262
|
|
|
public static function replace(array $keyAndValues): array { |
263
|
|
|
foreach ( $keyAndValues as $k => $v ) { |
264
|
|
|
self::$sessionInstance->set ( $k, $v ); |
265
|
|
|
} |
266
|
|
|
return self::$sessionInstance->getAll (); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Returns the associative array of session vars |
271
|
|
|
* |
272
|
|
|
* @return array |
273
|
|
|
*/ |
274
|
|
|
public static function getAll(): array { |
275
|
|
|
return self::$sessionInstance->getAll (); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Start new or resume existing session |
280
|
|
|
* |
281
|
|
|
* @param string|null $name the name of the session |
282
|
|
|
*/ |
283
|
70 |
|
public static function start($name = null): void { |
284
|
70 |
|
if (! isset ( self::$sessionInstance )) { |
285
|
40 |
|
self::$sessionInstance = Startup::getSessionInstance (); |
286
|
|
|
} |
287
|
70 |
|
self::$sessionInstance->start ( $name ); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Returns true if the session is started |
292
|
|
|
* |
293
|
|
|
* @return boolean |
294
|
|
|
*/ |
295
|
|
|
public static function isStarted(): bool { |
296
|
|
|
return self::$sessionInstance->isStarted (); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Returns true if the key exists in Session |
301
|
|
|
* |
302
|
|
|
* @param string $key the key to test |
303
|
|
|
* @return boolean |
304
|
|
|
*/ |
305
|
14 |
|
public static function exists(string $key): bool { |
306
|
14 |
|
return self::$sessionInstance->exists ( $key ); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Initialize the key in Session if key does not exists |
311
|
|
|
* |
312
|
|
|
* @param string $key |
313
|
|
|
* @param mixed $value |
314
|
|
|
* @return mixed |
315
|
|
|
*/ |
316
|
1 |
|
public static function init(string $key, $value) { |
317
|
1 |
|
if (! self::$sessionInstance->exists ( $key )) { |
318
|
1 |
|
self::$sessionInstance->set ( $key, $value ); |
319
|
|
|
} |
320
|
1 |
|
return self::$sessionInstance->get ( $key ); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Terminates the active session |
325
|
|
|
*/ |
326
|
3 |
|
public static function terminate(): void { |
327
|
3 |
|
self::$sessionInstance->terminate (); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Return the Csrf protection class name. |
332
|
|
|
* |
333
|
|
|
* @return string |
334
|
|
|
*/ |
335
|
|
|
public static function getCsrfProtectionClass() { |
336
|
|
|
if (isset ( self::$sessionInstance )) { |
337
|
|
|
return \get_class ( self::$sessionInstance->getVerifyCsrf () ); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Return the instance class name for the session. |
343
|
|
|
* |
344
|
|
|
* @return string |
345
|
|
|
*/ |
346
|
|
|
public static function getInstanceClass() { |
347
|
|
|
if (isset ( self::$sessionInstance )) { |
348
|
|
|
return \get_class ( self::$sessionInstance ); |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Returns the number of sessions started. |
354
|
|
|
* |
355
|
|
|
* @return number |
356
|
|
|
*/ |
357
|
|
|
public static function visitorCount() { |
358
|
|
|
return self::$sessionInstance->visitorCount (); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Re-generates the session id. |
363
|
|
|
* @param boolean $deleteOldSession if true, deletes the old session |
364
|
|
|
* @return bool |
365
|
|
|
*/ |
366
|
|
|
public static function regenerateId(bool $deleteOldSession=false):bool { |
367
|
|
|
return self::$sessionInstance->regenerateId($deleteOldSession); |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
|