]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/DbSession/dba.php
seperate DbSession classes: less memory, a bit slower
[SourceForge/phpwiki.git] / lib / DbSession / dba.php
1 <?php rcs_id('$Id: dba.php,v 1.1 2005-02-11 14:41:40 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             $dbfile = "$directory/$prefix" . 'session' . '.' . $dba_handler;
40             $dbh = new DbaDatabase($dbfile, false, $dba_handler);
41             $dbh->set_timeout($timeout);
42             if (!$dbh->open('c')) {
43                 trigger_error(sprintf(_("%s: Can't open dba database"), $dbfile), E_USER_ERROR);
44                 global $request;
45                 $request->finish(fmt("%s: Can't open dba database", $dbfile));
46             }
47             $this->_dbh = &$dbh;
48         }
49         return $dbh;
50     }
51
52     function _disconnect() {
53         if (0 and isset($this->_dbh))
54             $this->_dbh->close();
55     }
56
57     function open ($save_path, $session_name) {
58         $dbh = &$this->_connect();
59         $dbh->open();
60     }
61
62     function close() {
63         if ($this->_dbh)
64             $this->_dbh->close();
65     }
66
67     function read ($id) {
68         $dbh = &$this->_connect();
69         $result = $dbh->get($id);
70         if (!$result) {
71             return false;
72         }
73         list(,,$packed) = explode(':', $result, 3);
74         $this->_disconnect();
75         if (strlen($packed) > 4000) {
76             trigger_error("Overlarge session data!", E_USER_WARNING);
77             $packed = '';
78             //$res = preg_replace('/s:6:"_cache";O:12:"WikiDB_cache".+}$/',"",$res);
79         }
80         return $packed;
81     }
82   
83     function write ($id, $sess_data) {
84         $dbh = &$this->_connect();
85         $time = time();
86         $ip = $GLOBALS['request']->get('REMOTE_ADDR');
87         if (strlen($sess_data) > 4000) {
88             trigger_error("Overlarge session data!", E_USER_WARNING);
89             $sess_data = '';
90         }
91         $dbh->set($id,$time.':'.$ip.':'.$sess_data);
92         $this->_disconnect();
93         return true;
94     }
95
96     function destroy ($id) {
97         $dbh = &$this->_connect();
98         $dbh->delete($id);
99         $this->_disconnect();
100         return true;
101     }
102
103     function gc ($maxlifetime) {
104         $dbh = &$this->_connect();
105         $threshold = time() - $maxlifetime;
106         for ($id = $dbh->firstkey(); $id !== false; $id = $dbh->nextkey()) {
107             $result = $dbh->get($id);
108             list($date,,) = explode(':', $result, 3);
109             if ($date < $threshold)
110                 $dbh->delete($id);
111         }
112         $this->_disconnect();
113         return true;
114     }
115
116     // WhoIsOnline support. 
117     // TODO: ip-accesstime dynamic blocking API
118     function currentSessions() {
119         $sessions = array();
120         $dbh = &$this->_connect();
121         for ($id = $dbh->firstkey(); $id !== false; $id = $dbh->nextkey()) {
122             $result = $dbh->get($id);
123             list($date,$ip,$packed) = explode(':', $result, 3);
124             if (!$packed) continue;
125             // session_data contains the <variable name> + "|" + <packed string>
126             // we need just the wiki_user object (might be array as well)
127             if ($date < 908437560 or $date > 1588437560)
128                 $date = 0;
129             $user = strstr($packed, "wiki_user|");
130             $sessions[] = array('wiki_user' => substr($user,10), // from "O:" onwards
131                                 'date' => $date,
132                                 'ip' => $ip);
133         }
134         $this->_disconnect();
135         return $sessions;
136     }
137 }
138
139 // $Log: not supported by cvs2svn $
140
141 // Local Variables:
142 // mode: php
143 // tab-width: 8
144 // c-basic-offset: 4
145 // c-hanging-comment-ender-p: nil
146 // indent-tabs-mode: nil
147 // End:
148 ?>