PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/phpgwapi/inc/adodb/session/adodb-session.php

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