1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
<?php rcs_id('$Id: ADODB.php,v 1.2 2005/11/21 20:48:48 rurban Exp $');
/*
Copyright 2005 $ThePhpWikiProgrammingTeam
This file is part of PhpWiki.
PhpWiki is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
PhpWiki is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PhpWiki; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* ADODB db sessions, based on pear DB Sessions.
*
* @author: Reini Urban
*/
class DbSession_ADODB
extends DbSession
{
var $_backend_type = "ADODB";
function DbSession_ADODB ($dbh, $table) {
$this->_dbh = $dbh;
$this->_table = $table;
ini_set('session.save_handler','user');
session_module_name('user'); // new style
session_set_save_handler(array(&$this, 'open'),
array(&$this, 'close'),
array(&$this, 'read'),
array(&$this, 'write'),
array(&$this, 'destroy'),
array(&$this, 'gc'));
return $this;
}
function & _connect() {
global $request;
static $parsed = false;
$dbh = &$this->_dbh;
if (!$dbh or !is_resource($dbh->_connectionID)) {
if (!$parsed) $parsed = parseDSN($request->_dbi->getParam('dsn'));
$this->_dbh =& ADONewConnection($parsed['phptype']); // Probably only MySql works just now
$this->_dbh->Connect($parsed['hostspec'],$parsed['username'],
$parsed['password'], $parsed['database']);
$dbh = &$this->_dbh;
}
return $dbh;
}
function query($sql) {
return $this->_dbh->Execute($sql);
}
function quote($string) {
return $this->_dbh->qstr($string);
}
function _disconnect() {
if (0 and $this->_dbh)
$this->_dbh->close();
}
/**
* Opens a session.
*
* Actually this function is a fake for session_set_save_handle.
* @param string $save_path a path to stored files
* @param string $session_name a name of the concrete file
* @return boolean true just a variable to notify PHP that everything
* is good.
* @access private
*/
function open ($save_path, $session_name) {
//$this->log("_open($save_path, $session_name)");
return true;
}
/**
* Closes a session.
*
* This function is called just after <i>write</i> call.
*
* @return boolean true just a variable to notify PHP that everything
* is good.
* @access private
*/
function close() {
//$this->log("_close()");
return true;
}
/**
* Reads the session data from DB.
*
* @param string $id an id of current session
* @return string
* @access private
*/
function read ($id) {
//$this->log("_read($id)");
$dbh = $this->_connect();
$table = $this->_table;
$qid = $dbh->qstr($id);
$res = '';
$row = $dbh->GetRow("SELECT sess_data FROM $table WHERE sess_id=$qid");
if ($row)
$res = $row[0];
$this->_disconnect();
if (!empty($res) and preg_match('|^[a-zA-Z0-9/+=]+$|', $res))
$res = base64_decode($res);
if (strlen($res) > 4000) {
trigger_error("Overlarge session data! ".strlen($res).
" gt. 4000", E_USER_WARNING);
$res = preg_replace('/s:6:"_cache";O:12:"WikiDB_cache".+}$/',"",$res);
$res = preg_replace('/s:12:"_cached_html";s:.+",s:4:"hits"/','s:4:"hits"',$res);
if (strlen($res) > 4000) $res = '';
}
return $res;
}
/**
* Saves the session data into DB.
*
* Just a comment: The "write" handler is not
* executed until after the output stream is closed. Thus,
* output from debugging statements in the "write" handler
* will never be seen in the browser. If debugging output
* is necessary, it is suggested that the debug output be
* written to a file instead.
*
* @param string $id
* @param string $sess_data
* @return boolean true if data saved successfully and false
* otherwise.
* @access private
*/
function write ($id, $sess_data) {
$dbh = $this->_connect();
$table = $this->_table;
$qid = $dbh->qstr($id);
$qip = $dbh->qstr($GLOBALS['request']->get('REMOTE_ADDR'));
$time = $dbh->qstr(time());
// postgres can't handle binary data in a TEXT field.
if (isa($dbh, 'ADODB_postgres64'))
$sess_data = base64_encode($sess_data);
$qdata = $dbh->qstr($sess_data);
/* AffectedRows with sessions seems to be instable on certain platforms.
* Enable the safe and slow USE_SAFE_DBSESSION then.
*/
if (USE_SAFE_DBSESSION) {
$dbh->Execute("DELETE FROM $table"
. " WHERE sess_id=$qid");
$rs = $dbh->Execute("INSERT INTO $table"
. " (sess_id, sess_data, sess_date, sess_ip)"
. " VALUES ($qid, $qdata, $time, $qip)");
} else {
$rs = $dbh->Execute("UPDATE $table"
. " SET sess_data=$qdata, sess_date=$time, sess_ip=$qip"
. " WHERE sess_id=$qid");
$result = $dbh->Affected_Rows();
if ( $result === false or $result < 1 ) { // false or int > 0
$rs = $dbh->Execute("INSERT INTO $table"
. " (sess_id, sess_data, sess_date, sess_ip)"
. " VALUES ($qid, $qdata, $time, $qip)");
}
}
$result = ! $rs->EOF;
if ($result) $rs->free();
$this->_disconnect();
return $result;
}
/**
* Destroys a session.
*
* Removes a session from the table.
*
* @param string $id
* @return boolean true
* @access private
*/
function destroy ($id) {
$dbh = $this->_connect();
$table = $this->_table;
$qid = $dbh->qstr($id);
$dbh->Execute("DELETE FROM $table WHERE sess_id=$qid");
$this->_disconnect();
return true;
}
/**
* Cleans out all expired sessions.
*
* @param int $maxlifetime session's time to live.
* @return boolean true
* @access private
*/
function gc ($maxlifetime) {
$dbh = $this->_connect();
$table = $this->_table;
$threshold = time() - $maxlifetime;
$dbh->Execute("DELETE FROM $table WHERE sess_date < $threshold");
$this->_disconnect();
return true;
}
// WhoIsOnline support.
// TODO: ip-accesstime dynamic blocking API
function currentSessions() {
$sessions = array();
$dbh = $this->_connect();
$table = $this->_table;
$rs = $dbh->Execute("SELECT sess_data,sess_date,sess_ip FROM $table ORDER BY sess_date DESC");
if ($rs->EOF) {
$rs->free();
return $sessions;
}
while (!$rs->EOF) {
$row = $rs->fetchRow();
$data = $row[0];
$date = $row[1];
$ip = $row[2];
if (preg_match('|^[a-zA-Z0-9/+=]+$|', $data))
$data = base64_decode($data);
if ($date < 908437560 or $date > 1588437560)
$date = 0;
// session_data contains the <variable name> + "|" + <packed string>
// we need just the wiki_user object (might be array as well)
$user = strstr($data,"wiki_user|");
$sessions[] = array('wiki_user' => substr($user,10), // from "O:" onwards
'date' => $date,
'ip' => $ip);
$rs->MoveNext();
}
$rs->free();
$this->_disconnect();
return $sessions;
}
}
// $Log: ADODB.php,v $
// Revision 1.2 2005/11/21 20:48:48 rurban
// fix ref warnings reported by schorni
//
// Revision 1.1 2005/02/11 14:41:40 rurban
// seperate DbSession classes: less memory, a bit slower
//
// Local Variables:
// mode: php
// tab-width: 8
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:
?>
|