BxDolSession.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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_IDLE_TIMEOUT'))
  10. define('BX_DOL_SESSION_IDLE_TIMEOUT', 2*24*60*60);
  11. if (!defined('BX_DOL_SESSION_IDLE_TIMEOUT_GUESTS'))
  12. define('BX_DOL_SESSION_IDLE_TIMEOUT_GUESTS', 4*60*60);
  13. if (!defined('BX_DOL_SESSION_SKIP_UPDATE'))
  14. define('BX_DOL_SESSION_SKIP_UPDATE', 30);
  15. define('BX_DOL_SESSION_COOKIE', 'memberSession');
  16. class BxDolSession extends BxDolFactory implements iBxDolSingleton
  17. {
  18. protected $oDb;
  19. protected $sId;
  20. protected $iUserId;
  21. protected $aData;
  22. protected $iTTL;
  23. protected $bAutoLogout; //--- Auto logout with broken session
  24. protected function __construct()
  25. {
  26. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  27. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  28. parent::__construct();
  29. $this->oDb = new BxDolSessionQuery();
  30. $this->sId = '';
  31. $this->iUserId = 0;
  32. $this->aData = array();
  33. $this->iTTL = 0;
  34. $this->bAutoLogout = false; // when changing to 'true', it maybe you will need to increase BX_DOL_SESSION_IDLE_TIMEOUT
  35. if (defined('BX_SESSION_AUTO_LOGOUT'))
  36. $this->bAutoLogout = BX_SESSION_AUTO_LOGOUT;
  37. }
  38. /**
  39. * Prevent cloning the instance
  40. */
  41. public function __clone()
  42. {
  43. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  44. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  45. }
  46. /**
  47. * Get singleton instance of the class
  48. */
  49. public static function getInstance()
  50. {
  51. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  52. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolSession();
  53. return $GLOBALS['bxDolClasses'][__CLASS__];
  54. }
  55. public static function getSessionCookie()
  56. {
  57. if (isset($_COOKIE[BX_DOL_SESSION_COOKIE]))
  58. return bx_process_input($_COOKIE[BX_DOL_SESSION_COOKIE]);
  59. return null;
  60. }
  61. function start($bForceStart = false)
  62. {
  63. if (defined('BX_DOL_CRON_EXECUTE') || defined('BX_MANIFEST') || defined('BX_SERVICE_WORKER'))
  64. return true;
  65. if (!$bForceStart && $this->getSessionCookie() === null)
  66. return true;
  67. if (getParam('sys_session_auth')) {
  68. $this->exists($this->sId);
  69. }
  70. else {
  71. if ($this->exists($this->sId)) {
  72. if ($this->iUserId == getLoggedId())
  73. return true;
  74. $this->destroy(false);
  75. }
  76. }
  77. /**
  78. * Force logout a logged in user if his session wasn't found and required to be automatically recreated.
  79. * It's needed to avoid problems when different users are logged in at the same time under one account.
  80. */
  81. if($this->bAutoLogout && isLogged())
  82. bx_logout();
  83. // try to restore user's old session
  84. if (!getParam('sys_session_auth') && isLogged() && defined('BX_DOL_SESSION_RESTORATION') && constant('BX_DOL_SESSION_RESTORATION')) {
  85. $this->sId = $this->oDb->getOldSession(getLoggedId());
  86. if ($this->sId)
  87. $this->exists($this->sId); // it exists for sure but required for initializing some data there
  88. }
  89. // if an old session has not been found then generate a new one
  90. if (!$this->sId)
  91. $this->sId = genRndPwd(32, true);
  92. if (!isset($_COOKIE[BX_DOL_SESSION_COOKIE]) || $_COOKIE[BX_DOL_SESSION_COOKIE] != $this->sId || $this->isValue('remember_me')) {
  93. $iTime = 0;
  94. if (getParam('sys_session_auth') && $this->isValue('remember_me') && $this->getValue('remember_me'))
  95. $iTime = time() + getParam('sys_session_lifetime_in_min') * 60;
  96. bx_setcookie(BX_DOL_SESSION_COOKIE, $this->sId, $iTime, 'auto', '', 'auto', true);
  97. $this->iTTL = $iTime;
  98. unset($this->aData['remember_me']);
  99. }
  100. $this->save();
  101. return true;
  102. }
  103. function destroy($bDeleteCookies = true)
  104. {
  105. if(empty($this->sId))
  106. $this->start();
  107. if(empty($this->sId))
  108. return;
  109. if ($bDeleteCookies) {
  110. bx_setcookie(BX_DOL_SESSION_COOKIE, '', time() - 86400, 'auto', '', 'auto', true);
  111. unset($_COOKIE[BX_DOL_SESSION_COOKIE]);
  112. }
  113. $this->oDb->delete($this->sId);
  114. $this->sId = '';
  115. $this->iUserId = 0;
  116. $this->iTTL = 0;
  117. $this->aData = array();
  118. }
  119. function exists($sId = '')
  120. {
  121. if(empty($sId))
  122. $sId = $this->getSessionCookie();
  123. $mixedSession = $this->oDb->exists($sId);
  124. if($mixedSession === false)
  125. return false;
  126. $this->sId = $mixedSession['id'];
  127. $this->iUserId = (int)$mixedSession['user_id'];
  128. $this->iTTL = isset($mixedSession['ttl']) ? (int)$mixedSession['ttl'] : 0;
  129. $this->aData = unserialize($mixedSession['data']);
  130. $this->oDb->update($this->sId); //--- update session's time
  131. return true;
  132. }
  133. function getId()
  134. {
  135. return $this->sId;
  136. }
  137. function setUserId($iUserId)
  138. {
  139. if(empty($this->sId))
  140. $this->start(true);
  141. if(empty($this->sId))
  142. return;
  143. if (0 == $iUserId && $this->iUserId) { // update last active field when user is logged out
  144. $this->oDb->updateLastActivityAccount($this->iUserId, time());
  145. }
  146. $this->iUserId = $iUserId;
  147. $this->save();
  148. }
  149. function getUserId()
  150. {
  151. if(empty($this->sId))
  152. $this->start();
  153. return $this->iUserId;
  154. }
  155. function setTTL($iTTL)
  156. {
  157. if(empty($this->sId))
  158. $this->start();
  159. if(empty($this->sId))
  160. return;
  161. $this->iTTL = $iTTL;
  162. $this->save();
  163. }
  164. function getTTL()
  165. {
  166. if(empty($this->sId))
  167. $this->start();
  168. return $this->iTTL;
  169. }
  170. function setValue($sKey, $mixedValue)
  171. {
  172. if(empty($this->sId))
  173. $this->start(true);
  174. $this->aData[$sKey] = $mixedValue;
  175. $this->save();
  176. }
  177. function unsetValue($sKey)
  178. {
  179. if(empty($this->sId))
  180. $this->start(true);
  181. unset($this->aData[$sKey]);
  182. $this->save();
  183. }
  184. function isValue($sKey)
  185. {
  186. if(empty($this->sId))
  187. $this->start();
  188. return isset($this->aData[$sKey]);
  189. }
  190. function getValue($sKey)
  191. {
  192. if(empty($this->sId))
  193. $this->start();
  194. return isset($this->aData[$sKey]) ? $this->aData[$sKey] : false;
  195. }
  196. function getUnsetValue($sKey)
  197. {
  198. $mixedValue = $this->getValue($sKey);
  199. if($mixedValue !== false)
  200. $this->unsetValue($sKey);
  201. return $mixedValue;
  202. }
  203. public function maintenance()
  204. {
  205. return $this->oDb->deleteExpired();
  206. }
  207. protected function save()
  208. {
  209. if($this->iUserId == 0)
  210. $this->iUserId = getLoggedId();
  211. $this->oDb->save($this->sId, array(
  212. 'user_id' => $this->iUserId,
  213. 'ttl' => $this->iTTL,
  214. 'data' => serialize($this->aData)
  215. ));
  216. }
  217. }
  218. /** @} */