BxDolSessionQuery.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. $sSql = $this->prepare("SELECT `id`, `user_id`, `data` FROM `" . $this->sTable . "` WHERE `id`=? LIMIT 1", $sId);
  27. $aSession = $this->getRow($sSql);
  28. return !empty($aSession) ? $aSession : false;
  29. }
  30. function save($sId, $aSet)
  31. {
  32. $aSet['date'] = time();
  33. $aUpdate = $aSet;
  34. $aSet['id'] = $sId;
  35. $aBind = $aSet;
  36. unset($aBind['date']);
  37. 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))
  38. return true;
  39. return (int)$this->query("INSERT INTO `" . $this->sTable . "` SET " . $this->arrayToSQL($aSet) . " ON DUPLICATE KEY UPDATE " . $this->arrayToSQL($aUpdate)) > 0;
  40. }
  41. function update($sId, $aSet = array())
  42. {
  43. $sSet = !empty($aSet) ? $this->arrayToSQL($aSet) . ", " : "";
  44. $sSet .= "`date`=UNIX_TIMESTAMP()";
  45. $iRet = (int)$this->query("UPDATE `" . $this->sTable . "` SET " . $sSet . " WHERE `id`=:id", array('id' => $sId)) > 0;
  46. $this->setReadOnlyMode(true);
  47. return $iRet;
  48. }
  49. function delete($sId)
  50. {
  51. $sSql = $this->prepare("DELETE FROM `" . $this->sTable . "` WHERE `id`=? LIMIT 1", $sId);
  52. return (int)$this->query($sSql) > 0;
  53. }
  54. function deleteExpired()
  55. {
  56. $sSql = $this->prepare("DELETE FROM `" . $this->sTable . "` WHERE `date` < (UNIX_TIMESTAMP() - ?)", BX_DOL_SESSION_LIFETIME);
  57. $iRet = (int)$this->query($sSql);
  58. if ($iRet)
  59. $this->query("OPTIMIZE TABLE `" . $this->sTable . "`");
  60. return $iRet;
  61. }
  62. function getOldSession($iUserId) {
  63. $sSql = $this->prepare("SELECT `id` FROM `" . $this->sTable . "` WHERE `user_id`=? LIMIT 1", $iUserId);
  64. $sSession = $this->getOne($sSql);
  65. return !empty($sSession) ? $sSession : false;
  66. }
  67. }
  68. /** @} */