]> git.wh0rd.org - tt-rss.git/blob - classes/sessionhandler.php
more work on singleton-based DB
[tt-rss.git] / classes / sessionhandler.php
1 <?php
2 class SessionHandler implements SessionHandlerInterface {
3 private static $instance;
4 private $db;
5
6 public static function get() {
7 if (self::$instance == null)
8 self::$instance = new self();
9
10 return self::$instance;
11 }
12
13 private function __construct() {
14 $this->db = Db::get();
15
16 session_set_save_handler("SessionHandler::open", "SessionHandler::close",
17 "SessionHandler::read", "SessionHandler::write", "SessionHandler::destroy",
18 "SessionHandler::gc");
19 }
20
21 public static function open($save_path, $name) { }
22
23
24 public static function read ($id){
25
26 $query = "SELECT data FROM ttrss_sessions WHERE id='$id'";
27
28 $res = $this->db->query("SELECT data FROM ttrss_sessions WHERE id='$id'");
29
30 if ($this->db->num_rows($res) != 1) {
31
32 "INSERT INTO ttrss_sessions (id, data, expire)
33 VALUES ('$id', '$data', '$expire')";
34
35
36
37 } else {
38 $data = $this->db->fetch_result($res, 0, "data");
39 return base64_decode($data);
40 }
41
42 }
43
44 public static function write($id, $data) {
45 if (! $data) {
46 return false;
47 }
48
49 $data = $this->db->escape_string( base64_encode($data), false);
50
51 $expire = time() + max(SESSION_COOKIE_LIFETIME, 86400);
52
53 $query = "UPDATE ttrss_sessions SET data='$data',
54 expire = '$expire' WHERE id='$id'";
55
56 $this->db->query( $query);
57 return true;
58 }
59
60 public static function close () { }
61
62 public static function destroy($session_id) {
63 $this->db->query("DELETE FROM ttrss_sessions WHERE id = '$session_id'");
64 return true;
65 }
66
67 public static function gc($maxLifeTime) {
68 $this->db->query("DELETE FROM ttrss_sessions WHERE expire < " time() - $maxLifeTime);
69 return true;
70 }
71
72 }
73 ?>