BxDolSession.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 TridentCore Trident Core
  7. * @{
  8. */
  9. define('BX_DOL_SESSION_LIFETIME', 3600);
  10. define('BX_DOL_SESSION_COOKIE', 'memberSession');
  11. class BxDolSession extends BxDolFactory implements iBxDolSingleton
  12. {
  13. protected $oDb;
  14. protected $sId;
  15. protected $iUserId;
  16. protected $aData;
  17. protected $bAutoLogout; //--- Auto logout with broken session
  18. protected function __construct()
  19. {
  20. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  21. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  22. parent::__construct();
  23. $this->oDb = new BxDolSessionQuery();
  24. $this->sId = '';
  25. $this->iUserId = 0;
  26. $this->aData = array();
  27. $this->bAutoLogout = true;
  28. }
  29. /**
  30. * Prevent cloning the instance
  31. */
  32. public function __clone()
  33. {
  34. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  35. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  36. }
  37. /**
  38. * Get singleton instance of the class
  39. */
  40. public static function getInstance()
  41. {
  42. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  43. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolSession();
  44. if(!$GLOBALS['bxDolClasses'][__CLASS__]->getId())
  45. $GLOBALS['bxDolClasses'][__CLASS__]->start();
  46. return $GLOBALS['bxDolClasses'][__CLASS__];
  47. }
  48. function start()
  49. {
  50. if (defined('BX_DOL_CRON_EXECUTE'))
  51. return true;
  52. if($this->exists($this->sId))
  53. return true;
  54. /**
  55. * Force logout a logged in user if his session wasn't found and required to be automatically recreated.
  56. * It's needed to avoid problems when different users are logged in at the same time under one account.
  57. */
  58. if($this->bAutoLogout && isLogged())
  59. bx_logout();
  60. $this->sId = genRndPwd(32, true);
  61. $aUrl = parse_url(BX_DOL_URL_ROOT);
  62. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  63. setcookie(BX_DOL_SESSION_COOKIE, $this->sId, 0, $sPath, '', false, true);
  64. $this->save();
  65. return true;
  66. }
  67. function destroy()
  68. {
  69. $aUrl = parse_url(BX_DOL_URL_ROOT);
  70. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  71. setcookie(BX_DOL_SESSION_COOKIE, '', time() - 86400, $sPath, '', false, true);
  72. unset($_COOKIE[BX_DOL_SESSION_COOKIE]);
  73. $this->oDb->delete($this->sId);
  74. $this->sId = '';
  75. $this->iUserId = 0;
  76. $this->aData = array();
  77. }
  78. function exists($sId = '')
  79. {
  80. if(empty($sId) && isset($_COOKIE[BX_DOL_SESSION_COOKIE]))
  81. $sId = bx_process_input($_COOKIE[BX_DOL_SESSION_COOKIE]);
  82. $mixedSession = $this->oDb->exists($sId);
  83. if($mixedSession === false)
  84. return false;
  85. $this->sId = $mixedSession['id'];
  86. $this->iUserId = (int)$mixedSession['user_id'];
  87. $this->aData = unserialize($mixedSession['data']);
  88. $this->oDb->update($this->sId); //--- update session's time
  89. return true;
  90. }
  91. function getId()
  92. {
  93. return $this->sId;
  94. }
  95. function setUserId($iUserId)
  96. {
  97. $this->iUserId = $iUserId;
  98. $this->save();
  99. }
  100. function setValue($sKey, $mixedValue)
  101. {
  102. if(empty($this->sId))
  103. $this->start();
  104. $this->aData[$sKey] = $mixedValue;
  105. $this->save();
  106. }
  107. function unsetValue($sKey)
  108. {
  109. if(empty($this->sId))
  110. $this->start();
  111. unset($this->aData[$sKey]);
  112. if(!empty($this->aData))
  113. $this->save();
  114. else
  115. $this->destroy();
  116. }
  117. function isValue($sKey)
  118. {
  119. if(empty($this->sId))
  120. $this->start();
  121. return isset($this->aData[$sKey]);
  122. }
  123. function getValue($sKey)
  124. {
  125. if(empty($this->sId))
  126. $this->start();
  127. return isset($this->aData[$sKey]) ? $this->aData[$sKey] : false;
  128. }
  129. function getUnsetValue($sKey)
  130. {
  131. $mixedValue = $this->getValue($sKey);
  132. if($mixedValue !== false)
  133. $this->unsetValue($sKey);
  134. return $mixedValue;
  135. }
  136. public function maintenance()
  137. {
  138. return $this->oDb->deleteExpired();
  139. }
  140. protected function save()
  141. {
  142. if($this->iUserId == 0)
  143. $this->iUserId = getLoggedId();
  144. $this->oDb->save($this->sId, array(
  145. 'user_id' => $this->iUserId,
  146. 'data' => serialize($this->aData)
  147. ));
  148. }
  149. }
  150. /** @} */