PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/dotproject/lib/adodb/session/adodb-session.php

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