PageRenderTime 66ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/adodb/adodb-php/session/adodb-session2.php

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