]> git.wh0rd.org Git - tt-rss.git/blob - sessions.php
footer: change my homepage url
[tt-rss.git] / sessions.php
1 <?php
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         $session_name = (!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME;
9
10         ini_set("session.gc_probability", 50);
11         ini_set("session.name", $session_name);
12         ini_set("session.use_only_cookies", true);
13         ini_set("session.gc_maxlifetime", SESSION_EXPIRE_TIME);
14
15         function ttrss_open ($s, $n) {
16         
17                 global $session_connection;
18                 
19                 $session_connection = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
20                 
21                 return true;
22         }
23
24         function ttrss_read ($id){
25         
26                 global $session_connection,$session_read;                                        
27
28                 $query = "SELECT data FROM ttrss_sessions WHERE id='$id'";
29
30                 $res = db_query($session_connection, $query);
31                 
32                 if (db_num_rows($res) != 1) {
33                         return "";
34                 } else {
35                         $session_read = db_fetch_assoc($res);
36                         $session_read["data"] = base64_decode($session_read["data"]);
37                         return $session_read["data"];
38                 }
39         }
40
41         function ttrss_write ($id, $data) {
42  
43                 if (! $data) { 
44                         return false; 
45                 }
46                 
47                 global $session_connection, $session_read, $session_expire;
48                 
49                 $expire = time() + $session_expire;
50                 
51                 $data = db_escape_string(base64_encode($data), $session_connection);
52                 
53                 if ($session_read) {
54                         $query = "UPDATE ttrss_sessions SET data='$data', 
55                                         expire='$expire' WHERE id='$id'"; 
56                 } else {
57                         $query = "INSERT INTO ttrss_sessions (id, data, expire)
58                                         VALUES ('$id', '$data', '$expire')";
59                 }
60                 
61                 db_query($session_connection, $query);
62                 return true;
63         }
64
65         function ttrss_close () {
66         
67                 global $session_connection;
68                 
69                 db_close($session_connection);
70                 
71                 return true;
72         }
73
74         function ttrss_destroy ($id) {
75         
76                 global $session_connection;
77
78                 $query = "DELETE FROM ttrss_sessions WHERE id = '$id'";
79                 
80                 db_query($session_connection, $query);
81                 
82                 return true;
83         }
84
85         function ttrss_gc ($expire) {
86         
87                 global $session_connection;
88                 
89                 $query = "DELETE FROM ttrss_sessions WHERE expire < " . time();
90                 
91                 db_query($session_connection, $query);
92         }
93
94         if (DATABASE_BACKED_SESSIONS) {
95                 session_set_save_handler("ttrss_open", 
96                         "ttrss_close", "ttrss_read", "ttrss_write", 
97                         "ttrss_destroy", "ttrss_gc");
98         }
99
100         session_set_cookie_params(SESSION_COOKIE_LIFETIME);
101
102         session_start();
103 ?>