"TYPO3 4.0.1: typo3_src-4.0.1/typo3/sysext/adodb/adodb/session/adodb-session.php Source File", "datetime" => "Sat Dec 2 19:22:26 2006", "date" => "2 Dec 2006", "doxygenversion" => "1.4.6", "projectname" => "TYPO3 4.0.1", "projectnumber" => "4.0.1" ); get_header($doxygen_vars); ?>
00001 <?php 00002 00003 00004 /* 00005 V4.90 8 June 2006 (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved. 00006 Contributed by Ross Smith (adodb@netebb.com). 00007 Released under both BSD license and Lesser GPL library license. 00008 Whenever there is any discrepancy between the two licenses, 00009 the BSD license will take precedence. 00010 Set tabs to 4 for best viewing. 00011 */ 00012 00013 /* 00014 You may want to rename the 'data' field to 'session_data' as 00015 'data' appears to be a reserved word for one or more of the following: 00016 ANSI SQL 00017 IBM DB2 00018 MS SQL Server 00019 Postgres 00020 SAP 00021 00022 If you do, then execute: 00023 00024 ADODB_Session::dataFieldName('session_data'); 00025 00026 */ 00027 00028 if (!defined('_ADODB_LAYER')) { 00029 require realpath(dirname(__FILE__) . '/../adodb.inc.php'); 00030 } 00031 00032 if (defined('ADODB_SESSION')) return 1; 00033 00034 define('ADODB_SESSION', dirname(__FILE__)); 00035 00036 00037 /* 00038 Unserialize session data manually. See http://phplens.com/lens/lensforum/msgs.php?id=9821 00039 00040 From Kerr Schere, to unserialize session data stored via ADOdb. 00041 1. Pull the session data from the db and loop through it. 00042 2. Inside the loop, you will need to urldecode the data column. 00043 3. After urldecode, run the serialized string through this function: 00044 00045 */ 00046 function adodb_unserialize( $serialized_string ) 00047 { 00048 $variables = array( ); 00049 $a = preg_split( "/(\w+)\|/", $serialized_string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ); 00050 for( $i = 0; $i < count( $a ); $i = $i+2 ) { 00051 $variables[$a[$i]] = unserialize( $a[$i+1] ); 00052 } 00053 return( $variables ); 00054 } 00055 00056 /* 00057 Thanks Joe Li. See http://phplens.com/lens/lensforum/msgs.php?id=11487&x=1 00058 Since adodb 4.61. 00059 */ 00060 function adodb_session_regenerate_id() 00061 { 00062 $conn =& ADODB_Session::_conn(); 00063 if (!$conn) return false; 00064 00065 $old_id = session_id(); 00066 if (function_exists('session_regenerate_id')) { 00067 session_regenerate_id(); 00068 } else { 00069 session_id(md5(uniqid(rand(), true))); 00070 $ck = session_get_cookie_params(); 00071 setcookie(session_name(), session_id(), false, $ck['path'], $ck['domain'], $ck['secure']); 00072 //@session_start(); 00073 } 00074 $new_id = session_id(); 00075 $ok =& $conn->Execute('UPDATE '. ADODB_Session::table(). ' SET sesskey='. $conn->qstr($new_id). ' WHERE sesskey='.$conn->qstr($old_id)); 00076 00077 /* it is possible that the update statement fails due to a collision */ 00078 if (!$ok) { 00079 session_id($old_id); 00080 if (empty($ck)) $ck = session_get_cookie_params(); 00081 setcookie(session_name(), session_id(), false, $ck['path'], $ck['domain'], $ck['secure']); 00082 return false; 00083 } 00084 00085 return true; 00086 } 00087 00088 /* 00089 Generate database table for session data 00090 @see http://phplens.com/lens/lensforum/msgs.php?id=12280 00091 @return 0 if failure, 1 if errors, 2 if successful. 00092 @author Markus Staab http://www.public-4u.de 00093 */ 00094 function adodb_session_create_table($schemaFile=null,$conn = null) 00095 { 00096 // set default values 00097 if ($schemaFile===null) $schemaFile = ADODB_SESSION . '/session_schema.xml'; 00098 if ($conn===null) $conn =& ADODB_Session::_conn(); 00099 00100 if (!$conn) return 0; 00101 00102 $schema = new adoSchema($conn); 00103 $schema->ParseSchema($schemaFile); 00104 return $schema->ExecuteSchema(); 00105 } 00106 00110 class ADODB_Session { 00112 // getter/setter methods 00114 00115 /* 00116 00117 function Lock($lock=null) 00118 { 00119 static $_lock = false; 00120 00121 if (!is_null($lock)) $_lock = $lock; 00122 return $lock; 00123 } 00124 */ 00127 function driver($driver = null) { 00128 static $_driver = 'mysql'; 00129 static $set = false; 00130 00131 if (!is_null($driver)) { 00132 $_driver = trim($driver); 00133 $set = true; 00134 } elseif (!$set) { 00135 // backwards compatibility 00136 if (isset($GLOBALS['ADODB_SESSION_DRIVER'])) { 00137 return $GLOBALS['ADODB_SESSION_DRIVER']; 00138 } 00139 } 00140 00141 return $_driver; 00142 } 00143 00146 function host($host = null) { 00147 static $_host = 'localhost'; 00148 static $set = false; 00149 00150 if (!is_null($host)) { 00151 $_host = trim($host); 00152 $set = true; 00153 } elseif (!$set) { 00154 // backwards compatibility 00155 if (isset($GLOBALS['ADODB_SESSION_CONNECT'])) { 00156 return $GLOBALS['ADODB_SESSION_CONNECT']; 00157 } 00158 } 00159 00160 return $_host; 00161 } 00162 00165 function user($user = null) { 00166 static $_user = 'root'; 00167 static $set = false; 00168 00169 if (!is_null($user)) { 00170 $_user = trim($user); 00171 $set = true; 00172 } elseif (!$set) { 00173 // backwards compatibility 00174 if (isset($GLOBALS['ADODB_SESSION_USER'])) { 00175 return $GLOBALS['ADODB_SESSION_USER']; 00176 } 00177 } 00178 00179 return $_user; 00180 } 00181 00184 function password($password = null) { 00185 static $_password = ''; 00186 static $set = false; 00187 00188 if (!is_null($password)) { 00189 $_password = $password; 00190 $set = true; 00191 } elseif (!$set) { 00192 // backwards compatibility 00193 if (isset($GLOBALS['ADODB_SESSION_PWD'])) { 00194 return $GLOBALS['ADODB_SESSION_PWD']; 00195 } 00196 } 00197 00198 return $_password; 00199 } 00200 00203 function database($database = null) { 00204 static $_database = 'xphplens_2'; 00205 static $set = false; 00206 00207 if (!is_null($database)) { 00208 $_database = trim($database); 00209 $set = true; 00210 } elseif (!$set) { 00211 // backwards compatibility 00212 if (isset($GLOBALS['ADODB_SESSION_DB'])) { 00213 return $GLOBALS['ADODB_SESSION_DB']; 00214 } 00215 } 00216 00217 return $_database; 00218 } 00219 00222 function persist($persist = null) 00223 { 00224 static $_persist = true; 00225 00226 if (!is_null($persist)) { 00227 $_persist = trim($persist); 00228 } 00229 00230 return $_persist; 00231 } 00232 00235 function lifetime($lifetime = null) { 00236 static $_lifetime; 00237 static $set = false; 00238 00239 if (!is_null($lifetime)) { 00240 $_lifetime = (int) $lifetime; 00241 $set = true; 00242 } elseif (!$set) { 00243 // backwards compatibility 00244 if (isset($GLOBALS['ADODB_SESS_LIFE'])) { 00245 return $GLOBALS['ADODB_SESS_LIFE']; 00246 } 00247 } 00248 if (!$_lifetime) { 00249 $_lifetime = ini_get('session.gc_maxlifetime'); 00250 if ($_lifetime <= 1) { 00251 // bug in PHP 4.0.3 pl 1 -- how about other versions? 00252 //print "<h3>Session Error: PHP.INI setting <i>session.gc_maxlifetime</i>not set: $lifetime</h3>"; 00253 $_lifetime = 1440; 00254 } 00255 } 00256 00257 return $_lifetime; 00258 } 00259 00262 function debug($debug = null) { 00263 static $_debug = false; 00264 static $set = false; 00265 00266 if (!is_null($debug)) { 00267 $_debug = (bool) $debug; 00268 00269 $conn = ADODB_Session::_conn(); 00270 if ($conn) { 00271 $conn->debug = $_debug; 00272 } 00273 $set = true; 00274 } elseif (!$set) { 00275 // backwards compatibility 00276 if (isset($GLOBALS['ADODB_SESS_DEBUG'])) { 00277 return $GLOBALS['ADODB_SESS_DEBUG']; 00278 } 00279 } 00280 00281 return $_debug; 00282 } 00283 00286 function expireNotify($expire_notify = null) { 00287 static $_expire_notify; 00288 static $set = false; 00289 00290 if (!is_null($expire_notify)) { 00291 $_expire_notify = $expire_notify; 00292 $set = true; 00293 } elseif (!$set) { 00294 // backwards compatibility 00295 if (isset($GLOBALS['ADODB_SESSION_EXPIRE_NOTIFY'])) { 00296 return $GLOBALS['ADODB_SESSION_EXPIRE_NOTIFY']; 00297 } 00298 } 00299 00300 return $_expire_notify; 00301 } 00302 00305 function table($table = null) { 00306 static $_table = 'sessions'; 00307 static $set = false; 00308 00309 if (!is_null($table)) { 00310 $_table = trim($table); 00311 $set = true; 00312 } elseif (!$set) { 00313 // backwards compatibility 00314 if (isset($GLOBALS['ADODB_SESSION_TBL'])) { 00315 return $GLOBALS['ADODB_SESSION_TBL']; 00316 } 00317 } 00318 00319 return $_table; 00320 } 00321 00324 function optimize($optimize = null) { 00325 static $_optimize = false; 00326 static $set = false; 00327 00328 if (!is_null($optimize)) { 00329 $_optimize = (bool) $optimize; 00330 $set = true; 00331 } elseif (!$set) { 00332 // backwards compatibility 00333 if (defined('ADODB_SESSION_OPTIMIZE')) { 00334 return true; 00335 } 00336 } 00337 00338 return $_optimize; 00339 } 00340 00343 function syncSeconds($sync_seconds = null) { 00344 static $_sync_seconds = 60; 00345 static $set = false; 00346 00347 if (!is_null($sync_seconds)) { 00348 $_sync_seconds = (int) $sync_seconds; 00349 $set = true; 00350 } elseif (!$set) { 00351 // backwards compatibility 00352 if (defined('ADODB_SESSION_SYNCH_SECS')) { 00353 return ADODB_SESSION_SYNCH_SECS; 00354 } 00355 } 00356 00357 return $_sync_seconds; 00358 } 00359 00362 function clob($clob = null) { 00363 static $_clob = false; 00364 static $set = false; 00365 00366 if (!is_null($clob)) { 00367 $_clob = strtolower(trim($clob)); 00368 $set = true; 00369 } elseif (!$set) { 00370 // backwards compatibility 00371 if (isset($GLOBALS['ADODB_SESSION_USE_LOBS'])) { 00372 return $GLOBALS['ADODB_SESSION_USE_LOBS']; 00373 } 00374 } 00375 00376 return $_clob; 00377 } 00378 00381 function dataFieldName($data_field_name = null) { 00382 static $_data_field_name = 'data'; 00383 00384 if (!is_null($data_field_name)) { 00385 $_data_field_name = trim($data_field_name); 00386 } 00387 00388 return $_data_field_name; 00389 } 00390 00393 function filter($filter = null) { 00394 static $_filter = array(); 00395 00396 if (!is_null($filter)) { 00397 if (!is_array($filter)) { 00398 $filter = array($filter); 00399 } 00400 $_filter = $filter; 00401 } 00402 00403 return $_filter; 00404 } 00405 00408 function encryptionKey($encryption_key = null) { 00409 static $_encryption_key = 'CRYPTED ADODB SESSIONS ROCK!'; 00410 00411 if (!is_null($encryption_key)) { 00412 $_encryption_key = $encryption_key; 00413 } 00414 00415 return $_encryption_key; 00416 } 00417 00419 // private methods 00421 00424 function &_conn($conn=null) { 00425 return $GLOBALS['ADODB_SESS_CONN']; 00426 } 00427 00430 function _crc($crc = null) { 00431 static $_crc = false; 00432 00433 if (!is_null($crc)) { 00434 $_crc = $crc; 00435 } 00436 00437 return $_crc; 00438 } 00439 00442 function _init() { 00443 session_module_name('user'); 00444 session_set_save_handler( 00445 array('ADODB_Session', 'open'), 00446 array('ADODB_Session', 'close'), 00447 array('ADODB_Session', 'read'), 00448 array('ADODB_Session', 'write'), 00449 array('ADODB_Session', 'destroy'), 00450 array('ADODB_Session', 'gc') 00451 ); 00452 } 00453 00454 00457 function _sessionKey() { 00458 // use this function to create the encryption key for crypted sessions 00459 // crypt the used key, ADODB_Session::encryptionKey() as key and session_id() as salt 00460 return crypt(ADODB_Session::encryptionKey(), session_id()); 00461 } 00462 00465 function _dumprs($rs) { 00466 $conn =& ADODB_Session::_conn(); 00467 $debug = ADODB_Session::debug(); 00468 00469 if (!$conn) { 00470 return; 00471 } 00472 00473 if (!$debug) { 00474 return; 00475 } 00476 00477 if (!$rs) { 00478 echo "<br />\$rs is null or false<br />\n"; 00479 return; 00480 } 00481 00482 //echo "<br />\nAffected_Rows=",$conn->Affected_Rows(),"<br />\n"; 00483 00484 if (!is_object($rs)) { 00485 return; 00486 } 00487 00488 require_once ADODB_SESSION.'/../tohtml.inc.php'; 00489 rs2html($rs); 00490 } 00491 00493 // public methods 00495 00496 function config($driver, $host, $user, $password, $database=false,$options=false) 00497 { 00498 ADODB_Session::driver($driver); 00499 ADODB_Session::host($host); 00500 ADODB_Session::user($user); 00501 ADODB_Session::password($password); 00502 ADODB_Session::database($database); 00503 00504 if (isset($options['table'])) ADODB_Session::table($options['table']); 00505 if (isset($options['clob'])) ADODB_Session::table($options['clob']); 00506 if (isset($options['field'])) ADODB_Session::dataFieldName($options['field']); 00507 } 00508 00514 function open($save_path, $session_name, $persist = null) { 00515 $conn =& ADODB_Session::_conn(); 00516 00517 if ($conn) { 00518 return true; 00519 } 00520 00521 $database = ADODB_Session::database(); 00522 $debug = ADODB_Session::debug(); 00523 $driver = ADODB_Session::driver(); 00524 $host = ADODB_Session::host(); 00525 $password = ADODB_Session::password(); 00526 $user = ADODB_Session::user(); 00527 00528 if (!is_null($persist)) { 00529 ADODB_Session::persist($persist); 00530 } else { 00531 $persist = ADODB_Session::persist(); 00532 } 00533 00534 # these can all be defaulted to in php.ini 00535 # assert('$database'); 00536 # assert('$driver'); 00537 # assert('$host'); 00538 00539 $conn =& ADONewConnection($driver); 00540 00541 if ($debug) { 00542 $conn->debug = true; 00543 // ADOConnection::outp( " driver=$driver user=$user pwd=$password db=$database "); 00544 } 00545 00546 if ($persist) { 00547 switch($persist) { 00548 default: 00549 case 'P': $ok = $conn->PConnect($host, $user, $password, $database); break; 00550 case 'C': $ok = $conn->Connect($host, $user, $password, $database); break; 00551 case 'N': $ok = $conn->NConnect($host, $user, $password, $database); break; 00552 } 00553 } else { 00554 $ok = $conn->Connect($host, $user, $password, $database); 00555 } 00556 00557 if ($ok) $GLOBALS['ADODB_SESS_CONN'] =& $conn; 00558 else 00559 ADOConnection::outp('<p>Session: connection failed</p>', false); 00560 00561 00562 return $ok; 00563 } 00564 00568 function close() { 00569 /* 00570 $conn =& ADODB_Session::_conn(); 00571 if ($conn) $conn->Close(); 00572 */ 00573 return true; 00574 } 00575 00576 /* 00577 Slurp in the session variables and return the serialized string 00578 */ 00579 function read($key) { 00580 $conn =& ADODB_Session::_conn(); 00581 $data = ADODB_Session::dataFieldName(); 00582 $filter = ADODB_Session::filter(); 00583 $table = ADODB_Session::table(); 00584 00585 if (!$conn) { 00586 return ''; 00587 } 00588 00589 assert('$table'); 00590 00591 $qkey = $conn->quote($key); 00592 $binary = $conn->dataProvider === 'mysql' ? '' : ''; 00593 00594 $sql = "SELECT $data FROM $table WHERE sesskey = $binary $qkey AND expiry >= " . time(); 00595 /* Lock code does not work as it needs to hold transaction within whole page, and we don't know if 00596 developer has commited elsewhere... :( 00597 */ 00598 #if (ADODB_Session::Lock()) 00599 # $rs =& $conn->RowLock($table, "$binary sesskey = $qkey AND expiry >= " . time(), $data); 00600 #else 00601 00602 $rs =& $conn->Execute($sql); 00603 //ADODB_Session::_dumprs($rs); 00604 if ($rs) { 00605 if ($rs->EOF) { 00606 $v = ''; 00607 } else { 00608 $v = reset($rs->fields); 00609 $filter = array_reverse($filter); 00610 foreach ($filter as $f) { 00611 if (is_object($f)) { 00612 $v = $f->read($v, ADODB_Session::_sessionKey()); 00613 } 00614 } 00615 $v = rawurldecode($v); 00616 } 00617 00618 $rs->Close(); 00619 00620 ADODB_Session::_crc(strlen($v) . crc32($v)); 00621 return $v; 00622 } 00623 00624 return ''; 00625 } 00626 00632 function write($key, $val) { 00633 global $ADODB_SESSION_READONLY; 00634 00635 if (!empty($ADODB_SESSION_READONLY)) return; 00636 00637 $clob = ADODB_Session::clob(); 00638 $conn =& ADODB_Session::_conn(); 00639 $crc = ADODB_Session::_crc(); 00640 $data = ADODB_Session::dataFieldName(); 00641 $debug = ADODB_Session::debug(); 00642 $driver = ADODB_Session::driver(); 00643 $expire_notify = ADODB_Session::expireNotify(); 00644 $filter = ADODB_Session::filter(); 00645 $lifetime = ADODB_Session::lifetime(); 00646 $table = ADODB_Session::table(); 00647 00648 if (!$conn) { 00649 return false; 00650 } 00651 $qkey = $conn->qstr($key); 00652 00653 assert('$table'); 00654 00655 $expiry = time() + $lifetime; 00656 00657 $binary = $conn->dataProvider === 'mysql' ? '' : ''; 00658 00659 // crc32 optimization since adodb 2.1 00660 // now we only update expiry date, thx to sebastian thom in adodb 2.32 00661 if ($crc !== false && $crc == (strlen($val) . crc32($val))) { 00662 if ($debug) { 00663 echo '<p>Session: Only updating date - crc32 not changed</p>'; 00664 } 00665 00666 $expirevar = ''; 00667 if ($expire_notify) { 00668 $var = reset($expire_notify); 00669 global $$var; 00670 if (isset($$var)) { 00671 $expirevar = $$var; 00672 } 00673 } 00674 00675 00676 $sql = "UPDATE $table SET expiry = ".$conn->Param('0').",expireref=".$conn->Param('1')." WHERE $binary sesskey = ".$conn->Param('2')." AND expiry >= ".$conn->Param('3'); 00677 $rs =& $conn->Execute($sql,array($expiry,$expirevar,$key,time())); 00678 return true; 00679 } 00680 $val = rawurlencode($val); 00681 foreach ($filter as $f) { 00682 if (is_object($f)) { 00683 $val = $f->write($val, ADODB_Session::_sessionKey()); 00684 } 00685 } 00686 00687 $arr = array('sesskey' => $key, 'expiry' => $expiry, $data => $val, 'expireref' => ''); 00688 if ($expire_notify) { 00689 $var = reset($expire_notify); 00690 global $$var; 00691 if (isset($$var)) { 00692 $arr['expireref'] = $$var; 00693 } 00694 } 00695 00696 if (!$clob) { // no lobs, simply use replace() 00697 $arr[$data] = $conn->qstr($val); 00698 $rs = $conn->Replace($table, $arr, 'sesskey', $autoQuote = true); 00699 00700 } else { 00701 // what value shall we insert/update for lob row? 00702 switch ($driver) { 00703 // empty_clob or empty_lob for oracle dbs 00704 case 'oracle': 00705 case 'oci8': 00706 case 'oci8po': 00707 case 'oci805': 00708 $lob_value = sprintf('empty_%s()', strtolower($clob)); 00709 break; 00710 00711 // null for all other 00712 default: 00713 $lob_value = 'null'; 00714 break; 00715 } 00716 00717 $expiryref = $conn->qstr($arr['expireref']); 00718 // do we insert or update? => as for sesskey 00719 $rs =& $conn->Execute("SELECT COUNT(*) AS cnt FROM $table WHERE $binary sesskey = $qkey"); 00720 if ($rs && reset($rs->fields) > 0) { 00721 $sql = "UPDATE $table SET expiry = $expiry, $data = $lob_value, expireref=$expiryref WHERE sesskey = $qkey"; 00722 } else { 00723 $sql = "INSERT INTO $table (expiry, $data, sesskey,expireref) VALUES ($expiry, $lob_value, $qkey,$expiryref)"; 00724 } 00725 if ($rs) { 00726 $rs->Close(); 00727 } 00728 00729 $err = ''; 00730 $rs1 =& $conn->Execute($sql); 00731 if (!$rs1) { 00732 $err = $conn->ErrorMsg()."\n"; 00733 } 00734 $rs2 =& $conn->UpdateBlob($table, $data, $val, " sesskey=$qkey", strtoupper($clob)); 00735 00736 if (!$rs2) { 00737 $err .= $conn->ErrorMsg()."\n"; 00738 } 00739 $rs = ($rs && $rs2) ? true : false; 00740 if ($rs1) { 00741 $rs1->Close(); 00742 } 00743 if (is_object($rs2)) { 00744 $rs2->Close(); 00745 } 00746 } 00747 00748 if (!$rs) { 00749 ADOConnection::outp('<p>Session Replace: ' . $conn->ErrorMsg() . '</p>', false); 00750 return false; 00751 } else { 00752 // bug in access driver (could be odbc?) means that info is not committed 00753 // properly unless select statement executed in Win2000 00754 if ($conn->databaseType == 'access') { 00755 $sql = "SELECT sesskey FROM $table WHERE $binary sesskey = $qkey"; 00756 $rs =& $conn->Execute($sql); 00757 ADODB_Session::_dumprs($rs); 00758 if ($rs) { 00759 $rs->Close(); 00760 } 00761 } 00762 }/* 00763 if (ADODB_Session::Lock()) { 00764 $conn->CommitTrans(); 00765 }*/ 00766 return $rs ? true : false; 00767 } 00768 00771 function destroy($key) { 00772 $conn =& ADODB_Session::_conn(); 00773 $table = ADODB_Session::table(); 00774 $expire_notify = ADODB_Session::expireNotify(); 00775 00776 if (!$conn) { 00777 return false; 00778 } 00779 00780 assert('$table'); 00781 00782 $qkey = $conn->quote($key); 00783 $binary = $conn->dataProvider === 'mysql' ? '' : ''; 00784 00785 if ($expire_notify) { 00786 reset($expire_notify); 00787 $fn = next($expire_notify); 00788 $savem = $conn->SetFetchMode(ADODB_FETCH_NUM); 00789 $sql = "SELECT expireref, sesskey FROM $table WHERE $binary sesskey = $qkey"; 00790 $rs =& $conn->Execute($sql); 00791 ADODB_Session::_dumprs($rs); 00792 $conn->SetFetchMode($savem); 00793 if (!$rs) { 00794 return false; 00795 } 00796 if (!$rs->EOF) { 00797 $ref = $rs->fields[0]; 00798 $key = $rs->fields[1]; 00799 //assert('$ref'); 00800 //assert('$key'); 00801 $fn($ref, $key); 00802 } 00803 $rs->Close(); 00804 } 00805 00806 $sql = "DELETE FROM $table WHERE $binary sesskey = $qkey"; 00807 $rs =& $conn->Execute($sql); 00808 ADODB_Session::_dumprs($rs); 00809 if ($rs) { 00810 $rs->Close(); 00811 } 00812 00813 return $rs ? true : false; 00814 } 00815 00818 function gc($maxlifetime) { 00819 $conn =& ADODB_Session::_conn(); 00820 $debug = ADODB_Session::debug(); 00821 $expire_notify = ADODB_Session::expireNotify(); 00822 $optimize = ADODB_Session::optimize(); 00823 $sync_seconds = ADODB_Session::syncSeconds(); 00824 $table = ADODB_Session::table(); 00825 00826 if (!$conn) { 00827 return false; 00828 } 00829 00830 assert('$table'); 00831 00832 $time = time(); 00833 00834 $binary = $conn->dataProvider === 'mysql' ? '' : ''; 00835 00836 if ($expire_notify) { 00837 reset($expire_notify); 00838 $fn = next($expire_notify); 00839 $savem = $conn->SetFetchMode(ADODB_FETCH_NUM); 00840 $sql = "SELECT expireref, sesskey FROM $table WHERE expiry < $time"; 00841 $rs =& $conn->Execute($sql); 00842 ADODB_Session::_dumprs($rs); 00843 $conn->SetFetchMode($savem); 00844 if ($rs) { 00845 $conn->BeginTrans(); 00846 $keys = array(); 00847 while (!$rs->EOF) { 00848 $ref = $rs->fields[0]; 00849 $key = $rs->fields[1]; 00850 $fn($ref, $key); 00851 $del = $conn->Execute("DELETE FROM $table WHERE sesskey='$key'"); 00852 $rs->MoveNext(); 00853 } 00854 $rs->Close(); 00855 00856 $conn->CommitTrans(); 00857 } 00858 } else { 00859 00860 if (1) { 00861 $sql = "SELECT sesskey FROM $table WHERE expiry < $time"; 00862 $arr =& $conn->GetAll($sql); 00863 foreach ($arr as $row) { 00864 $sql2 = "DELETE FROM $table WHERE sesskey='$row[0]'"; 00865 $conn->Execute($sql2); 00866 } 00867 } else { 00868 $sql = "DELETE FROM $table WHERE expiry < $time"; 00869 $rs =& $conn->Execute($sql); 00870 ADODB_Session::_dumprs($rs); 00871 if ($rs) $rs->Close(); 00872 } 00873 if ($debug) { 00874 ADOConnection::outp("<p><b>Garbage Collection</b>: $sql</p>"); 00875 } 00876 } 00877 00878 // suggested by Cameron, "GaM3R" <gamr@outworld.cx> 00879 if ($optimize) { 00880 $driver = ADODB_Session::driver(); 00881 00882 if (preg_match('/mysql/i', $driver)) { 00883 $sql = "OPTIMIZE TABLE $table"; 00884 } 00885 if (preg_match('/postgres/i', $driver)) { 00886 $sql = "VACUUM $table"; 00887 } 00888 if (!empty($sql)) { 00889 $conn->Execute($sql); 00890 } 00891 } 00892 00893 if ($sync_seconds) { 00894 $sql = 'SELECT '; 00895 if ($conn->dataProvider === 'oci8') { 00896 $sql .= "TO_CHAR({$conn->sysTimeStamp}, 'RRRR-MM-DD HH24:MI:SS')"; 00897 } else { 00898 $sql .= $conn->sysTimeStamp; 00899 } 00900 $sql .= " FROM $table"; 00901 00902 $rs =& $conn->SelectLimit($sql, 1); 00903 if ($rs && !$rs->EOF) { 00904 $dbts = reset($rs->fields); 00905 $rs->Close(); 00906 $dbt = $conn->UnixTimeStamp($dbts); 00907 $t = time(); 00908 00909 if (abs($dbt - $t) >= $sync_seconds) { 00910 $msg = __FILE__ . 00911 ": Server time for webserver {$_SERVER['HTTP_HOST']} not in synch with database: " . 00912 " database=$dbt ($dbts), webserver=$t (diff=". (abs($dbt - $t) / 60) . ' minutes)'; 00913 error_log($msg); 00914 if ($debug) { 00915 ADOConnection::outp("<p>$msg</p>"); 00916 } 00917 } 00918 } 00919 } 00920 00921 return true; 00922 } 00923 } 00924 00925 ADODB_Session::_init(); 00926 if (empty($ADODB_SESSION_READONLY)) 00927 register_shutdown_function('session_write_close'); 00928 00929 // for backwards compatability only 00930 function adodb_sess_open($save_path, $session_name, $persist = true) { 00931 return ADODB_Session::open($save_path, $session_name, $persist); 00932 } 00933 00934 // for backwards compatability only 00935 function adodb_sess_gc($t) 00936 { 00937 return ADODB_Session::gc($t); 00938 } 00939 00940 ?>