Session.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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-2009 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. * @see Zend_Session_Abstract
  24. */
  25. require_once 'Zend/Session/Abstract.php';
  26. /**
  27. * @see Zend_Session_Namespace
  28. */
  29. require_once 'Zend/Session/Namespace.php';
  30. /**
  31. * @see Zend_Session_SaveHandler_Interface
  32. */
  33. require_once 'Zend/Session/SaveHandler/Interface.php';
  34. /**
  35. * Zend_Session
  36. *
  37. * @category Zend
  38. * @package Zend_Session
  39. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Zend_Session extends Zend_Session_Abstract
  43. {
  44. /**
  45. * Whether or not Zend_Session is being used with unit tests
  46. *
  47. * @internal
  48. * @var bool
  49. */
  50. public static $_unitTestEnabled = false;
  51. /**
  52. * $_throwStartupException
  53. *
  54. * @var bool|bitset This could also be a combiniation of error codes to catch
  55. */
  56. protected static $_throwStartupExceptions = true;
  57. /**
  58. * Check whether or not the session was started
  59. *
  60. * @var bool
  61. */
  62. private static $_sessionStarted = false;
  63. /**
  64. * Whether or not the session id has been regenerated this request.
  65. *
  66. * Id regeneration state
  67. * <0 - regenerate requested when session is started
  68. * 0 - do nothing
  69. * >0 - already called session_regenerate_id()
  70. *
  71. * @var int
  72. */
  73. private static $_regenerateIdState = 0;
  74. /**
  75. * Private list of php's ini values for ext/session
  76. * null values will default to the php.ini value, otherwise
  77. * the value below will overwrite the default ini value, unless
  78. * the user has set an option explicity with setOptions()
  79. *
  80. * @var array
  81. */
  82. private static $_defaultOptions = array(
  83. 'save_path' => null,
  84. 'name' => null, /* this should be set to a unique value for each application */
  85. 'save_handler' => null,
  86. //'auto_start' => null, /* intentionally excluded (see manual) */
  87. 'gc_probability' => null,
  88. 'gc_divisor' => null,
  89. 'gc_maxlifetime' => null,
  90. 'serialize_handler' => null,
  91. 'cookie_lifetime' => null,
  92. 'cookie_path' => null,
  93. 'cookie_domain' => null,
  94. 'cookie_secure' => null,
  95. 'cookie_httponly' => null,
  96. 'use_cookies' => null,
  97. 'use_only_cookies' => 'on',
  98. 'referer_check' => null,
  99. 'entropy_file' => null,
  100. 'entropy_length' => null,
  101. 'cache_limiter' => null,
  102. 'cache_expire' => null,
  103. 'use_trans_sid' => null,
  104. 'bug_compat_42' => null,
  105. 'bug_compat_warn' => null,
  106. 'hash_function' => null,
  107. 'hash_bits_per_character' => null
  108. );
  109. /**
  110. * List of options pertaining to Zend_Session that can be set by developers
  111. * using Zend_Session::setOptions(). This list intentionally duplicates
  112. * the individual declaration of static "class" variables by the same names.
  113. *
  114. * @var array
  115. */
  116. private static $_localOptions = array(
  117. 'strict' => '_strict',
  118. 'remember_me_seconds' => '_rememberMeSeconds',
  119. 'throw_startup_exceptions' => '_throwStartupExceptions'
  120. );
  121. /**
  122. * Whether or not write close has been performed.
  123. *
  124. * @var bool
  125. */
  126. private static $_writeClosed = false;
  127. /**
  128. * Whether or not session id cookie has been deleted
  129. *
  130. * @var bool
  131. */
  132. private static $_sessionCookieDeleted = false;
  133. /**
  134. * Whether or not session has been destroyed via session_destroy()
  135. *
  136. * @var bool
  137. */
  138. private static $_destroyed = false;
  139. /**
  140. * Whether or not session must be initiated before usage
  141. *
  142. * @var bool
  143. */
  144. private static $_strict = false;
  145. /**
  146. * Default number of seconds the session will be remembered for when asked to be remembered
  147. *
  148. * @var int
  149. */
  150. private static $_rememberMeSeconds = 1209600; // 2 weeks
  151. /**
  152. * Whether the default options listed in Zend_Session::$_localOptions have been set
  153. *
  154. * @var bool
  155. */
  156. private static $_defaultOptionsSet = false;
  157. /**
  158. * A reference to the set session save handler
  159. *
  160. * @var Zend_Session_SaveHandler_Interface
  161. */
  162. private static $_saveHandler = null;
  163. /**
  164. * Constructor overriding - make sure that a developer cannot instantiate
  165. */
  166. protected function __construct()
  167. {
  168. }
  169. /**
  170. * setOptions - set both the class specified
  171. *
  172. * @param array $userOptions - pass-by-keyword style array of <option name, option value> pairs
  173. * @throws Zend_Session_Exception
  174. * @return void
  175. */
  176. public static function setOptions(array $userOptions = array())
  177. {
  178. // set default options on first run only (before applying user settings)
  179. if (!self::$_defaultOptionsSet) {
  180. foreach (self::$_defaultOptions as $defaultOptionName => $defaultOptionValue) {
  181. if (isset(self::$_defaultOptions[$defaultOptionName])) {
  182. ini_set("session.$defaultOptionName", $defaultOptionValue);
  183. }
  184. }
  185. self::$_defaultOptionsSet = true;
  186. }
  187. // set the options the user has requested to set
  188. foreach ($userOptions as $userOptionName => $userOptionValue) {
  189. $userOptionName = strtolower($userOptionName);
  190. // set the ini based values
  191. if (array_key_exists($userOptionName, self::$_defaultOptions)) {
  192. ini_set("session.$userOptionName", $userOptionValue);
  193. }
  194. elseif (isset(self::$_localOptions[$userOptionName])) {
  195. self::${self::$_localOptions[$userOptionName]} = $userOptionValue;
  196. }
  197. else {
  198. /** @see Zend_Session_Exception */
  199. require_once 'Zend/Session/Exception.php';
  200. throw new Zend_Session_Exception("Unknown option: $userOptionName = $userOptionValue");
  201. }
  202. }
  203. }
  204. /**
  205. * getOptions()
  206. *
  207. * @param string $optionName OPTIONAL
  208. * @return array|string
  209. */
  210. public static function getOptions($optionName = null)
  211. {
  212. $options = array();
  213. foreach (ini_get_all('session') as $sysOptionName => $sysOptionValues) {
  214. $options[substr($sysOptionName, 8)] = $sysOptionValues['local_value'];
  215. }
  216. foreach (self::$_localOptions as $localOptionName => $localOptionMemberName) {
  217. $options[$localOptionName] = self::${$localOptionMemberName};
  218. }
  219. if ($optionName) {
  220. if (array_key_exists($optionName, $options)) {
  221. return $options[$optionName];
  222. }
  223. return null;
  224. }
  225. return $options;
  226. }
  227. /**
  228. * setSaveHandler() - Session Save Handler assignment
  229. *
  230. * @param Zend_Session_SaveHandler_Interface $interface
  231. * @return void
  232. */
  233. public static function setSaveHandler(Zend_Session_SaveHandler_Interface $saveHandler)
  234. {
  235. self::$_saveHandler = $saveHandler;
  236. if (self::$_unitTestEnabled) {
  237. return;
  238. }
  239. session_set_save_handler(
  240. array(&$saveHandler, 'open'),
  241. array(&$saveHandler, 'close'),
  242. array(&$saveHandler, 'read'),
  243. array(&$saveHandler, 'write'),
  244. array(&$saveHandler, 'destroy'),
  245. array(&$saveHandler, 'gc')
  246. );
  247. }
  248. /**
  249. * getSaveHandler() - Get the session Save Handler
  250. *
  251. * @return Zend_Session_SaveHandler_Interface
  252. */
  253. public static function getSaveHandler()
  254. {
  255. return self::$_saveHandler;
  256. }
  257. /**
  258. * regenerateId() - Regenerate the session id. Best practice is to call this after
  259. * session is started. If called prior to session starting, session id will be regenerated
  260. * at start time.
  261. *
  262. * @throws Zend_Session_Exception
  263. * @return void
  264. */
  265. public static function regenerateId()
  266. {
  267. if (!self::$_unitTestEnabled && headers_sent($filename, $linenum)) {
  268. /** @see Zend_Session_Exception */
  269. require_once 'Zend/Session/Exception.php';
  270. throw new Zend_Session_Exception("You must call " . __CLASS__ . '::' . __FUNCTION__ .
  271. "() before any output has been sent to the browser; output started in {$filename}/{$linenum}");
  272. }
  273. if (self::$_sessionStarted && self::$_regenerateIdState <= 0) {
  274. if (!self::$_unitTestEnabled) {
  275. session_regenerate_id(true);
  276. }
  277. self::$_regenerateIdState = 1;
  278. } else {
  279. /**
  280. * @todo If we can detect that this requester had no session previously,
  281. * then why regenerate the id before the session has started?
  282. * Feedback wanted for:
  283. //
  284. if (isset($_COOKIE[session_name()]) || (!use only cookies && isset($_REQUEST[session_name()]))) {
  285. self::$_regenerateIdState = 1;
  286. } else {
  287. self::$_regenerateIdState = -1;
  288. }
  289. //*/
  290. self::$_regenerateIdState = -1;
  291. }
  292. }
  293. /**
  294. * rememberMe() - Write a persistent cookie that expires after a number of seconds in the future. If no number of
  295. * seconds is specified, then this defaults to self::$_rememberMeSeconds. Due to clock errors on end users' systems,
  296. * large values are recommended to avoid undesirable expiration of session cookies.
  297. *
  298. * @param $seconds integer - OPTIONAL specifies TTL for cookie in seconds from present time
  299. * @return void
  300. */
  301. public static function rememberMe($seconds = null)
  302. {
  303. $seconds = (int) $seconds;
  304. $seconds = ($seconds > 0) ? $seconds : self::$_rememberMeSeconds;
  305. self::rememberUntil($seconds);
  306. }
  307. /**
  308. * forgetMe() - Write a volatile session cookie, removing any persistent cookie that may have existed. The session
  309. * would end upon, for example, termination of a web browser program.
  310. *
  311. * @return void
  312. */
  313. public static function forgetMe()
  314. {
  315. self::rememberUntil(0);
  316. }
  317. /**
  318. * rememberUntil() - This method does the work of changing the state of the session cookie and making
  319. * sure that it gets resent to the browser via regenerateId()
  320. *
  321. * @param int $seconds
  322. * @return void
  323. */
  324. public static function rememberUntil($seconds = 0)
  325. {
  326. if (self::$_unitTestEnabled) {
  327. self::regenerateId();
  328. return;
  329. }
  330. $cookieParams = session_get_cookie_params();
  331. session_set_cookie_params(
  332. $seconds,
  333. $cookieParams['path'],
  334. $cookieParams['domain'],
  335. $cookieParams['secure']
  336. );
  337. // normally "rememberMe()" represents a security context change, so should use new session id
  338. self::regenerateId();
  339. }
  340. /**
  341. * sessionExists() - whether or not a session exists for the current request
  342. *
  343. * @return bool
  344. */
  345. public static function sessionExists()
  346. {
  347. if (ini_get('session.use_cookies') == '1' && isset($_COOKIE[session_name()])) {
  348. return true;
  349. } elseif (!empty($_REQUEST[session_name()])) {
  350. return true;
  351. } elseif (self::$_unitTestEnabled) {
  352. return true;
  353. }
  354. return false;
  355. }
  356. /**
  357. * Whether or not session has been destroyed via session_destroy()
  358. *
  359. * @return bool
  360. */
  361. public static function isDestroyed()
  362. {
  363. return self::$_destroyed;
  364. }
  365. /**
  366. * start() - Start the session.
  367. *
  368. * @param bool|array $options OPTIONAL Either user supplied options, or flag indicating if start initiated automatically
  369. * @throws Zend_Session_Exception
  370. * @return void
  371. */
  372. public static function start($options = false)
  373. {
  374. if (self::$_sessionStarted && self::$_destroyed) {
  375. require_once 'Zend/Session/Exception.php';
  376. throw new Zend_Session_Exception('The session was explicitly destroyed during this request, attempting to re-start is not allowed.');
  377. }
  378. if (self::$_sessionStarted) {
  379. return; // already started
  380. }
  381. // make sure our default options (at the least) have been set
  382. if (!self::$_defaultOptionsSet) {
  383. self::setOptions(is_array($options) ? $options : array());
  384. }
  385. // In strict mode, do not allow auto-starting Zend_Session, such as via "new Zend_Session_Namespace()"
  386. if (self::$_strict && $options === true) {
  387. /** @see Zend_Session_Exception */
  388. require_once 'Zend/Session/Exception.php';
  389. throw new Zend_Session_Exception('You must explicitly start the session with Zend_Session::start() when session options are set to strict.');
  390. }
  391. $filename = $linenum = null;
  392. if (!self::$_unitTestEnabled && headers_sent($filename, $linenum)) {
  393. /** @see Zend_Session_Exception */
  394. require_once 'Zend/Session/Exception.php';
  395. throw new Zend_Session_Exception("Session must be started before any output has been sent to the browser;"
  396. . " output started in {$filename}/{$linenum}");
  397. }
  398. // See http://www.php.net/manual/en/ref.session.php for explanation
  399. if (!self::$_unitTestEnabled && defined('SID')) {
  400. /** @see Zend_Session_Exception */
  401. require_once 'Zend/Session/Exception.php';
  402. throw new Zend_Session_Exception('session has already been started by session.auto-start or session_start()');
  403. }
  404. /**
  405. * Hack to throw exceptions on start instead of php errors
  406. * @see http://framework.zend.com/issues/browse/ZF-1325
  407. */
  408. $errorLevel = (is_int(self::$_throwStartupExceptions)) ? self::$_throwStartupExceptions : E_ALL;
  409. /** @see Zend_Session_Exception */
  410. if (!self::$_unitTestEnabled) {
  411. if (self::$_throwStartupExceptions) {
  412. require_once 'Zend/Session/Exception.php';
  413. set_error_handler(array('Zend_Session_Exception', 'handleSessionStartError'), $errorLevel);
  414. }
  415. $startedCleanly = session_start();
  416. if (self::$_throwStartupExceptions) {
  417. restore_error_handler();
  418. }
  419. if (!$startedCleanly || Zend_Session_Exception::$sessionStartError != null) {
  420. if (self::$_throwStartupExceptions) {
  421. set_error_handler(array('Zend_Session_Exception', 'handleSilentWriteClose'), $errorLevel);
  422. }
  423. session_write_close();
  424. if (self::$_throwStartupExceptions) {
  425. restore_error_handler();
  426. throw new Zend_Session_Exception(__CLASS__ . '::' . __FUNCTION__ . '() - ' . Zend_Session_Exception::$sessionStartError);
  427. }
  428. }
  429. }
  430. parent::$_readable = true;
  431. parent::$_writable = true;
  432. self::$_sessionStarted = true;
  433. if (self::$_regenerateIdState === -1) {
  434. self::regenerateId();
  435. }
  436. // run validators if they exist
  437. if (isset($_SESSION['__ZF']['VALID'])) {
  438. self::_processValidators();
  439. }
  440. self::_processStartupMetadataGlobal();
  441. }
  442. /**
  443. * _processGlobalMetadata() - this method initizes the sessions GLOBAL
  444. * metadata, mostly global data expiration calculations.
  445. *
  446. * @return void
  447. */
  448. private static function _processStartupMetadataGlobal()
  449. {
  450. // process global metadata
  451. if (isset($_SESSION['__ZF'])) {
  452. // expire globally expired values
  453. foreach ($_SESSION['__ZF'] as $namespace => $namespace_metadata) {
  454. // Expire Namespace by Time (ENT)
  455. if (isset($namespace_metadata['ENT']) && ($namespace_metadata['ENT'] > 0) && (time() > $namespace_metadata['ENT']) ) {
  456. unset($_SESSION[$namespace]);
  457. unset($_SESSION['__ZF'][$namespace]['ENT']);
  458. }
  459. // Expire Namespace by Global Hop (ENGH)
  460. if (isset($namespace_metadata['ENGH']) && $namespace_metadata['ENGH'] >= 1) {
  461. $_SESSION['__ZF'][$namespace]['ENGH']--;
  462. if ($_SESSION['__ZF'][$namespace]['ENGH'] === 0) {
  463. if (isset($_SESSION[$namespace])) {
  464. parent::$_expiringData[$namespace] = $_SESSION[$namespace];
  465. unset($_SESSION[$namespace]);
  466. }
  467. unset($_SESSION['__ZF'][$namespace]['ENGH']);
  468. }
  469. }
  470. // Expire Namespace Variables by Time (ENVT)
  471. if (isset($namespace_metadata['ENVT'])) {
  472. foreach ($namespace_metadata['ENVT'] as $variable => $time) {
  473. if (time() > $time) {
  474. unset($_SESSION[$namespace][$variable]);
  475. unset($_SESSION['__ZF'][$namespace]['ENVT'][$variable]);
  476. if (empty($_SESSION['__ZF'][$namespace]['ENVT'])) {
  477. unset($_SESSION['__ZF'][$namespace]['ENVT']);
  478. }
  479. }
  480. }
  481. }
  482. // Expire Namespace Variables by Global Hop (ENVGH)
  483. if (isset($namespace_metadata['ENVGH'])) {
  484. foreach ($namespace_metadata['ENVGH'] as $variable => $hops) {
  485. $_SESSION['__ZF'][$namespace]['ENVGH'][$variable]--;
  486. if ($_SESSION['__ZF'][$namespace]['ENVGH'][$variable] === 0) {
  487. if (isset($_SESSION[$namespace][$variable])) {
  488. parent::$_expiringData[$namespace][$variable] = $_SESSION[$namespace][$variable];
  489. unset($_SESSION[$namespace][$variable]);
  490. }
  491. unset($_SESSION['__ZF'][$namespace]['ENVGH'][$variable]);
  492. }
  493. }
  494. }
  495. }
  496. if (isset($namespace) && empty($_SESSION['__ZF'][$namespace])) {
  497. unset($_SESSION['__ZF'][$namespace]);
  498. }
  499. }
  500. if (isset($_SESSION['__ZF']) && empty($_SESSION['__ZF'])) {
  501. unset($_SESSION['__ZF']);
  502. }
  503. }
  504. /**
  505. * isStarted() - convenience method to determine if the session is already started.
  506. *
  507. * @return bool
  508. */
  509. public static function isStarted()
  510. {
  511. return self::$_sessionStarted;
  512. }
  513. /**
  514. * isRegenerated() - convenience method to determine if session_regenerate_id()
  515. * has been called during this request by Zend_Session.
  516. *
  517. * @return bool
  518. */
  519. public static function isRegenerated()
  520. {
  521. return ( (self::$_regenerateIdState > 0) ? true : false );
  522. }
  523. /**
  524. * getId() - get the current session id
  525. *
  526. * @return string
  527. */
  528. public static function getId()
  529. {
  530. return session_id();
  531. }
  532. /**
  533. * setId() - set an id to a user specified id
  534. *
  535. * @throws Zend_Session_Exception
  536. * @param string $id
  537. * @return void
  538. */
  539. public static function setId($id)
  540. {
  541. if (!self::$_unitTestEnabled && defined('SID')) {
  542. /** @see Zend_Session_Exception */
  543. require_once 'Zend/Session/Exception.php';
  544. throw new Zend_Session_Exception('The session has already been started. The session id must be set first.');
  545. }
  546. if (!self::$_unitTestEnabled && headers_sent($filename, $linenum)) {
  547. /** @see Zend_Session_Exception */
  548. require_once 'Zend/Session/Exception.php';
  549. throw new Zend_Session_Exception("You must call ".__CLASS__.'::'.__FUNCTION__.
  550. "() before any output has been sent to the browser; output started in {$filename}/{$linenum}");
  551. }
  552. if (!is_string($id) || $id === '') {
  553. /** @see Zend_Session_Exception */
  554. require_once 'Zend/Session/Exception.php';
  555. throw new Zend_Session_Exception('You must provide a non-empty string as a session identifier.');
  556. }
  557. session_id($id);
  558. }
  559. /**
  560. * registerValidator() - register a validator that will attempt to validate this session for
  561. * every future request
  562. *
  563. * @param Zend_Session_Validator_Interface $validator
  564. * @return void
  565. */
  566. public static function registerValidator(Zend_Session_Validator_Interface $validator)
  567. {
  568. $validator->setup();
  569. }
  570. /**
  571. * stop() - Disable write access. Optionally disable read (not implemented).
  572. *
  573. * @return void
  574. */
  575. public static function stop()
  576. {
  577. parent::$_writable = false;
  578. }
  579. /**
  580. * writeClose() - Shutdown the sesssion, close writing and detach $_SESSION from the back-end storage mechanism.
  581. * This will complete the internal data transformation on this request.
  582. *
  583. * @param bool $readonly - OPTIONAL remove write access (i.e. throw error if Zend_Session's attempt writes)
  584. * @return void
  585. */
  586. public static function writeClose($readonly = true)
  587. {
  588. if (self::$_unitTestEnabled) {
  589. return;
  590. }
  591. if (self::$_writeClosed) {
  592. return;
  593. }
  594. if ($readonly) {
  595. parent::$_writable = false;
  596. }
  597. session_write_close();
  598. self::$_writeClosed = true;
  599. }
  600. /**
  601. * destroy() - This is used to destroy session data, and optionally, the session cookie itself
  602. *
  603. * @param bool $remove_cookie - OPTIONAL remove session id cookie, defaults to true (remove cookie)
  604. * @param bool $readonly - OPTIONAL remove write access (i.e. throw error if Zend_Session's attempt writes)
  605. * @return void
  606. */
  607. public static function destroy($remove_cookie = true, $readonly = true)
  608. {
  609. if (self::$_unitTestEnabled) {
  610. return;
  611. }
  612. if (self::$_destroyed) {
  613. return;
  614. }
  615. if ($readonly) {
  616. parent::$_writable = false;
  617. }
  618. session_destroy();
  619. self::$_destroyed = true;
  620. if ($remove_cookie) {
  621. self::expireSessionCookie();
  622. }
  623. }
  624. /**
  625. * expireSessionCookie() - Sends an expired session id cookie, causing the client to delete the session cookie
  626. *
  627. * @return void
  628. */
  629. public static function expireSessionCookie()
  630. {
  631. if (self::$_unitTestEnabled) {
  632. return;
  633. }
  634. if (self::$_sessionCookieDeleted) {
  635. return;
  636. }
  637. self::$_sessionCookieDeleted = true;
  638. if (isset($_COOKIE[session_name()])) {
  639. $cookie_params = session_get_cookie_params();
  640. setcookie(
  641. session_name(),
  642. false,
  643. 315554400, // strtotime('1980-01-01'),
  644. $cookie_params['path'],
  645. $cookie_params['domain'],
  646. $cookie_params['secure']
  647. );
  648. }
  649. }
  650. /**
  651. * _processValidator() - internal function that is called in the existence of VALID metadata
  652. *
  653. * @throws Zend_Session_Exception
  654. * @return void
  655. */
  656. private static function _processValidators()
  657. {
  658. foreach ($_SESSION['__ZF']['VALID'] as $validator_name => $valid_data) {
  659. if (!class_exists($validator_name)) {
  660. require_once 'Zend/Loader.php';
  661. Zend_Loader::loadClass($validator_name);
  662. }
  663. $validator = new $validator_name;
  664. if ($validator->validate() === false) {
  665. /** @see Zend_Session_Exception */
  666. require_once 'Zend/Session/Exception.php';
  667. throw new Zend_Session_Exception("This session is not valid according to {$validator_name}.");
  668. }
  669. }
  670. }
  671. /**
  672. * namespaceIsset() - check to see if a namespace is set
  673. *
  674. * @param string $namespace
  675. * @return bool
  676. */
  677. public static function namespaceIsset($namespace)
  678. {
  679. return parent::_namespaceIsset($namespace);
  680. }
  681. /**
  682. * namespaceUnset() - unset a namespace or a variable within a namespace
  683. *
  684. * @param string $namespace
  685. * @throws Zend_Session_Exception
  686. * @return void
  687. */
  688. public static function namespaceUnset($namespace)
  689. {
  690. parent::_namespaceUnset($namespace);
  691. Zend_Session_Namespace::resetSingleInstance($namespace);
  692. }
  693. /**
  694. * namespaceGet() - get all variables in a namespace
  695. * Deprecated: Use getIterator() in Zend_Session_Namespace.
  696. *
  697. * @param string $namespace
  698. * @return array
  699. */
  700. public static function namespaceGet($namespace)
  701. {
  702. return parent::_namespaceGetAll($namespace);
  703. }
  704. /**
  705. * getIterator() - return an iteratable object for use in foreach and the like,
  706. * this completes the IteratorAggregate interface
  707. *
  708. * @throws Zend_Session_Exception
  709. * @return ArrayObject
  710. */
  711. public static function getIterator()
  712. {
  713. if (parent::$_readable === false) {
  714. /** @see Zend_Session_Exception */
  715. require_once 'Zend/Session/Exception.php';
  716. throw new Zend_Session_Exception(parent::_THROW_NOT_READABLE_MSG);
  717. }
  718. $spaces = array();
  719. if (isset($_SESSION)) {
  720. $spaces = array_keys($_SESSION);
  721. foreach($spaces as $key => $space) {
  722. if (!strncmp($space, '__', 2) || !is_array($_SESSION[$space])) {
  723. unset($spaces[$key]);
  724. }
  725. }
  726. }
  727. return new ArrayObject(array_merge($spaces, array_keys(parent::$_expiringData)));
  728. }
  729. /**
  730. * isWritable() - returns a boolean indicating if namespaces can write (use setters)
  731. *
  732. * @return bool
  733. */
  734. public static function isWritable()
  735. {
  736. return parent::$_writable;
  737. }
  738. /**
  739. * isReadable() - returns a boolean indicating if namespaces can write (use setters)
  740. *
  741. * @return bool
  742. */
  743. public static function isReadable()
  744. {
  745. return parent::$_readable;
  746. }
  747. }