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;
13:
14: use Xmf\Module\Helper\AbstractHelper;
15:
16: /**
17: * Manage session variables for a module. Session variable will be
18: * prefixed with the module name to separate it from variables set
19: * by other modules or system functions. All data is serialized, so
20: * any arbitrary data (i.e. array) can be stored.
21: *
22: * @category Xmf\Module\Helper\Session
23: * @package Xmf
24: * @author trabis <lusopoemas@gmail.com>
25: * @author Richard Griffith <richard@geekwright.com>
26: * @copyright 2011-2013 XOOPS Project (http://xoops.org)
27: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
28: * @version Release: 1.0
29: * @link http://xoops.org
30: * @since 1.0
31: */
32: class Session extends AbstractHelper
33: {
34: /**
35: * @var string
36: */
37: private $prefix;
38:
39: /**
40: * Initialize parent::__constuct calls this after verifying module object.
41: *
42: * @return void
43: */
44: public function init()
45: {
46: $this->prefix = $this->module->getVar('dirname') . '_';
47: }
48:
49: /**
50: * Add our module prefix to a name
51: *
52: * @param string $name name to prefix
53: *
54: * @return string module prefixed name
55: */
56: private function prefix($name)
57: {
58: $prefixedName = $this->prefix . $name;
59:
60: return $prefixedName;
61: }
62:
63: /**
64: * Sets a named session variable respecting our module prefix
65: *
66: * @param string $name name of variable
67: * @param mixed $value value of variable
68: *
69: * @return void
70: */
71: public function set($name, $value)
72: {
73: $prefixedName = $this->prefix($name);
74: $_SESSION[$prefixedName] = serialize($value);
75: }
76:
77: /**
78: * Fetch a named session variable respecting our module prefix
79: *
80: * @param string $name name of variable
81: *
82: * @return mixed $value value of session variable or false if not set
83: */
84: public function get($name)
85: {
86: $prefixedName = $this->prefix($name);
87: if (isset($_SESSION[$prefixedName])) {
88: return unserialize($_SESSION[$prefixedName]);
89: } else {
90: return false;
91: }
92: }
93:
94: /**
95: * Deletes a named session variable respecting our module prefix
96: *
97: * @param string $name name of variable
98: *
99: * @return void
100: */
101: public function del($name)
102: {
103: $prefixedName = $this->prefix($name);
104: $_SESSION[$prefixedName] = null;
105: unset($_SESSION[$prefixedName]);
106: }
107:
108: /**
109: * Delete all session variable starting with our module prefix
110: *
111: * @return void
112: */
113: public function destroy()
114: {
115: foreach ($_SESSION as $key => $value) {
116: if (0 == substr_compare($key, $this->prefix, 0, strlen($this->prefix))) {
117: $_SESSION[$key] = null;
118: unset($_SESSION[$key]);
119: }
120: }
121: }
122: }
123: