1 | <?php |
||
30 | class Manager implements AttributeInterface |
||
31 | { |
||
32 | /** |
||
33 | * @var \Xoops |
||
34 | */ |
||
35 | protected $xoops = null; |
||
36 | |||
37 | /** |
||
38 | * @var \Xoops\Core\HttpRequest |
||
39 | */ |
||
40 | protected $httpRequest = null; |
||
41 | |||
42 | /** |
||
43 | * @var Fingerprint fingerprint object |
||
44 | */ |
||
45 | protected $fingerprint = null; |
||
46 | |||
47 | /** |
||
48 | * @var SessionUser session user object |
||
49 | */ |
||
50 | protected $sessionUser = null; |
||
51 | |||
52 | /** |
||
53 | * establish access to other classes we will use |
||
54 | */ |
||
55 | 2 | public function __construct() |
|
62 | |||
63 | /** |
||
64 | * Configure and start the session |
||
65 | * |
||
66 | * @return void |
||
67 | */ |
||
68 | public function sessionStart() |
||
131 | |||
132 | /** |
||
133 | * Clear the current session and reset fingerprint |
||
134 | * |
||
135 | * @return void |
||
136 | */ |
||
137 | public function clearSession() |
||
143 | |||
144 | /** |
||
145 | * Expire the current session and replace with a fresh one. |
||
146 | * |
||
147 | * @return void |
||
148 | */ |
||
149 | public function expireSession() |
||
150 | { |
||
151 | // If this session is obsolete it means there already is a new id |
||
152 | if ($this->has('SESSION_MANAGER_OBSOLETE')) { |
||
153 | return; |
||
154 | } |
||
155 | |||
156 | // Set current session to expire in 10 seconds |
||
157 | $this->set('SESSION_MANAGER_OBSOLETE', true); |
||
158 | $this->set('SESSION_MANAGER_EXPIRES', time() + 10); |
||
159 | |||
160 | // Grab current session ID and close it |
||
161 | //$sessionId = session_id(); |
||
162 | //session_write_close(); |
||
163 | |||
164 | // reopen the old session |
||
165 | //session_id($sessionId); |
||
166 | //session_start(); |
||
167 | |||
168 | // Create new session without destroying the old one |
||
169 | session_regenerate_id(false); |
||
170 | |||
171 | // Now we unset the obsolete and expiration values since we ant to keep this one |
||
172 | $this->remove('SESSION_MANAGER_OBSOLETE'); |
||
173 | $this->remove('SESSION_MANAGER_EXPIRES'); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Generate a new id and delete the old session. |
||
178 | * |
||
179 | * This should be called whenever permission levels for a user change. |
||
180 | * |
||
181 | * @return void |
||
182 | */ |
||
183 | public function regenerateSession() |
||
187 | |||
188 | /** |
||
189 | * Validate that the session has not expired. |
||
190 | * |
||
191 | * @return boolean true is session is valid and not expired, otherwise false |
||
192 | */ |
||
193 | protected function validateSession() |
||
207 | |||
208 | /** |
||
209 | * Get the user object used by this session. |
||
210 | * |
||
211 | * @return SessionUser |
||
212 | */ |
||
213 | public function user() |
||
217 | |||
218 | /** |
||
219 | * shutdown function |
||
220 | */ |
||
221 | public function sessionShutdown() |
||
226 | |||
227 | // access session variables as attribute object |
||
228 | |||
229 | /** |
||
230 | * Retrieve a session variable value. |
||
231 | * |
||
232 | * @param string $name Name of an session variable |
||
233 | * @param mixed $default A default value returned if the requested |
||
234 | * named session variable is not set. |
||
235 | * |
||
236 | * @return mixed The value of the session variable, or $default if not set. |
||
237 | */ |
||
238 | public function get($name, $default = null) |
||
242 | |||
243 | /** |
||
244 | * Set an attribute value. |
||
245 | * |
||
246 | * @param string $name Name of the attribute option |
||
247 | * @param mixed $value Value of the attribute option |
||
248 | * |
||
249 | * @return $this |
||
250 | */ |
||
251 | public function set($name, $value) |
||
252 | { |
||
253 | $_SESSION[$name] = $value; |
||
254 | return $this; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Determine if an attribute exists. |
||
259 | * |
||
260 | * @param string $name An attribute name. |
||
261 | * |
||
262 | * @return boolean TRUE if the given attribute exists, otherwise FALSE. |
||
263 | */ |
||
264 | public function has($name) |
||
268 | |||
269 | /** |
||
270 | * Remove an attribute. |
||
271 | * |
||
272 | * @param string $name An attribute name. |
||
273 | * |
||
274 | * @return mixed An attribute value, if the named attribute existed and |
||
275 | * has been removed, otherwise NULL. |
||
276 | */ |
||
277 | public function remove($name) |
||
284 | |||
285 | /** |
||
286 | * Remove all attributes. |
||
287 | * |
||
288 | * @return array old values |
||
289 | */ |
||
290 | public function clear() |
||
296 | } |
||
297 |
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: