BxDolSession.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolSessionQuery');
  10. define('BX_DOL_SESSION_LIFETIME', 3600);
  11. define('BX_DOL_SESSION_COOKIE', 'memberSession');
  12. class BxDolSession extends BxDol implements iBxDolSingleton {
  13. var $oDb;
  14. var $sId;
  15. var $iUserId;
  16. var $aData;
  17. private function BxDolSession() {
  18. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  19. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  20. parent::BxDol();
  21. $this->oDb = new BxDolSessionQuery();
  22. $this->sId = '';
  23. $this->iUserId = 0;
  24. $this->aData = array();
  25. }
  26. /**
  27. * Prevent cloning the instance
  28. */
  29. public function __clone() {
  30. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  31. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  32. }
  33. /**
  34. * Get singleton instance of the class
  35. */
  36. public static function getInstance() {
  37. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  38. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolSession();
  39. if(!$GLOBALS['bxDolClasses'][__CLASS__]->getId())
  40. $GLOBALS['bxDolClasses'][__CLASS__]->start();
  41. return $GLOBALS['bxDolClasses'][__CLASS__];
  42. }
  43. function start(){
  44. if (defined('BX_DOL_CRON_EXECUTE'))
  45. return true;
  46. if($this->exists($this->sId))
  47. return true;
  48. $this->sId = genRndPwd(32, true);
  49. $aUrl = parse_url(BX_DOL_URL_ROOT);
  50. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  51. setcookie(BX_DOL_SESSION_COOKIE, $this->sId, 0, $sPath, '', false, true);
  52. $this->save();
  53. return true;
  54. }
  55. function destroy() {
  56. $aUrl = parse_url(BX_DOL_URL_ROOT);
  57. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  58. setcookie(BX_DOL_SESSION_COOKIE, '', time() - 86400, $sPath, '', false, true);
  59. unset($_COOKIE[BX_DOL_SESSION_COOKIE]);
  60. $this->oDb->delete($this->sId);
  61. $this->sId = '';
  62. $this->iUserId = 0;
  63. $this->aData = array();
  64. }
  65. function exists($sId = '') {
  66. if(empty($sId) && isset($_COOKIE[BX_DOL_SESSION_COOKIE]))
  67. $sId = bx_process_input($_COOKIE[BX_DOL_SESSION_COOKIE]);
  68. $mixedSession = array();
  69. if(($mixedSession = $this->oDb->exists($sId)) !== false) {
  70. $this->sId = $mixedSession['id'];
  71. $this->iUserId = (int)$mixedSession['user_id'];
  72. $this->aData = unserialize($mixedSession['data']);
  73. return true;
  74. }
  75. else
  76. return false;
  77. }
  78. function getId() {
  79. return $this->sId;
  80. }
  81. function setUserId($iUserId) {
  82. $this->iUserId = $iUserId;
  83. $this->save();
  84. }
  85. function setValue($sKey, $mixedValue) {
  86. if(empty($this->sId))
  87. $this->start();
  88. $this->aData[$sKey] = $mixedValue;
  89. $this->save();
  90. }
  91. function unsetValue($sKey) {
  92. if(empty($this->sId))
  93. $this->start();
  94. unset($this->aData[$sKey]);
  95. if(!empty($this->aData))
  96. $this->save();
  97. else
  98. $this->destroy();
  99. }
  100. function getValue($sKey) {
  101. if(empty($this->sId))
  102. $this->start();
  103. return isset($this->aData[$sKey]) ? $this->aData[$sKey] : false;
  104. }
  105. function getUnsetValue($sKey) {
  106. $mixedValue = $this->getValue($sKey);
  107. if($mixedValue !== false)
  108. $this->unsetValue($sKey);
  109. return $mixedValue;
  110. }
  111. private function save() {
  112. if($this->iUserId == 0)
  113. $this->iUserId = getLoggedId();
  114. $this->oDb->save($this->sId, array(
  115. 'user_id' => $this->iUserId,
  116. 'data' => serialize($this->aData)
  117. ));
  118. }
  119. }
  120. /** @} */