aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Web/SessionHandler.php
blob: 670e8f216d9ec995a842430417b480d3a9df4111 (plain) (blame)
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
74
75
76
77
78
79
80
81
82
83
<?php

namespace Zotlabs\Web;


class SessionHandler implements \SessionHandlerInterface {

	private $session_exists;
	private $session_expire;


	function open ($s, $n) {
		$this->session_exists = 0;
		$this->session_expire = 180000;
		return true;
	}

	function read ($id) {

		if(x($id))
			$r = q("SELECT `data` FROM `session` WHERE `sid`= '%s'", dbesc($id));

		if($r) {
			$this->session_exists = true;
			return $r[0]['data'];
		}

		return '';
	}


	function write ($id, $data) {

		if(! $id || ! $data) {
			return false;
		}

		// Can't just use $data here because we can't be certain of the serialisation algorithm

		if($_SESSION && array_key_exists('remember_me',$_SESSION) && intval($_SESSION['remember_me']))
			$expire = time() + (60 * 60 * 24 * 365);
		else
			$expire = time() + $this->session_expire;
		$default_expire = time() + 300;

		if($this->session_exists) {
			q("UPDATE `session`
				SET `data` = '%s', `expire` = '%s' WHERE `sid` = '%s'",
				dbesc($data),
				dbesc($expire),
				dbesc($id)
			);
		} 
		else {
			q("INSERT INTO `session` (sid, expire, data) values ('%s', '%s', '%s')",
				dbesc($id),
				dbesc($default_expire),
				dbesc($data)
			);
		}

		return true;
	}

	
	function close() {
		return true;
	}


	function destroy ($id) {
		q("DELETE FROM `session` WHERE `sid` = '%s'", dbesc($id));
		return true;
	}


	function gc($expire) {
		q("DELETE FROM session WHERE expire < %d", dbesc(time()));
		return true;
	}


}