PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/libraries/3rdparty/adodb/session/adodb-session.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 934 lines | 604 code | 150 blank | 180 comment | 119 complexity | 8100e3ca20cf10cb797533db4b2f47fb MD5 | raw file
  1. <?php
  2. /*
  3. V5.18 3 Sep 2012 (c) 2000-2012 John Lim (jlim#natsoft.com). 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. 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. function config($driver, $host, $user, $password, $database=false,$options=false)
  408. {
  409. ADODB_Session::driver($driver);
  410. ADODB_Session::host($host);
  411. ADODB_Session::user($user);
  412. ADODB_Session::password($password);
  413. ADODB_Session::database($database);
  414. if ($driver == 'oci8' || $driver == 'oci8po') $options['lob'] = 'CLOB';
  415. if (isset($options['table'])) ADODB_Session::table($options['table']);
  416. if (isset($options['lob'])) ADODB_Session::clob($options['lob']);
  417. if (isset($options['debug'])) ADODB_Session::debug($options['debug']);
  418. }
  419. /*!
  420. Create the connection to the database.
  421. If $conn already exists, reuse that connection
  422. */
  423. function open($save_path, $session_name, $persist = null)
  424. {
  425. $conn = ADODB_Session::_conn();
  426. if ($conn) {
  427. return true;
  428. }
  429. $database = ADODB_Session::database();
  430. $debug = ADODB_Session::debug();
  431. $driver = ADODB_Session::driver();
  432. $host = ADODB_Session::host();
  433. $password = ADODB_Session::password();
  434. $user = ADODB_Session::user();
  435. if (!is_null($persist)) {
  436. ADODB_Session::persist($persist);
  437. } else {
  438. $persist = ADODB_Session::persist();
  439. }
  440. # these can all be defaulted to in php.ini
  441. # assert('$database');
  442. # assert('$driver');
  443. # assert('$host');
  444. $conn = ADONewConnection($driver);
  445. if ($debug) {
  446. $conn->debug = true;
  447. // ADOConnection::outp( " driver=$driver user=$user pwd=$password db=$database ");
  448. }
  449. if ($persist) {
  450. switch($persist) {
  451. default:
  452. case 'P': $ok = $conn->PConnect($host, $user, $password, $database); break;
  453. case 'C': $ok = $conn->Connect($host, $user, $password, $database); break;
  454. case 'N': $ok = $conn->NConnect($host, $user, $password, $database); break;
  455. }
  456. } else {
  457. $ok = $conn->Connect($host, $user, $password, $database);
  458. }
  459. if ($ok) $GLOBALS['ADODB_SESS_CONN'] = $conn;
  460. else
  461. ADOConnection::outp('<p>Session: connection failed</p>', false);
  462. return $ok;
  463. }
  464. /*!
  465. Close the connection
  466. */
  467. function close()
  468. {
  469. /*
  470. $conn = ADODB_Session::_conn();
  471. if ($conn) $conn->Close();
  472. */
  473. return true;
  474. }
  475. /*
  476. Slurp in the session variables and return the serialized string
  477. */
  478. function read($key)
  479. {
  480. $conn = ADODB_Session::_conn();
  481. $data = ADODB_Session::dataFieldName();
  482. $filter = ADODB_Session::filter();
  483. $table = ADODB_Session::table();
  484. if (!$conn) {
  485. return '';
  486. }
  487. //assert('$table');
  488. $qkey = $conn->quote($key);
  489. $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
  490. $sql = "SELECT $data FROM $table WHERE sesskey = $binary $qkey AND expiry >= " . time();
  491. /* Lock code does not work as it needs to hold transaction within whole page, and we don't know if
  492. developer has commited elsewhere... :(
  493. */
  494. #if (ADODB_Session::Lock())
  495. # $rs = $conn->RowLock($table, "$binary sesskey = $qkey AND expiry >= " . time(), $data);
  496. #else
  497. $rs = $conn->Execute($sql);
  498. //ADODB_Session::_dumprs($rs);
  499. if ($rs) {
  500. if ($rs->EOF) {
  501. $v = '';
  502. } else {
  503. $v = reset($rs->fields);
  504. $filter = array_reverse($filter);
  505. foreach ($filter as $f) {
  506. if (is_object($f)) {
  507. $v = $f->read($v, ADODB_Session::_sessionKey());
  508. }
  509. }
  510. $v = rawurldecode($v);
  511. }
  512. $rs->Close();
  513. ADODB_Session::_crc(strlen($v) . crc32($v));
  514. return $v;
  515. }
  516. return '';
  517. }
  518. /*!
  519. Write the serialized data to a database.
  520. If the data has not been modified since the last read(), we do not write.
  521. */
  522. function write($key, $val)
  523. {
  524. global $ADODB_SESSION_READONLY;
  525. if (!empty($ADODB_SESSION_READONLY)) return;
  526. $clob = ADODB_Session::clob();
  527. $conn = ADODB_Session::_conn();
  528. $crc = ADODB_Session::_crc();
  529. $data = ADODB_Session::dataFieldName();
  530. $debug = ADODB_Session::debug();
  531. $driver = ADODB_Session::driver();
  532. $expire_notify = ADODB_Session::expireNotify();
  533. $filter = ADODB_Session::filter();
  534. $lifetime = ADODB_Session::lifetime();
  535. $table = ADODB_Session::table();
  536. if (!$conn) {
  537. return false;
  538. }
  539. $qkey = $conn->qstr($key);
  540. //assert('$table');
  541. $expiry = time() + $lifetime;
  542. $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
  543. // crc32 optimization since adodb 2.1
  544. // now we only update expiry date, thx to sebastian thom in adodb 2.32
  545. if ($crc !== false && $crc == (strlen($val) . crc32($val))) {
  546. if ($debug) {
  547. ADOConnection::outp( '<p>Session: Only updating date - crc32 not changed</p>');
  548. }
  549. $expirevar = '';
  550. if ($expire_notify) {
  551. $var = reset($expire_notify);
  552. global $$var;
  553. if (isset($$var)) {
  554. $expirevar = $$var;
  555. }
  556. }
  557. $sql = "UPDATE $table SET expiry = ".$conn->Param('0').",expireref=".$conn->Param('1')." WHERE $binary sesskey = ".$conn->Param('2')." AND expiry >= ".$conn->Param('3');
  558. $rs = $conn->Execute($sql,array($expiry,$expirevar,$key,time()));
  559. return true;
  560. }
  561. $val = rawurlencode($val);
  562. foreach ($filter as $f) {
  563. if (is_object($f)) {
  564. $val = $f->write($val, ADODB_Session::_sessionKey());
  565. }
  566. }
  567. $arr = array('sesskey' => $key, 'expiry' => $expiry, $data => $val, 'expireref' => '');
  568. if ($expire_notify) {
  569. $var = reset($expire_notify);
  570. global $$var;
  571. if (isset($$var)) {
  572. $arr['expireref'] = $$var;
  573. }
  574. }
  575. if (!$clob) { // no lobs, simply use replace()
  576. $arr[$data] = $val;
  577. $rs = $conn->Replace($table, $arr, 'sesskey', $autoQuote = true);
  578. } else {
  579. // what value shall we insert/update for lob row?
  580. switch ($driver) {
  581. // empty_clob or empty_lob for oracle dbs
  582. case 'oracle':
  583. case 'oci8':
  584. case 'oci8po':
  585. case 'oci805':
  586. $lob_value = sprintf('empty_%s()', strtolower($clob));
  587. break;
  588. // null for all other
  589. default:
  590. $lob_value = 'null';
  591. break;
  592. }
  593. $conn->StartTrans();
  594. $expiryref = $conn->qstr($arr['expireref']);
  595. // do we insert or update? => as for sesskey
  596. $rs = $conn->Execute("SELECT COUNT(*) AS cnt FROM $table WHERE $binary sesskey = $qkey");
  597. if ($rs && reset($rs->fields) > 0) {
  598. $sql = "UPDATE $table SET expiry = $expiry, $data = $lob_value, expireref=$expiryref WHERE sesskey = $qkey";
  599. } else {
  600. $sql = "INSERT INTO $table (expiry, $data, sesskey,expireref) VALUES ($expiry, $lob_value, $qkey,$expiryref)";
  601. }
  602. if ($rs)$rs->Close();
  603. $err = '';
  604. $rs1 = $conn->Execute($sql);
  605. if (!$rs1) $err = $conn->ErrorMsg()."\n";
  606. $rs2 = $conn->UpdateBlob($table, $data, $val, " sesskey=$qkey", strtoupper($clob));
  607. if (!$rs2) $err .= $conn->ErrorMsg()."\n";
  608. $rs = ($rs && $rs2) ? true : false;
  609. $conn->CompleteTrans();
  610. }
  611. if (!$rs) {
  612. ADOConnection::outp('<p>Session Replace: ' . $conn->ErrorMsg() . '</p>', false);
  613. return false;
  614. } else {
  615. // bug in access driver (could be odbc?) means that info is not committed
  616. // properly unless select statement executed in Win2000
  617. if ($conn->databaseType == 'access') {
  618. $sql = "SELECT sesskey FROM $table WHERE $binary sesskey = $qkey";
  619. $rs = $conn->Execute($sql);
  620. ADODB_Session::_dumprs($rs);
  621. if ($rs) {
  622. $rs->Close();
  623. }
  624. }
  625. }/*
  626. if (ADODB_Session::Lock()) {
  627. $conn->CommitTrans();
  628. }*/
  629. return $rs ? true : false;
  630. }
  631. /*!
  632. */
  633. function destroy($key) {
  634. $conn = ADODB_Session::_conn();
  635. $table = ADODB_Session::table();
  636. $expire_notify = ADODB_Session::expireNotify();
  637. if (!$conn) {
  638. return false;
  639. }
  640. //assert('$table');
  641. $qkey = $conn->quote($key);
  642. $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
  643. if ($expire_notify) {
  644. reset($expire_notify);
  645. $fn = next($expire_notify);
  646. $savem = $conn->SetFetchMode(ADODB_FETCH_NUM);
  647. $sql = "SELECT expireref, sesskey FROM $table WHERE $binary sesskey = $qkey";
  648. $rs = $conn->Execute($sql);
  649. ADODB_Session::_dumprs($rs);
  650. $conn->SetFetchMode($savem);
  651. if (!$rs) {
  652. return false;
  653. }
  654. if (!$rs->EOF) {
  655. $ref = $rs->fields[0];
  656. $key = $rs->fields[1];
  657. //assert('$ref');
  658. //assert('$key');
  659. $fn($ref, $key);
  660. }
  661. $rs->Close();
  662. }
  663. $sql = "DELETE FROM $table WHERE $binary sesskey = $qkey";
  664. $rs = $conn->Execute($sql);
  665. ADODB_Session::_dumprs($rs);
  666. return $rs ? true : false;
  667. }
  668. /*!
  669. */
  670. function gc($maxlifetime)
  671. {
  672. $conn = ADODB_Session::_conn();
  673. $debug = ADODB_Session::debug();
  674. $expire_notify = ADODB_Session::expireNotify();
  675. $optimize = ADODB_Session::optimize();
  676. $sync_seconds = ADODB_Session::syncSeconds();
  677. $table = ADODB_Session::table();
  678. if (!$conn) {
  679. return false;
  680. }
  681. $time = time();
  682. $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
  683. if ($expire_notify) {
  684. reset($expire_notify);
  685. $fn = next($expire_notify);
  686. $savem = $conn->SetFetchMode(ADODB_FETCH_NUM);
  687. $sql = "SELECT expireref, sesskey FROM $table WHERE expiry < $time";
  688. $rs = $conn->Execute($sql);
  689. ADODB_Session::_dumprs($rs);
  690. $conn->SetFetchMode($savem);
  691. if ($rs) {
  692. $conn->StartTrans();
  693. $keys = array();
  694. while (!$rs->EOF) {
  695. $ref = $rs->fields[0];
  696. $key = $rs->fields[1];
  697. $fn($ref, $key);
  698. $del = $conn->Execute("DELETE FROM $table WHERE sesskey=".$conn->Param('0'),array($key));
  699. $rs->MoveNext();
  700. }
  701. $rs->Close();
  702. $conn->CompleteTrans();
  703. }
  704. } else {
  705. if (1) {
  706. $sql = "SELECT sesskey FROM $table WHERE expiry < $time";
  707. $arr = $conn->GetAll($sql);
  708. foreach ($arr as $row) {
  709. $sql2 = "DELETE FROM $table WHERE sesskey=".$conn->Param('0');
  710. $conn->Execute($sql2,array(reset($row)));
  711. }
  712. } else {
  713. $sql = "DELETE FROM $table WHERE expiry < $time";
  714. $rs = $conn->Execute($sql);
  715. ADODB_Session::_dumprs($rs);
  716. if ($rs) $rs->Close();
  717. }
  718. if ($debug) {
  719. ADOConnection::outp("<p><b>Garbage Collection</b>: $sql</p>");
  720. }
  721. }
  722. // suggested by Cameron, "GaM3R" <gamr@outworld.cx>
  723. if ($optimize) {
  724. $driver = ADODB_Session::driver();
  725. if (preg_match('/mysql/i', $driver)) {
  726. $sql = "OPTIMIZE TABLE $table";
  727. }
  728. if (preg_match('/postgres/i', $driver)) {
  729. $sql = "VACUUM $table";
  730. }
  731. if (!empty($sql)) {
  732. $conn->Execute($sql);
  733. }
  734. }
  735. if ($sync_seconds) {
  736. $sql = 'SELECT ';
  737. if ($conn->dataProvider === 'oci8') {
  738. $sql .= "TO_CHAR({$conn->sysTimeStamp}, 'RRRR-MM-DD HH24:MI:SS')";
  739. } else {
  740. $sql .= $conn->sysTimeStamp;
  741. }
  742. $sql .= " FROM $table";
  743. $rs = $conn->SelectLimit($sql, 1);
  744. if ($rs && !$rs->EOF) {
  745. $dbts = reset($rs->fields);
  746. $rs->Close();
  747. $dbt = $conn->UnixTimeStamp($dbts);
  748. $t = time();
  749. if (abs($dbt - $t) >= $sync_seconds) {
  750. $msg = __FILE__ .
  751. ": Server time for webserver {$_SERVER['HTTP_HOST']} not in synch with database: " .
  752. " database=$dbt ($dbts), webserver=$t (diff=". (abs($dbt - $t) / 60) . ' minutes)';
  753. error_log($msg);
  754. if ($debug) {
  755. ADOConnection::outp("<p>$msg</p>");
  756. }
  757. }
  758. }
  759. }
  760. return true;
  761. }
  762. }
  763. ADODB_Session::_init();
  764. if (empty($ADODB_SESSION_READONLY))
  765. register_shutdown_function('session_write_close');
  766. // for backwards compatability only
  767. function adodb_sess_open($save_path, $session_name, $persist = true) {
  768. return ADODB_Session::open($save_path, $session_name, $persist);
  769. }
  770. // for backwards compatability only
  771. function adodb_sess_gc($t)
  772. {
  773. return ADODB_Session::gc($t);
  774. }
  775. ?>