PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/php/extlib/adodb5/session/adodb-session.php

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