1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
You may not change or alter any portion of this comment or credits |
4
|
|
|
of supporting developers from this source code or any supporting source code |
5
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
6
|
|
|
|
7
|
|
|
This program is distributed in the hope that it will be useful, |
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xmf\Module\Helper; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Manage session variables for a module. Session variable will be |
16
|
|
|
* prefixed with the module name to separate it from variables set |
17
|
|
|
* by other modules or system functions. All data is serialized, so |
18
|
|
|
* any arbitrary data (i.e. array) can be stored. |
19
|
|
|
* |
20
|
|
|
* @category Xmf\Module\Helper\Session |
21
|
|
|
* @package Xmf |
22
|
|
|
* @author trabis <[email protected]> |
23
|
|
|
* @author Richard Griffith <[email protected]> |
24
|
|
|
* @copyright 2011-2016 XOOPS Project (http://xoops.org) |
25
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
26
|
|
|
* @version Release: 1.0 |
27
|
|
|
* @link http://xoops.org |
28
|
|
|
* @since 1.0 |
29
|
|
|
*/ |
30
|
|
|
class Session extends AbstractHelper |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $prefix; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Initialize parent::__constuct calls this after verifying module object. |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public function init() |
43
|
|
|
{ |
44
|
|
|
$this->prefix = $this->module->getVar('dirname') . '_'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Add our module prefix to a name |
49
|
|
|
* |
50
|
|
|
* @param string $name name to prefix |
51
|
|
|
* |
52
|
|
|
* @return string module prefixed name |
53
|
|
|
*/ |
54
|
|
|
protected function prefix($name) |
55
|
|
|
{ |
56
|
|
|
$prefixedName = $this->prefix . $name; |
57
|
|
|
|
58
|
|
|
return $prefixedName; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Sets a named session variable respecting our module prefix |
63
|
|
|
* |
64
|
|
|
* @param string $name name of variable |
65
|
|
|
* @param mixed $value value of variable |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
public function set($name, $value) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$prefixedName = $this->prefix($name); |
72
|
|
|
$_SESSION[$prefixedName] = serialize($value); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Fetch a named session variable respecting our module prefix |
77
|
|
|
* |
78
|
|
|
* @param string $name name of variable |
79
|
|
|
* @param mixed $default default value to return if config $name is not set |
80
|
|
|
* |
81
|
|
|
* @return mixed $value value of session variable or false if not set |
82
|
|
|
*/ |
83
|
|
|
public function get($name, $default = false) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$prefixedName = $this->prefix($name); |
86
|
|
|
if (isset($_SESSION[$prefixedName])) { |
87
|
|
|
return unserialize($_SESSION[$prefixedName]); |
88
|
|
|
} else { |
89
|
|
|
return $default; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Deletes a named session variable respecting our module prefix |
95
|
|
|
* |
96
|
|
|
* @param string $name name of variable |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
public function del($name) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$prefixedName = $this->prefix($name); |
103
|
|
|
$_SESSION[$prefixedName] = null; |
104
|
|
|
unset($_SESSION[$prefixedName]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Delete all session variable starting with our module prefix |
109
|
|
|
* |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
|
|
public function destroy() |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
foreach ($_SESSION as $key => $value) { |
115
|
|
|
if (0 == substr_compare($key, $this->prefix, 0, strlen($this->prefix))) { |
116
|
|
|
$_SESSION[$key] = null; |
117
|
|
|
unset($_SESSION[$key]); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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: