BxDolSession.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 BxDol implements iBxDolSingleton
  12. {
  13. protected $oDb;
  14. protected $sId;
  15. protected $iUserId;
  16. protected $aData;
  17. private function __construct()
  18. {
  19. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  20. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  21. parent::__construct();
  22. $this->oDb = new BxDolSessionQuery();
  23. $this->sId = '';
  24. $this->iUserId = 0;
  25. $this->aData = array();
  26. }
  27. /**
  28. * Prevent cloning the instance
  29. */
  30. public function __clone()
  31. {
  32. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  33. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  34. }
  35. /**
  36. * Get singleton instance of the class
  37. */
  38. public static function getInstance()
  39. {
  40. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  41. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolSession();
  42. if(!$GLOBALS['bxDolClasses'][__CLASS__]->getId())
  43. $GLOBALS['bxDolClasses'][__CLASS__]->start();
  44. return $GLOBALS['bxDolClasses'][__CLASS__];
  45. }
  46. function start()
  47. {
  48. if (defined('BX_DOL_CRON_EXECUTE'))
  49. return true;
  50. if($this->exists($this->sId))
  51. return true;
  52. $this->sId = genRndPwd(32, true);
  53. $aUrl = parse_url(BX_DOL_URL_ROOT);
  54. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  55. setcookie(BX_DOL_SESSION_COOKIE, $this->sId, 0, $sPath, '', false, true);
  56. $this->save();
  57. return true;
  58. }
  59. function destroy()
  60. {
  61. $aUrl = parse_url(BX_DOL_URL_ROOT);
  62. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  63. setcookie(BX_DOL_SESSION_COOKIE, '', time() - 86400, $sPath, '', false, true);
  64. unset($_COOKIE[BX_DOL_SESSION_COOKIE]);
  65. $this->oDb->delete($this->sId);
  66. $this->sId = '';
  67. $this->iUserId = 0;
  68. $this->aData = array();
  69. }
  70. function exists($sId = '')
  71. {
  72. if(empty($sId) && isset($_COOKIE[BX_DOL_SESSION_COOKIE]))
  73. $sId = bx_process_input($_COOKIE[BX_DOL_SESSION_COOKIE]);
  74. $mixedSession = array();
  75. if(($mixedSession = $this->oDb->exists($sId)) !== false) {
  76. $this->sId = $mixedSession['id'];
  77. $this->iUserId = (int)$mixedSession['user_id'];
  78. $this->aData = unserialize($mixedSession['data']);
  79. return true;
  80. } else
  81. return false;
  82. }
  83. function getId()
  84. {
  85. return $this->sId;
  86. }
  87. function setUserId($iUserId)
  88. {
  89. $this->iUserId = $iUserId;
  90. $this->save();
  91. }
  92. function setValue($sKey, $mixedValue)
  93. {
  94. if(empty($this->sId))
  95. $this->start();
  96. $this->aData[$sKey] = $mixedValue;
  97. $this->save();
  98. }
  99. function unsetValue($sKey)
  100. {
  101. if(empty($this->sId))
  102. $this->start();
  103. unset($this->aData[$sKey]);
  104. if(!empty($this->aData))
  105. $this->save();
  106. else
  107. $this->destroy();
  108. }
  109. function getValue($sKey)
  110. {
  111. if(empty($this->sId))
  112. $this->start();
  113. return isset($this->aData[$sKey]) ? $this->aData[$sKey] : false;
  114. }
  115. function getUnsetValue($sKey)
  116. {
  117. $mixedValue = $this->getValue($sKey);
  118. if($mixedValue !== false)
  119. $this->unsetValue($sKey);
  120. return $mixedValue;
  121. }
  122. public function maintenance()
  123. {
  124. return $this->oDb->deleteExpired();
  125. }
  126. protected function save()
  127. {
  128. if($this->iUserId == 0)
  129. $this->iUserId = getLoggedId();
  130. $this->oDb->save($this->sId, array(
  131. 'user_id' => $this->iUserId,
  132. 'data' => serialize($this->aData)
  133. ));
  134. }
  135. }
  136. /** @} */