Session.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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. * @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-2015 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. * @throws Zend_Session_Exception When the session_set_save_handler call fails
  232. * @return void
  233. */
  234. public static function setSaveHandler(Zend_Session_SaveHandler_Interface $saveHandler)
  235. {
  236. self::$_saveHandler = $saveHandler;
  237. if (self::$_unitTestEnabled) {
  238. return;
  239. }
  240. $result = session_set_save_handler(
  241. array(&$saveHandler, 'open'),
  242. array(&$saveHandler, 'close'),
  243. array(&$saveHandler, 'read'),
  244. array(&$saveHandler, 'write'),
  245. array(&$saveHandler, 'destroy'),
  246. array(&$saveHandler, 'gc')
  247. );
  248. if (!$result) {
  249. throw new Zend_Session_Exception('Unable to set session handler');
  250. }
  251. }
  252. /**
  253. * getSaveHandler() - Get the session Save Handler
  254. *
  255. * @return Zend_Session_SaveHandler_Interface
  256. */
  257. public static function getSaveHandler()
  258. {
  259. return self::$_saveHandler;
  260. }
  261. /**
  262. * regenerateId() - Regenerate the session id. Best practice is to call this after
  263. * session is started. If called prior to session starting, session id will be regenerated
  264. * at start time.
  265. *
  266. * @throws Zend_Session_Exception
  267. * @return void
  268. */
  269. public static function regenerateId()
  270. {
  271. if (!self::$_unitTestEnabled && headers_sent($filename, $linenum)) {
  272. /** @see Zend_Session_Exception */
  273. #require_once 'Zend/Session/Exception.php';
  274. throw new Zend_Session_Exception("You must call " . __CLASS__ . '::' . __FUNCTION__ .
  275. "() before any output has been sent to the browser; output started in {$filename}/{$linenum}");
  276. }
  277. if ( !self::$_sessionStarted ) {
  278. self::$_regenerateIdState = -1;
  279. } else {
  280. if (!self::$_unitTestEnabled) {
  281. session_regenerate_id(true);
  282. }
  283. self::$_regenerateIdState = 1;
  284. }
  285. }
  286. /**
  287. * rememberMe() - Write a persistent cookie that expires after a number of seconds in the future. If no number of
  288. * seconds is specified, then this defaults to self::$_rememberMeSeconds. Due to clock errors on end users' systems,
  289. * large values are recommended to avoid undesirable expiration of session cookies.
  290. *
  291. * @param int $seconds OPTIONAL specifies TTL for cookie in seconds from present time
  292. * @return void
  293. */
  294. public static function rememberMe($seconds = null)
  295. {
  296. $seconds = (int) $seconds;
  297. $seconds = ($seconds > 0) ? $seconds : self::$_rememberMeSeconds;
  298. self::rememberUntil($seconds);
  299. }
  300. /**
  301. * forgetMe() - Write a volatile session cookie, removing any persistent cookie that may have existed. The session
  302. * would end upon, for example, termination of a web browser program.
  303. *
  304. * @return void
  305. */
  306. public static function forgetMe()
  307. {
  308. self::rememberUntil(0);
  309. }
  310. /**
  311. * rememberUntil() - This method does the work of changing the state of the session cookie and making
  312. * sure that it gets resent to the browser via regenerateId()
  313. *
  314. * @param int $seconds
  315. * @return void
  316. */
  317. public static function rememberUntil($seconds = 0)
  318. {
  319. if (self::$_unitTestEnabled) {
  320. self::regenerateId();
  321. return;
  322. }
  323. $cookieParams = session_get_cookie_params();
  324. session_set_cookie_params(
  325. $seconds,
  326. $cookieParams['path'],
  327. $cookieParams['domain'],
  328. $cookieParams['secure']
  329. );
  330. // normally "rememberMe()" represents a security context change, so should use new session id
  331. self::regenerateId();
  332. }
  333. /**
  334. * sessionExists() - whether or not a session exists for the current request
  335. *
  336. * @return bool
  337. */
  338. public static function sessionExists()
  339. {
  340. if ((bool)ini_get('session.use_cookies') == true && isset($_COOKIE[session_name()])) {
  341. return true;
  342. } elseif ((bool)ini_get('session.use_only_cookies') == false && isset($_REQUEST[session_name()])) {
  343. return true;
  344. } elseif (self::$_unitTestEnabled) {
  345. return true;
  346. }
  347. return false;
  348. }
  349. /**
  350. * Whether or not session has been destroyed via session_destroy()
  351. *
  352. * @return bool
  353. */
  354. public static function isDestroyed()
  355. {
  356. return self::$_destroyed;
  357. }
  358. /**
  359. * start() - Start the session.
  360. *
  361. * @param bool|array $options OPTIONAL Either user supplied options, or flag indicating if start initiated automatically
  362. * @throws Zend_Session_Exception
  363. * @return void
  364. */
  365. public static function start($options = false)
  366. {
  367. // Check to see if we've been passed an invalid session ID
  368. if ( self::getId() && !self::_checkId(self::getId()) ) {
  369. // Generate a valid, temporary replacement
  370. self::setId(md5(self::getId()));
  371. // Force a regenerate after session is started
  372. self::$_regenerateIdState = -1;
  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. * Perform a hash-bits check on the session ID
  444. *
  445. * @param string $id Session ID
  446. * @return bool
  447. */
  448. protected static function _checkId($id)
  449. {
  450. $saveHandler = ini_get('session.save_handler');
  451. if ($saveHandler == 'cluster') { // Zend Server SC, validate only after last dash
  452. $dashPos = strrpos($id, '-');
  453. if ($dashPos) {
  454. $id = substr($id, $dashPos + 1);
  455. }
  456. }
  457. $hashBitsPerChar = ini_get('session.hash_bits_per_character');
  458. if (!$hashBitsPerChar) {
  459. $hashBitsPerChar = 5; // the default value
  460. }
  461. switch($hashBitsPerChar) {
  462. case 4: $pattern = '^[0-9a-f]*$'; break;
  463. case 5: $pattern = '^[0-9a-v]*$'; break;
  464. case 6: $pattern = '^[0-9a-zA-Z-,]*$'; break;
  465. }
  466. return preg_match('#'.$pattern.'#', $id);
  467. }
  468. /**
  469. * _processGlobalMetadata() - this method initizes the sessions GLOBAL
  470. * metadata, mostly global data expiration calculations.
  471. *
  472. * @return void
  473. */
  474. private static function _processStartupMetadataGlobal()
  475. {
  476. // process global metadata
  477. if (isset($_SESSION['__ZF'])) {
  478. // expire globally expired values
  479. foreach ($_SESSION['__ZF'] as $namespace => $namespace_metadata) {
  480. // Expire Namespace by Time (ENT)
  481. if (isset($namespace_metadata['ENT']) && ($namespace_metadata['ENT'] > 0) && (time() > $namespace_metadata['ENT']) ) {
  482. unset($_SESSION[$namespace]);
  483. unset($_SESSION['__ZF'][$namespace]);
  484. }
  485. // Expire Namespace by Global Hop (ENGH) if it wasnt expired above
  486. if (isset($_SESSION['__ZF'][$namespace]) && isset($namespace_metadata['ENGH']) && $namespace_metadata['ENGH'] >= 1) {
  487. $_SESSION['__ZF'][$namespace]['ENGH']--;
  488. if ($_SESSION['__ZF'][$namespace]['ENGH'] === 0) {
  489. if (isset($_SESSION[$namespace])) {
  490. parent::$_expiringData[$namespace] = $_SESSION[$namespace];
  491. unset($_SESSION[$namespace]);
  492. }
  493. unset($_SESSION['__ZF'][$namespace]);
  494. }
  495. }
  496. // Expire Namespace Variables by Time (ENVT)
  497. if (isset($namespace_metadata['ENVT'])) {
  498. foreach ($namespace_metadata['ENVT'] as $variable => $time) {
  499. if (time() > $time) {
  500. unset($_SESSION[$namespace][$variable]);
  501. unset($_SESSION['__ZF'][$namespace]['ENVT'][$variable]);
  502. }
  503. }
  504. if (empty($_SESSION['__ZF'][$namespace]['ENVT'])) {
  505. unset($_SESSION['__ZF'][$namespace]['ENVT']);
  506. }
  507. }
  508. // Expire Namespace Variables by Global Hop (ENVGH)
  509. if (isset($namespace_metadata['ENVGH'])) {
  510. foreach ($namespace_metadata['ENVGH'] as $variable => $hops) {
  511. $_SESSION['__ZF'][$namespace]['ENVGH'][$variable]--;
  512. if ($_SESSION['__ZF'][$namespace]['ENVGH'][$variable] === 0) {
  513. if (isset($_SESSION[$namespace][$variable])) {
  514. parent::$_expiringData[$namespace][$variable] = $_SESSION[$namespace][$variable];
  515. unset($_SESSION[$namespace][$variable]);
  516. }
  517. unset($_SESSION['__ZF'][$namespace]['ENVGH'][$variable]);
  518. }
  519. }
  520. if (empty($_SESSION['__ZF'][$namespace]['ENVGH'])) {
  521. unset($_SESSION['__ZF'][$namespace]['ENVGH']);
  522. }
  523. }
  524. if (isset($namespace) && empty($_SESSION['__ZF'][$namespace])) {
  525. unset($_SESSION['__ZF'][$namespace]);
  526. }
  527. }
  528. }
  529. if (isset($_SESSION['__ZF']) && empty($_SESSION['__ZF'])) {
  530. unset($_SESSION['__ZF']);
  531. }
  532. }
  533. /**
  534. * isStarted() - convenience method to determine if the session is already started.
  535. *
  536. * @return bool
  537. */
  538. public static function isStarted()
  539. {
  540. return self::$_sessionStarted;
  541. }
  542. /**
  543. * isRegenerated() - convenience method to determine if session_regenerate_id()
  544. * has been called during this request by Zend_Session.
  545. *
  546. * @return bool
  547. */
  548. public static function isRegenerated()
  549. {
  550. return ( (self::$_regenerateIdState > 0) ? true : false );
  551. }
  552. /**
  553. * getId() - get the current session id
  554. *
  555. * @return string
  556. */
  557. public static function getId()
  558. {
  559. return session_id();
  560. }
  561. /**
  562. * setId() - set an id to a user specified id
  563. *
  564. * @throws Zend_Session_Exception
  565. * @param string $id
  566. * @return void
  567. */
  568. public static function setId($id)
  569. {
  570. if (!self::$_unitTestEnabled && defined('SID')) {
  571. /** @see Zend_Session_Exception */
  572. #require_once 'Zend/Session/Exception.php';
  573. throw new Zend_Session_Exception('The session has already been started. The session id must be set first.');
  574. }
  575. if (!self::$_unitTestEnabled && headers_sent($filename, $linenum)) {
  576. /** @see Zend_Session_Exception */
  577. #require_once 'Zend/Session/Exception.php';
  578. throw new Zend_Session_Exception("You must call ".__CLASS__.'::'.__FUNCTION__.
  579. "() before any output has been sent to the browser; output started in {$filename}/{$linenum}");
  580. }
  581. if (!is_string($id) || $id === '') {
  582. /** @see Zend_Session_Exception */
  583. #require_once 'Zend/Session/Exception.php';
  584. throw new Zend_Session_Exception('You must provide a non-empty string as a session identifier.');
  585. }
  586. session_id($id);
  587. }
  588. /**
  589. * registerValidator() - register a validator that will attempt to validate this session for
  590. * every future request
  591. *
  592. * @param Zend_Session_Validator_Interface $validator
  593. * @return void
  594. */
  595. public static function registerValidator(Zend_Session_Validator_Interface $validator)
  596. {
  597. $validator->setup();
  598. }
  599. /**
  600. * stop() - Disable write access. Optionally disable read (not implemented).
  601. *
  602. * @return void
  603. */
  604. public static function stop()
  605. {
  606. parent::$_writable = false;
  607. }
  608. /**
  609. * writeClose() - Shutdown the sesssion, close writing and detach $_SESSION from the back-end storage mechanism.
  610. * This will complete the internal data transformation on this request.
  611. *
  612. * @param bool $readonly - OPTIONAL remove write access (i.e. throw error if Zend_Session's attempt writes)
  613. * @return void
  614. */
  615. public static function writeClose($readonly = true)
  616. {
  617. if (self::$_unitTestEnabled) {
  618. return;
  619. }
  620. if (self::$_writeClosed) {
  621. return;
  622. }
  623. if ($readonly) {
  624. parent::$_writable = false;
  625. }
  626. session_write_close();
  627. self::$_writeClosed = true;
  628. }
  629. /**
  630. * destroy() - This is used to destroy session data, and optionally, the session cookie itself
  631. *
  632. * @param bool $remove_cookie - OPTIONAL remove session id cookie, defaults to true (remove cookie)
  633. * @param bool $readonly - OPTIONAL remove write access (i.e. throw error if Zend_Session's attempt writes)
  634. * @return void
  635. */
  636. public static function destroy($remove_cookie = true, $readonly = true)
  637. {
  638. if (self::$_unitTestEnabled) {
  639. return;
  640. }
  641. if (self::$_destroyed) {
  642. return;
  643. }
  644. if ($readonly) {
  645. parent::$_writable = false;
  646. }
  647. session_destroy();
  648. self::$_destroyed = true;
  649. if ($remove_cookie) {
  650. self::expireSessionCookie();
  651. }
  652. }
  653. /**
  654. * expireSessionCookie() - Sends an expired session id cookie, causing the client to delete the session cookie
  655. *
  656. * @return void
  657. */
  658. public static function expireSessionCookie()
  659. {
  660. if (self::$_unitTestEnabled) {
  661. return;
  662. }
  663. if (self::$_sessionCookieDeleted) {
  664. return;
  665. }
  666. self::$_sessionCookieDeleted = true;
  667. if (isset($_COOKIE[session_name()])) {
  668. $cookie_params = session_get_cookie_params();
  669. setcookie(
  670. session_name(),
  671. false,
  672. 315554400, // strtotime('1980-01-01'),
  673. $cookie_params['path'],
  674. $cookie_params['domain'],
  675. $cookie_params['secure']
  676. );
  677. }
  678. }
  679. /**
  680. * _processValidator() - internal function that is called in the existence of VALID metadata
  681. *
  682. * @throws Zend_Session_Exception
  683. * @return void
  684. */
  685. private static function _processValidators()
  686. {
  687. foreach ($_SESSION['__ZF']['VALID'] as $validator_name => $valid_data) {
  688. if (!class_exists($validator_name)) {
  689. #require_once 'Zend/Loader.php';
  690. Zend_Loader::loadClass($validator_name);
  691. }
  692. $validator = new $validator_name;
  693. if ($validator->validate() === false) {
  694. /** @see Zend_Session_Validator_Exception */
  695. #require_once 'Zend/Session/Validator/Exception.php';
  696. throw new Zend_Session_Validator_Exception("This session is not valid according to {$validator_name}.");
  697. }
  698. }
  699. }
  700. /**
  701. * namespaceIsset() - check to see if a namespace is set
  702. *
  703. * @param string $namespace
  704. * @return bool
  705. */
  706. public static function namespaceIsset($namespace)
  707. {
  708. return parent::_namespaceIsset($namespace);
  709. }
  710. /**
  711. * namespaceUnset() - unset a namespace or a variable within a namespace
  712. *
  713. * @param string $namespace
  714. * @throws Zend_Session_Exception
  715. * @return void
  716. */
  717. public static function namespaceUnset($namespace)
  718. {
  719. parent::_namespaceUnset($namespace);
  720. Zend_Session_Namespace::resetSingleInstance($namespace);
  721. }
  722. /**
  723. * namespaceGet() - get all variables in a namespace
  724. * Deprecated: Use getIterator() in Zend_Session_Namespace.
  725. *
  726. * @param string $namespace
  727. * @return array
  728. */
  729. public static function namespaceGet($namespace)
  730. {
  731. return parent::_namespaceGetAll($namespace);
  732. }
  733. /**
  734. * getIterator() - return an iteratable object for use in foreach and the like,
  735. * this completes the IteratorAggregate interface
  736. *
  737. * @throws Zend_Session_Exception
  738. * @return ArrayObject
  739. */
  740. public static function getIterator()
  741. {
  742. if (parent::$_readable === false) {
  743. /** @see Zend_Session_Exception */
  744. #require_once 'Zend/Session/Exception.php';
  745. throw new Zend_Session_Exception(parent::_THROW_NOT_READABLE_MSG);
  746. }
  747. $spaces = array();
  748. if (isset($_SESSION)) {
  749. $spaces = array_keys($_SESSION);
  750. foreach($spaces as $key => $space) {
  751. if (!strncmp($space, '__', 2) || !is_array($_SESSION[$space])) {
  752. unset($spaces[$key]);
  753. }
  754. }
  755. }
  756. return new ArrayObject(array_merge($spaces, array_keys(parent::$_expiringData)));
  757. }
  758. /**
  759. * isWritable() - returns a boolean indicating if namespaces can write (use setters)
  760. *
  761. * @return bool
  762. */
  763. public static function isWritable()
  764. {
  765. return parent::$_writable;
  766. }
  767. /**
  768. * isReadable() - returns a boolean indicating if namespaces can write (use setters)
  769. *
  770. * @return bool
  771. */
  772. public static function isReadable()
  773. {
  774. return parent::$_readable;
  775. }
  776. }