BxDolSessionQuery.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. /**
  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. if(isset($aSet['user_id']) && (int)$aSet['user_id'] > 0)
  33. $this->query("DELETE FROM `" . $this->sTable . "` WHERE `id`<>:id AND `user_id`=:user_id", array(
  34. 'id' => $sId,
  35. 'user_id' => (int)$aSet['user_id']
  36. ));
  37. $aSet['id'] = $sId;
  38. return (int)$this->query("REPLACE INTO `" . $this->sTable . "` SET " . $this->arrayToSQL($aSet) . ", `date`=UNIX_TIMESTAMP()") > 0;
  39. }
  40. function update($sId, $aSet = array())
  41. {
  42. $sSet = !empty($aSet) ? $this->arrayToSQL($aSet) . ", " : "";
  43. $sSet .= "`date`=UNIX_TIMESTAMP()";
  44. return (int)$this->query("UPDATE `" . $this->sTable . "` SET " . $sSet . " WHERE `id`=:id", array('id' => $sId)) > 0;
  45. }
  46. function delete($sId)
  47. {
  48. $sSql = $this->prepare("DELETE FROM `" . $this->sTable . "` WHERE `id`=? LIMIT 1", $sId);
  49. return (int)$this->query($sSql) > 0;
  50. }
  51. function deleteExpired()
  52. {
  53. $sSql = $this->prepare("DELETE FROM `" . $this->sTable . "` WHERE `date` < (UNIX_TIMESTAMP() - ?)", BX_DOL_SESSION_LIFETIME);
  54. $iRet = (int)$this->query($sSql);
  55. if ($iRet)
  56. $this->query("OPTIMIZE TABLE `" . $this->sTable . "`");
  57. return $iRet;
  58. }
  59. }
  60. /** @} */