SessionStoreSqlite.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. class LtSessionSqlite implements LtSessionStore
  3. {
  4. public $conf;
  5. private $lifeTime; //session.gc_maxlifetime
  6. private $dbHandle;
  7. private $dbName;
  8. private $tableName;
  9. public function __construct()
  10. {
  11. // --
  12. }
  13. public function init()
  14. {
  15. if (isset($this->conf['gc_maxlifetime']))
  16. {
  17. $this->lifeTime = $this->conf['gc_maxlifetime'];
  18. }
  19. else
  20. {
  21. $this->lifeTime = get_cfg_var("session.gc_maxlifetime");
  22. }
  23. if (isset($this->conf['table_name']))
  24. {
  25. $this->tableName = $this->conf['table_name'];
  26. }
  27. else
  28. {
  29. $this->tableName = 'lotus_session';
  30. }
  31. if (isset($this->conf['db_name']))
  32. {
  33. $this->dbName = $this->conf['db_name'];
  34. }
  35. else
  36. {
  37. $this->dbName = '/tmp/Lotus/session/session_sqlite2.db';
  38. }
  39. if (!$this->dbHandle = sqlite_open($this->dbName, 0666))
  40. {
  41. trigger_error('session sqlite db error');
  42. return false;
  43. }
  44. return true;
  45. }
  46. public function open($savePath, $sessName)
  47. {
  48. return true;
  49. }
  50. public function close()
  51. {
  52. $this->gc($this->lifeTime);
  53. return @sqlite_close($this->dbHandle);
  54. }
  55. public function read($sessID)
  56. {
  57. $res = sqlite_query("SELECT session_data AS d FROM $this->tableName
  58. WHERE session_id = '$sessID'
  59. AND session_expires > " . time(), $this->dbHandle);
  60. if ($row = sqlite_fetch_array($res, SQLITE_ASSOC))
  61. {
  62. return $row['d'];
  63. }
  64. else
  65. {
  66. return "";
  67. }
  68. }
  69. public function write($sessID, $sessData)
  70. {
  71. $newExp = time() + $this->lifeTime;
  72. $res = sqlite_query("SELECT * FROM $this->tableName
  73. WHERE session_id = '$sessID'", $this->dbHandle);
  74. if (sqlite_num_rows($res))
  75. {
  76. sqlite_exec("UPDATE $this->tableName
  77. SET session_expires = '$newExp',
  78. session_data = '$sessData'
  79. WHERE session_id = '$sessID'", $this->dbHandle);
  80. if (sqlite_changes($this->dbHandle))
  81. {
  82. return true;
  83. }
  84. }
  85. else
  86. {
  87. sqlite_exec("INSERT INTO $this->tableName (
  88. session_id,
  89. session_expires,
  90. session_data)
  91. VALUES(
  92. '$sessID',
  93. '$newExp',
  94. '$sessData')", $this->dbHandle);
  95. if (sqlite_changes($this->dbHandle))
  96. {
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. public function destroy($sessID)
  103. {
  104. sqlite_exec("DELETE FROM $this->tableName WHERE session_id = '$sessID'", $this->dbHandle);
  105. if (sqlite_changes($this->dbHandle))
  106. {
  107. return true;
  108. }
  109. return false;
  110. }
  111. public function gc($sessMaxLifeTime)
  112. {
  113. sqlite_exec("DELETE FROM $this->tableName WHERE session_expires < " . time(), $this->dbHandle);
  114. return sqlite_changes($this->dbHandle);
  115. }
  116. public function runOnce()
  117. {
  118. $sql = "SELECT name FROM sqlite_master WHERE type='table' UNION ALL SELECT name FROM sqlite_temp_master WHERE type='table' AND name='" . $this->tableName . "'";
  119. $res = sqlite_query($sql, $this->dbHandle);
  120. $row = sqlite_fetch_array($res, SQLITE_ASSOC);
  121. if (empty($row))
  122. {
  123. $sql = "CREATE TABLE $this->tableName (
  124. [session_id] VARCHAR(255) NOT NULL PRIMARY KEY,
  125. [session_expires] INTEGER DEFAULT '0' NOT NULL,
  126. [session_data] TEXT NULL
  127. )";
  128. return sqlite_exec($sql, $this->dbHandle);
  129. }
  130. return false;
  131. }
  132. }