ChangeLog
[Packages/TYPO3.CMS.git] / typo3 / sysext / adodb / adodb / session / adodb-session.php
1 <?php
2
3
4 /*
5 V4.90 8 June 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 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 function config($driver, $host, $user, $password, $database=false,$options=false)
497 {
498 ADODB_Session::driver($driver);
499 ADODB_Session::host($host);
500 ADODB_Session::user($user);
501 ADODB_Session::password($password);
502 ADODB_Session::database($database);
503
504 if (isset($options['table'])) ADODB_Session::table($options['table']);
505 if (isset($options['clob'])) ADODB_Session::table($options['clob']);
506 if (isset($options['field'])) ADODB_Session::dataFieldName($options['field']);
507 }
508
509 /*!
510 Create the connection to the database.
511
512 If $conn already exists, reuse that connection
513 */
514 function open($save_path, $session_name, $persist = null) {
515 $conn =& ADODB_Session::_conn();
516
517 if ($conn) {
518 return true;
519 }
520
521 $database = ADODB_Session::database();
522 $debug = ADODB_Session::debug();
523 $driver = ADODB_Session::driver();
524 $host = ADODB_Session::host();
525 $password = ADODB_Session::password();
526 $user = ADODB_Session::user();
527
528 if (!is_null($persist)) {
529 ADODB_Session::persist($persist);
530 } else {
531 $persist = ADODB_Session::persist();
532 }
533
534 # these can all be defaulted to in php.ini
535 # assert('$database');
536 # assert('$driver');
537 # assert('$host');
538
539 $conn =& ADONewConnection($driver);
540
541 if ($debug) {
542 $conn->debug = true;
543 // ADOConnection::outp( " driver=$driver user=$user pwd=$password db=$database ");
544 }
545
546 if ($persist) {
547 switch($persist) {
548 default:
549 case 'P': $ok = $conn->PConnect($host, $user, $password, $database); break;
550 case 'C': $ok = $conn->Connect($host, $user, $password, $database); break;
551 case 'N': $ok = $conn->NConnect($host, $user, $password, $database); break;
552 }
553 } else {
554 $ok = $conn->Connect($host, $user, $password, $database);
555 }
556
557 if ($ok) $GLOBALS['ADODB_SESS_CONN'] =& $conn;
558 else
559 ADOConnection::outp('<p>Session: connection failed</p>', false);
560
561
562 return $ok;
563 }
564
565 /*!
566 Close the connection
567 */
568 function close() {
569 /*
570 $conn =& ADODB_Session::_conn();
571 if ($conn) $conn->Close();
572 */
573 return true;
574 }
575
576 /*
577 Slurp in the session variables and return the serialized string
578 */
579 function read($key) {
580 $conn =& ADODB_Session::_conn();
581 $data = ADODB_Session::dataFieldName();
582 $filter = ADODB_Session::filter();
583 $table = ADODB_Session::table();
584
585 if (!$conn) {
586 return '';
587 }
588
589 assert('$table');
590
591 $qkey = $conn->quote($key);
592 $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
593
594 $sql = "SELECT $data FROM $table WHERE sesskey = $binary $qkey AND expiry >= " . time();
595 /* Lock code does not work as it needs to hold transaction within whole page, and we don't know if
596 developer has commited elsewhere... :(
597 */
598 #if (ADODB_Session::Lock())
599 # $rs =& $conn->RowLock($table, "$binary sesskey = $qkey AND expiry >= " . time(), $data);
600 #else
601
602 $rs =& $conn->Execute($sql);
603 //ADODB_Session::_dumprs($rs);
604 if ($rs) {
605 if ($rs->EOF) {
606 $v = '';
607 } else {
608 $v = reset($rs->fields);
609 $filter = array_reverse($filter);
610 foreach ($filter as $f) {
611 if (is_object($f)) {
612 $v = $f->read($v, ADODB_Session::_sessionKey());
613 }
614 }
615 $v = rawurldecode($v);
616 }
617
618 $rs->Close();
619
620 ADODB_Session::_crc(strlen($v) . crc32($v));
621 return $v;
622 }
623
624 return '';
625 }
626
627 /*!
628 Write the serialized data to a database.
629
630 If the data has not been modified since the last read(), we do not write.
631 */
632 function write($key, $val) {
633 global $ADODB_SESSION_READONLY;
634
635 if (!empty($ADODB_SESSION_READONLY)) return;
636
637 $clob = ADODB_Session::clob();
638 $conn =& ADODB_Session::_conn();
639 $crc = ADODB_Session::_crc();
640 $data = ADODB_Session::dataFieldName();
641 $debug = ADODB_Session::debug();
642 $driver = ADODB_Session::driver();
643 $expire_notify = ADODB_Session::expireNotify();
644 $filter = ADODB_Session::filter();
645 $lifetime = ADODB_Session::lifetime();
646 $table = ADODB_Session::table();
647
648 if (!$conn) {
649 return false;
650 }
651 $qkey = $conn->qstr($key);
652
653 assert('$table');
654
655 $expiry = time() + $lifetime;
656
657 $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
658
659 // crc32 optimization since adodb 2.1
660 // now we only update expiry date, thx to sebastian thom in adodb 2.32
661 if ($crc !== false && $crc == (strlen($val) . crc32($val))) {
662 if ($debug) {
663 echo '<p>Session: Only updating date - crc32 not changed</p>';
664 }
665
666 $expirevar = '';
667 if ($expire_notify) {
668 $var = reset($expire_notify);
669 global $$var;
670 if (isset($$var)) {
671 $expirevar = $$var;
672 }
673 }
674
675
676 $sql = "UPDATE $table SET expiry = ".$conn->Param('0').",expireref=".$conn->Param('1')." WHERE $binary sesskey = ".$conn->Param('2')." AND expiry >= ".$conn->Param('3');
677 $rs =& $conn->Execute($sql,array($expiry,$expirevar,$key,time()));
678 return true;
679 }
680 $val = rawurlencode($val);
681 foreach ($filter as $f) {
682 if (is_object($f)) {
683 $val = $f->write($val, ADODB_Session::_sessionKey());
684 }
685 }
686
687 $arr = array('sesskey' => $key, 'expiry' => $expiry, $data => $val, 'expireref' => '');
688 if ($expire_notify) {
689 $var = reset($expire_notify);
690 global $$var;
691 if (isset($$var)) {
692 $arr['expireref'] = $$var;
693 }
694 }
695
696 if (!$clob) { // no lobs, simply use replace()
697 $arr[$data] = $conn->qstr($val);
698 $rs = $conn->Replace($table, $arr, 'sesskey', $autoQuote = true);
699
700 } else {
701 // what value shall we insert/update for lob row?
702 switch ($driver) {
703 // empty_clob or empty_lob for oracle dbs
704 case 'oracle':
705 case 'oci8':
706 case 'oci8po':
707 case 'oci805':
708 $lob_value = sprintf('empty_%s()', strtolower($clob));
709 break;
710
711 // null for all other
712 default:
713 $lob_value = 'null';
714 break;
715 }
716
717 $expiryref = $conn->qstr($arr['expireref']);
718 // do we insert or update? => as for sesskey
719 $rs =& $conn->Execute("SELECT COUNT(*) AS cnt FROM $table WHERE $binary sesskey = $qkey");
720 if ($rs && reset($rs->fields) > 0) {
721 $sql = "UPDATE $table SET expiry = $expiry, $data = $lob_value, expireref=$expiryref WHERE sesskey = $qkey";
722 } else {
723 $sql = "INSERT INTO $table (expiry, $data, sesskey,expireref) VALUES ($expiry, $lob_value, $qkey,$expiryref)";
724 }
725 if ($rs) {
726 $rs->Close();
727 }
728
729 $err = '';
730 $rs1 =& $conn->Execute($sql);
731 if (!$rs1) {
732 $err = $conn->ErrorMsg()."\n";
733 }
734 $rs2 =& $conn->UpdateBlob($table, $data, $val, " sesskey=$qkey", strtoupper($clob));
735
736 if (!$rs2) {
737 $err .= $conn->ErrorMsg()."\n";
738 }
739 $rs = ($rs && $rs2) ? true : false;
740 if ($rs1) {
741 $rs1->Close();
742 }
743 if (is_object($rs2)) {
744 $rs2->Close();
745 }
746 }
747
748 if (!$rs) {
749 ADOConnection::outp('<p>Session Replace: ' . $conn->ErrorMsg() . '</p>', false);
750 return false;
751 } else {
752 // bug in access driver (could be odbc?) means that info is not committed
753 // properly unless select statement executed in Win2000
754 if ($conn->databaseType == 'access') {
755 $sql = "SELECT sesskey FROM $table WHERE $binary sesskey = $qkey";
756 $rs =& $conn->Execute($sql);
757 ADODB_Session::_dumprs($rs);
758 if ($rs) {
759 $rs->Close();
760 }
761 }
762 }/*
763 if (ADODB_Session::Lock()) {
764 $conn->CommitTrans();
765 }*/
766 return $rs ? true : false;
767 }
768
769 /*!
770 */
771 function destroy($key) {
772 $conn =& ADODB_Session::_conn();
773 $table = ADODB_Session::table();
774 $expire_notify = ADODB_Session::expireNotify();
775
776 if (!$conn) {
777 return false;
778 }
779
780 assert('$table');
781
782 $qkey = $conn->quote($key);
783 $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
784
785 if ($expire_notify) {
786 reset($expire_notify);
787 $fn = next($expire_notify);
788 $savem = $conn->SetFetchMode(ADODB_FETCH_NUM);
789 $sql = "SELECT expireref, sesskey FROM $table WHERE $binary sesskey = $qkey";
790 $rs =& $conn->Execute($sql);
791 ADODB_Session::_dumprs($rs);
792 $conn->SetFetchMode($savem);
793 if (!$rs) {
794 return false;
795 }
796 if (!$rs->EOF) {
797 $ref = $rs->fields[0];
798 $key = $rs->fields[1];
799 //assert('$ref');
800 //assert('$key');
801 $fn($ref, $key);
802 }
803 $rs->Close();
804 }
805
806 $sql = "DELETE FROM $table WHERE $binary sesskey = $qkey";
807 $rs =& $conn->Execute($sql);
808 ADODB_Session::_dumprs($rs);
809 if ($rs) {
810 $rs->Close();
811 }
812
813 return $rs ? true : false;
814 }
815
816 /*!
817 */
818 function gc($maxlifetime) {
819 $conn =& ADODB_Session::_conn();
820 $debug = ADODB_Session::debug();
821 $expire_notify = ADODB_Session::expireNotify();
822 $optimize = ADODB_Session::optimize();
823 $sync_seconds = ADODB_Session::syncSeconds();
824 $table = ADODB_Session::table();
825
826 if (!$conn) {
827 return false;
828 }
829
830 assert('$table');
831
832 $time = time();
833
834 $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
835
836 if ($expire_notify) {
837 reset($expire_notify);
838 $fn = next($expire_notify);
839 $savem = $conn->SetFetchMode(ADODB_FETCH_NUM);
840 $sql = "SELECT expireref, sesskey FROM $table WHERE expiry < $time";
841 $rs =& $conn->Execute($sql);
842 ADODB_Session::_dumprs($rs);
843 $conn->SetFetchMode($savem);
844 if ($rs) {
845 $conn->BeginTrans();
846 $keys = array();
847 while (!$rs->EOF) {
848 $ref = $rs->fields[0];
849 $key = $rs->fields[1];
850 $fn($ref, $key);
851 $del = $conn->Execute("DELETE FROM $table WHERE sesskey='$key'");
852 $rs->MoveNext();
853 }
854 $rs->Close();
855
856 $conn->CommitTrans();
857 }
858 } else {
859
860 if (1) {
861 $sql = "SELECT sesskey FROM $table WHERE expiry < $time";
862 $arr =& $conn->GetAll($sql);
863 foreach ($arr as $row) {
864 $sql2 = "DELETE FROM $table WHERE sesskey='$row[0]'";
865 $conn->Execute($sql2);
866 }
867 } else {
868 $sql = "DELETE FROM $table WHERE expiry < $time";
869 $rs =& $conn->Execute($sql);
870 ADODB_Session::_dumprs($rs);
871 if ($rs) $rs->Close();
872 }
873 if ($debug) {
874 ADOConnection::outp("<p><b>Garbage Collection</b>: $sql</p>");
875 }
876 }
877
878 // suggested by Cameron, "GaM3R" <gamr@outworld.cx>
879 if ($optimize) {
880 $driver = ADODB_Session::driver();
881
882 if (preg_match('/mysql/i', $driver)) {
883 $sql = "OPTIMIZE TABLE $table";
884 }
885 if (preg_match('/postgres/i', $driver)) {
886 $sql = "VACUUM $table";
887 }
888 if (!empty($sql)) {
889 $conn->Execute($sql);
890 }
891 }
892
893 if ($sync_seconds) {
894 $sql = 'SELECT ';
895 if ($conn->dataProvider === 'oci8') {
896 $sql .= "TO_CHAR({$conn->sysTimeStamp}, 'RRRR-MM-DD HH24:MI:SS')";
897 } else {
898 $sql .= $conn->sysTimeStamp;
899 }
900 $sql .= " FROM $table";
901
902 $rs =& $conn->SelectLimit($sql, 1);
903 if ($rs && !$rs->EOF) {
904 $dbts = reset($rs->fields);
905 $rs->Close();
906 $dbt = $conn->UnixTimeStamp($dbts);
907 $t = time();
908
909 if (abs($dbt - $t) >= $sync_seconds) {
910 $msg = __FILE__ .
911 ": Server time for webserver {$_SERVER['HTTP_HOST']} not in synch with database: " .
912 " database=$dbt ($dbts), webserver=$t (diff=". (abs($dbt - $t) / 60) . ' minutes)';
913 error_log($msg);
914 if ($debug) {
915 ADOConnection::outp("<p>$msg</p>");
916 }
917 }
918 }
919 }
920
921 return true;
922 }
923 }
924
925 ADODB_Session::_init();
926 if (empty($ADODB_SESSION_READONLY))
927 register_shutdown_function('session_write_close');
928
929 // for backwards compatability only
930 function adodb_sess_open($save_path, $session_name, $persist = true) {
931 return ADODB_Session::open($save_path, $session_name, $persist);
932 }
933
934 // for backwards compatability only
935 function adodb_sess_gc($t)
936 {
937 return ADODB_Session::gc($t);
938 }
939
940 ?>