]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/DbSession/dba.php
var --> public
[SourceForge/phpwiki.git] / lib / DbSession / dba.php
1 <?php
2
3 /** DBA Sessions
4  *  session:
5  *     Index: session_id
6  *     Values: date : IP : data
7  *  Already open sessions, e.g. interim xmlrpc requests are
8  *  are treated specially. see write().
9  *  To avoid deadlocks in the session.db3 access,
10  *  the db is opened and closed for each access.
11  * @author: Reini Urban.
12  */
13 class DbSession_dba
14     extends DbSession
15 {
16     public $_backend_type = "dba";
17
18     function DbSession_dba(&$dbh, $table)
19     {
20         $this->_dbh = $dbh;
21         ini_set('session.save_handler', 'user');
22         session_module_name('user'); // new style
23         session_set_save_handler(array(&$this, 'open'),
24             array(&$this, 'close'),
25             array(&$this, 'read'),
26             array(&$this, 'write'),
27             array(&$this, 'destroy'),
28             array(&$this, 'gc'));
29         return $this;
30     }
31
32     function quote($str)
33     {
34         return $str;
35     }
36
37     function query($sql)
38     {
39         return false;
40     }
41
42     function & _connect()
43     {
44         global $DBParams;
45         $dbh = &$this->_dbh;
46         if (!$dbh) {
47             $directory = '/tmp';
48             $prefix = 'wiki_';
49             $dba_handler = 'gdbm';
50             $timeout = 12;
51             extract($DBParams); // overwrite the defaults
52             $dbfile = "$directory/$prefix" . 'session' . '.' . $dba_handler;
53             $dbh = new DbaDatabase($dbfile, 'c', $dba_handler);
54             $this->_dbh = &$dbh;
55         }
56         return $dbh;
57     }
58
59     function _disconnect()
60     {
61         if (isset($this->_dbh)) {
62             $this->_dbh->close();
63             unset($this->_dbh);
64         }
65     }
66
67     function open($save_path, $session_name)
68     {
69         $dbh = $this->_connect();
70         $dbh->open();
71     }
72
73     function close()
74     {
75         $this->_disconnect();
76     }
77
78     function read($id)
79     {
80         $dbh = $this->_connect();
81         $result = $dbh->get($id);
82         if (!$result) {
83             return false;
84         }
85         list(, , $packed) = explode(':', $result, 3);
86         $this->_disconnect();
87         if (strlen($packed) > 4000) {
88             trigger_error("Overlarge session data!", E_USER_WARNING);
89             $packed = '';
90             //$res = preg_replace('/s:6:"_cache";O:12:"WikiDB_cache".+}$/',"",$res);
91         }
92         return $packed;
93     }
94
95     function write($id, $sess_data)
96     {
97         if (defined("WIKI_XMLRPC") or defined("WIKI_SOAP")) return;
98
99         $dbh = $this->_connect();
100         $time = time();
101         $ip = $GLOBALS['request']->get('REMOTE_ADDR');
102         if (strlen($sess_data) > 4000) {
103             trigger_error("Overlarge session data!", E_USER_WARNING);
104             $sess_data = '';
105         }
106         $dbh->set($id, $time . ':' . $ip . ':' . $sess_data);
107         $this->_disconnect();
108         return true;
109     }
110
111     function destroy($id)
112     {
113         $dbh = $this->_connect();
114         $dbh->delete($id);
115         $this->_disconnect();
116         return true;
117     }
118
119     function gc($maxlifetime)
120     {
121         $dbh = $this->_connect();
122         $threshold = time() - $maxlifetime;
123         for ($id = $dbh->firstkey(); $id !== false; $id = $dbh->nextkey()) {
124             $result = $dbh->get($id);
125             list($date, ,) = explode(':', $result, 3);
126             if ($date < $threshold)
127                 $dbh->delete($id);
128         }
129         $this->_disconnect();
130         return true;
131     }
132
133     // WhoIsOnline support.
134     // TODO: ip-accesstime dynamic blocking API
135     function currentSessions()
136     {
137         $sessions = array();
138         $dbh = $this->_connect();
139         for ($id = $dbh->firstkey(); $id !== false; $id = $dbh->nextkey()) {
140             $result = $dbh->get($id);
141             list($date, $ip, $packed) = explode(':', $result, 3);
142             if (!$packed) continue;
143             // session_data contains the <variable name> + "|" + <packed string>
144             // we need just the wiki_user object (might be array as well)
145             if ($date < 908437560 or $date > 1588437560)
146                 $date = 0;
147             $user = strstr($packed, "wiki_user|");
148             $sessions[] = array('wiki_user' => substr($user, 10), // from "O:" onwards
149                 'date' => $date,
150                 'ip' => $ip);
151         }
152         $this->_disconnect();
153         return $sessions;
154     }
155 }
156
157 // Local Variables:
158 // mode: php
159 // tab-width: 8
160 // c-basic-offset: 4
161 // c-hanging-comment-ender-p: nil
162 // indent-tabs-mode: nil
163 // End: