Abstract.php
171 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: Abstract.php 23775 2011-03-01 17:25:24Z ralph $ |
| 22 | * @since Preview Release 0.2 |
| 23 | */ |
| 24 | /** |
| 25 | * Zend_Session_Abstract |
| 26 | * |
| 27 | * @category Zend |
| 28 | * @package Zend_Session |
| 29 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 30 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 31 | */ |
| 32 | abstract class Zend_Session_Abstract |
| 33 | { |
| 34 | /** |
| 35 | * Whether or not session permits writing (modification of $_SESSION[]) |
| 36 | * |
| 37 | * @var bool |
| 38 | */ |
| 39 | protected static $_writable = \false; |
| 40 | /** |
| 41 | * Whether or not session permits reading (reading data in $_SESSION[]) |
| 42 | * |
| 43 | * @var bool |
| 44 | */ |
| 45 | protected static $_readable = \false; |
| 46 | /** |
| 47 | * Since expiring data is handled at startup to avoid __destruct difficulties, |
| 48 | * the data that will be expiring at end of this request is held here |
| 49 | * |
| 50 | * @var array |
| 51 | */ |
| 52 | protected static $_expiringData = array(); |
| 53 | /** |
| 54 | * Error message thrown when an action requires modification, |
| 55 | * but current Zend_Session has been marked as read-only. |
| 56 | */ |
| 57 | const _THROW_NOT_WRITABLE_MSG = 'Zend_Session is currently marked as read-only.'; |
| 58 | /** |
| 59 | * Error message thrown when an action requires reading session data, |
| 60 | * but current Zend_Session is not marked as readable. |
| 61 | */ |
| 62 | const _THROW_NOT_READABLE_MSG = 'Zend_Session is not marked as readable.'; |
| 63 | /** |
| 64 | * namespaceIsset() - check to see if a namespace or a variable within a namespace is set |
| 65 | * |
| 66 | * @param string $namespace |
| 67 | * @param string $name |
| 68 | * @return bool |
| 69 | */ |
| 70 | protected static function _namespaceIsset($namespace, $name = null) |
| 71 | { |
| 72 | if (self::$_readable === \false) { |
| 73 | /** |
| 74 | * @see Zend_Session_Exception |
| 75 | */ |
| 76 | // require_once 'Zend/Session/Exception.php'; |
| 77 | throw new \Zend_Session_Exception(self::_THROW_NOT_READABLE_MSG); |
| 78 | } |
| 79 | if ($name === null) { |
| 80 | return isset($_SESSION[$namespace]) || isset(self::$_expiringData[$namespace]); |
| 81 | } else { |
| 82 | return isset($_SESSION[$namespace][$name]) || isset(self::$_expiringData[$namespace][$name]); |
| 83 | } |
| 84 | } |
| 85 | /** |
| 86 | * namespaceUnset() - unset a namespace or a variable within a namespace |
| 87 | * |
| 88 | * @param string $namespace |
| 89 | * @param string $name |
| 90 | * @throws Zend_Session_Exception |
| 91 | * @return void |
| 92 | */ |
| 93 | protected static function _namespaceUnset($namespace, $name = null) |
| 94 | { |
| 95 | if (self::$_writable === \false) { |
| 96 | /** |
| 97 | * @see Zend_Session_Exception |
| 98 | */ |
| 99 | // require_once 'Zend/Session/Exception.php'; |
| 100 | throw new \Zend_Session_Exception(self::_THROW_NOT_WRITABLE_MSG); |
| 101 | } |
| 102 | $name = (string) $name; |
| 103 | // check to see if the api wanted to remove a var from a namespace or a namespace |
| 104 | if ($name === '') { |
| 105 | unset($_SESSION[$namespace]); |
| 106 | unset(self::$_expiringData[$namespace]); |
| 107 | } else { |
| 108 | unset($_SESSION[$namespace][$name]); |
| 109 | unset(self::$_expiringData[$namespace]); |
| 110 | } |
| 111 | // if we remove the last value, remove namespace. |
| 112 | if (empty($_SESSION[$namespace])) { |
| 113 | unset($_SESSION[$namespace]); |
| 114 | } |
| 115 | } |
| 116 | /** |
| 117 | * namespaceGet() - Get $name variable from $namespace, returning by reference. |
| 118 | * |
| 119 | * @param string $namespace |
| 120 | * @param string $name |
| 121 | * @return mixed |
| 122 | */ |
| 123 | protected static function &_namespaceGet($namespace, $name = null) |
| 124 | { |
| 125 | if (self::$_readable === \false) { |
| 126 | /** |
| 127 | * @see Zend_Session_Exception |
| 128 | */ |
| 129 | // require_once 'Zend/Session/Exception.php'; |
| 130 | throw new \Zend_Session_Exception(self::_THROW_NOT_READABLE_MSG); |
| 131 | } |
| 132 | if ($name === null) { |
| 133 | if (isset($_SESSION[$namespace])) { |
| 134 | // check session first for data requested |
| 135 | return $_SESSION[$namespace]; |
| 136 | } elseif (isset(self::$_expiringData[$namespace])) { |
| 137 | // check expiring data for data reqeusted |
| 138 | return self::$_expiringData[$namespace]; |
| 139 | } else { |
| 140 | return $_SESSION[$namespace]; |
| 141 | // satisfy return by reference |
| 142 | } |
| 143 | } else { |
| 144 | if (isset($_SESSION[$namespace][$name])) { |
| 145 | // check session first |
| 146 | return $_SESSION[$namespace][$name]; |
| 147 | } elseif (isset(self::$_expiringData[$namespace][$name])) { |
| 148 | // check expiring data |
| 149 | return self::$_expiringData[$namespace][$name]; |
| 150 | } else { |
| 151 | return $_SESSION[$namespace][$name]; |
| 152 | // satisfy return by reference |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | /** |
| 157 | * namespaceGetAll() - Get an array containing $namespace, including expiring data. |
| 158 | * |
| 159 | * @param string $namespace |
| 160 | * @param string $name |
| 161 | * @return mixed |
| 162 | */ |
| 163 | protected static function _namespaceGetAll($namespace) |
| 164 | { |
| 165 | $currentData = isset($_SESSION[$namespace]) && \is_array($_SESSION[$namespace]) ? $_SESSION[$namespace] : array(); |
| 166 | $expiringData = isset(self::$_expiringData[$namespace]) && \is_array(self::$_expiringData[$namespace]) ? self::$_expiringData[$namespace] : array(); |
| 167 | return \array_merge($currentData, $expiringData); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 |