7
* This source file is subject to the new BSD license that is bundled
8
* with this package in the file LICENSE.txt.
9
* It is also available through the world-wide-web at this URL:
10
* http://framework.zend.com/license/new-bsd
11
* If you did not receive a copy of the license and are unable to
12
* obtain it through the world-wide-web, please send an email
13
* to license@zend.com so we can send you a copy immediately.
16
* @package Zend_Session
17
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
18
* @license http://framework.zend.com/license/new-bsd New BSD License
19
* @version $Id: Namespace.php 11003 2008-08-24 14:43:15Z matthew $
20
* @since Preview Release 0.2
27
require_once 'Zend/Session.php';
31
* @see Zend_Session_Abstract
33
require_once 'Zend/Session/Abstract.php';
37
* Zend_Session_Namespace
40
* @package Zend_Session
41
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
42
* @license http://framework.zend.com/license/new-bsd New BSD License
44
class Zend_Session_Namespace extends Zend_Session_Abstract implements IteratorAggregate
48
* used as option to constructor to prevent additional instances to the same namespace
50
const SINGLE_INSTANCE = true;
53
* Namespace - which namespace this instance of zend-session is saving-to/getting-from
57
protected $_namespace = "Default";
60
* Namespace locking mechanism
64
protected static $_namespaceLocks = array();
67
* Single instance namespace array to ensure data security.
71
protected static $_singleInstances = array();
74
* __construct() - Returns an instance object bound to a particular, isolated section
75
* of the session, identified by $namespace name (defaulting to 'Default').
76
* The optional argument $singleInstance will prevent construction of additional
77
* instance objects acting as accessors to this $namespace.
79
* @param string $namespace - programmatic name of the requested namespace
80
* @param bool $singleInstance - prevent creation of additional accessor instance objects for this namespace
83
public function __construct($namespace = 'Default', $singleInstance = false)
85
if ($namespace === '') {
87
* @see Zend_Session_Exception
89
require_once 'Zend/Session/Exception.php';
90
throw new Zend_Session_Exception('Session namespace must be a non-empty string.');
93
if ($namespace[0] == "_") {
95
* @see Zend_Session_Exception
97
require_once 'Zend/Session/Exception.php';
98
throw new Zend_Session_Exception('Session namespace must not start with an underscore.');
101
if (isset(self::$_singleInstances[$namespace])) {
103
* @see Zend_Session_Exception
105
require_once 'Zend/Session/Exception.php';
106
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.");
109
if ($singleInstance === true) {
110
self::$_singleInstances[$namespace] = true;
113
$this->_namespace = $namespace;
115
// Process metadata specific only to this namespace.
116
Zend_Session::start(true); // attempt auto-start (throws exception if strict option set)
118
if (self::$_readable === false) {
120
* @see Zend_Session_Exception
122
require_once 'Zend/Session/Exception.php';
123
throw new Zend_Session_Exception(self::_THROW_NOT_READABLE_MSG);
126
if (!isset($_SESSION['__ZF'])) {
127
return; // no further processing needed
130
// do not allow write access to namespaces, after stop() or writeClose()
131
if (parent::$_writable === true) {
132
if (isset($_SESSION['__ZF'][$namespace])) {
134
// Expire Namespace by Namespace Hop (ENNH)
135
if (isset($_SESSION['__ZF'][$namespace]['ENNH'])) {
136
$_SESSION['__ZF'][$namespace]['ENNH']--;
138
if ($_SESSION['__ZF'][$namespace]['ENNH'] === 0) {
139
if (isset($_SESSION[$namespace])) {
140
self::$_expiringData[$namespace] = $_SESSION[$namespace];
141
unset($_SESSION[$namespace]);
143
unset($_SESSION['__ZF'][$namespace]['ENNH']);
147
// Expire Namespace Variables by Namespace Hop (ENVNH)
148
if (isset($_SESSION['__ZF'][$namespace]['ENVNH'])) {
149
foreach ($_SESSION['__ZF'][$namespace]['ENVNH'] as $variable => $hops) {
150
$_SESSION['__ZF'][$namespace]['ENVNH'][$variable]--;
152
if ($_SESSION['__ZF'][$namespace]['ENVNH'][$variable] === 0) {
153
if (isset($_SESSION[$namespace][$variable])) {
154
self::$_expiringData[$namespace][$variable] = $_SESSION[$namespace][$variable];
155
unset($_SESSION[$namespace][$variable]);
157
unset($_SESSION['__ZF'][$namespace]['ENVNH'][$variable]);
163
if (empty($_SESSION['__ZF'][$namespace])) {
164
unset($_SESSION['__ZF'][$namespace]);
167
if (empty($_SESSION['__ZF'])) {
168
unset($_SESSION['__ZF']);
175
* getIterator() - return an iteratable object for use in foreach and the like,
176
* this completes the IteratorAggregate interface
178
* @return ArrayObject - iteratable container of the namespace contents
180
public function getIterator()
182
return new ArrayObject(parent::_namespaceGetAll($this->_namespace));
187
* lock() - mark a session/namespace as readonly
191
public function lock()
193
self::$_namespaceLocks[$this->_namespace] = true;
198
* unlock() - unmark a session/namespace to enable read & write
202
public function unlock()
204
unset(self::$_namespaceLocks[$this->_namespace]);
209
* unlockAll() - unmark all session/namespaces to enable read & write
213
public static function unlockAll()
215
self::$_namespaceLocks = array();
220
* isLocked() - return lock status, true if, and only if, read-only
224
public function isLocked()
226
return isset(self::$_namespaceLocks[$this->_namespace]);
231
* unsetAll() - unset all variables in this namespace
235
public function unsetAll()
237
return parent::_namespaceUnset($this->_namespace);
242
* __get() - method to get a variable in this object's current namespace
244
* @param string $name - programmatic name of a key, in a <key,value> pair in the current namespace
247
public function & __get($name)
251
* @see Zend_Session_Exception
253
require_once 'Zend/Session/Exception.php';
254
throw new Zend_Session_Exception("The '$name' key must be a non-empty string");
257
return parent::_namespaceGet($this->_namespace, $name);
262
* __set() - method to set a variable/value in this object's namespace
264
* @param string $name - programmatic name of a key, in a <key,value> pair in the current namespace
265
* @param mixed $value - value in the <key,value> pair to assign to the $name key
266
* @throws Zend_Session_Exception
269
public function __set($name, $value)
271
if (isset(self::$_namespaceLocks[$this->_namespace])) {
273
* @see Zend_Session_Exception
275
require_once 'Zend/Session/Exception.php';
276
throw new Zend_Session_Exception('This session/namespace has been marked as read-only.');
281
* @see Zend_Session_Exception
283
require_once 'Zend/Session/Exception.php';
284
throw new Zend_Session_Exception("The '$name' key must be a non-empty string");
287
if (parent::$_writable === false) {
289
* @see Zend_Session_Exception
291
require_once 'Zend/Session/Exception.php';
292
throw new Zend_Session_Exception(parent::_THROW_NOT_WRITABLE_MSG);
295
$name = (string) $name;
297
$_SESSION[$this->_namespace][$name] = $value;
302
* apply() - enables applying user-selected function, such as array_merge() to the namespace
303
* Parameters following the $callback argument are passed to the callback function.
304
* Caveat: ignores members expiring now.
307
* $namespace->apply('array_merge', array('tree' => 'apple', 'fruit' => 'peach'), array('flower' => 'rose'));
308
* $namespace->apply('count');
310
* @param string|array $callback - callback function
312
public function apply($callback)
314
$arg_list = func_get_args();
315
$arg_list[0] = $_SESSION[$this->_namespace];
316
return call_user_func_array($callback, $arg_list);
321
* applySet() - enables applying user-selected function, and sets entire namespace to the result
322
* Result of $callback must be an array.
323
* Parameters following the $callback argument are passed to the callback function.
324
* Caveat: ignores members expiring now.
327
* $namespace->applySet('array_merge', array('tree' => 'apple', 'fruit' => 'peach'), array('flower' => 'rose'));
329
* @param string|array $callback - callback function
331
public function applySet($callback)
333
$arg_list = func_get_args();
334
$arg_list[0] = $_SESSION[$this->_namespace];
335
$result = call_user_func_array($callback, $arg_list);
336
if (!is_array($result)) {
338
* @see Zend_Session_Exception
340
require_once 'Zend/Session/Exception.php';
341
throw new Zend_Session_Exception('Result must be an array. Got: ' . gettype($result));
343
$_SESSION[$this->_namespace] = $result;
349
* __isset() - determine if a variable in this object's namespace is set
351
* @param string $name - programmatic name of a key, in a <key,value> pair in the current namespace
354
public function __isset($name)
358
* @see Zend_Session_Exception
360
require_once 'Zend/Session/Exception.php';
361
throw new Zend_Session_Exception("The '$name' key must be a non-empty string");
364
return parent::_namespaceIsset($this->_namespace, $name);
369
* __unset() - unset a variable in this object's namespace.
371
* @param string $name - programmatic name of a key, in a <key,value> pair in the current namespace
374
public function __unset($name)
378
* @see Zend_Session_Exception
380
require_once 'Zend/Session/Exception.php';
381
throw new Zend_Session_Exception("The '$name' key must be a non-empty string");
384
return parent::_namespaceUnset($this->_namespace, $name);
389
* setExpirationSeconds() - expire the namespace, or specific variables after a specified
392
* @param int $seconds - expires in this many seconds
393
* @param mixed $variables - OPTIONAL list of variables to expire (defaults to all)
394
* @throws Zend_Session_Exception
397
public function setExpirationSeconds($seconds, $variables = null)
399
if (parent::$_writable === false) {
401
* @see Zend_Session_Exception
403
require_once 'Zend/Session/Exception.php';
404
throw new Zend_Session_Exception(parent::_THROW_NOT_WRITABLE_MSG);
409
* @see Zend_Session_Exception
411
require_once 'Zend/Session/Exception.php';
412
throw new Zend_Session_Exception('Seconds must be positive.');
415
if ($variables === null) {
417
// apply expiration to entire namespace
418
$_SESSION['__ZF'][$this->_namespace]['ENT'] = time() + $seconds;
422
if (is_string($variables)) {
423
$variables = array($variables);
426
foreach ($variables as $variable) {
427
if (!empty($variable)) {
428
$_SESSION['__ZF'][$this->_namespace]['ENVT'][$variable] = time() + $seconds;
436
* setExpirationHops() - expire the namespace, or specific variables after a specified
437
* number of page hops
439
* @param int $hops - how many "hops" (number of subsequent requests) before expiring
440
* @param mixed $variables - OPTIONAL list of variables to expire (defaults to all)
441
* @param boolean $hopCountOnUsageOnly - OPTIONAL if set, only count a hop/request if this namespace is used
442
* @throws Zend_Session_Exception
445
public function setExpirationHops($hops, $variables = null, $hopCountOnUsageOnly = false)
447
if (parent::$_writable === false) {
449
* @see Zend_Session_Exception
451
require_once 'Zend/Session/Exception.php';
452
throw new Zend_Session_Exception(parent::_THROW_NOT_WRITABLE_MSG);
457
* @see Zend_Session_Exception
459
require_once 'Zend/Session/Exception.php';
460
throw new Zend_Session_Exception('Hops must be positive number.');
463
if ($variables === null) {
465
// apply expiration to entire namespace
466
if ($hopCountOnUsageOnly === false) {
467
$_SESSION['__ZF'][$this->_namespace]['ENGH'] = $hops;
469
$_SESSION['__ZF'][$this->_namespace]['ENNH'] = $hops;
474
if (is_string($variables)) {
475
$variables = array($variables);
478
foreach ($variables as $variable) {
479
if (!empty($variable)) {
480
if ($hopCountOnUsageOnly === false) {
481
$_SESSION['__ZF'][$this->_namespace]['ENVGH'][$variable] = $hops;
483
$_SESSION['__ZF'][$this->_namespace]['ENVNH'][$variable] = $hops;