~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Session/Namespace.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
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.
 
14
 *
 
15
 * @category   Zend
 
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
 
21
 */
 
22
 
 
23
 
 
24
/**
 
25
 * @see Zend_Session
 
26
 */
 
27
require_once 'Zend/Session.php';
 
28
 
 
29
 
 
30
/**
 
31
 * @see Zend_Session_Abstract
 
32
 */
 
33
require_once 'Zend/Session/Abstract.php';
 
34
 
 
35
 
 
36
/**
 
37
 * Zend_Session_Namespace
 
38
 *
 
39
 * @category   Zend
 
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
 
43
 */
 
44
class Zend_Session_Namespace extends Zend_Session_Abstract implements IteratorAggregate
 
45
{
 
46
 
 
47
    /**
 
48
     * used as option to constructor to prevent additional instances to the same namespace
 
49
     */
 
50
    const SINGLE_INSTANCE = true;
 
51
 
 
52
    /**
 
53
     * Namespace - which namespace this instance of zend-session is saving-to/getting-from
 
54
     *
 
55
     * @var string
 
56
     */
 
57
    protected $_namespace = "Default";
 
58
 
 
59
    /**
 
60
     * Namespace locking mechanism
 
61
     *
 
62
     * @var array
 
63
     */
 
64
    protected static $_namespaceLocks = array();
 
65
 
 
66
    /**
 
67
     * Single instance namespace array to ensure data security.
 
68
     *
 
69
     * @var array
 
70
     */
 
71
    protected static $_singleInstances = array();
 
72
 
 
73
    /**
 
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.
 
78
     *
 
79
     * @param string $namespace       - programmatic name of the requested namespace
 
80
     * @param bool $singleInstance    - prevent creation of additional accessor instance objects for this namespace
 
81
     * @return void
 
82
     */
 
83
    public function __construct($namespace = 'Default', $singleInstance = false)
 
84
    {
 
85
        if ($namespace === '') {
 
86
            /**
 
87
             * @see Zend_Session_Exception
 
88
             */
 
89
            require_once 'Zend/Session/Exception.php';
 
90
            throw new Zend_Session_Exception('Session namespace must be a non-empty string.');
 
91
        }
 
92
 
 
93
        if ($namespace[0] == "_") {
 
94
            /**
 
95
             * @see Zend_Session_Exception
 
96
             */
 
97
            require_once 'Zend/Session/Exception.php';
 
98
            throw new Zend_Session_Exception('Session namespace must not start with an underscore.');
 
99
        }
 
100
 
 
101
        if (isset(self::$_singleInstances[$namespace])) {
 
102
            /**
 
103
             * @see Zend_Session_Exception
 
104
             */
 
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.");
 
107
        }
 
108
 
 
109
        if ($singleInstance === true) {
 
110
            self::$_singleInstances[$namespace] = true;
 
111
        }
 
112
 
 
113
        $this->_namespace = $namespace;
 
114
 
 
115
        // Process metadata specific only to this namespace.
 
116
        Zend_Session::start(true); // attempt auto-start (throws exception if strict option set)
 
117
 
 
118
        if (self::$_readable === false) {
 
119
            /**
 
120
             * @see Zend_Session_Exception
 
121
             */
 
122
            require_once 'Zend/Session/Exception.php';
 
123
            throw new Zend_Session_Exception(self::_THROW_NOT_READABLE_MSG);
 
124
        }
 
125
 
 
126
        if (!isset($_SESSION['__ZF'])) {
 
127
            return; // no further processing needed
 
128
        }
 
129
 
 
130
        // do not allow write access to namespaces, after stop() or writeClose()
 
131
        if (parent::$_writable === true) {
 
132
            if (isset($_SESSION['__ZF'][$namespace])) {
 
133
 
 
134
                // Expire Namespace by Namespace Hop (ENNH)
 
135
                if (isset($_SESSION['__ZF'][$namespace]['ENNH'])) {
 
136
                    $_SESSION['__ZF'][$namespace]['ENNH']--;
 
137
 
 
138
                    if ($_SESSION['__ZF'][$namespace]['ENNH'] === 0) {
 
139
                        if (isset($_SESSION[$namespace])) {
 
140
                            self::$_expiringData[$namespace] = $_SESSION[$namespace];
 
141
                            unset($_SESSION[$namespace]);
 
142
                        }
 
143
                        unset($_SESSION['__ZF'][$namespace]['ENNH']);
 
144
                    }
 
145
                }
 
146
 
 
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]--;
 
151
 
 
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]);
 
156
                            }
 
157
                            unset($_SESSION['__ZF'][$namespace]['ENVNH'][$variable]);
 
158
                        }
 
159
                    }
 
160
                }
 
161
            }
 
162
 
 
163
            if (empty($_SESSION['__ZF'][$namespace])) {
 
164
                unset($_SESSION['__ZF'][$namespace]);
 
165
            }
 
166
 
 
167
            if (empty($_SESSION['__ZF'])) {
 
168
                unset($_SESSION['__ZF']);
 
169
            }
 
170
        }
 
171
    }
 
