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

/lib/adodb/session/adodb-session2.php

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