summaryrefslogtreecommitdiff
path: root/classes/sessionhandler.php
blob: 66d8dd86cce453cf35ad83cf401d24e7a88930a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
class SessionHandler implements SessionHandlerInterface {
	private static $instance;
	private $db;

	public static function get() {
		if (self::$instance == null)
			self::$instance = new self();

		return self::$instance;
	}

	private function __construct() {
		$this->db = Db::get();

		session_set_save_handler("SessionHandler::open", "SessionHandler::close",
			"SessionHandler::read", "SessionHandler::write", "SessionHandler::destroy",
			"SessionHandler::gc");
	}

	public static function open($save_path, $name) { }


	public static function read ($id){

		$query = "SELECT data FROM ttrss_sessions WHERE id='$id'";

		$res = $this->db->query("SELECT data FROM ttrss_sessions WHERE id='$id'");

		if ($this->db->num_rows($res) != 1) {

			"INSERT INTO ttrss_sessions (id, data, expire)
					VALUES ('$id', '$data', '$expire')";



		} else {
			$data = $this->db->fetch_result($res, 0, "data");
			return base64_decode($data);
		}

	}

	public static function write($id, $data) {
		if (! $data) {
			return false;
		}

		$data = $this->db->escape_string( base64_encode($data), false);

		$expire = time() + max(SESSION_COOKIE_LIFETIME, 86400);

	 	$query = "UPDATE ttrss_sessions SET data='$data',
				expire = '$expire' WHERE id='$id'";

		$this->db->query( $query);
		return true;
	}

	public static function close () { }

	public static function destroy($session_id) {
		$this->db->query("DELETE FROM ttrss_sessions WHERE id = '$session_id'");
		return true;
	}

	public static function gc($maxLifeTime) {
		$this->db->query("DELETE FROM ttrss_sessions WHERE expire < " time() - $maxLifeTime);
		return true;
	}

}
?>