BxDolSession.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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') || defined('BX_SERVICE_WORKER'))
  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. $iTime = time() + BX_DOL_SESSION_LIFETIME;
  83. if (getParam('sys_session_auth') && $this->isValue('remember_me') && !$this->getValue('remember_me'))
  84. $iTime = 0;
  85. bx_setcookie(BX_DOL_SESSION_COOKIE, $this->sId, $iTime, 'auto', '', 'auto', true);
  86. $this->save();
  87. return true;
  88. }
  89. function destroy($bDeleteCookies = true)
  90. {
  91. if ($bDeleteCookies) {
  92. bx_setcookie(BX_DOL_SESSION_COOKIE, '', time() - 86400, 'auto', '', 'auto', true);
  93. unset($_COOKIE[BX_DOL_SESSION_COOKIE]);
  94. }
  95. $this->oDb->delete($this->sId);
  96. $this->sId = '';
  97. $this->iUserId = 0;
  98. $this->aData = array();
  99. }
  100. function exists($sId = '')
  101. {
  102. if(empty($sId) && isset($_COOKIE[BX_DOL_SESSION_COOKIE]))
  103. $sId = bx_process_input($_COOKIE[BX_DOL_SESSION_COOKIE]);
  104. $mixedSession = $this->oDb->exists($sId);
  105. if($mixedSession === false)
  106. return false;
  107. $this->sId = $mixedSession['id'];
  108. $this->iUserId = (int)$mixedSession['user_id'];
  109. $this->aData = unserialize($mixedSession['data']);
  110. $this->oDb->update($this->sId); //--- update session's time
  111. return true;
  112. }
  113. function getId()
  114. {
  115. return $this->sId;
  116. }
  117. function setUserId($iUserId)
  118. {
  119. $this->iUserId = $iUserId;
  120. $this->save();
  121. }
  122. function getUserId()
  123. {
  124. return $this->iUserId;
  125. }
  126. function setValue($sKey, $mixedValue)
  127. {
  128. if(empty($this->sId))
  129. $this->start();
  130. $this->aData[$sKey] = $mixedValue;
  131. $this->save();
  132. }
  133. function unsetValue($sKey)
  134. {
  135. if(empty($this->sId))
  136. $this->start();
  137. unset($this->aData[$sKey]);
  138. $this->save();
  139. }
  140. function isValue($sKey)
  141. {
  142. if(empty($this->sId))
  143. $this->start();
  144. return isset($this->aData[$sKey]);
  145. }
  146. function getValue($sKey)
  147. {
  148. if(empty($this->sId))
  149. $this->start();
  150. return isset($this->aData[$sKey]) ? $this->aData[$sKey] : false;
  151. }
  152. function getUnsetValue($sKey)
  153. {
  154. $mixedValue = $this->getValue($sKey);
  155. if($mixedValue !== false)
  156. $this->unsetValue($sKey);
  157. return $mixedValue;
  158. }
  159. public function maintenance()
  160. {
  161. return $this->oDb->deleteExpired();
  162. }
  163. protected function save()
  164. {
  165. if($this->iUserId == 0)
  166. $this->iUserId = getLoggedId();
  167. $this->oDb->save($this->sId, array(
  168. 'user_id' => $this->iUserId,
  169. 'data' => serialize($this->aData)
  170. ));
  171. }
  172. }
  173. /** @} */