1: <?php
2: /**
3: * Maniaplanet Web Services SDK for PHP
4: *
5: * @see http://code.google.com/p/maniaplanet-ws-sdk/
6: * @copyright Copyright (c) 2009-2011 NADEO (http://www.nadeo.com)
7: * @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
8: * @author $Author: maximeraoust $:
9: * @version $Revision: 48 $:
10: * @date $Date: 2011-09-29 16:47:40 +0200 (jeu., 29 sept. 2011) $:
11: */
12:
13: namespace Maniaplanet\WebServices\ManiaConnect;
14:
15: class Session implements Persistance
16: {
17:
18: /**
19: * Local array to store variables for the setVariable()/getVariable() methods.
20: *
21: * @var array
22: */
23: protected $vars;
24:
25: /**
26: * Generates an almost safe key to store stuff in the PHP session without
27: * collisioning with other variables.
28: */
29: private function getVariableKey($name)
30: {
31: return md5(sprintf('maniaplanet:%s:', $name));
32: }
33:
34: function init()
35: {
36: if(!session_id())
37: {
38: if(!session_start())
39: {
40: throw new \Maniaplanet\WebServices\Exception('Failed to start session');
41: }
42: }
43: $this->vars = array();
44: }
45:
46: function destroy()
47: {
48: session_destroy();
49: $this->vars = array();
50: }
51:
52: function getVariable($name, $default=null)
53: {
54: $key = $this->getVariableKey($name);
55: if(!array_key_exists($key, $this->vars))
56: {
57: if(!array_key_exists($key, $_SESSION))
58: {
59: return $default;
60: }
61: $this->vars[$key] = unserialize($_SESSION[$key]);
62: }
63: return $this->vars[$key];
64: }
65:
66: function setVariable($name, $value)
67: {
68: $key = $this->getVariableKey($name);
69: $this->vars[$key] = $value;
70: $_SESSION[$key] = serialize($value);
71: }
72:
73: function deleteVariable($name)
74: {
75: $key = $this->getVariableKey($name);
76: unset($this->vars[$key]);
77: unset($_SESSION[$key]);
78: }
79:
80: }
81:
82: ?>