BxDolSessionQuery.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. /**
  10. * @see BxDolSession
  11. */
  12. class BxDolSessionQuery extends BxDolDb
  13. {
  14. protected $sTable;
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. $this->sTable = 'sys_sessions';
  19. }
  20. function getTableName()
  21. {
  22. return $this->sTable;
  23. }
  24. function exists($sId)
  25. {
  26. $aSession = $this->getRow("SELECT `id`, `user_id`, `data` FROM `" . $this->sTable . "` WHERE `id`=:id LIMIT 1", [
  27. 'id' => (string)$sId
  28. ]);
  29. return !empty($aSession) ? $aSession : false;
  30. }
  31. function save($sId, $aSet)
  32. {
  33. $aSet['date'] = time();
  34. $aUpdate = $aSet;
  35. $aSet['id'] = $sId;
  36. $aBind = $aSet;
  37. unset($aBind['date']);
  38. if ($this->getOne("SELECT `date` FROM `" . $this->sTable . "` WHERE `id` = :id AND `data` = :data AND `user_id` = :user_id AND `date` > UNIX_TIMESTAMP() - " . BX_DOL_SESSION_SKIP_UPDATE, $aBind))
  39. return true;
  40. $this->updateLastActivity($sId);
  41. return (int)$this->query("INSERT INTO `" . $this->sTable . "` SET " . $this->arrayToSQL($aSet) . " ON DUPLICATE KEY UPDATE " . $this->arrayToSQL($aUpdate)) > 0;
  42. }
  43. function update($sId, $aSet = array())
  44. {
  45. $sSet = !empty($aSet) ? $this->arrayToSQL($aSet) . ", " : "";
  46. $sSet .= "`date`=UNIX_TIMESTAMP()";
  47. $this->updateLastActivity($sId);
  48. $iRet = (int)$this->query("UPDATE `" . $this->sTable . "` SET " . $sSet . " WHERE `id`=:id", array('id' => $sId)) > 0;
  49. $this->setReadOnlyMode(true);
  50. return $iRet;
  51. }
  52. function delete($sId)
  53. {
  54. $aRow = $this->getRow("SELECT `user_id`, `date` FROM `" . $this->sTable . "` WHERE `id`=:id LIMIT 1", ['id' => $sId]);
  55. if(!empty($aRow) && is_array($aRow))
  56. $this->updateLastActivityAccount($aRow['user_id'], $aRow['date']);
  57. $sSql = $this->prepare("DELETE FROM `" . $this->sTable . "` WHERE `id`=? LIMIT 1", $sId);
  58. return (int)$this->query($sSql) > 0;
  59. }
  60. function deleteExpired()
  61. {
  62. $iTime = time() - BX_DOL_SESSION_LIFETIME;
  63. $sSql = $this->prepare("SELECT `user_id`, `date` FROM `" . $this->sTable . "` WHERE `date` < ?", $iTime);
  64. $aRows = $this->getAll($sSql);
  65. foreach ($aRows as $aRow) {
  66. $this->updateLastActivityAccount($aRow['user_id'], $aRow['date']);
  67. }
  68. $sSql = $this->prepare("DELETE FROM `" . $this->sTable . "` WHERE `date` < ?", $iTime);
  69. $iRet = (int)$this->query($sSql);
  70. if ($iRet)
  71. $this->query("OPTIMIZE TABLE `" . $this->sTable . "`");
  72. return $iRet;
  73. }
  74. function deleteByAccountId($iAccountId)
  75. {
  76. $iDate = $this->getOne("SELECT `date` FROM `" . $this->sTable . "` WHERE `user_id` = :id ORDER BY `date` DESC", ['id' => $iAccountId]);
  77. if ($iDate)
  78. $this->updateLastActivityAccount($iAccountId, $iDate);
  79. return (int)$this->query("DELETE FROM `" . $this->sTable . "` WHERE `user_id` = :id", ['id' => $iAccountId]);
  80. }
  81. function updateLastActivity($iId)
  82. {
  83. if (defined('BX_DOL_SESSION_UPDATE_ACTIVE') && ($aRow = $this->getRow("SELECT `date`, `user_id` FROM `" . $this->sTable . "` WHERE `id` = :id AND `date` < UNIX_TIMESTAMP() - " . BX_DOL_SESSION_UPDATE_ACTIVE, ['id' => $iId]))) {
  84. $this->updateLastActivityAccount($aRow['user_id'], $aRow['date']);
  85. }
  86. }
  87. function updateLastActivityAccount($iAccountId, $iDate)
  88. {
  89. if ($iDate > 0) {
  90. $a = BxDolAccountQuery::getInstance()->getInfoById($iAccountId);
  91. if ($iDate > $a['active'])
  92. BxDolAccountQuery::getInstance()->_updateField($iAccountId, 'active', $iDate);
  93. }
  94. }
  95. function getLastActivityAccount($iUserId)
  96. {
  97. $sSql = $this->prepare("SELECT `date` FROM `" . $this->sTable . "` WHERE `user_id`=? ORDER BY `date` DESC LIMIT 1", $iUserId);
  98. return $this->getOne($sSql);
  99. }
  100. function getOldSession($iUserId) {
  101. $sSql = $this->prepare("SELECT `id` FROM `" . $this->sTable . "` WHERE `user_id`=? LIMIT 1", $iUserId);
  102. $sSession = $this->getOne($sSql);
  103. return !empty($sSession) ? $sSession : false;
  104. }
  105. }
  106. /** @} */