172
 
 
173
 
 
174
    /**
 
175
     * getIterator() - return an iteratable object for use in foreach and the like,
 
176
     * this completes the IteratorAggregate interface
 
177
     *
 
178
     * @return ArrayObject - iteratable container of the namespace contents
 
179
     */
 
180
    public function getIterator()
 
181
    {
 
182
        return new ArrayObject(parent::_namespaceGetAll($this->_namespace));
 
183
    }
 
184
 
 
185
 
 
186
    /**
 
187
     * lock() - mark a session/namespace as readonly
 
188
     *
 
189
     * @return void
 
190
     */
 
191
    public function lock()
 
192
    {
 
193
        self::$_namespaceLocks[$this->_namespace] = true;
 
194
    }
 
195
 
 
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
 
 
208
    /**
 
209
     * unlockAll() - unmark all session/namespaces to enable read & write
 
210
     *
 
211
     * @return void
 
212
     */
 
213
    public static function unlockAll()
 
214
    {
 
215
        self::$_namespaceLocks = array();
 
216
    }
 
217
 
 
218
 
 
219
    /**
 
220
     * isLocked() - return lock status, true if, and only if, read-only
 
221
     *
 
222
     * @return bool
 
223
     */
 
224
    public function isLocked()
 
225
    {
 
226
        return isset(self::$_namespaceLocks[$this->_namespace]);
 
227
    }
 
228
 
 
229
 
 
230
    /**
 
231
     * unsetAll() - unset all variables in this namespace
 
232
     *
 
233
     * @return true
 
234
     */
 
235
    public function unsetAll()
 
236
    {
 
237
        return parent::_namespaceUnset($this->_namespace);
 
238
    }
 
239
 
 
240
 
 
241
    /**
 
242
     * __get() - method to get a variable in this object's current namespace
 
243
     *
 
244
     * @param string $name - programmatic name of a key, in a <key,value> pair in the current namespace
 
245
     * @return mixed
 
246
     */
 
247
    public function & __get($name)
 
248
    {
 
249
        if ($name === '') {
 
250
            /**
 
251
             * @see Zend_Session_Exception
 
252
             */
 
253
            require_once 'Zend/Session/Exception.php';
 
254
            throw new Zend_Session_Exception("The '$name' key must be a non-empty string");
 
255
        }
 
256
 
 
257
        return parent::_namespaceGet($this->_namespace, $name);
 
258
    }
 
259
 
 
260
 
 
261
    /**
 
262
     * __set() - method to set a variable/value in this object's namespace
 
263
     *
 
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
 
267
     * @return true
 
268
     */
 
269
    public function __set($name, $value)
 
270
    {
 
271
        if (isset(self::$_namespaceLocks[$this->_namespace])) {
 
272
            /**
 
273
             * @see Zend_Session_Exception
 
274
             */
 
275
            require_once 'Zend/Session/Exception.php';
 
276
            throw new Zend_Session_Exception('This session/namespace has been marked as read-only.');
 
277
        }
 
278
 
 
279
        if ($name === '') {
 
280
            /**
 
281
             * @see Zend_Session_Exception
 
282
             */
 
283
            require_once 'Zend/Session/Exception.php';
 
284
            throw new Zend_Session_Exception("The '$name' key must be a non-empty string");
 
285
        }
 
286
 
 
287
        if (parent::$_writable === false) {
 
288
            /**
 
289
             * @see Zend_Session_Exception
 
290
             */
 
291
            require_once 'Zend/Session/Exception.php';
 
292
            throw new Zend_Session_Exception(parent::_THROW_NOT_WRITABLE_MSG);
 
293
        }
 
294
 
 
295
        $name = (string) $name;
 
296
 
 
297
        $_SESSION[$this->_namespace][$name] = $value;
 
298
    }
 
299
 
 
300
 
 
301
    /**
 
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.
 
305
     *
 
306
     * Example:
 
307
     *   $namespace->apply('array_merge', array('tree' => 'apple', 'fruit' => 'peach'), array('flower' => 'rose'));
 
308
     *   $namespace->apply('count');
 
309
     *
 
310
     * @param string|array $callback - callback function
 
311
     */
 
312
    public function apply($callback)
 
313
    {
 
314
        $arg_list = func_get_args();
 
315
        $arg_list[0] = $_SESSION[$this->_namespace];
 
316
        return call_user_func_array($callback, $arg_list);
 
317
    }
 
318
 
 
319
 
 
320
    /**
 
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.
 
325
     *
 
326
     * Example:
 
327
     *   $namespace->applySet('array_merge', array('tree' => 'apple', 'fruit' => 'peach'), array('flower' => 'rose'));
 
328
     *
 
329
     * @param string|array $callback - callback function
 
330
     */
 
331
    public function applySet($callback)
 
332
    {
 
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)) {
 
337
            /**
 
338
             * @see Zend_Session_Exception
 
339
             */
 
340
            require_once 'Zend/Session/Exception.php';
 
341
            throw new Zend_Session_Exception('Result must be an array. Got: ' . gettype($result));
 
342
        }
 
343
        $_SESSION[$this->_namespace] = $result;
 
344
        return $result;
 
