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