Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

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