Abstract.php 5.9 KB

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