PageRenderTime 49ms CodeModel.GetById 11ms app.highlight 30ms RepoModel.GetById 1ms app.codeStats 0ms

/05_Desarrollo/lib/adodb/session/adodb-session2.php

https://bitbucket.org/SerafinAkatsuki/consultorio
PHP | 947 lines | 586 code | 151 blank | 210 comment | 111 complexity | 121948c9542f93bf1b5dd1733c51563f MD5 | raw file
  1<?php
  2
  3
  4/*
  5V4.990 11 July 2008  (c) 2000-2008 John Lim (jlim#natsoft.com). All rights reserved.
  6         Contributed by Ross Smith (adodb@netebb.com). 
  7  Released under both BSD license and Lesser GPL library license.
  8  Whenever there is any discrepancy between the two licenses,
  9  the BSD license will take precedence.
 10	  Set tabs to 4 for best viewing.
 11	  
 12
 13*/
 14
 15/*
 16
 17CREATE Table SCripts
 18
 19Oracle
 20======
 21
 22CREATE TABLE SESSIONS2
 23(
 24  SESSKEY    VARCHAR2(48 BYTE)                  NOT NULL,
 25  EXPIRY     DATE                               NOT NULL,
 26  EXPIREREF  VARCHAR2(200 BYTE),
 27  CREATED    DATE                               NOT NULL,
 28  MODIFIED   DATE                               NOT NULL,
 29  SESSDATA   CLOB,
 30  PRIMARY KEY(SESSKEY)
 31);
 32
 33
 34CREATE INDEX SESS2_EXPIRY ON SESSIONS2(EXPIRY);
 35CREATE UNIQUE INDEX SESS2_PK ON SESSIONS2(SESSKEY);
 36CREATE INDEX SESS2_EXP_REF ON SESSIONS2(EXPIREREF);
 37
 38
 39 
 40 MySQL
 41 =====
 42 
 43CREATE TABLE sessions2(
 44	sesskey VARCHAR( 64 ) NOT NULL DEFAULT '',
 45	expiry TIMESTAMP NOT NULL ,
 46	expireref VARCHAR( 250 ) DEFAULT '',
 47	created TIMESTAMP NOT NULL ,
 48	modified TIMESTAMP NOT NULL ,
 49	sessdata LONGTEXT DEFAULT '',
 50	PRIMARY KEY ( sesskey ) ,
 51	INDEX sess2_expiry( expiry ),
 52	INDEX sess2_expireref( expireref )
 53)
 54
 55
 56*/
 57
 58if (!defined('_ADODB_LAYER')) {
 59	require realpath(dirname(__FILE__) . '/../adodb.inc.php');
 60}
 61
 62if (defined('ADODB_SESSION')) return 1;
 63
 64define('ADODB_SESSION', dirname(__FILE__));
 65define('ADODB_SESSION2', ADODB_SESSION);
 66
 67/* 
 68	Unserialize session data manually. See http://phplens.com/lens/lensforum/msgs.php?id=9821 
 69	
 70	From Kerr Schere, to unserialize session data stored via ADOdb. 
 71	1. Pull the session data from the db and loop through it. 
 72	2. Inside the loop, you will need to urldecode the data column. 
 73	3. After urldecode, run the serialized string through this function:
 74
 75*/
 76function adodb_unserialize( $serialized_string ) 
 77{
 78	$variables = array( );
 79	$a = preg_split( "/(\w+)\|/", $serialized_string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
 80	for( $i = 0; $i < count( $a ); $i = $i+2 ) {
 81		$variables[$a[$i]] = unserialize( $a[$i+1] );
 82	}
 83	return( $variables );
 84}
 85
 86/*
 87	Thanks Joe Li. See http://phplens.com/lens/lensforum/msgs.php?id=11487&x=1
 88	Since adodb 4.61.
 89*/
 90function adodb_session_regenerate_id() 
 91{
 92	$conn =& ADODB_Session::_conn();
 93	if (!$conn) return false;
 94
 95	$old_id = session_id();
 96	if (function_exists('session_regenerate_id')) {
 97		session_regenerate_id();
 98	} else {
 99		session_id(md5(uniqid(rand(), true)));
100		$ck = session_get_cookie_params();
101		setcookie(session_name(), session_id(), false, $ck['path'], $ck['domain'], $ck['secure']);
102		//@session_start();
103	}
104	$new_id = session_id();
105	$ok =& $conn->Execute('UPDATE '. ADODB_Session::table(). ' SET sesskey='. $conn->qstr($new_id). ' WHERE sesskey='.$conn->qstr($old_id));
106	
107	/* it is possible that the update statement fails due to a collision */
108	if (!$ok) {
109		session_id($old_id);
110		if (empty($ck)) $ck = session_get_cookie_params();
111		setcookie(session_name(), session_id(), false, $ck['path'], $ck['domain'], $ck['secure']);
112		return false;
113	}
114	
115	return true;
116}
117
118/*
119    Generate database table for session data
120    @see http://phplens.com/lens/lensforum/msgs.php?id=12280
121    @return 0 if failure, 1 if errors, 2 if successful.
122	@author Markus Staab http://www.public-4u.de
123*/
124function adodb_session_create_table($schemaFile=null,$conn = null)
125{
126    // set default values
127    if ($schemaFile===null) $schemaFile = ADODB_SESSION . '/session_schema2.xml';
128    if ($conn===null) $conn =& ADODB_Session::_conn();
129
130	if (!$conn) return 0;
131
132    $schema = new adoSchema($conn);
133    $schema->ParseSchema($schemaFile);
134    return $schema->ExecuteSchema();
135}
136
137/*!
138	\static
139*/
140class ADODB_Session {
141	/////////////////////
142	// getter/setter methods
143	/////////////////////
144	
145	/*
146	
147	function Lock($lock=null)
148	{
149	static $_lock = false;
150	
151		if (!is_null($lock)) $_lock = $lock;
152		return $lock;
153	}
154	*/
155	/*!
156	*/
157	function driver($driver = null) 
158	{
159		static $_driver = 'mysql';
160		static $set = false;
161
162		if (!is_null($driver)) {
163			$_driver = trim($driver);
164			$set = true;
165		} elseif (!$set) {
166			// backwards compatibility
167			if (isset($GLOBALS['ADODB_SESSION_DRIVER'])) {
168				return $GLOBALS['ADODB_SESSION_DRIVER'];
169			}
170		}
171
172		return $_driver;
173	}
174
175	/*!
176	*/
177	function host($host = null) {
178		static $_host = 'localhost';
179		static $set = false;
180
181		if (!is_null($host)) {
182			$_host = trim($host);
183			$set = true;
184		} elseif (!$set) {
185			// backwards compatibility
186			if (isset($GLOBALS['ADODB_SESSION_CONNECT'])) {
187				return $GLOBALS['ADODB_SESSION_CONNECT'];
188			}
189		}
190
191		return $_host;
192	}
193
194	/*!
195	*/
196	function user($user = null) 
197	{
198		static $_user = 'root';
199		static $set = false;
200
201		if (!is_null($user)) {
202			$_user = trim($user);
203			$set = true;
204		} elseif (!$set) {
205			// backwards compatibility
206			if (isset($GLOBALS['ADODB_SESSION_USER'])) {
207				return $GLOBALS['ADODB_SESSION_USER'];
208			}
209		}
210
211		return $_user;
212	}
213
214	/*!
215	*/
216	function password($password = null) 
217	{
218		static $_password = '';
219		static $set = false;
220
221		if (!is_null($password)) {
222			$_password = $password;
223			$set = true;
224		} elseif (!$set) {
225			// backwards compatibility
226			if (isset($GLOBALS['ADODB_SESSION_PWD'])) {
227				return $GLOBALS['ADODB_SESSION_PWD'];
228			}
229		}
230
231		return $_password;
232	}
233
234	/*!
235	*/
236	function database($database = null) 
237	{
238		static $_database = '';
239		static $set = false;
240		
241		if (!is_null($database)) {
242			$_database = trim($database);
243			$set = true;
244		} elseif (!$set) {
245			// backwards compatibility
246			if (isset($GLOBALS['ADODB_SESSION_DB'])) {
247				return $GLOBALS['ADODB_SESSION_DB'];
248			}
249		}
250		return $_database;
251	}
252
253	/*!
254	*/
255	function persist($persist = null) 
256	{
257		static $_persist = true;
258
259		if (!is_null($persist)) {
260			$_persist = trim($persist);
261		}
262
263		return $_persist;
264	}
265
266	/*!
267	*/
268	function lifetime($lifetime = null) 
269	{
270		static $_lifetime;
271		static $set = false;
272
273		if (!is_null($lifetime)) {
274			$_lifetime = (int) $lifetime;
275			$set = true;
276		} elseif (!$set) {
277			// backwards compatibility
278			if (isset($GLOBALS['ADODB_SESS_LIFE'])) {
279				return $GLOBALS['ADODB_SESS_LIFE'];
280			}
281		}
282		if (!$_lifetime) {
283			$_lifetime = ini_get('session.gc_maxlifetime');
284			if ($_lifetime <= 1) {
285				// bug in PHP 4.0.3 pl 1  -- how about other versions?
286				//print "<h3>Session Error: PHP.INI setting <i>session.gc_maxlifetime</i>not set: $lifetime</h3>";
287				$_lifetime = 1440;
288			}
289		}
290
291		return $_lifetime;
292	}
293
294	/*!
295	*/
296	function debug($debug = null) 
297	{
298		static $_debug = false;
299		static $set = false;
300
301		if (!is_null($debug)) {
302			$_debug = (bool) $debug;
303
304			$conn = ADODB_Session::_conn();
305			if ($conn) {
306				$conn->debug = $_debug;
307			}
308			$set = true;
309		} elseif (!$set) {
310			// backwards compatibility
311			if (isset($GLOBALS['ADODB_SESS_DEBUG'])) {
312				return $GLOBALS['ADODB_SESS_DEBUG'];
313			}
314		}
315
316		return $_debug;
317	}
318
319	/*!
320	*/
321	function expireNotify($expire_notify = null) 
322	{
323		static $_expire_notify;
324		static $set = false;
325
326		if (!is_null($expire_notify)) {
327			$_expire_notify = $expire_notify;
328			$set = true;
329		} elseif (!$set) {
330			// backwards compatibility
331			if (isset($GLOBALS['ADODB_SESSION_EXPIRE_NOTIFY'])) {
332				return $GLOBALS['ADODB_SESSION_EXPIRE_NOTIFY'];
333			}
334		}
335
336		return $_expire_notify;
337	}
338
339	/*!
340	*/
341	function table($table = null) 
342	{
343		static $_table = 'sessions2';
344		static $set = false;
345
346		if (!is_null($table)) {
347			$_table = trim($table);
348			$set = true;
349		} elseif (!$set) {
350			// backwards compatibility
351			if (isset($GLOBALS['ADODB_SESSION_TBL'])) {
352				return $GLOBALS['ADODB_SESSION_TBL'];
353			}
354		}
355
356		return $_table;
357	}
358
359	/*!
360	*/
361	function optimize($optimize = null) 
362	{
363		static $_optimize = false;
364		static $set = false;
365
366		if (!is_null($optimize)) {
367			$_optimize = (bool) $optimize;
368			$set = true;
369		} elseif (!$set) {
370			// backwards compatibility
371			if (defined('ADODB_SESSION_OPTIMIZE')) {
372				return true;
373			}
374		}
375
376		return $_optimize;
377	}
378
379	/*!
380	*/
381	function syncSeconds($sync_seconds = null) {
382		//echo ("<p>WARNING: ADODB_SESSION::syncSeconds is longer used, please remove this function for your code</p>");
383		
384		return 0;
385	}
386
387	/*!
388	*/
389	function clob($clob = null) {
390		static $_clob = false;
391		static $set = false;
392
393		if (!is_null($clob)) {
394			$_clob = strtolower(trim($clob));
395			$set = true;
396		} elseif (!$set) {
397			// backwards compatibility
398			if (isset($GLOBALS['ADODB_SESSION_USE_LOBS'])) {
399				return $GLOBALS['ADODB_SESSION_USE_LOBS'];
400			}
401		}
402
403		return $_clob;
404	}
405
406	/*!
407	*/
408	function dataFieldName($data_field_name = null) {
409		//echo ("<p>WARNING: ADODB_SESSION::dataFieldName() is longer used, please remove this function for your code</p>");
410		return '';
411	}
412
413	/*!
414	*/
415	function filter($filter = null) {
416		static $_filter = array();
417
418		if (!is_null($filter)) {
419			if (!is_array($filter)) {
420				$filter = array($filter);
421			}
422			$_filter = $filter;
423		}
424
425		return $_filter;
426	}
427
428	/*!
429	*/
430	function encryptionKey($encryption_key = null) {
431		static $_encryption_key = 'CRYPTED ADODB SESSIONS ROCK!';
432
433		if (!is_null($encryption_key)) {
434			$_encryption_key = $encryption_key;
435		}
436
437		return $_encryption_key;
438	}
439
440	/////////////////////
441	// private methods
442	/////////////////////
443
444	/*!
445	*/
446	function &_conn($conn=null) 
447	{
448		if (isset($GLOBALS['ADODB_SESS_CONN'])) {
449			$conn =& $GLOBALS['ADODB_SESS_CONN'];
450			return $conn;
451		}
452		$false = false;
453		return $false;
454	}
455
456	/*!
457	*/
458	function _crc($crc = null) {
459		static $_crc = false;
460
461		if (!is_null($crc)) {
462			$_crc = $crc;
463		}
464
465		return $_crc;
466	}
467
468	/*!
469	*/
470	function _init() {
471		session_module_name('user');
472		session_set_save_handler(
473			array('ADODB_Session', 'open'),
474			array('ADODB_Session', 'close'),
475			array('ADODB_Session', 'read'),
476			array('ADODB_Session', 'write'),
477			array('ADODB_Session', 'destroy'),
478			array('ADODB_Session', 'gc')
479		);
480	}
481
482
483	/*!
484	*/
485	function _sessionKey() {
486		// use this function to create the encryption key for crypted sessions
487		// crypt the used key, ADODB_Session::encryptionKey() as key and session_id() as salt
488		return crypt(ADODB_Session::encryptionKey(), session_id());
489	}
490
491	/*!
492	*/
493	function _dumprs($rs) {
494		$conn	=& ADODB_Session::_conn();
495		$debug	= ADODB_Session::debug();
496
497		if (!$conn) {
498			return;
499		}
500
501		if (!$debug) {
502			return;
503		}
504
505		if (!$rs) {
506			echo "<br />\$rs is null or false<br />\n";
507			return;
508		}
509
510		//echo "<br />\nAffected_Rows=",$conn->Affected_Rows(),"<br />\n";
511
512		if (!is_object($rs)) {
513			return;
514		}
515
516		require_once ADODB_SESSION.'/../tohtml.inc.php';
517		rs2html($rs);
518	}
519
520	/////////////////////
521	// public methods
522	/////////////////////
523	
524	function config($driver, $host, $user, $password, $database=false,$options=false)
525	{
526		ADODB_Session::driver($driver);
527		ADODB_Session::host($host);
528		ADODB_Session::user($user);
529		ADODB_Session::password($password);
530		ADODB_Session::database($database);
531		
532		if ($driver == 'oci8' || $driver == 'oci8po') $options['lob'] = 'CLOB';
533		
534		if (isset($options['table'])) ADODB_Session::table($options['table']);
535		if (isset($options['lob'])) ADODB_Session::clob($options['lob']);
536		if (isset($options['debug'])) ADODB_Session::debug($options['debug']);
537	}
538
539	/*!
540		Create the connection to the database.
541
542		If $conn already exists, reuse that connection
543	*/
544	function open($save_path, $session_name, $persist = null) 
545	{
546		$conn =& ADODB_Session::_conn();
547
548		if ($conn) {
549			return true;
550		}
551
552		$database	= ADODB_Session::database();
553		$debug		= ADODB_Session::debug();
554		$driver		= ADODB_Session::driver();
555		$host		= ADODB_Session::host();
556		$password	= ADODB_Session::password();
557		$user		= ADODB_Session::user();
558
559		if (!is_null($persist)) {
560			ADODB_Session::persist($persist);
561		} else {
562			$persist = ADODB_Session::persist();
563		}
564
565# these can all be defaulted to in php.ini
566#		assert('$database');
567#		assert('$driver');
568#		assert('$host');
569
570		$conn =& ADONewConnection($driver);
571
572		if ($debug) {
573			$conn->debug = true;		
574			ADOConnection::outp( " driver=$driver user=$user db=$database ");
575		}
576		
577		if ($persist) {
578			switch($persist) {
579			default:
580			case 'P': $ok = $conn->PConnect($host, $user, $password, $database); break;
581			case 'C': $ok = $conn->Connect($host, $user, $password, $database); break;
582			case 'N': $ok = $conn->NConnect($host, $user, $password, $database); break;
583			}
584		} else {
585			$ok = $conn->Connect($host, $user, $password, $database);
586		}
587
588		if ($ok) $GLOBALS['ADODB_SESS_CONN'] =& $conn;
589		else
590			ADOConnection::outp('<p>Session: connection failed</p>', false);
591		
592
593		return $ok;
594	}
595
596	/*!
597		Close the connection
598	*/
599	function close() 
600	{
601/*
602		$conn =& ADODB_Session::_conn();
603		if ($conn) $conn->Close();
604*/
605		return true;
606	}
607
608	/*
609		Slurp in the session variables and return the serialized string
610	*/
611	function read($key) 
612	{
613		$conn	=& ADODB_Session::_conn();
614		$filter	= ADODB_Session::filter();
615		$table	= ADODB_Session::table();
616
617		if (!$conn) {
618			return '';
619		}
620
621		//assert('$table');
622
623		$qkey = $conn->quote($key);
624		$binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
625	
626		$sql = "SELECT sessdata FROM $table WHERE sesskey = $binary $qkey AND expiry >= " . $conn->sysTimeStamp;
627		/* Lock code does not work as it needs to hold transaction within whole page, and we don't know if 
628		  developer has commited elsewhere... :(
629		 */
630		#if (ADODB_Session::Lock())
631		#	$rs =& $conn->RowLock($table, "$binary sesskey = $qkey AND expiry >= " . time(), sessdata);
632		#else
633		
634			$rs =& $conn->Execute($sql);
635		//ADODB_Session::_dumprs($rs);
636		if ($rs) {
637			if ($rs->EOF) {
638				$v = '';
639			} else {
640				$v = reset($rs->fields);
641				$filter = array_reverse($filter);
642				foreach ($filter as $f) {
643					if (is_object($f)) {
644						$v = $f->read($v, ADODB_Session::_sessionKey());
645					}
646				}
647				$v = rawurldecode($v);
648			}
649
650			$rs->Close();
651
652			ADODB_Session::_crc(strlen($v) . crc32($v));
653			return $v;
654		}
655
656		return '';
657	}
658
659	/*!
660		Write the serialized data to a database.
661
662		If the data has not been modified since the last read(), we do not write.
663	*/
664	function write($key, $val) 
665	{
666	global $ADODB_SESSION_READONLY;
667	
668		if (!empty($ADODB_SESSION_READONLY)) return;
669		
670		$clob			= ADODB_Session::clob();
671		$conn			=& ADODB_Session::_conn();
672		$crc			= ADODB_Session::_crc();
673		$debug			= ADODB_Session::debug();
674		$driver			= ADODB_Session::driver();
675		$expire_notify	= ADODB_Session::expireNotify();
676		$filter			= ADODB_Session::filter();
677		$lifetime		= ADODB_Session::lifetime();
678		$table			= ADODB_Session::table();
679	
680		if (!$conn) {
681			return false;
682		}
683	
684		$sysTimeStamp = $conn->sysTimeStamp;
685		
686		//assert('$table');
687
688		$expiry = $conn->OffsetDate($lifetime/(24*3600),$sysTimeStamp);
689
690		$binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
691
692		// crc32 optimization since adodb 2.1
693		// now we only update expiry date, thx to sebastian thom in adodb 2.32
694		if ($crc !== false && $crc == (strlen($val) . crc32($val))) {
695			if ($debug) {
696				echo '<p>Session: Only updating date - crc32 not changed</p>';
697			}
698			
699			$expirevar = '';
700			if ($expire_notify) {
701				$var = reset($expire_notify);
702				global $$var;
703				if (isset($$var)) {
704					$expirevar = $$var;
705				}
706			}
707			
708			
709			$sql = "UPDATE $table SET expiry = $expiry ,expireref=".$conn->Param('0').", modified = $sysTimeStamp WHERE $binary sesskey = ".$conn->Param('1')." AND expiry >= $sysTimeStamp";
710			$rs =& $conn->Execute($sql,array($expirevar,$key));
711			return true;
712		}
713		$val = rawurlencode($val);
714		foreach ($filter as $f) {
715			if (is_object($f)) {
716				$val = $f->write($val, ADODB_Session::_sessionKey());
717			}
718		}
719
720		$expireref = '';
721		if ($expire_notify) {
722			$var = reset($expire_notify);
723			global $$var;
724			if (isset($$var)) {
725				$expireref = $$var;
726			}
727		} 
728
729		if (!$clob) {	// no lobs, simply use replace()
730			$rs =& $conn->Execute("SELECT COUNT(*) AS cnt FROM $table WHERE $binary sesskey = ".$conn->Param(0),array($key));
731			if ($rs) $rs->Close();
732					
733			if ($rs && reset($rs->fields) > 0) {
734				$sql = "UPDATE $table SET expiry=$expiry, sessdata=".$conn->Param(0).", expireref= ".$conn->Param(1).",modified=$sysTimeStamp WHERE sesskey = ".$conn->Param('2');
735				
736			} else {
737				$sql = "INSERT INTO $table (expiry, sessdata, expireref, sesskey, created, modified) 
738					VALUES ($expiry,".$conn->Param('0').", ". $conn->Param('1').", ".$conn->Param('2').", $sysTimeStamp, $sysTimeStamp)";
739			}
740			
741	
742			$rs =& $conn->Execute($sql,array($val,$expireref,$key));
743			
744		} else {
745			// what value shall we insert/update for lob row?
746			switch ($driver) {
747				// empty_clob or empty_lob for oracle dbs
748				case 'oracle':
749				case 'oci8':
750				case 'oci8po':
751				case 'oci805':
752					$lob_value = sprintf('empty_%s()', strtolower($clob));
753					break;
754
755				// null for all other
756				default:
757					$lob_value = 'null';
758					break;
759			}
760			
761			$conn->StartTrans();
762			
763			$rs =& $conn->Execute("SELECT COUNT(*) AS cnt FROM $table WHERE $binary sesskey = ".$conn->Param(0),array($key));
764			if ($rs) $rs->Close();
765					
766			if ($rs && reset($rs->fields) > 0) {
767				$sql = "UPDATE $table SET expiry=$expiry, sessdata=$lob_value, expireref= ".$conn->Param(0).",modified=$sysTimeStamp WHERE sesskey = ".$conn->Param('1');
768				
769			} else {
770				$sql = "INSERT INTO $table (expiry, sessdata, expireref, sesskey, created, modified) 
771					VALUES ($expiry,$lob_value, ". $conn->Param('0').", ".$conn->Param('1').", $sysTimeStamp, $sysTimeStamp)";
772			}
773			
774			$rs =& $conn->Execute($sql,array($expireref,$key));
775			
776			$qkey = $conn->qstr($key);
777			$rs2 = $conn->UpdateBlob($table, 'sessdata', $val, " sesskey=$qkey", strtoupper($clob));
778			$rs = @$conn->CompleteTrans();
779			
780			
781		}
782
783		if (!$rs) {
784			ADOConnection::outp('<p>Session Replace: ' . $conn->ErrorMsg() . '</p>', false);
785			return false;
786		}  else {
787			// bug in access driver (could be odbc?) means that info is not committed
788			// properly unless select statement executed in Win2000
789			if ($conn->databaseType == 'access') {
790				$sql = "SELECT sesskey FROM $table WHERE $binary sesskey = $qkey";
791				$rs =& $conn->Execute($sql);
792				ADODB_Session::_dumprs($rs);
793				if ($rs) {
794					$rs->Close();
795				}
796			}
797		}/*
798		if (ADODB_Session::Lock()) {
799			$conn->CommitTrans();
800		}*/
801		return $rs ? true : false;
802	}
803
804	/*!
805	*/
806	function destroy($key) {
807		$conn			=& ADODB_Session::_conn();
808		$table			= ADODB_Session::table();
809		$expire_notify	= ADODB_Session::expireNotify();
810
811		if (!$conn) {
812			return false;
813		}
814
815		//assert('$table');
816
817		$qkey = $conn->quote($key);
818		$binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
819
820		if ($expire_notify) {
821			reset($expire_notify);
822			$fn = next($expire_notify);
823			$savem = $conn->SetFetchMode(ADODB_FETCH_NUM);
824			$sql = "SELECT expireref, sesskey FROM $table WHERE $binary sesskey = $qkey";
825			$rs =& $conn->Execute($sql);
826			ADODB_Session::_dumprs($rs);
827			$conn->SetFetchMode($savem);
828			if (!$rs) {
829				return false;
830			}
831			if (!$rs->EOF) {
832				$ref = $rs->fields[0];
833				$key = $rs->fields[1];
834				//assert('$ref');
835				//assert('$key');
836				$fn($ref, $key);
837			}
838			$rs->Close();
839		}
840
841		$sql = "DELETE FROM $table WHERE $binary sesskey = $qkey";
842		$rs =& $conn->Execute($sql);
843		ADODB_Session::_dumprs($rs);
844		if ($rs) {
845			$rs->Close();
846		}
847
848		return $rs ? true : false;
849	}
850
851	/*!
852	*/
853	function gc($maxlifetime) 
854	{
855		$conn			=& ADODB_Session::_conn();
856		$debug			= ADODB_Session::debug();
857		$expire_notify	= ADODB_Session::expireNotify();
858		$optimize		= ADODB_Session::optimize();
859		$table			= ADODB_Session::table();
860
861		if (!$conn) {
862			return false;
863		}
864
865		//assert('$table');
866
867		$time = $conn->sysTimeStamp;
868		$binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
869
870		if ($expire_notify) {
871			reset($expire_notify);
872			$fn = next($expire_notify);
873			$savem = $conn->SetFetchMode(ADODB_FETCH_NUM);
874			$sql = "SELECT expireref, sesskey FROM $table WHERE expiry < $time";
875			$rs =& $conn->Execute($sql);
876			ADODB_Session::_dumprs($rs);
877			$conn->SetFetchMode($savem);
878			if ($rs) {
879				$conn->StartTrans();
880				$keys = array();
881				while (!$rs->EOF) {
882					$ref = $rs->fields[0];
883					$key = $rs->fields[1];
884					$fn($ref, $key);
885					$del = $conn->Execute("DELETE FROM $table WHERE sesskey=".$conn->Param('0'),array($key));
886					$rs->MoveNext();
887				}
888				$rs->Close();
889				
890				$conn->CompleteTrans();
891			}
892		} else {
893		
894			if (0) {
895				$sql = "SELECT sesskey FROM $table WHERE expiry < $time";
896				$arr =& $conn->GetAll($sql);
897				foreach ($arr as $row) {
898					$sql2 = "DELETE FROM $table WHERE sesskey=".$conn->Param('0');
899					$conn->Execute($sql2,array($row[0]));
900				}
901			} else {
902				$sql = "DELETE FROM $table WHERE expiry < $time";
903				$rs =& $conn->Execute($sql);
904				ADODB_Session::_dumprs($rs);
905				if ($rs) $rs->Close();
906			}
907			if ($debug) {
908				ADOConnection::outp("<p><b>Garbage Collection</b>: $sql</p>");
909			}
910		}
911
912		// suggested by Cameron, "GaM3R" <gamr@outworld.cx>
913		if ($optimize) {
914			$driver = ADODB_Session::driver();
915
916			if (preg_match('/mysql/i', $driver)) {
917				$sql = "OPTIMIZE TABLE $table";
918			}
919			if (preg_match('/postgres/i', $driver)) {
920				$sql = "VACUUM $table";
921			}
922			if (!empty($sql)) {
923				$conn->Execute($sql);
924			}
925		}
926
927		
928		return true;
929	}
930}
931
932ADODB_Session::_init();
933if (empty($ADODB_SESSION_READONLY))
934	register_shutdown_function('session_write_close');
935
936// for backwards compatability only
937function adodb_sess_open($save_path, $session_name, $persist = true) {
938	return ADODB_Session::open($save_path, $session_name, $persist);
939}
940
941// for backwards compatability only
942function adodb_sess_gc($t)
943{	
944	return ADODB_Session::gc($t);
945}
946
947?>