|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\utils; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Session utilities |
|
7
|
|
|
* @author jc |
|
8
|
|
|
* @version 1.0.0.2 |
|
9
|
|
|
*/ |
|
10
|
|
|
class SessionUtils { |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Retourne un tableau stocké en variable de session sous le nom $arrayKey |
|
14
|
|
|
* @param string $arrayKey |
|
15
|
|
|
* @return array |
|
16
|
|
|
*/ |
|
17
|
|
|
public static function getArray($arrayKey) { |
|
18
|
|
|
if (isset($_SESSION[$arrayKey])) { |
|
19
|
|
|
$array=$_SESSION[$arrayKey]; |
|
20
|
|
|
if (!is_array($array)) |
|
21
|
|
|
$array=array (); |
|
22
|
|
|
} else |
|
23
|
|
|
$array=array (); |
|
24
|
|
|
return $array; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public static function addOrRemoveValueFromArray($arrayKey, $value, $add=true) { |
|
28
|
|
|
$array=self::getArray($arrayKey); |
|
29
|
|
|
$_SESSION[$arrayKey]=$array; |
|
30
|
|
|
$search=array_search($value, $array); |
|
31
|
|
|
if ($search === FALSE && $add) { |
|
32
|
|
|
$_SESSION[$arrayKey][]=$value; |
|
33
|
|
|
return true; |
|
34
|
|
|
} else { |
|
35
|
|
|
unset($_SESSION[$arrayKey][$search]); |
|
36
|
|
|
$_SESSION[$arrayKey]=array_values($_SESSION[$arrayKey]); |
|
37
|
|
|
return false; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public static function removeValueFromArray($arrayKey, $value) { |
|
42
|
|
|
return self::addOrRemoveValueFromArray($arrayKey, $value, false); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function checkBoolean($key) { |
|
46
|
|
|
$_SESSION[$key]=!self::getBoolean($key); |
|
47
|
|
|
return $_SESSION[$key]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public static function getBoolean($key) { |
|
51
|
|
|
$ret=false; |
|
52
|
|
|
if (isset($_SESSION[$key])) { |
|
53
|
|
|
$ret=$_SESSION[$key]; |
|
54
|
|
|
} |
|
55
|
|
|
return $ret; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function session($key, $default=NULL) { |
|
59
|
|
|
return isset($_SESSION[$key]) ? $_SESSION[$key] : $default; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public static function get($key, $default=NULL) { |
|
|
|
|
|
|
63
|
|
|
return isset($_SESSION[$key]) ? $_SESSION[$key] : $default; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public static function set($key, $value) { |
|
|
|
|
|
|
67
|
|
|
$_SESSION[$key]=$value; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public static function delete($key) { |
|
71
|
|
|
unset($_SESSION[$key]); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public static function start($name=null){ |
|
|
|
|
|
|
75
|
|
|
if(isset($name)) \session_name(); |
|
76
|
|
|
if (!isset($_SESSION)) { \session_start(); } |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public static function isStarted(){ |
|
|
|
|
|
|
80
|
|
|
return isset($_SESSION); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public static function terminate(){ |
|
|
|
|
|
|
84
|
|
|
if(!self::isStarted()) |
|
85
|
|
|
return; |
|
86
|
|
|
$_SESSION = array(); |
|
87
|
|
|
|
|
88
|
|
|
if (\ini_get("session.use_cookies")) { |
|
89
|
|
|
$params = \session_get_cookie_params(); |
|
90
|
|
|
\setcookie(\session_name(), '', \time() - 42000, |
|
91
|
|
|
$params["path"], $params["domain"], |
|
92
|
|
|
$params["secure"], $params["httponly"] |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
\session_destroy(); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: