]> git.wh0rd.org - tt-rss.git/blob - sessions.php
headline display fixes for RTL
[tt-rss.git] / sessions.php
1 <?
2 // Original from http://www.daniweb.com/code/snippet43.html
3
4 require_once "config.php";
5 require_once "db.php";
6
7 $session_expire = SESSION_EXPIRE_TIME; //seconds
8
9 ini_set("session.gc_probability", 50);
10 ini_set("session.name", "ttrss_sid");
11 ini_set("session.use_only_cookies", true);
12
13 function open ($s, $n) {
14
15 global $session_connection;
16
17 $session_connection = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
18
19 return true;
20 }
21
22 function read ($id){
23
24 global $session_connection,$session_read;
25
26 $ip_address = $_SERVER["REMOTE_ADDR"];
27
28 if (SESSION_CHECK_ADDRESS) {
29 $address_check_qpart = " AND ip_address = '$ip_address'";
30 }
31
32 $query = "SELECT data FROM ttrss_sessions WHERE id='$id' $address_check_qpart";
33
34 $res = db_query($session_connection, $query);
35
36 if (db_num_rows($res) != 1) {
37 return "";
38 } else {
39 $session_read = db_fetch_assoc($res);
40 $session_read["data"] = base64_decode($session_read["data"]);
41 return $session_read["data"];
42 }
43 }
44
45 function write ($id, $data) {
46
47 if (! $data) {
48 return false;
49 }
50
51 global $session_connection, $session_read, $session_expire;
52
53 $expire = time() + $session_expire;
54
55 $data = db_escape_string(base64_encode($data), $session_connection);
56
57 $ip_address = $_SERVER["REMOTE_ADDR"];
58
59 if (SESSION_CHECK_ADDRESS) {
60 $address_check_qpart = " AND ip_address = '$ip_address'";
61 }
62
63 if ($session_read) {
64 $query = "UPDATE ttrss_sessions SET data='$data',
65 expire='$expire' WHERE id='$id' $address_check_qpart";
66 } else {
67 $query = "INSERT INTO ttrss_sessions (id, data, expire, ip_address)
68 VALUES ('$id', '$data', '$expire', '$ip_address')";
69 }
70
71 db_query($session_connection, $query);
72 return true;
73 }
74
75 function close () {
76
77 global $session_connection;
78
79 db_close($session_connection);
80
81 return true;
82 }
83
84 function destroy ($id) {
85
86 global $session_connection;
87
88 $ip_address = $_SERVER["REMOTE_ADDR"];
89
90 if (SESSION_CHECK_ADDRESS) {
91 $address_check_qpart = " AND ip_address = '$ip_address'";
92 }
93
94 $query = "DELETE FROM ttrss_sessions WHERE id = '$id' $address_check_qpart";
95
96 db_query($session_connection, $query);
97
98 return true;
99 }
100
101 function gc ($expire) {
102
103 global $session_connection;
104
105 $query = "DELETE FROM ttrss_sessions WHERE expire < " . time();
106
107 db_query($session_connection, $query);
108 }
109
110 // session_set_cookie_params(SESSION_COOKIE_LIFETIME);
111
112 if (DATABASE_BACKED_SESSIONS) {
113 session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
114 }
115
116 session_start();
117 ?>