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