Adodb Dokumentation  V5.14 8 Sept 2011
session/adodb-session2.php
00001 <?php
00002 
00003 
00004 /*
00005 V5.14 8 Sept 2011  (c) 2000-2011 John Lim (jlim#natsoft.com). 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 
00015 /*
00016 
00017 CREATE Table SCripts
00018 
00019 Oracle
00020 ======
00021 
00022 CREATE TABLE SESSIONS2
00023 (
00024   SESSKEY    VARCHAR2(48 BYTE)                  NOT NULL,
00025   EXPIRY     DATE                               NOT NULL,
00026   EXPIREREF  VARCHAR2(200 BYTE),
00027   CREATED    DATE                               NOT NULL,
00028   MODIFIED   DATE                               NOT NULL,
00029   SESSDATA   CLOB,
00030   PRIMARY KEY(SESSKEY)
00031 );
00032 
00033 
00034 CREATE INDEX SESS2_EXPIRY ON SESSIONS2(EXPIRY);
00035 CREATE UNIQUE INDEX SESS2_PK ON SESSIONS2(SESSKEY);
00036 CREATE INDEX SESS2_EXP_REF ON SESSIONS2(EXPIREREF);
00037 
00038 
00039  
00040  MySQL
00041  =====
00042  
00043 CREATE TABLE sessions2(
00044         sesskey VARCHAR( 64 ) NOT NULL DEFAULT '',
00045         expiry TIMESTAMP NOT NULL ,
00046         expireref VARCHAR( 250 ) DEFAULT '',
00047         created TIMESTAMP NOT NULL ,
00048         modified TIMESTAMP NOT NULL ,
00049         sessdata LONGTEXT DEFAULT '',
00050         PRIMARY KEY ( sesskey ) ,
00051         INDEX sess2_expiry( expiry ),
00052         INDEX sess2_expireref( expireref )
00053 )
00054 
00055 
00056 */
00057 
00058 if (!defined('_ADODB_LAYER')) {
00059         require realpath(dirname(__FILE__) . '/../adodb.inc.php');
00060 }
00061 
00062 if (defined('ADODB_SESSION')) return 1;
00063 
00064 define('ADODB_SESSION', dirname(__FILE__));
00065 define('ADODB_SESSION2', ADODB_SESSION);
00066 
00067 /* 
00068         Unserialize session data manually. See http://phplens.com/lens/lensforum/msgs.php?id=9821 
00069         
00070         From Kerr Schere, to unserialize session data stored via ADOdb. 
00071         1. Pull the session data from the db and loop through it. 
00072         2. Inside the loop, you will need to urldecode the data column. 
00073         3. After urldecode, run the serialized string through this function:
00074 
00075 */
00076 function adodb_unserialize( $serialized_string ) 
00077 {
00078         $variables = array( );
00079         $a = preg_split( "/(\w+)\|/", $serialized_string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
00080         for( $i = 0; $i < count( $a ); $i = $i+2 ) {
00081                 $variables[$a[$i]] = unserialize( $a[$i+1] );
00082         }
00083         return( $variables );
00084 }
00085 
00086 /*
00087         Thanks Joe Li. See http://phplens.com/lens/lensforum/msgs.php?id=11487&x=1
00088         Since adodb 4.61.
00089 */
00090 function adodb_session_regenerate_id() 
00091 {
00092         $conn = ADODB_Session::_conn();
00093         if (!$conn) return false;
00094 
00095         $old_id = session_id();
00096         if (function_exists('session_regenerate_id')) {
00097                 session_regenerate_id();
00098         } else {
00099                 session_id(md5(uniqid(rand(), true)));
00100                 $ck = session_get_cookie_params();
00101                 setcookie(session_name(), session_id(), false, $ck['path'], $ck['domain'], $ck['secure']);
00102                 //@session_start();
00103         }
00104         $new_id = session_id();
00105         $ok = $conn->Execute('UPDATE '. ADODB_Session::table(). ' SET sesskey='. $conn->qstr($new_id). ' WHERE sesskey='.$conn->qstr($old_id));
00106         
00107         /* it is possible that the update statement fails due to a collision */
00108         if (!$ok) {
00109                 session_id($old_id);
00110                 if (empty($ck)) $ck = session_get_cookie_params();
00111                 setcookie(session_name(), session_id(), false, $ck['path'], $ck['domain'], $ck['secure']);
00112                 return false;
00113         }
00114         
00115         return true;
00116 }
00117 
00118 /*
00119     Generate database table for session data
00120     @see http://phplens.com/lens/lensforum/msgs.php?id=12280
00121     @return 0 if failure, 1 if errors, 2 if successful.
00122         @author Markus Staab http://www.public-4u.de
00123 */
00124 function adodb_session_create_table($schemaFile=null,$conn = null)
00125 {
00126     // set default values
00127     if ($schemaFile===null) $schemaFile = ADODB_SESSION . '/session_schema2.xml';
00128     if ($conn===null) $conn = ADODB_Session::_conn();
00129 
00130         if (!$conn) return 0;
00131 
00132     $schema = new adoSchema($conn);
00133     $schema->ParseSchema($schemaFile);
00134     return $schema->ExecuteSchema();
00135 }
00136 
00140 class ADODB_Session {
00142         // getter/setter methods
00144         
00145         /*
00146         
00147         function Lock($lock=null)
00148         {
00149         static $_lock = false;
00150         
00151                 if (!is_null($lock)) $_lock = $lock;
00152                 return $lock;
00153         }
00154         */
00157         static function driver($driver = null) 
00158         {
00159                 static $_driver = 'mysql';
00160                 static $set = false;
00161 
00162                 if (!is_null($driver)) {
00163                         $_driver = trim($driver);
00164                         $set = true;
00165                 } elseif (!$set) {
00166                         // backwards compatibility
00167                         if (isset($GLOBALS['ADODB_SESSION_DRIVER'])) {
00168                                 return $GLOBALS['ADODB_SESSION_DRIVER'];
00169                         }
00170                 }
00171 
00172                 return $_driver;
00173         }
00174 
00177         static function host($host = null) {
00178                 static $_host = 'localhost';
00179                 static $set = false;
00180 
00181                 if (!is_null($host)) {
00182                         $_host = trim($host);
00183                         $set = true;
00184                 } elseif (!$set) {
00185                         // backwards compatibility
00186                         if (isset($GLOBALS['ADODB_SESSION_CONNECT'])) {
00187                                 return $GLOBALS['ADODB_SESSION_CONNECT'];
00188                         }
00189                 }
00190 
00191                 return $_host;
00192         }
00193 
00196         static function user($user = null) 
00197         {
00198                 static $_user = 'root';
00199                 static $set = false;
00200 
00201                 if (!is_null($user)) {
00202                         $_user = trim($user);
00203                         $set = true;
00204                 } elseif (!$set) {
00205                         // backwards compatibility
00206                         if (isset($GLOBALS['ADODB_SESSION_USER'])) {
00207                                 return $GLOBALS['ADODB_SESSION_USER'];
00208                         }
00209                 }
00210 
00211                 return $_user;
00212         }
00213 
00216         static function password($password = null) 
00217         {
00218                 static $_password = '';
00219                 static $set = false;
00220 
00221                 if (!is_null($password)) {
00222                         $_password = $password;
00223                         $set = true;
00224                 } elseif (!$set) {
00225                         // backwards compatibility
00226                         if (isset($GLOBALS['ADODB_SESSION_PWD'])) {
00227                                 return $GLOBALS['ADODB_SESSION_PWD'];
00228                         }
00229                 }
00230 
00231                 return $_password;
00232         }
00233 
00236         static function database($database = null) 
00237         {
00238                 static $_database = '';
00239                 static $set = false;
00240                 
00241                 if (!is_null($database)) {
00242                         $_database = trim($database);
00243                         $set = true;
00244                 } elseif (!$set) {
00245                         // backwards compatibility
00246                         if (isset($GLOBALS['ADODB_SESSION_DB'])) {
00247                                 return $GLOBALS['ADODB_SESSION_DB'];
00248                         }
00249                 }
00250                 return $_database;
00251         }
00252 
00255         static function persist($persist = null) 
00256         {
00257                 static $_persist = true;
00258 
00259                 if (!is_null($persist)) {
00260                         $_persist = trim($persist);
00261                 }
00262 
00263                 return $_persist;
00264         }
00265 
00268         static function lifetime($lifetime = null) 
00269         {
00270                 static $_lifetime;
00271                 static $set = false;
00272 
00273                 if (!is_null($lifetime)) {
00274                         $_lifetime = (int) $lifetime;
00275                         $set = true;
00276                 } elseif (!$set) {
00277                         // backwards compatibility
00278                         if (isset($GLOBALS['ADODB_SESS_LIFE'])) {
00279                                 return $GLOBALS['ADODB_SESS_LIFE'];
00280                         }
00281                 }
00282                 if (!$_lifetime) {
00283                         $_lifetime = ini_get('session.gc_maxlifetime');
00284                         if ($_lifetime <= 1) {
00285                                 // bug in PHP 4.0.3 pl 1  -- how about other versions?
00286                                 //print "<h3>Session Error: PHP.INI setting <i>session.gc_maxlifetime</i>not set: $lifetime</h3>";
00287                                 $_lifetime = 1440;
00288                         }
00289                 }
00290 
00291                 return $_lifetime;
00292         }
00293 
00296         static function debug($debug = null) 
00297         {
00298                 static $_debug = false;
00299                 static $set = false;
00300 
00301                 if (!is_null($debug)) {
00302                         $_debug = (bool) $debug;
00303 
00304                         $conn = ADODB_Session::_conn();
00305                         if ($conn) {
00306                                 #$conn->debug = $_debug;
00307                         }
00308                         $set = true;
00309                 } elseif (!$set) {
00310                         // backwards compatibility
00311                         if (isset($GLOBALS['ADODB_SESS_DEBUG'])) {
00312                                 return $GLOBALS['ADODB_SESS_DEBUG'];
00313                         }
00314                 }
00315 
00316                 return $_debug;
00317         }
00318 
00321         static function expireNotify($expire_notify = null) 
00322         {
00323                 static $_expire_notify;
00324                 static $set = false;
00325 
00326                 if (!is_null($expire_notify)) {
00327                         $_expire_notify = $expire_notify;
00328                         $set = true;
00329                 } elseif (!$set) {
00330                         // backwards compatibility
00331                         if (isset($GLOBALS['ADODB_SESSION_EXPIRE_NOTIFY'])) {
00332                                 return $GLOBALS['ADODB_SESSION_EXPIRE_NOTIFY'];
00333                         }
00334                 }
00335 
00336                 return $_expire_notify;
00337         }
00338 
00341         static function table($table = null) 
00342         {
00343                 static $_table = 'sessions2';
00344                 static $set = false;
00345 
00346                 if (!is_null($table)) {
00347                         $_table = trim($table);
00348                         $set = true;
00349                 } elseif (!$set) {
00350                         // backwards compatibility
00351                         if (isset($GLOBALS['ADODB_SESSION_TBL'])) {
00352                                 return $GLOBALS['ADODB_SESSION_TBL'];
00353                         }
00354                 }
00355 
00356                 return $_table;
00357         }
00358 
00361         static function optimize($optimize = null) 
00362         {
00363                 static $_optimize = false;
00364                 static $set = false;
00365 
00366                 if (!is_null($optimize)) {
00367                         $_optimize = (bool) $optimize;
00368                         $set = true;
00369                 } elseif (!$set) {
00370                         // backwards compatibility
00371                         if (defined('ADODB_SESSION_OPTIMIZE')) {
00372                                 return true;
00373                         }
00374                 }
00375 
00376                 return $_optimize;
00377         }
00378 
00381         static function syncSeconds($sync_seconds = null) {
00382                 //echo ("<p>WARNING: ADODB_SESSION::syncSeconds is longer used, please remove this function for your code</p>");
00383                 
00384                 return 0;
00385         }
00386 
00389         static function clob($clob = null) {
00390                 static $_clob = false;
00391                 static $set = false;
00392 
00393                 if (!is_null($clob)) {
00394                         $_clob = strtolower(trim($clob));
00395                         $set = true;
00396                 } elseif (!$set) {
00397                         // backwards compatibility
00398                         if (isset($GLOBALS['ADODB_SESSION_USE_LOBS'])) {
00399                                 return $GLOBALS['ADODB_SESSION_USE_LOBS'];
00400                         }
00401                 }
00402 
00403                 return $_clob;
00404         }
00405 
00408         static function dataFieldName($data_field_name = null) {
00409                 //echo ("<p>WARNING: ADODB_SESSION::dataFieldName() is longer used, please remove this function for your code</p>");
00410                 return '';
00411         }
00412 
00415         static function filter($filter = null) {
00416                 static $_filter = array();
00417 
00418                 if (!is_null($filter)) {
00419                         if (!is_array($filter)) {
00420                                 $filter = array($filter);
00421                         }
00422                         $_filter = $filter;
00423                 }
00424 
00425                 return $_filter;
00426         }
00427 
00430         static function encryptionKey($encryption_key = null) {
00431                 static $_encryption_key = 'CRYPTED ADODB SESSIONS ROCK!';
00432 
00433                 if (!is_null($encryption_key)) {
00434                         $_encryption_key = $encryption_key;
00435                 }
00436 
00437                 return $_encryption_key;
00438         }
00439 
00441         // private methods
00443 
00446         static function _conn($conn=null) {
00447                 return isset($GLOBALS['ADODB_SESS_CONN']) ? $GLOBALS['ADODB_SESS_CONN'] : false;
00448         }
00449 
00452         static function _crc($crc = null) {
00453                 static $_crc = false;
00454 
00455                 if (!is_null($crc)) {
00456                         $_crc = $crc;
00457                 }
00458 
00459                 return $_crc;
00460         }
00461 
00464         static function _init() {
00465                 session_module_name('user');
00466                 session_set_save_handler(
00467                         array('ADODB_Session', 'open'),
00468                         array('ADODB_Session', 'close'),
00469                         array('ADODB_Session', 'read'),
00470                         array('ADODB_Session', 'write'),
00471                         array('ADODB_Session', 'destroy'),
00472                         array('ADODB_Session', 'gc')
00473                 );
00474         }
00475 
00476 
00479         static function _sessionKey() {
00480                 // use this function to create the encryption key for crypted sessions
00481                 // crypt the used key, ADODB_Session::encryptionKey() as key and session_id() as salt
00482                 return crypt(ADODB_Session::encryptionKey(), session_id());
00483         }
00484 
00487         static function _dumprs(&$rs) {
00488                 $conn   = ADODB_Session::_conn();
00489                 $debug  = ADODB_Session::debug();
00490 
00491                 if (!$conn) {
00492                         return;
00493                 }
00494 
00495                 if (!$debug) {
00496                         return;
00497                 }
00498 
00499                 if (!$rs) {
00500                         echo "<br />\$rs is null or false<br />\n";
00501                         return;
00502                 }
00503 
00504                 //echo "<br />\nAffected_Rows=",$conn->Affected_Rows(),"<br />\n";
00505 
00506                 if (!is_object($rs)) {
00507                         return;
00508                 }
00509                 $rs = $conn->_rs2rs($rs);
00510                 
00511                 require_once ADODB_SESSION.'/../tohtml.inc.php';
00512                 rs2html($rs);
00513                 $rs->MoveFirst();
00514         }
00515 
00517         // public methods
00519         
00520         static function config($driver, $host, $user, $password, $database=false,$options=false)
00521         {
00522                 ADODB_Session::driver($driver);
00523                 ADODB_Session::host($host);
00524                 ADODB_Session::user($user);
00525                 ADODB_Session::password($password);
00526                 ADODB_Session::database($database);
00527                 
00528                 if ($driver == 'oci8' || $driver == 'oci8po') $options['lob'] = 'CLOB';
00529                 
00530                 if (isset($options['table'])) ADODB_Session::table($options['table']);
00531                 if (isset($options['lob'])) ADODB_Session::clob($options['lob']);
00532                 if (isset($options['debug'])) ADODB_Session::debug($options['debug']);
00533         }
00534 
00540         static function open($save_path, $session_name, $persist = null) 
00541         {
00542                 $conn = ADODB_Session::_conn();
00543 
00544                 if ($conn) {
00545                         return true;
00546                 }
00547 
00548                 $database       = ADODB_Session::database();
00549                 $debug          = ADODB_Session::debug();
00550                 $driver         = ADODB_Session::driver();
00551                 $host           = ADODB_Session::host();
00552                 $password       = ADODB_Session::password();
00553                 $user           = ADODB_Session::user();
00554 
00555                 if (!is_null($persist)) {
00556                         ADODB_Session::persist($persist);
00557                 } else {
00558                         $persist = ADODB_Session::persist();
00559                 }
00560 
00561 # these can all be defaulted to in php.ini
00562 #               assert('$database');
00563 #               assert('$driver');
00564 #               assert('$host');
00565 
00566                 $conn = ADONewConnection($driver);
00567 
00568                 if ($debug) {
00569                         $conn->debug = true;            
00570                         ADOConnection::outp( " driver=$driver user=$user db=$database ");
00571                 }
00572                 
00573                 if (empty($conn->_connectionID)) { // not dsn
00574                         if ($persist) {
00575                                 switch($persist) {
00576                                 default:
00577                                 case 'P': $ok = $conn->PConnect($host, $user, $password, $database); break;
00578                                 case 'C': $ok = $conn->Connect($host, $user, $password, $database); break;
00579                                 case 'N': $ok = $conn->NConnect($host, $user, $password, $database); break;
00580                                 }
00581                         } else {
00582                                 $ok = $conn->Connect($host, $user, $password, $database);
00583                         }
00584                 }
00585 
00586                 if ($ok) $GLOBALS['ADODB_SESS_CONN'] = $conn;
00587                 else
00588                         ADOConnection::outp('<p>Session: connection failed</p>', false);
00589                 
00590 
00591                 return $ok;
00592         }
00593 
00597         static function close() 
00598         {
00599 /*
00600                 $conn = ADODB_Session::_conn();
00601                 if ($conn) $conn->Close();
00602 */
00603                 return true;
00604         }
00605 
00606         /*
00607                 Slurp in the session variables and return the serialized string
00608         */
00609         static function read($key) 
00610         {
00611                 $conn   = ADODB_Session::_conn();
00612                 $filter = ADODB_Session::filter();
00613                 $table  = ADODB_Session::table();
00614 
00615                 if (!$conn) {
00616                         return '';
00617                 }
00618 
00619                 //assert('$table');
00620 
00621                 $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
00622         
00623                 $sql = "SELECT sessdata FROM $table WHERE sesskey = $binary ".$conn->Param(0)." AND expiry >= " . $conn->sysTimeStamp;
00624                 /* Lock code does not work as it needs to hold transaction within whole page, and we don't know if 
00625                   developer has commited elsewhere... :(
00626                  */
00627                 #if (ADODB_Session::Lock())
00628                 #       $rs = $conn->RowLock($table, "$binary sesskey = $qkey AND expiry >= " . time(), sessdata);
00629                 #else
00630                         $rs = $conn->Execute($sql, array($key));
00631                 //ADODB_Session::_dumprs($rs);
00632                 if ($rs) {
00633                         if ($rs->EOF) {
00634                                 $v = '';
00635                         } else {
00636                                 $v = reset($rs->fields);
00637                                 $filter = array_reverse($filter);
00638                                 foreach ($filter as $f) {
00639                                         if (is_object($f)) {
00640                                                 $v = $f->read($v, ADODB_Session::_sessionKey());
00641                                         }
00642                                 }
00643                                 $v = rawurldecode($v);
00644                         }
00645 
00646                         $rs->Close();
00647 
00648                         ADODB_Session::_crc(strlen($v) . crc32($v));
00649                         return $v;
00650                 }
00651 
00652                 return '';
00653         }
00654 
00660         static function write($key, $oval) 
00661         {
00662         global $ADODB_SESSION_READONLY;
00663         
00664                 if (!empty($ADODB_SESSION_READONLY)) return;
00665                 
00666                 $clob                   = ADODB_Session::clob();
00667                 $conn                   = ADODB_Session::_conn();
00668                 $crc                    = ADODB_Session::_crc();
00669                 $debug                  = ADODB_Session::debug();
00670                 $driver                 = ADODB_Session::driver();
00671                 $expire_notify  = ADODB_Session::expireNotify();
00672                 $filter                 = ADODB_Session::filter();
00673                 $lifetime               = ADODB_Session::lifetime();
00674                 $table                  = ADODB_Session::table();
00675         
00676                 if (!$conn) {
00677                         return false;
00678                 }
00679                 if ($debug) $conn->debug = 1;
00680                 $sysTimeStamp = $conn->sysTimeStamp;
00681                 
00682                 //assert('$table');
00683 
00684                 $expiry = $conn->OffsetDate($lifetime/(24*3600),$sysTimeStamp);
00685 
00686                 $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
00687 
00688                 // crc32 optimization since adodb 2.1
00689                 // now we only update expiry date, thx to sebastian thom in adodb 2.32
00690                 if ($crc !== false && $crc == (strlen($oval) . crc32($oval))) {
00691                         if ($debug) {
00692                                 echo '<p>Session: Only updating date - crc32 not changed</p>';
00693                         }
00694                         
00695                         $expirevar = '';
00696                         if ($expire_notify) {
00697                                 $var = reset($expire_notify);
00698                                 global $$var;
00699                                 if (isset($$var)) {
00700                                         $expirevar = $$var;
00701                                 }
00702                         }
00703                         
00704                         
00705                         $sql = "UPDATE $table SET expiry = $expiry ,expireref=".$conn->Param('0').", modified = $sysTimeStamp WHERE $binary sesskey = ".$conn->Param('1')." AND expiry >= $sysTimeStamp";
00706                         $rs = $conn->Execute($sql,array($expirevar,$key));
00707                         return true;
00708                 }
00709                 $val = rawurlencode($oval);
00710                 foreach ($filter as $f) {
00711                         if (is_object($f)) {
00712                                 $val = $f->write($val, ADODB_Session::_sessionKey());
00713                         }
00714                 }
00715 
00716                 $expireref = '';
00717                 if ($expire_notify) {
00718                         $var = reset($expire_notify);
00719                         global $$var;
00720                         if (isset($$var)) {
00721                                 $expireref = $$var;
00722                         }
00723                 } 
00724 
00725                 if (!$clob) {   // no lobs, simply use replace()
00726                         $rs = $conn->Execute("SELECT COUNT(*) AS cnt FROM $table WHERE $binary sesskey = ".$conn->Param(0),array($key));
00727                         if ($rs) $rs->Close();
00728                                         
00729                         if ($rs && reset($rs->fields) > 0) {
00730                                 $sql = "UPDATE $table SET expiry=$expiry, sessdata=".$conn->Param(0).", expireref= ".$conn->Param(1).",modified=$sysTimeStamp WHERE sesskey = ".$conn->Param(2);
00731                                 
00732                         } else {
00733                                 $sql = "INSERT INTO $table (expiry, sessdata, expireref, sesskey, created, modified) 
00734                                         VALUES ($expiry,".$conn->Param('0').", ". $conn->Param('1').", ".$conn->Param('2').", $sysTimeStamp, $sysTimeStamp)";
00735                         }
00736                         
00737         
00738                         $rs = $conn->Execute($sql,array($val,$expireref,$key));
00739                         
00740                 } else {
00741                         // what value shall we insert/update for lob row?
00742                         switch ($driver) {
00743                                 // empty_clob or empty_lob for oracle dbs
00744                                 case 'oracle':
00745                                 case 'oci8':
00746                                 case 'oci8po':
00747                                 case 'oci805':
00748                                         $lob_value = sprintf('empty_%s()', strtolower($clob));
00749                                         break;
00750 
00751                                 // null for all other
00752                                 default:
00753                                         $lob_value = 'null';
00754                                         break;
00755                         }
00756                         
00757                         $conn->StartTrans();
00758                         
00759                         $rs = $conn->Execute("SELECT COUNT(*) AS cnt FROM $table WHERE $binary sesskey = ".$conn->Param(0),array($key));
00760                                         
00761                         if ($rs && reset($rs->fields) > 0) {
00762                                 $sql = "UPDATE $table SET expiry=$expiry, sessdata=$lob_value, expireref= ".$conn->Param(0).",modified=$sysTimeStamp WHERE sesskey = ".$conn->Param('1');
00763                                 
00764                         } else {
00765                                 $sql = "INSERT INTO $table (expiry, sessdata, expireref, sesskey, created, modified) 
00766                                         VALUES ($expiry,$lob_value, ". $conn->Param('0').", ".$conn->Param('1').", $sysTimeStamp, $sysTimeStamp)";
00767                         }
00768                         
00769                         $rs = $conn->Execute($sql,array($expireref,$key));
00770                         
00771                         $qkey = $conn->qstr($key);
00772                         $rs2 = $conn->UpdateBlob($table, 'sessdata', $val, " sesskey=$qkey", strtoupper($clob));
00773                         if ($debug) echo "<hr>",htmlspecialchars($oval), "<hr>";
00774                         $rs = @$conn->CompleteTrans();
00775                         
00776                         
00777                 }
00778 
00779                 if (!$rs) {
00780                         ADOConnection::outp('<p>Session Replace: ' . $conn->ErrorMsg() . '</p>', false);
00781                         return false;
00782                 }  else {
00783                         // bug in access driver (could be odbc?) means that info is not committed
00784                         // properly unless select statement executed in Win2000
00785                         if ($conn->databaseType == 'access') {
00786                                 $sql = "SELECT sesskey FROM $table WHERE $binary sesskey = $qkey";
00787                                 $rs = $conn->Execute($sql);
00788                                 ADODB_Session::_dumprs($rs);
00789                                 if ($rs) {
00790                                         $rs->Close();
00791                                 }
00792                         }
00793                 }/*
00794                 if (ADODB_Session::Lock()) {
00795                         $conn->CommitTrans();
00796                 }*/
00797                 return $rs ? true : false;
00798         }
00799 
00802         static function destroy($key) {
00803                 $conn                   = ADODB_Session::_conn();
00804                 $table                  = ADODB_Session::table();
00805                 $expire_notify  = ADODB_Session::expireNotify();
00806 
00807                 if (!$conn) {
00808                         return false;
00809                 }
00810                 $debug                  = ADODB_Session::debug();
00811                 if ($debug) $conn->debug = 1;
00812                 //assert('$table');
00813 
00814                 $qkey = $conn->quote($key);
00815                 $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
00816 
00817                 if ($expire_notify) {
00818                         reset($expire_notify);
00819                         $fn = next($expire_notify);
00820                         $savem = $conn->SetFetchMode(ADODB_FETCH_NUM);
00821                         $sql = "SELECT expireref, sesskey FROM $table WHERE $binary sesskey = $qkey";
00822                         $rs = $conn->Execute($sql);
00823                         ADODB_Session::_dumprs($rs);
00824                         $conn->SetFetchMode($savem);
00825                         if (!$rs) {
00826                                 return false;
00827                         }
00828                         if (!$rs->EOF) {
00829                                 $ref = $rs->fields[0];
00830                                 $key = $rs->fields[1];
00831                                 //assert('$ref');
00832                                 //assert('$key');
00833                                 $fn($ref, $key);
00834                         }
00835                         $rs->Close();
00836                 }
00837 
00838                 $sql = "DELETE FROM $table WHERE $binary sesskey = $qkey";
00839                 $rs = $conn->Execute($sql);
00840                 if ($rs) {
00841                         $rs->Close();
00842                 }
00843 
00844                 return $rs ? true : false;
00845         }
00846 
00849         static function gc($maxlifetime) 
00850         {
00851                 $conn                   = ADODB_Session::_conn();
00852                 $debug                  = ADODB_Session::debug();
00853                 $expire_notify  = ADODB_Session::expireNotify();
00854                 $optimize               = ADODB_Session::optimize();
00855                 $table                  = ADODB_Session::table();
00856 
00857                 if (!$conn) {
00858                         return false;
00859                 }
00860 
00861 
00862                 $debug                  = ADODB_Session::debug();
00863                 if ($debug) {
00864                         $conn->debug = 1;
00865                         $COMMITNUM = 2;
00866                 } else {
00867                         $COMMITNUM = 20;
00868                 }
00869                 
00870                 //assert('$table');
00871 
00872                 $time = $conn->OffsetDate(-$maxlifetime/24/3600,$conn->sysTimeStamp);
00873                 $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
00874 
00875                 if ($expire_notify) {
00876                         reset($expire_notify);
00877                         $fn = next($expire_notify);
00878                 } else {
00879                         $fn = false;
00880                 }
00881                 
00882                 $savem = $conn->SetFetchMode(ADODB_FETCH_NUM);
00883                 $sql = "SELECT expireref, sesskey FROM $table WHERE expiry < $time ORDER BY 2"; # add order by to prevent deadlock
00884                 $rs = $conn->SelectLimit($sql,1000);
00885                 if ($debug) ADODB_Session::_dumprs($rs);
00886                 $conn->SetFetchMode($savem);
00887                 if ($rs) {
00888                         $tr = $conn->hasTransactions;
00889                         if ($tr) $conn->BeginTrans();
00890                         $keys = array();
00891                         $ccnt = 0;
00892                         while (!$rs->EOF) {
00893                                 $ref = $rs->fields[0];
00894                                 $key = $rs->fields[1];
00895                                 if ($fn) $fn($ref, $key);
00896                                 $del = $conn->Execute("DELETE FROM $table WHERE sesskey=".$conn->Param('0'),array($key));
00897                                 $rs->MoveNext();
00898                                 $ccnt += 1;
00899                                 if ($tr && $ccnt % $COMMITNUM == 0) {
00900                                         if ($debug) echo "Commit<br>\n";
00901                                         $conn->CommitTrans();
00902                                         $conn->BeginTrans();
00903                                 }
00904                         }
00905                         $rs->Close();
00906                         
00907                         if ($tr) $conn->CommitTrans();
00908                 }
00909                 
00910 
00911                 // suggested by Cameron, "GaM3R" <gamr@outworld.cx>
00912                 if ($optimize) {
00913                         $driver = ADODB_Session::driver();
00914 
00915                         if (preg_match('/mysql/i', $driver)) {
00916                                 $sql = "OPTIMIZE TABLE $table";
00917                         }
00918                         if (preg_match('/postgres/i', $driver)) {
00919                                 $sql = "VACUUM $table";
00920                         }
00921                         if (!empty($sql)) {
00922                                 $conn->Execute($sql);
00923                         }
00924                 }
00925 
00926                 
00927                 return true;
00928         }
00929 }
00930 
00931 ADODB_Session::_init();
00932 if (empty($ADODB_SESSION_READONLY))
00933         register_shutdown_function('session_write_close');
00934 
00935 // for backwards compatability only
00936 function adodb_sess_open($save_path, $session_name, $persist = true) {
00937         return ADODB_Session::open($save_path, $session_name, $persist);
00938 }
00939 
00940 // for backwards compatability only
00941 function adodb_sess_gc($t)
00942 {       
00943         return ADODB_Session::gc($t);
00944 }
00945 
00946 ?>