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