web/lib/Zend/Session/Abstract.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * Zend Framework
       
     5  *
       
     6  * LICENSE
       
     7  *
       
     8  * This source file is subject to the new BSD license that is bundled
       
     9  * with this package in the file LICENSE.txt.
       
    10  * It is also available through the world-wide-web at this URL:
       
    11  * http://framework.zend.com/license/new-bsd
       
    12  * If you did not receive a copy of the license and are unable to
       
    13  * obtain it through the world-wide-web, please send an email
       
    14  * to license@zend.com so we can send you a copy immediately.
       
    15  *
       
    16  * @category   Zend
       
    17  * @package    Zend_Session
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Abstract.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  * @since      Preview Release 0.2
       
    22  */
       
    23 
       
    24 
       
    25 /**
       
    26  * Zend_Session_Abstract
       
    27  *
       
    28  * @category   Zend
       
    29  * @package    Zend_Session
       
    30  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    32  */
       
    33 abstract class Zend_Session_Abstract
       
    34 {
       
    35     /**
       
    36      * Whether or not session permits writing (modification of $_SESSION[])
       
    37      *
       
    38      * @var bool
       
    39      */
       
    40     protected static $_writable = false;
       
    41 
       
    42     /**
       
    43      * Whether or not session permits reading (reading data in $_SESSION[])
       
    44      *
       
    45      * @var bool
       
    46      */
       
    47     protected static $_readable = false;
       
    48 
       
    49     /**
       
    50      * Since expiring data is handled at startup to avoid __destruct difficulties,
       
    51      * the data that will be expiring at end of this request is held here
       
    52      *
       
    53      * @var array
       
    54      */
       
    55     protected static $_expiringData = array();
       
    56 
       
    57 
       
    58     /**
       
    59      * Error message thrown when an action requires modification,
       
    60      * but current Zend_Session has been marked as read-only.
       
    61      */
       
    62     const _THROW_NOT_WRITABLE_MSG = 'Zend_Session is currently marked as read-only.';
       
    63 
       
    64 
       
    65     /**
       
    66      * Error message thrown when an action requires reading session data,
       
    67      * but current Zend_Session is not marked as readable.
       
    68      */
       
    69     const _THROW_NOT_READABLE_MSG = 'Zend_Session is not marked as readable.';
       
    70 
       
    71 
       
    72     /**
       
    73      * namespaceIsset() - check to see if a namespace or a variable within a namespace is set
       
    74      *
       
    75      * @param  string $namespace
       
    76      * @param  string $name
       
    77      * @return bool
       
    78      */
       
    79     protected static function _namespaceIsset($namespace, $name = null)
       
    80     {
       
    81         if (self::$_readable === false) {
       
    82             /**
       
    83              * @see Zend_Session_Exception
       
    84              */
       
    85             require_once 'Zend/Session/Exception.php';
       
    86             throw new Zend_Session_Exception(self::_THROW_NOT_READABLE_MSG);
       
    87         }
       
    88 
       
    89         if ($name === null) {
       
    90             return ( isset($_SESSION[$namespace]) || isset(self::$_expiringData[$namespace]) );
       
    91         } else {
       
    92             return ( isset($_SESSION[$namespace][$name]) || isset(self::$_expiringData[$namespace][$name]) );
       
    93         }
       
    94     }
       
    95 
       
    96 
       
    97     /**
       
    98      * namespaceUnset() - unset a namespace or a variable within a namespace
       
    99      *
       
   100      * @param  string $namespace
       
   101      * @param  string $name
       
   102      * @throws Zend_Session_Exception
       
   103      * @return void
       
   104      */
       
   105     protected static function _namespaceUnset($namespace, $name = null)
       
   106     {
       
   107         if (self::$_writable === false) {
       
   108             /**
       
   109              * @see Zend_Session_Exception
       
   110              */
       
   111             require_once 'Zend/Session/Exception.php';
       
   112             throw new Zend_Session_Exception(self::_THROW_NOT_WRITABLE_MSG);
       
   113         }
       
   114 
       
   115         $name = (string) $name;
       
   116 
       
   117         // check to see if the api wanted to remove a var from a namespace or a namespace
       
   118         if ($name === '') {
       
   119             unset($_SESSION[$namespace]);
       
   120             unset(self::$_expiringData[$namespace]);
       
   121         } else {
       
   122             unset($_SESSION[$namespace][$name]);
       
   123             unset(self::$_expiringData[$namespace]);
       
   124         }
       
   125 
       
   126         // if we remove the last value, remove namespace.
       
   127         if (empty($_SESSION[$namespace])) {
       
   128             unset($_SESSION[$namespace]);
       
   129         }
       
   130     }
       
   131 
       
   132 
       
   133     /**
       
   134      * namespaceGet() - Get $name variable from $namespace, returning by reference.
       
   135      *
       
   136      * @param  string $namespace
       
   137      * @param  string $name
       
   138      * @return mixed
       
   139      */
       
   140     protected static function & _namespaceGet($namespace, $name = null)
       
   141     {
       
   142         if (self::$_readable === false) {
       
   143             /**
       
   144              * @see Zend_Session_Exception
       
   145              */
       
   146             require_once 'Zend/Session/Exception.php';
       
   147             throw new Zend_Session_Exception(self::_THROW_NOT_READABLE_MSG);
       
   148         }
       
   149 
       
   150         if ($name === null) {
       
   151             if (isset($_SESSION[$namespace])) { // check session first for data requested
       
   152                 return $_SESSION[$namespace];
       
   153             } elseif (isset(self::$_expiringData[$namespace])) { // check expiring data for data reqeusted
       
   154                 return self::$_expiringData[$namespace];
       
   155             } else {
       
   156                 return $_SESSION[$namespace]; // satisfy return by reference
       
   157             }
       
   158         } else {
       
   159             if (isset($_SESSION[$namespace][$name])) { // check session first
       
   160                 return $_SESSION[$namespace][$name];
       
   161             } elseif (isset(self::$_expiringData[$namespace][$name])) { // check expiring data
       
   162                 return self::$_expiringData[$namespace][$name];
       
   163             } else {
       
   164                 return $_SESSION[$namespace][$name]; // satisfy return by reference
       
   165             }
       
   166         }
       
   167     }
       
   168 
       
   169 
       
   170     /**
       
   171      * namespaceGetAll() - Get an array containing $namespace, including expiring data.
       
   172      *
       
   173      * @param string $namespace
       
   174      * @param string $name
       
   175      * @return mixed
       
   176      */
       
   177     protected static function _namespaceGetAll($namespace)
       
   178     {
       
   179         $currentData  = (isset($_SESSION[$namespace]) && is_array($_SESSION[$namespace])) ?
       
   180             $_SESSION[$namespace] : array();
       
   181         $expiringData = (isset(self::$_expiringData[$namespace]) && is_array(self::$_expiringData[$namespace])) ?
       
   182             self::$_expiringData[$namespace] : array();
       
   183         return array_merge($currentData, $expiringData);
       
   184     }
       
   185 }