BxDolSession.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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', 3600);
  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'))
  56. return true;
  57. if ($this->exists($this->sId)) {
  58. if ($this->iUserId == getLoggedId())
  59. return true;
  60. $this->destroy(false);
  61. }
  62. /**
  63. * Force logout a logged in user if his session wasn't found and required to be automatically recreated.
  64. * It's needed to avoid problems when different users are logged in at the same time under one account.
  65. */
  66. if($this->bAutoLogout && isLogged())
  67. bx_logout();
  68. // try to restore user's old session
  69. if (isLogged()) {
  70. $this->sId = $this->oDb->getOldSession(getLoggedId());
  71. if ($this->sId)
  72. $this->exists($this->sId); // it exists for sure but required for initializing some data there
  73. }
  74. // if an old session has not been found then generate a new one
  75. if (!$this->sId)
  76. $this->sId = genRndPwd(32, true);
  77. $aUrl = parse_url(BX_DOL_URL_ROOT);
  78. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  79. setcookie(BX_DOL_SESSION_COOKIE, $this->sId, 0, $sPath, '', false, true);
  80. $this->save();
  81. return true;
  82. }
  83. function destroy($bDeleteCookies = true)
  84. {
  85. if ($bDeleteCookies) {
  86. $aUrl = parse_url(BX_DOL_URL_ROOT);
  87. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  88. setcookie(BX_DOL_SESSION_COOKIE, '', time() - 86400, $sPath, '', false, true);
  89. unset($_COOKIE[BX_DOL_SESSION_COOKIE]);
  90. }
  91. $this->oDb->delete($this->sId);
  92. $this->sId = '';
  93. $this->iUserId = 0;
  94. $this->aData = array();
  95. }
  96. function exists($sId = '')
  97. {
  98. if(empty($sId) && isset($_COOKIE[BX_DOL_SESSION_COOKIE]))
  99. $sId = bx_process_input($_COOKIE[BX_DOL_SESSION_COOKIE]);
  100. $mixedSession = $this->oDb->exists($sId);
  101. if($mixedSession === false)
  102. return false;
  103. $this->sId = $mixedSession['id'];
  104. $this->iUserId = (int)$mixedSession['user_id'];
  105. $this->aData = unserialize($mixedSession['data']);
  106. $this->oDb->update($this->sId); //--- update session's time
  107. return true;
  108. }
  109. function getId()
  110. {
  111. return $this->sId;
  112. }
  113. function setUserId($iUserId)
  114. {
  115. $this->iUserId = $iUserId;
  116. $this->save();
  117. }
  118. function setValue($sKey, $mixedValue)
  119. {
  120. if(empty($this->sId))
  121. $this->start();
  122. $this->aData[$sKey] = $mixedValue;
  123. $this->save();
  124. }
  125. function unsetValue($sKey)
  126. {
  127. if(empty($this->sId))
  128. $this->start();
  129. unset($this->aData[$sKey]);
  130. if(!empty($this->aData))
  131. $this->save();
  132. else
  133. $this->destroy();
  134. }
  135. function isValue($sKey)
  136. {
  137. if(empty($this->sId))
  138. $this->start();
  139. return isset($this->aData[$sKey]);
  140. }
  141. function getValue($sKey)
  142. {
  143. if(empty($this->sId))
  144. $this->start();
  145. return isset($this->aData[$sKey]) ? $this->aData[$sKey] : false;
  146. }
  147. function getUnsetValue($sKey)
  148. {
  149. $mixedValue = $this->getValue($sKey);
  150. if($mixedValue !== false)
  151. $this->unsetValue($sKey);
  152. return $mixedValue;
  153. }
  154. public function maintenance()
  155. {
  156. return $this->oDb->deleteExpired();
  157. }
  158. protected function save()
  159. {
  160. if($this->iUserId == 0)
  161. $this->iUserId = getLoggedId();
  162. $this->oDb->save($this->sId, array(
  163. 'user_id' => $this->iUserId,
  164. 'data' => serialize($this->aData)
  165. ));
  166. }
  167. }
  168. /** @} */