BxDolSession.php 4.2 KB

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