Namespace.php
459 lines
| 1 | <?php |
| 2 | |
| 3 | namespace { |
| 4 | /** |
| 5 | * Zend Framework |
| 6 | * |
| 7 | * LICENSE |
| 8 | * |
| 9 | * This source file is subject to the new BSD license that is bundled |
| 10 | * with this package in the file LICENSE.txt. |
| 11 | * It is also available through the world-wide-web at this URL: |
| 12 | * http://framework.zend.com/license/new-bsd |
| 13 | * If you did not receive a copy of the license and are unable to |
| 14 | * obtain it through the world-wide-web, please send an email |
| 15 | * to license@zend.com so we can send you a copy immediately. |
| 16 | * |
| 17 | * @category Zend |
| 18 | * @package Zend_Session |
| 19 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 20 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 21 | * @version $Id: Namespace.php 23775 2011-03-01 17:25:24Z ralph $ |
| 22 | * @since Preview Release 0.2 |
| 23 | */ |
| 24 | /** |
| 25 | * @see Zend_Session |
| 26 | */ |
| 27 | // require_once 'Zend/Session.php'; |
| 28 | /** |
| 29 | * @see Zend_Session_Abstract |
| 30 | */ |
| 31 | // require_once 'Zend/Session/Abstract.php'; |
| 32 | /** |
| 33 | * Zend_Session_Namespace |
| 34 | * |
| 35 | * @category Zend |
| 36 | * @package Zend_Session |
| 37 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 38 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 39 | */ |
| 40 | class Zend_Session_Namespace extends \Zend_Session_Abstract implements \IteratorAggregate |
| 41 | { |
| 42 | /** |
| 43 | * used as option to constructor to prevent additional instances to the same namespace |
| 44 | */ |
| 45 | const SINGLE_INSTANCE = \true; |
| 46 | /** |
| 47 | * Namespace - which namespace this instance of zend-session is saving-to/getting-from |
| 48 | * |
| 49 | * @var string |
| 50 | */ |
| 51 | protected $_namespace = "Default"; |
| 52 | /** |
| 53 | * Namespace locking mechanism |
| 54 | * |
| 55 | * @var array |
| 56 | */ |
| 57 | protected static $_namespaceLocks = array(); |
| 58 | /** |
| 59 | * Single instance namespace array to ensure data security. |
| 60 | * |
| 61 | * @var array |
| 62 | */ |
| 63 | protected static $_singleInstances = array(); |
| 64 | /** |
| 65 | * resetSingleInstance() |
| 66 | * |
| 67 | * @param string $namespaceName |
| 68 | * @return null |
| 69 | */ |
| 70 | public static function resetSingleInstance($namespaceName = null) |
| 71 | { |
| 72 | if ($namespaceName != null) { |
| 73 | if (\array_key_exists($namespaceName, self::$_singleInstances)) { |
| 74 | unset(self::$_singleInstances[$namespaceName]); |
| 75 | } |
| 76 | return; |
| 77 | } |
| 78 | self::$_singleInstances = array(); |
| 79 | return; |
| 80 | } |
| 81 | /** |
| 82 | * __construct() - Returns an instance object bound to a particular, isolated section |
| 83 | * of the session, identified by $namespace name (defaulting to 'Default'). |
| 84 | * The optional argument $singleInstance will prevent construction of additional |
| 85 | * instance objects acting as accessors to this $namespace. |
| 86 | * |
| 87 | * @param string $namespace - programmatic name of the requested namespace |
| 88 | * @param bool $singleInstance - prevent creation of additional accessor instance objects for this namespace |
| 89 | * @return void |
| 90 | */ |
| 91 | public function __construct($namespace = 'Default', $singleInstance = \false) |
| 92 | { |
| 93 | if ($namespace === '') { |
| 94 | /** |
| 95 | * @see Zend_Session_Exception |
| 96 | */ |
| 97 | // require_once 'Zend/Session/Exception.php'; |
| 98 | throw new \Zend_Session_Exception('Session namespace must be a non-empty string.'); |
| 99 | } |
| 100 | if ($namespace[0] == "_") { |
| 101 | /** |
| 102 | * @see Zend_Session_Exception |
| 103 | */ |
| 104 | // require_once 'Zend/Session/Exception.php'; |
| 105 | throw new \Zend_Session_Exception('Session namespace must not start with an underscore.'); |
| 106 | } |
| 107 | if (\preg_match('#(^[0-9])#i', $namespace[0])) { |
| 108 | /** |
| 109 | * @see Zend_Session_Exception |
| 110 | */ |
| 111 | // require_once 'Zend/Session/Exception.php'; |
| 112 | throw new \Zend_Session_Exception('Session namespace must not start with a number.'); |
| 113 | } |
| 114 | if (isset(self::$_singleInstances[$namespace])) { |
| 115 | /** |
| 116 | * @see Zend_Session_Exception |
| 117 | */ |
| 118 | // require_once 'Zend/Session/Exception.php'; |
| 119 | throw new \Zend_Session_Exception("A session namespace object already exists for this namespace ('{$namespace}'), and no additional accessors (session namespace objects) for this namespace are permitted."); |
| 120 | } |
| 121 | if ($singleInstance === \true) { |
| 122 | self::$_singleInstances[$namespace] = \true; |
| 123 | } |
| 124 | $this->_namespace = $namespace; |
| 125 | // Process metadata specific only to this namespace. |
| 126 | \Zend_Session::start(\true); |
| 127 | // attempt auto-start (throws exception if strict option set) |
| 128 | if (self::$_readable === \false) { |
| 129 | /** |
| 130 | * @see Zend_Session_Exception |
| 131 | */ |
| 132 | // require_once 'Zend/Session/Exception.php'; |
| 133 | throw new \Zend_Session_Exception(self::_THROW_NOT_READABLE_MSG); |
| 134 | } |
| 135 | if (!isset($_SESSION['__ZF'])) { |
| 136 | return; |
| 137 | // no further processing needed |
| 138 | } |
| 139 | // do not allow write access to namespaces, after stop() or writeClose() |
| 140 | if (parent::$_writable === \true) { |
| 141 | if (isset($_SESSION['__ZF'][$namespace])) { |
| 142 | // Expire Namespace by Namespace Hop (ENNH) |
| 143 | if (isset($_SESSION['__ZF'][$namespace]['ENNH'])) { |
| 144 | $_SESSION['__ZF'][$namespace]['ENNH']--; |
| 145 | if ($_SESSION['__ZF'][$namespace]['ENNH'] === 0) { |
| 146 | if (isset($_SESSION[$namespace])) { |
| 147 | self::$_expiringData[$namespace] = $_SESSION[$namespace]; |
| 148 | unset($_SESSION[$namespace]); |
| 149 | } |
| 150 | unset($_SESSION['__ZF'][$namespace]); |
| 151 | } |
| 152 | } |
| 153 | // Expire Namespace Variables by Namespace Hop (ENVNH) |
| 154 | if (isset($_SESSION['__ZF'][$namespace]['ENVNH'])) { |
| 155 | foreach ($_SESSION['__ZF'][$namespace]['ENVNH'] as $variable => $hops) { |
| 156 | $_SESSION['__ZF'][$namespace]['ENVNH'][$variable]--; |
| 157 | if ($_SESSION['__ZF'][$namespace]['ENVNH'][$variable] === 0) { |
| 158 | if (isset($_SESSION[$namespace][$variable])) { |
| 159 | self::$_expiringData[$namespace][$variable] = $_SESSION[$namespace][$variable]; |
| 160 | unset($_SESSION[$namespace][$variable]); |
| 161 | } |
| 162 | unset($_SESSION['__ZF'][$namespace]['ENVNH'][$variable]); |
| 163 | } |
| 164 | } |
| 165 | if (empty($_SESSION['__ZF'][$namespace]['ENVNH'])) { |
| 166 | unset($_SESSION['__ZF'][$namespace]['ENVNH']); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | if (empty($_SESSION['__ZF'][$namespace])) { |
| 171 | unset($_SESSION['__ZF'][$namespace]); |
| 172 | } |
| 173 | if (empty($_SESSION['__ZF'])) { |
| 174 | unset($_SESSION['__ZF']); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | /** |
| 179 | * getIterator() - return an iteratable object for use in foreach and the like, |
| 180 | * this completes the IteratorAggregate interface |
| 181 | * |
| 182 | * @return ArrayObject - iteratable container of the namespace contents |
| 183 | */ |
| 184 | public function getIterator() : \ArrayObject |
| 185 | { |
| 186 | return new \ArrayObject(parent::_namespaceGetAll($this->_namespace)); |
| 187 | } |
| 188 | /** |
| 189 | * lock() - mark a session/namespace as readonly |
| 190 | * |
| 191 | * @return void |
| 192 | */ |
| 193 | public function lock() |
| 194 | { |
| 195 | self::$_namespaceLocks[$this->_namespace] = \true; |
| 196 | } |
| 197 | /** |
| 198 | * unlock() - unmark a session/namespace to enable read & write |
| 199 | * |
| 200 | * @return void |
| 201 | */ |
| 202 | public function unlock() |
| 203 | { |
| 204 | unset(self::$_namespaceLocks[$this->_namespace]); |
| 205 | } |
| 206 | /** |
| 207 | * unlockAll() - unmark all session/namespaces to enable read & write |
| 208 | * |
| 209 | * @return void |
| 210 | */ |
| 211 | public static function unlockAll() |
| 212 | { |
| 213 | self::$_namespaceLocks = array(); |
| 214 | } |
| 215 | /** |
| 216 | * isLocked() - return lock status, true if, and only if, read-only |
| 217 | * |
| 218 | * @return bool |
| 219 | */ |
| 220 | public function isLocked() |
| 221 | { |
| 222 | return isset(self::$_namespaceLocks[$this->_namespace]); |
| 223 | } |
| 224 | /** |
| 225 | * unsetAll() - unset all variables in this namespace |
| 226 | * |
| 227 | * @return true |
| 228 | */ |
| 229 | public function unsetAll() |
| 230 | { |
| 231 | return parent::_namespaceUnset($this->_namespace); |
| 232 | } |
| 233 | /** |
| 234 | * __get() - method to get a variable in this object's current namespace |
| 235 | * |
| 236 | * @param string $name - programmatic name of a key, in a <key,value> pair in the current namespace |
| 237 | * @return mixed |
| 238 | */ |
| 239 | public function &__get($name) |
| 240 | { |
| 241 | if ($name === '') { |
| 242 | /** |
| 243 | * @see Zend_Session_Exception |
| 244 | */ |
| 245 | // require_once 'Zend/Session/Exception.php'; |
| 246 | throw new \Zend_Session_Exception("The '{$name}' key must be a non-empty string"); |
| 247 | } |
| 248 | return parent::_namespaceGet($this->_namespace, $name); |
| 249 | } |
| 250 | /** |
| 251 | * __set() - method to set a variable/value in this object's namespace |
| 252 | * |
| 253 | * @param string $name - programmatic name of a key, in a <key,value> pair in the current namespace |
| 254 | * @param mixed $value - value in the <key,value> pair to assign to the $name key |
| 255 | * @throws Zend_Session_Exception |
| 256 | * @return true |
| 257 | */ |
| 258 | public function __set($name, $value) |
| 259 | { |
| 260 | if (isset(self::$_namespaceLocks[$this->_namespace])) { |
| 261 | /** |
| 262 | * @see Zend_Session_Exception |
| 263 | */ |
| 264 | // require_once 'Zend/Session/Exception.php'; |
| 265 | throw new \Zend_Session_Exception('This session/namespace has been marked as read-only.'); |
| 266 | } |
| 267 | if ($name === '') { |
| 268 | /** |
| 269 | * @see Zend_Session_Exception |
| 270 | */ |
| 271 | // require_once 'Zend/Session/Exception.php'; |
| 272 | throw new \Zend_Session_Exception("The '{$name}' key must be a non-empty string"); |
| 273 | } |
| 274 | if (parent::$_writable === \false) { |
| 275 | /** |
| 276 | * @see Zend_Session_Exception |
| 277 | */ |
| 278 | // require_once 'Zend/Session/Exception.php'; |
| 279 | throw new \Zend_Session_Exception(parent::_THROW_NOT_WRITABLE_MSG); |
| 280 | } |
| 281 | $name = (string) $name; |
| 282 | $_SESSION[$this->_namespace][$name] = $value; |
| 283 | } |
| 284 | /** |
| 285 | * apply() - enables applying user-selected function, such as array_merge() to the namespace |
| 286 | * Parameters following the $callback argument are passed to the callback function. |
| 287 | * Caveat: ignores members expiring now. |
| 288 | * |
| 289 | * Example: |
| 290 | * $namespace->apply('array_merge', array('tree' => 'apple', 'fruit' => 'peach'), array('flower' => 'rose')); |
| 291 | * $namespace->apply('count'); |
| 292 | * |
| 293 | * @param string|array $callback - callback function |
| 294 | */ |
| 295 | public function apply($callback) |
| 296 | { |
| 297 | $arg_list = \func_get_args(); |
| 298 | $arg_list[0] = $_SESSION[$this->_namespace]; |
| 299 | return \call_user_func_array($callback, $arg_list); |
| 300 | } |
| 301 | /** |
| 302 | * applySet() - enables applying user-selected function, and sets entire namespace to the result |
| 303 | * Result of $callback must be an array. |
| 304 | * Parameters following the $callback argument are passed to the callback function. |
| 305 | * Caveat: ignores members expiring now. |
| 306 | * |
| 307 | * Example: |
| 308 | * $namespace->applySet('array_merge', array('tree' => 'apple', 'fruit' => 'peach'), array('flower' => 'rose')); |
| 309 | * |
| 310 | * @param string|array $callback - callback function |
| 311 | */ |
| 312 | public function applySet($callback) |
| 313 | { |
| 314 | $arg_list = \func_get_args(); |
| 315 | $arg_list[0] = $_SESSION[$this->_namespace]; |
| 316 | $result = \call_user_func_array($callback, $arg_list); |
| 317 | if (!\is_array($result)) { |
| 318 | /** |
| 319 | * @see Zend_Session_Exception |
| 320 | */ |
| 321 | // require_once 'Zend/Session/Exception.php'; |
| 322 | throw new \Zend_Session_Exception('Result must be an array. Got: ' . \gettype($result)); |
| 323 | } |
| 324 | $_SESSION[$this->_namespace] = $result; |
| 325 | return $result; |
| 326 | } |
| 327 | /** |
| 328 | * __isset() - determine if a variable in this object's namespace is set |
| 329 | * |
| 330 | * @param string $name - programmatic name of a key, in a <key,value> pair in the current namespace |
| 331 | * @return bool |
| 332 | */ |
| 333 | public function __isset($name) |
| 334 | { |
| 335 | if ($name === '') { |
| 336 | /** |
| 337 | * @see Zend_Session_Exception |
| 338 | */ |
| 339 | // require_once 'Zend/Session/Exception.php'; |
| 340 | throw new \Zend_Session_Exception("The '{$name}' key must be a non-empty string"); |
| 341 | } |
| 342 | return parent::_namespaceIsset($this->_namespace, $name); |
| 343 | } |
| 344 | /** |
| 345 | * __unset() - unset a variable in this object's namespace. |
| 346 | * |
| 347 | * @param string $name - programmatic name of a key, in a <key,value> pair in the current namespace |
| 348 | * @return true |
| 349 | */ |
| 350 | public function __unset($name) |
| 351 | { |
| 352 | if ($name === '') { |
| 353 | /** |
| 354 | * @see Zend_Session_Exception |
| 355 | */ |
| 356 | // require_once 'Zend/Session/Exception.php'; |
| 357 | throw new \Zend_Session_Exception("The '{$name}' key must be a non-empty string"); |
| 358 | } |
| 359 | return parent::_namespaceUnset($this->_namespace, $name); |
| 360 | } |
| 361 | /** |
| 362 | * setExpirationSeconds() - expire the namespace, or specific variables after a specified |
| 363 | * number of seconds |
| 364 | * |
| 365 | * @param int $seconds - expires in this many seconds |
| 366 | * @param mixed $variables - OPTIONAL list of variables to expire (defaults to all) |
| 367 | * @throws Zend_Session_Exception |
| 368 | * @return void |
| 369 | */ |
| 370 | public function setExpirationSeconds($seconds, $variables = null) |
| 371 | { |
| 372 | if (parent::$_writable === \false) { |
| 373 | /** |
| 374 | * @see Zend_Session_Exception |
| 375 | */ |
| 376 | // require_once 'Zend/Session/Exception.php'; |
| 377 | throw new \Zend_Session_Exception(parent::_THROW_NOT_WRITABLE_MSG); |
| 378 | } |
| 379 | if ($seconds <= 0) { |
| 380 | /** |
| 381 | * @see Zend_Session_Exception |
| 382 | */ |
| 383 | // require_once 'Zend/Session/Exception.php'; |
| 384 | throw new \Zend_Session_Exception('Seconds must be positive.'); |
| 385 | } |
| 386 | if ($variables === null) { |
| 387 | // apply expiration to entire namespace |
| 388 | $_SESSION['__ZF'][$this->_namespace]['ENT'] = \time() + $seconds; |
| 389 | } else { |
| 390 | if (\is_string($variables)) { |
| 391 | $variables = array($variables); |
| 392 | } |
| 393 | foreach ($variables as $variable) { |
| 394 | if (!empty($variable)) { |
| 395 | $_SESSION['__ZF'][$this->_namespace]['ENVT'][$variable] = \time() + $seconds; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | /** |
| 401 | * setExpirationHops() - expire the namespace, or specific variables after a specified |
| 402 | * number of page hops |
| 403 | * |
| 404 | * @param int $hops - how many "hops" (number of subsequent requests) before expiring |
| 405 | * @param mixed $variables - OPTIONAL list of variables to expire (defaults to all) |
| 406 | * @param boolean $hopCountOnUsageOnly - OPTIONAL if set, only count a hop/request if this namespace is used |
| 407 | * @throws Zend_Session_Exception |
| 408 | * @return void |
| 409 | */ |
| 410 | public function setExpirationHops($hops, $variables = null, $hopCountOnUsageOnly = \false) |
| 411 | { |
| 412 | if (parent::$_writable === \false) { |
| 413 | /** |
| 414 | * @see Zend_Session_Exception |
| 415 | */ |
| 416 | // require_once 'Zend/Session/Exception.php'; |
| 417 | throw new \Zend_Session_Exception(parent::_THROW_NOT_WRITABLE_MSG); |
| 418 | } |
| 419 | if ($hops <= 0) { |
| 420 | /** |
| 421 | * @see Zend_Session_Exception |
| 422 | */ |
| 423 | // require_once 'Zend/Session/Exception.php'; |
| 424 | throw new \Zend_Session_Exception('Hops must be positive number.'); |
| 425 | } |
| 426 | if ($variables === null) { |
| 427 | // apply expiration to entire namespace |
| 428 | if ($hopCountOnUsageOnly === \false) { |
| 429 | $_SESSION['__ZF'][$this->_namespace]['ENGH'] = $hops; |
| 430 | } else { |
| 431 | $_SESSION['__ZF'][$this->_namespace]['ENNH'] = $hops; |
| 432 | } |
| 433 | } else { |
| 434 | if (\is_string($variables)) { |
| 435 | $variables = array($variables); |
| 436 | } |
| 437 | foreach ($variables as $variable) { |
| 438 | if (!empty($variable)) { |
| 439 | if ($hopCountOnUsageOnly === \false) { |
| 440 | $_SESSION['__ZF'][$this->_namespace]['ENVGH'][$variable] = $hops; |
| 441 | } else { |
| 442 | $_SESSION['__ZF'][$this->_namespace]['ENVNH'][$variable] = $hops; |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | /** |
| 449 | * Returns the namespace name |
| 450 | * |
| 451 | * @return string |
| 452 | */ |
| 453 | public function getNamespace() |
| 454 | { |
| 455 | return $this->_namespace; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 |