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
|
<?php rcs_id('$Id: SQL.php,v 1.2 2005/11/21 20:57:58 rurban Exp $');
/**
* DB sessions for pear DB
*
* History
*
* Originally by Stanislav Shramko <stanis@movingmail.com>
* Minor rewrite by Reini Urban <rurban@x-ray.at> for Phpwiki.
* Quasi-major rewrite/decruft/fix by Jeff Dairiki <dairiki@dairiki.org>.
*/
class DbSession_SQL
extends DbSession
{
var $_backend_type = "SQL";
function DbSession_SQL (&$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() {
$dbh = &$this->_dbh;
$this->_connected = is_resource($dbh->connection);
if (!$this->_connected) {
$res = $dbh->connect($dbh->dsn);
if (DB::isError($res)) {
error_log("PhpWiki::DbSession::_connect: " . $res->getMessage());
}
}
return $dbh;
}
function query($sql) {
return $this->_dbh->query($sql);
}
// adds surrounding quotes
function quote($string) {
return $this->_dbh->quote($string);
}
function _disconnect() {
if (0 and $this->_connected)
$this->_dbh->disconnect();
}
/**
* 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->quote($id);
$res = $dbh->getOne("SELECT sess_data FROM $table WHERE sess_id=$qid");
$this->_disconnect();
if (DB::isError($res) || empty($res))
return '';
if (isa($dbh, 'DB_pgsql'))
//if (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();
//$dbh->unlock(false,1);
$table = $this->_table;
$qid = $dbh->quote($id);
$qip = $dbh->quote($GLOBALS['request']->get('REMOTE_ADDR'));
$time = $dbh->quote(time());
if (DEBUG and $sess_data == 'wiki_user|N;') {
trigger_error("delete empty session $qid", E_USER_WARNING);
}
// postgres can't handle binary data in a TEXT field.
if (isa($dbh, 'DB_pgsql'))
$sess_data = base64_encode($sess_data);
$qdata = $dbh->quote($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->query("DELETE FROM $table"
. " WHERE sess_id=$qid");
$res = $dbh->query("INSERT INTO $table"
. " (sess_id, sess_data, sess_date, sess_ip)"
. " VALUES ($qid, $qdata, $time, $qip)");
} else {
$res = $dbh->query("UPDATE $table"
. " SET sess_data=$qdata, sess_date=$time, sess_ip=$qip"
. " WHERE sess_id=$qid");
$result = $dbh->AffectedRows();
if ( $result === false or $result < 1 ) { // 0 cannot happen: time, -1 (failure) on mysql
$res = $dbh->query("INSERT INTO $table"
. " (sess_id, sess_data, sess_date, sess_ip)"
. " VALUES ($qid, $qdata, $time, $qip)");
}
}
$this->_disconnect();
return ! DB::isError($res);
}
/**
* 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->quote($id);
$dbh->query("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->query("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;
$res = $dbh->query("SELECT sess_data,sess_date,sess_ip FROM $table ORDER BY sess_date DESC");
if (DB::isError($res) || empty($res))
return $sessions;
while ($row = $res->fetchRow()) {
$data = $row['sess_data'];
$date = $row['sess_date'];
$ip = $row['sess_ip'];
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);
}
$this->_disconnect();
return $sessions;
}
}
// $Log: SQL.php,v $
// Revision 1.2 2005/11/21 20:57:58 rurban
// fix ref warnings, analog to ADODB
//
// 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:
?>
|