| 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 <lusopoemas@gmail.com> | 
| 23: | * @author Richard Griffith <richard@geekwright.com> | 
| 24: | * @copyright 2011-2018 XOOPS Project (https://xoops.org) | 
| 25: | * @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html) | 
| 26: | * @link https://xoops.org | 
| 27: | */ | 
| 28: | class Session extends AbstractHelper | 
| 29: | { | 
| 30: | /** | 
| 31: | * @var string | 
| 32: | */ | 
| 33: | protected $prefix; | 
| 34: | |
| 35: | /** | 
| 36: | * Initialize parent::__construct calls this after verifying module object. | 
| 37: | * | 
| 38: | * @return void | 
| 39: | */ | 
| 40: | public function init() | 
| 41: | { | 
| 42: | $this->prefix = $this->module->getVar('dirname') . '_'; | 
| 43: | } | 
| 44: | |
| 45: | /** | 
| 46: | * Add our module prefix to a name | 
| 47: | * | 
| 48: | * @param string $name name to prefix | 
| 49: | * | 
| 50: | * @return string module prefixed name | 
| 51: | */ | 
| 52: | protected function prefix($name) | 
| 53: | { | 
| 54: | $prefixedName = $this->prefix . $name; | 
| 55: | |
| 56: | return $prefixedName; | 
| 57: | } | 
| 58: | |
| 59: | /** | 
| 60: | * Sets a named session variable respecting our module prefix | 
| 61: | * | 
| 62: | * @param string $name name of variable | 
| 63: | * @param mixed $value value of variable | 
| 64: | * | 
| 65: | * @return void | 
| 66: | */ | 
| 67: | public function set($name, $value) | 
| 68: | { | 
| 69: | $prefixedName = $this->prefix($name); | 
| 70: | $_SESSION[$prefixedName] = serialize($value); | 
| 71: | } | 
| 72: | |
| 73: | /** | 
| 74: | * Fetch a named session variable respecting our module prefix | 
| 75: | * | 
| 76: | * @param string $name name of variable | 
| 77: | * @param mixed $default default value to return if config $name is not set | 
| 78: | * | 
| 79: | * @return mixed $value value of session variable or false if not set | 
| 80: | */ | 
| 81: | public function get($name, $default = false) | 
| 82: | { | 
| 83: | $prefixedName = $this->prefix($name); | 
| 84: | if (isset($_SESSION[$prefixedName])) { | 
| 85: | return unserialize($_SESSION[$prefixedName]); | 
| 86: | } else { | 
| 87: | return $default; | 
| 88: | } | 
| 89: | } | 
| 90: | |
| 91: | /** | 
| 92: | * Deletes a named session variable respecting our module prefix | 
| 93: | * | 
| 94: | * @param string $name name of variable | 
| 95: | * | 
| 96: | * @return void | 
| 97: | */ | 
| 98: | public function del($name) | 
| 99: | { | 
| 100: | $prefixedName = $this->prefix($name); | 
| 101: | $_SESSION[$prefixedName] = null; | 
| 102: | unset($_SESSION[$prefixedName]); | 
| 103: | } | 
| 104: | |
| 105: | /** | 
| 106: | * Delete all session variable starting with our module prefix | 
| 107: | * | 
| 108: | * @return void | 
| 109: | */ | 
| 110: | public function destroy() | 
| 111: | { | 
| 112: | foreach ($_SESSION as $key => $value) { | 
| 113: | if (0 == substr_compare($key, $this->prefix, 0, strlen($this->prefix))) { | 
| 114: | $_SESSION[$key] = null; | 
| 115: | unset($_SESSION[$key]); | 
| 116: | } | 
| 117: | } | 
| 118: | } | 
| 119: | } | 
| 120: |