File indexing completed on 2024-05-12 06:03:06

0001 <?php
0002 
0003 /**
0004  * Zend Framework
0005  *
0006  * LICENSE
0007  *
0008  * This source file is subject to the new BSD license that is bundled
0009  * with this package in the file LICENSE.txt.
0010  * It is also available through the world-wide-web at this URL:
0011  * http://framework.zend.com/license/new-bsd
0012  * If you did not receive a copy of the license and are unable to
0013  * obtain it through the world-wide-web, please send an email
0014  * to license@zend.com so we can send you a copy immediately.
0015  *
0016  * @category   Zend
0017  * @package    Zend_Session
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  * @since      Preview Release 0.2
0022  */
0023 
0024 
0025 /**
0026  * Zend_Session_Abstract
0027  *
0028  * @category   Zend
0029  * @package    Zend_Session
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 abstract class Zend_Session_Abstract
0034 {
0035     /**
0036      * Whether or not session permits writing (modification of $_SESSION[])
0037      *
0038      * @var bool
0039      */
0040     protected static $_writable = false;
0041 
0042     /**
0043      * Whether or not session permits reading (reading data in $_SESSION[])
0044      *
0045      * @var bool
0046      */
0047     protected static $_readable = false;
0048 
0049     /**
0050      * Since expiring data is handled at startup to avoid __destruct difficulties,
0051      * the data that will be expiring at end of this request is held here
0052      *
0053      * @var array
0054      */
0055     protected static $_expiringData = array();
0056 
0057 
0058     /**
0059      * Error message thrown when an action requires modification,
0060      * but current Zend_Session has been marked as read-only.
0061      */
0062     const _THROW_NOT_WRITABLE_MSG = 'Zend_Session is currently marked as read-only.';
0063 
0064 
0065     /**
0066      * Error message thrown when an action requires reading session data,
0067      * but current Zend_Session is not marked as readable.
0068      */
0069     const _THROW_NOT_READABLE_MSG = 'Zend_Session is not marked as readable.';
0070 
0071 
0072     /**
0073      * namespaceIsset() - check to see if a namespace or a variable within a namespace is set
0074      *
0075      * @param  string $namespace
0076      * @param  string $name
0077      * @return bool
0078      */
0079     protected static function _namespaceIsset($namespace, $name = null)
0080     {
0081         if (self::$_readable === false) {
0082             /**
0083              * @see Zend_Session_Exception
0084              */
0085             // require_once 'Zend/Session/Exception.php';
0086             throw new Zend_Session_Exception(self::_THROW_NOT_READABLE_MSG);
0087         }
0088 
0089         if ($name === null) {
0090             return ( isset($_SESSION[$namespace]) || isset(self::$_expiringData[$namespace]) );
0091         } else {
0092             return ( isset($_SESSION[$namespace][$name]) || isset(self::$_expiringData[$namespace][$name]) );
0093         }
0094     }
0095 
0096 
0097     /**
0098      * namespaceUnset() - unset a namespace or a variable within a namespace
0099      *
0100      * @param  string $namespace
0101      * @param  string $name
0102      * @throws Zend_Session_Exception
0103      * @return void
0104      */
0105     protected static function _namespaceUnset($namespace, $name = null)
0106     {
0107         if (self::$_writable === false) {
0108             /**
0109              * @see Zend_Session_Exception
0110              */
0111             // require_once 'Zend/Session/Exception.php';
0112             throw new Zend_Session_Exception(self::_THROW_NOT_WRITABLE_MSG);
0113         }
0114 
0115         $name = (string) $name;
0116 
0117         // check to see if the api wanted to remove a var from a namespace or a namespace
0118         if ($name === '') {
0119             unset($_SESSION[$namespace]);
0120             unset(self::$_expiringData[$namespace]);
0121         } else {
0122             unset($_SESSION[$namespace][$name]);
0123             unset(self::$_expiringData[$namespace][$name]);
0124         }
0125 
0126         // if we remove the last value, remove namespace.
0127         if (empty($_SESSION[$namespace])) {
0128             unset($_SESSION[$namespace]);
0129         }
0130     }
0131 
0132 
0133     /**
0134      * namespaceGet() - Get $name variable from $namespace, returning by reference.
0135      *
0136      * @param  string $namespace
0137      * @param  string $name
0138      * @return mixed
0139      */
0140     protected static function & _namespaceGet($namespace, $name = null)
0141     {
0142         if (self::$_readable === false) {
0143             /**
0144              * @see Zend_Session_Exception
0145              */
0146             // require_once 'Zend/Session/Exception.php';
0147             throw new Zend_Session_Exception(self::_THROW_NOT_READABLE_MSG);
0148         }
0149 
0150         if ($name === null) {
0151             if (isset($_SESSION[$namespace])) { // check session first for data requested
0152                 return $_SESSION[$namespace];
0153             } elseif (isset(self::$_expiringData[$namespace])) { // check expiring data for data reqeusted
0154                 return self::$_expiringData[$namespace];
0155             } else {
0156                 return $_SESSION[$namespace]; // satisfy return by reference
0157             }
0158         } else {
0159             if (isset($_SESSION[$namespace][$name])) { // check session first
0160                 return $_SESSION[$namespace][$name];
0161             } elseif (isset(self::$_expiringData[$namespace][$name])) { // check expiring data
0162                 return self::$_expiringData[$namespace][$name];
0163             } else {
0164                 return $_SESSION[$namespace][$name]; // satisfy return by reference
0165             }
0166         }
0167     }
0168 
0169 
0170     /**
0171      * namespaceGetAll() - Get an array containing $namespace, including expiring data.
0172      *
0173      * @param string $namespace
0174      * @param string $name
0175      * @return mixed
0176      */
0177     protected static function _namespaceGetAll($namespace)
0178     {
0179         $currentData  = (isset($_SESSION[$namespace]) && is_array($_SESSION[$namespace])) ?
0180             $_SESSION[$namespace] : array();
0181         $expiringData = (isset(self::$_expiringData[$namespace]) && is_array(self::$_expiringData[$namespace])) ?
0182             self::$_expiringData[$namespace] : array();
0183         return array_merge($currentData, $expiringData);
0184     }
0185 }