BxDolSession.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. if (!defined('BX_DOL_SESSION_LIFETIME'))
  10. define('BX_DOL_SESSION_LIFETIME', 7*24*60*60);
  11. if (!defined('BX_DOL_SESSION_SKIP_UPDATE'))
  12. define('BX_DOL_SESSION_SKIP_UPDATE', 30);
  13. define('BX_DOL_SESSION_COOKIE', 'memberSession');
  14. class BxDolSession extends BxDolFactory implements iBxDolSingleton
  15. {
  16. protected $oDb;
  17. protected $sId;
  18. protected $iUserId;
  19. protected $aData;
  20. protected $bAutoLogout; //--- Auto logout with broken session
  21. protected function __construct()
  22. {
  23. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  24. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  25. parent::__construct();
  26. $this->oDb = new BxDolSessionQuery();
  27. $this->sId = '';
  28. $this->iUserId = 0;
  29. $this->aData = array();
  30. $this->bAutoLogout = false; // when changing to 'true', it's better to increase BX_DOL_SESSION_LIFETIME to 1 month or so
  31. if (defined('BX_SESSION_AUTO_LOGOUT'))
  32. $this->bAutoLogout = BX_SESSION_AUTO_LOGOUT;
  33. }
  34. /**
  35. * Prevent cloning the instance
  36. */
  37. public function __clone()
  38. {
  39. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  40. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  41. }
  42. /**
  43. * Get singleton instance of the class
  44. */
  45. public static function getInstance()
  46. {
  47. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  48. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolSession();
  49. if(!$GLOBALS['bxDolClasses'][__CLASS__]->getId())
  50. $GLOBALS['bxDolClasses'][__CLASS__]->start();
  51. return $GLOBALS['bxDolClasses'][__CLASS__];
  52. }
  53. function start()
  54. {
  55. if (defined('BX_DOL_CRON_EXECUTE') || defined('BX_MANIFEST'))
  56. return true;
  57. if (getParam('sys_session_auth')) {
  58. $this->exists($this->sId);
  59. }
  60. else {
  61. if ($this->exists($this->sId)) {
  62. if ($this->iUserId == getLoggedId())
  63. return true;
  64. $this->destroy(false);
  65. }
  66. }
  67. /**
  68. * Force logout a logged in user if his session wasn't found and required to be automatically recreated.
  69. * It's needed to avoid problems when different users are logged in at the same time under one account.
  70. */
  71. if($this->bAutoLogout && isLogged())
  72. bx_logout();
  73. // try to restore user's old session
  74. if (!getParam('sys_session_auth') && isLogged() && defined('BX_DOL_SESSION_RESTORATION') && constant('BX_DOL_SESSION_RESTORATION')) {
  75. $this->sId = $this->oDb->getOldSession(getLoggedId());
  76. if ($this->sId)
  77. $this->exists($this->sId); // it exists for sure but required for initializing some data there
  78. }
  79. // if an old session has not been found then generate a new one
  80. if (!$this->sId)
  81. $this->sId = genRndPwd(32, true);
  82. bx_setcookie(BX_DOL_SESSION_COOKIE, $this->sId, time() + BX_DOL_SESSION_LIFETIME, 'auto', '', 'auto', true);
  83. $this->save();
  84. return true;
  85. }
  86. function destroy($bDeleteCookies = true)
  87. {
  88. if ($bDeleteCookies) {
  89. bx_setcookie(BX_DOL_SESSION_COOKIE, '', time() - 86400, 'auto', '', 'auto', true);
  90. unset($_COOKIE[BX_DOL_SESSION_COOKIE]);
  91. }
  92. $this->oDb->delete($this->sId);
  93. $this->sId = '';
  94. $this->iUserId = 0;
  95. $this->aData = array();
  96. }
  97. function exists($sId = '')
  98. {
  99. if(empty($sId) && isset($_COOKIE[BX_DOL_SESSION_COOKIE]))
  100. $sId = bx_process_input($_COOKIE[BX_DOL_SESSION_COOKIE]);
  101. $mixedSession = $this->oDb->exists($sId);
  102. if($mixedSession === false)
  103. return false;
  104. $this->sId = $mixedSession['id'];
  105. $this->iUserId = (int)$mixedSession['user_id'];
  106. $this->aData = unserialize($mixedSession['data']);
  107. $this->oDb->update($this->sId); //--- update session's time
  108. return true;
  109. }
  110. function getId()
  111. {
  112. return $this->sId;
  113. }
  114. function setUserId($iUserId)
  115. {
  116. $this->iUserId = $iUserId;
  117. $this->save();
  118. }
  119. function getUserId()
  120. {
  121. return $this->iUserId;
  122. }
  123. function setValue($sKey, $mixedValue)
  124. {
  125. if(empty($this->sId))
  126. $this->start();
  127. $this->aData[$sKey] = $mixedValue;
  128. $this->save();
  129. }
  130. function unsetValue($sKey)
  131. {
  132. if(empty($this->sId))
  133. $this->start();
  134. unset($this->aData[$sKey]);
  135. $this->save();
  136. }
  137. function isValue($sKey)
  138. {
  139. if(empty($this->sId))
  140. $this->start();
  141. return isset($this->aData[$sKey]);
  142. }
  143. function getValue($sKey)
  144. {
  145. if(empty($this->sId))
  146. $this->start();
  147. return isset($this->aData[$sKey]) ? $this->aData[$sKey] : false;
  148. }
  149. function getUnsetValue($sKey)
  150. {
  151. $mixedValue = $this->getValue($sKey);
  152. if($mixedValue !== false)
  153. $this->unsetValue($sKey);
  154. return $mixedValue;
  155. }
  156. public function maintenance()
  157. {
  158. return $this->oDb->deleteExpired();
  159. }
  160. protected function save()
  161. {
  162. if($this->iUserId == 0)
  163. $this->iUserId = getLoggedId();
  164. $this->oDb->save($this->sId, array(
  165. 'user_id' => $this->iUserId,
  166. 'data' => serialize($this->aData)
  167. ));
  168. }
  169. }
  170. /** @} */