BxDolSessionQuery.php 4.5 KB

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