|
1
|
|
|
<?php |
|
2
|
|
|
/* For licensing terms, see /license.txt */ |
|
3
|
|
|
|
|
4
|
|
|
use Chamilo\CoreBundle\Framework\Container; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @todo replace all $_SESSION calls with this class. |
|
8
|
|
|
*/ |
|
9
|
|
|
class ChamiloSession implements \ArrayAccess |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @param string $variable |
|
13
|
|
|
* @param null $default |
|
|
|
|
|
|
14
|
|
|
* @return mixed|null |
|
15
|
|
|
*/ |
|
16
|
|
|
public static function read($variable, $default = null) |
|
17
|
|
|
{ |
|
18
|
|
|
$session = Container::getSession(); |
|
19
|
|
|
$result = null; |
|
20
|
|
|
if (isset($session)) { |
|
21
|
|
|
$result = $session->get($variable); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
// Check if the value exists in the $_SESSION array |
|
25
|
|
|
if (empty($result)) { |
|
26
|
|
|
if (isset($_SESSION[$variable])) { |
|
27
|
|
|
return $_SESSION[$variable]; |
|
28
|
|
|
} |
|
29
|
|
|
return $default; |
|
30
|
|
|
} else { |
|
31
|
|
|
return $result; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $variable |
|
37
|
|
|
* @param mixed $value |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function write($variable, $value) |
|
40
|
|
|
{ |
|
41
|
|
|
//$_SESSION[$variable] = $value; |
|
42
|
|
|
$session = Container::getSession(); |
|
43
|
|
|
// Writing the session in 2 instances because |
|
44
|
|
|
$_SESSION[$variable] = $value; |
|
45
|
|
|
$session->set($variable, $value); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $variable |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function erase($variable) |
|
52
|
|
|
{ |
|
53
|
|
|
$variable = (string) $variable; |
|
54
|
|
|
$session = Container::getSession(); |
|
55
|
|
|
$session->remove($variable); |
|
56
|
|
|
|
|
57
|
|
|
if (isset($GLOBALS[$variable])) { |
|
58
|
|
|
unset($GLOBALS[$variable]); |
|
59
|
|
|
} |
|
60
|
|
|
if (isset($_SESSION[$variable])) { |
|
61
|
|
|
unset($_SESSION[$variable]); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns true if session has variable set up, false otherwise. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $variable |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function has($variable) |
|
73
|
|
|
{ |
|
74
|
|
|
return isset($_SESSION[$variable]); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Clear |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function clear() |
|
81
|
|
|
{ |
|
82
|
|
|
$session = Container::getSession(); |
|
83
|
|
|
$session->clear(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Destroy |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function destroy() |
|
90
|
|
|
{ |
|
91
|
|
|
$session = Container::getSession(); |
|
92
|
|
|
$session->invalidate(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/* |
|
96
|
|
|
* ArrayAccess |
|
97
|
|
|
*/ |
|
98
|
|
|
public function offsetExists($offset) |
|
99
|
|
|
{ |
|
100
|
|
|
return isset($_SESSION[$offset]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* It it exists returns the value stored at the specified offset. |
|
105
|
|
|
* If offset does not exists returns null. Do not trigger a warning. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $offset |
|
108
|
|
|
* @return any |
|
109
|
|
|
*/ |
|
110
|
|
|
public function offsetGet($offset) |
|
111
|
|
|
{ |
|
112
|
|
|
return self::read($offset); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function offsetSet($offset, $value) |
|
116
|
|
|
{ |
|
117
|
|
|
self::write($offset, $value); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function offsetUnset($offset) |
|
121
|
|
|
{ |
|
122
|
|
|
unset($_SESSION[$offset]); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $name |
|
127
|
|
|
*/ |
|
128
|
|
|
public function __unset($name) |
|
129
|
|
|
{ |
|
130
|
|
|
unset($_SESSION[$name]); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param string $name |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
|
|
public function __isset($name) |
|
138
|
|
|
{ |
|
139
|
|
|
return self::has($name); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* It it exists returns the value stored at the specified offset. |
|
144
|
|
|
* If offset does not exists returns null. Do not trigger a warning. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $name |
|
147
|
|
|
* |
|
148
|
|
|
* @return mixed |
|
149
|
|
|
* |
|
150
|
|
|
*/ |
|
151
|
|
|
public function __get($name) |
|
152
|
|
|
{ |
|
153
|
|
|
return self::read($name); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* |
|
158
|
|
|
* @param string $name |
|
159
|
|
|
* @param mixed $value |
|
160
|
|
|
*/ |
|
161
|
|
|
public function __set($name, $value) |
|
162
|
|
|
{ |
|
163
|
|
|
self::write($name, $value); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|