345
    }
 
346
 
 
347
 
 
348
    /**
 
349
     * __isset() - determine if a variable in this object's namespace is set
 
350
     *
 
351
     * @param string $name - programmatic name of a key, in a <key,value> pair in the current namespace
 
352
     * @return bool
 
353
     */
 
354
    public function __isset($name)
 
355
    {
 
356
        if ($name === '') {
 
357
            /**
 
358
             * @see Zend_Session_Exception
 
359
             */
 
360
            require_once 'Zend/Session/Exception.php';
 
361
            throw new Zend_Session_Exception("The '$name' key must be a non-empty string");
 
362
        }
 
363
 
 
364
        return parent::_namespaceIsset($this->_namespace, $name);
 
365
    }
 
366
 
 
367
 
 
368
    /**
 
369
     * __unset() - unset a variable in this object's namespace.
 
370
     *
 
371
     * @param string $name - programmatic name of a key, in a <key,value> pair in the current namespace
 
372
     * @return true
 
373
     */
 
374
    public function __unset($name)
 
375
    {
 
376
        if ($name === '') {
 
377
            /**
 
378
             * @see Zend_Session_Exception
 
379
             */
 
380
            require_once 'Zend/Session/Exception.php';
 
381
            throw new Zend_Session_Exception("The '$name' key must be a non-empty string");
 
382
        }
 
383
 
 
384
        return parent::_namespaceUnset($this->_namespace, $name);
 
385
    }
 
386
 
 
387
 
 
388
    /**
 
389
     * setExpirationSeconds() - expire the namespace, or specific variables after a specified
 
390
     * number of seconds
 
391
     *
 
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
 
395
     * @return void
 
396
     */
 
397
    public function setExpirationSeconds($seconds, $variables = null)
 
398
    {
 
399
        if (parent::$_writable === false) {
 
400
            /**
 
401
             * @see Zend_Session_Exception
 
402
             */
 
403
            require_once 'Zend/Session/Exception.php';
 
404
            throw new Zend_Session_Exception(parent::_THROW_NOT_WRITABLE_MSG);
 
405
        }
 
406
 
 
407
        if ($seconds <= 0) {
 
408
            /**
 
409
             * @see Zend_Session_Exception
 
410
             */
 
411
            require_once 'Zend/Session/Exception.php';
 
412
            throw new Zend_Session_Exception('Seconds must be positive.');
 
413
        }
 
414
 
 
415
        if ($variables === null) {
 
416
 
 
417
            // apply expiration to entire namespace
 
418
            $_SESSION['__ZF'][$this->_namespace]['ENT'] = time() + $seconds;
 
419
 
 
420
        } else {
 
421
 
 
422
            if (is_string($variables)) {
 
423
                $variables = array($variables);
 
424
            }
 
425
 
 
426
            foreach ($variables as $variable) {
 
427
                if (!empty($variable)) {
 
428
                    $_SESSION['__ZF'][$this->_namespace]['ENVT'][$variable] = time() + $seconds;
 
429
                }
 
430
            }
 
431
        }
 
432
    }
 
433
 
 
434
 
 
435
    /**
 
436
     * setExpirationHops() - expire the namespace, or specific variables after a specified
 
437
     * number of page hops
 
438
     *
 
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
 
443
     * @return void
 
444
     */
 
445
    public function setExpirationHops($hops, $variables = null, $hopCountOnUsageOnly = false)
 
446
    {
 
447
        if (parent::$_writable === false) {
 
448
            /**
 
449
             * @see Zend_Session_Exception
 
450
             */
 
451
            require_once 'Zend/Session/Exception.php';
 
452
            throw new Zend_Session_Exception(parent::_THROW_NOT_WRITABLE_MSG);
 
453
        }
 
454
 
 
455
        if ($hops <= 0) {
 
456
            /**
 
457
             * @see Zend_Session_Exception
 
458
             */
 
459
            require_once 'Zend/Session/Exception.php';
 
460
            throw new Zend_Session_Exception('Hops must be positive number.');
 
461
        }
 
462
 
 
463
        if ($variables === null) {
 
464
 
 
465
            // apply expiration to entire namespace
 
466
            if ($hopCountOnUsageOnly === false) {
 
467
                $_SESSION['__ZF'][$this->_namespace]['ENGH'] = $hops;
 
468
            } else {
 
469
                $_SESSION['__ZF'][$this->_namespace]['ENNH'] = $hops;
 
470
            }
 
471
 
 
472
        } else {
 
473
 
 
474
            if (is_string($variables)) {
 
475
                $variables = array($variables);
 
476
            }
 
477
 
 
478
            foreach ($variables as $variable) {
 
479
                if (!empty($variable)) {
 
480
                    if ($hopCountOnUsageOnly === false) {
 
481
                        $_SESSION['__ZF'][$this->_namespace]['ENVGH'][$variable] = $hops;
 
482
                    } else {
 
483
                        $_SESSION['__ZF'][$this->_namespace]['ENVNH'][$variable] = $hops;
 
484
                    }
 
485
                }
 
486
            }
 
487
        }
 
488
    }
 
489
 
 
490
}