PageRenderTime 55ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/seanom/concrete5
PHP | 945 lines | 583 code | 151 blank | 211 comment | 119 complexity | 4512c892768804efdcf2fbe43ae4d418 MD5 | raw file
  1. <?php
  2. /*
  3. V5.10 10 Nov 2009 (c) 2000-2009 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. 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. static 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. static 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. static 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. static 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. static 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. static 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. static 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. static 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. static 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. static 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. static 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. static 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. static 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. static 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. static 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. static 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. static function _conn($conn=null) {
  368. return isset($GLOBALS['ADODB_SESS_CONN']) ? $GLOBALS['ADODB_SESS_CONN'] : false;
  369. }
  370. /*!
  371. */
  372. static function _crc($crc = null) {
  373. static $_crc = false;
  374. if (!is_null($crc)) {
  375. $_crc = $crc;
  376. }
  377. return $_crc;
  378. }
  379. /*!
  380. */
  381. static function _init() {
  382. session_module_name('user');
  383. session_set_save_handler(
  384. array('ADODB_Session', 'open'),
  385. array('ADODB_Session', 'close'),
  386. array('ADODB_Session', 'read'),
  387. array('ADODB_Session', 'write'),
  388. array('ADODB_Session', 'destroy'),
  389. array('ADODB_Session', 'gc')
  390. );
  391. }
  392. /*!
  393. */
  394. static function _sessionKey() {
  395. // use this function to create the encryption key for crypted sessions
  396. // crypt the used key, ADODB_Session::encryptionKey() as key and session_id() as salt
  397. return crypt(ADODB_Session::encryptionKey(), session_id());
  398. }
  399. /*!
  400. */
  401. static function _dumprs(&$rs) {
  402. $conn = ADODB_Session::_conn();
  403. $debug = ADODB_Session::debug();
  404. if (!$conn) {
  405. return;
  406. }
  407. if (!$debug) {
  408. return;
  409. }
  410. if (!$rs) {
  411. echo "<br />\$rs is null or false<br />\n";
  412. return;
  413. }
  414. //echo "<br />\nAffected_Rows=",$conn->Affected_Rows(),"<br />\n";
  415. if (!is_object($rs)) {
  416. return;
  417. }
  418. $rs = $conn->_rs2rs($rs);
  419. require_once ADODB_SESSION.'/../tohtml.inc.php';
  420. rs2html($rs);
  421. $rs->MoveFirst();
  422. }
  423. /////////////////////
  424. // public methods
  425. /////////////////////
  426. static function config($driver, $host, $user, $password, $database=false,$options=false)
  427. {
  428. ADODB_Session::driver($driver);
  429. ADODB_Session::host($host);
  430. ADODB_Session::user($user);
  431. ADODB_Session::password($password);
  432. ADODB_Session::database($database);
  433. if ($driver == 'oci8' || $driver == 'oci8po') $options['lob'] = 'CLOB';
  434. if (isset($options['table'])) ADODB_Session::table($options['table']);
  435. if (isset($options['lob'])) ADODB_Session::clob($options['lob']);
  436. if (isset($options['debug'])) ADODB_Session::debug($options['debug']);
  437. }
  438. /*!
  439. Create the connection to the database.
  440. If $conn already exists, reuse that connection
  441. */
  442. static function open($save_path, $session_name, $persist = null)
  443. {
  444. $conn = ADODB_Session::_conn();
  445. if ($conn) {
  446. return true;
  447. }
  448. $database = ADODB_Session::database();
  449. $debug = ADODB_Session::debug();
  450. $driver = ADODB_Session::driver();
  451. $host = ADODB_Session::host();
  452. $password = ADODB_Session::password();
  453. $user = ADODB_Session::user();
  454. if (!is_null($persist)) {
  455. ADODB_Session::persist($persist);
  456. } else {
  457. $persist = ADODB_Session::persist();
  458. }
  459. # these can all be defaulted to in php.ini
  460. # assert('$database');
  461. # assert('$driver');
  462. # assert('$host');
  463. $conn = ADONewConnection($driver);
  464. if ($debug) {
  465. $conn->debug = true;
  466. ADOConnection::outp( " driver=$driver user=$user db=$database ");
  467. }
  468. if ($persist) {
  469. switch($persist) {
  470. default:
  471. case 'P': $ok = $conn->PConnect($host, $user, $password, $database); break;
  472. case 'C': $ok = $conn->Connect($host, $user, $password, $database); break;
  473. case 'N': $ok = $conn->NConnect($host, $user, $password, $database); break;
  474. }
  475. } else {
  476. $ok = $conn->Connect($host, $user, $password, $database);
  477. }
  478. if ($ok) $GLOBALS['ADODB_SESS_CONN'] = $conn;
  479. else
  480. ADOConnection::outp('<p>Session: connection failed</p>', false);
  481. return $ok;
  482. }
  483. /*!
  484. Close the connection
  485. */
  486. static function close()
  487. {
  488. /*
  489. $conn = ADODB_Session::_conn();
  490. if ($conn) $conn->Close();
  491. */
  492. return true;
  493. }
  494. /*
  495. Slurp in the session variables and return the serialized string
  496. */
  497. static function read($key)
  498. {
  499. $conn = ADODB_Session::_conn();
  500. $filter = ADODB_Session::filter();
  501. $table = ADODB_Session::table();
  502. if (!$conn) {
  503. return '';
  504. }
  505. //assert('$table');
  506. $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
  507. $sql = "SELECT sessdata FROM $table WHERE sesskey = $binary ".$conn->Param(0)." AND expiry >= " . $conn->sysTimeStamp;
  508. /* Lock code does not work as it needs to hold transaction within whole page, and we don't know if
  509. developer has commited elsewhere... :(
  510. */
  511. #if (ADODB_Session::Lock())
  512. # $rs = $conn->RowLock($table, "$binary sesskey = $qkey AND expiry >= " . time(), sessdata);
  513. #else
  514. $rs = $conn->Execute($sql, array($key));
  515. //ADODB_Session::_dumprs($rs);
  516. if ($rs) {
  517. if ($rs->EOF) {
  518. $v = '';
  519. } else {
  520. $v = reset($rs->fields);
  521. $filter = array_reverse($filter);
  522. foreach ($filter as $f) {
  523. if (is_object($f)) {
  524. $v = $f->read($v, ADODB_Session::_sessionKey());
  525. }
  526. }
  527. $v = rawurldecode($v);
  528. }
  529. $rs->Close();
  530. ADODB_Session::_crc(strlen($v) . crc32($v));
  531. return $v;
  532. }
  533. return '';
  534. }
  535. /*!
  536. Write the serialized data to a database.
  537. If the data has not been modified since the last read(), we do not write.
  538. */
  539. static function write($key, $oval)
  540. {
  541. global $ADODB_SESSION_READONLY;
  542. if (!empty($ADODB_SESSION_READONLY)) return;
  543. $clob = ADODB_Session::clob();
  544. $conn = ADODB_Session::_conn();
  545. $crc = ADODB_Session::_crc();
  546. $debug = ADODB_Session::debug();
  547. $driver = ADODB_Session::driver();
  548. $expire_notify = ADODB_Session::expireNotify();
  549. $filter = ADODB_Session::filter();
  550. $lifetime = ADODB_Session::lifetime();
  551. $table = ADODB_Session::table();
  552. if (!$conn) {
  553. return false;
  554. }
  555. if ($debug) $conn->debug = 1;
  556. $sysTimeStamp = $conn->sysTimeStamp;
  557. //assert('$table');
  558. $expiry = $conn->OffsetDate($lifetime/(24*3600),$sysTimeStamp);
  559. $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
  560. // crc32 optimization since adodb 2.1
  561. // now we only update expiry date, thx to sebastian thom in adodb 2.32
  562. if ($crc !== false && $crc == (strlen($oval) . crc32($oval))) {
  563. if ($debug) {
  564. echo '<p>Session: Only updating date - crc32 not changed</p>';
  565. }
  566. $expirevar = '';
  567. if ($expire_notify) {
  568. $var = reset($expire_notify);
  569. global $$var;
  570. if (isset($$var)) {
  571. $expirevar = $$var;
  572. }
  573. }
  574. $sql = "UPDATE $table SET expiry = $expiry ,expireref=".$conn->Param('0').", modified = $sysTimeStamp WHERE $binary sesskey = ".$conn->Param('1')." AND expiry >= $sysTimeStamp";
  575. $rs = $conn->Execute($sql,array($expirevar,$key));
  576. return true;
  577. }
  578. $val = rawurlencode($oval);
  579. foreach ($filter as $f) {
  580. if (is_object($f)) {
  581. $val = $f->write($val, ADODB_Session::_sessionKey());
  582. }
  583. }
  584. $expireref = '';
  585. if ($expire_notify) {
  586. $var = reset($expire_notify);
  587. global $$var;
  588. if (isset($$var)) {
  589. $expireref = $$var;
  590. }
  591. }
  592. if (!$clob) { // no lobs, simply use replace()
  593. $rs = $conn->Execute("SELECT COUNT(*) AS cnt FROM $table WHERE $binary sesskey = ".$conn->Param(0),array($key));
  594. if ($rs) $rs->Close();
  595. if ($rs && reset($rs->fields) > 0) {
  596. $sql = "UPDATE $table SET expiry=$expiry, sessdata=".$conn->Param(0).", expireref= ".$conn->Param(1).",modified=$sysTimeStamp WHERE sesskey = ".$conn->Param('2');
  597. } else {
  598. $sql = "INSERT INTO $table (expiry, sessdata, expireref, sesskey, created, modified)
  599. VALUES ($expiry,".$conn->Param('0').", ". $conn->Param('1').", ".$conn->Param('2').", $sysTimeStamp, $sysTimeStamp)";
  600. }
  601. $rs = $conn->Execute($sql,array($val,$expireref,$key));
  602. } else {
  603. // what value shall we insert/update for lob row?
  604. switch ($driver) {
  605. // empty_clob or empty_lob for oracle dbs
  606. case 'oracle':
  607. case 'oci8':
  608. case 'oci8po':
  609. case 'oci805':
  610. $lob_value = sprintf('empty_%s()', strtolower($clob));
  611. break;
  612. // null for all other
  613. default:
  614. $lob_value = 'null';
  615. break;
  616. }
  617. $conn->StartTrans();
  618. $rs = $conn->Execute("SELECT COUNT(*) AS cnt FROM $table WHERE $binary sesskey = ".$conn->Param(0),array($key));
  619. if ($rs) $rs->Close();
  620. if ($rs && reset($rs->fields) > 0) {
  621. $sql = "UPDATE $table SET expiry=$expiry, sessdata=$lob_value, expireref= ".$conn->Param(0).",modified=$sysTimeStamp WHERE sesskey = ".$conn->Param('1');
  622. } else {
  623. $sql = "INSERT INTO $table (expiry, sessdata, expireref, sesskey, created, modified)
  624. VALUES ($expiry,$lob_value, ". $conn->Param('0').", ".$conn->Param('1').", $sysTimeStamp, $sysTimeStamp)";
  625. }
  626. $rs = $conn->Execute($sql,array($expireref,$key));
  627. $qkey = $conn->qstr($key);
  628. $rs2 = $conn->UpdateBlob($table, 'sessdata', $val, " sesskey=$qkey", strtoupper($clob));
  629. if ($debug) echo "<hr>",htmlspecialchars($oval), "<hr>";
  630. $rs = @$conn->CompleteTrans();
  631. }
  632. if (!$rs) {
  633. ADOConnection::outp('<p>Session Replace: ' . $conn->ErrorMsg() . '</p>', false);
  634. return false;
  635. } else {
  636. // bug in access driver (could be odbc?) means that info is not committed
  637. // properly unless select statement executed in Win2000
  638. if ($conn->databaseType == 'access') {
  639. $sql = "SELECT sesskey FROM $table WHERE $binary sesskey = $qkey";
  640. $rs = $conn->Execute($sql);
  641. ADODB_Session::_dumprs($rs);
  642. if ($rs) {
  643. $rs->Close();
  644. }
  645. }
  646. }/*
  647. if (ADODB_Session::Lock()) {
  648. $conn->CommitTrans();
  649. }*/
  650. return $rs ? true : false;
  651. }
  652. /*!
  653. */
  654. static function destroy($key) {
  655. $conn = ADODB_Session::_conn();
  656. $table = ADODB_Session::table();
  657. $expire_notify = ADODB_Session::expireNotify();
  658. if (!$conn) {
  659. return false;
  660. }
  661. $debug = ADODB_Session::debug();
  662. if ($debug) $conn->debug = 1;
  663. //assert('$table');
  664. $qkey = $conn->quote($key);
  665. $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
  666. if ($expire_notify) {
  667. reset($expire_notify);
  668. $fn = next($expire_notify);
  669. $savem = $conn->SetFetchMode(ADODB_FETCH_NUM);
  670. $sql = "SELECT expireref, sesskey FROM $table WHERE $binary sesskey = $qkey";
  671. $rs = $conn->Execute($sql);
  672. ADODB_Session::_dumprs($rs);
  673. $conn->SetFetchMode($savem);
  674. if (!$rs) {
  675. return false;
  676. }
  677. if (!$rs->EOF) {
  678. $ref = $rs->fields[0];
  679. $key = $rs->fields[1];
  680. //assert('$ref');
  681. //assert('$key');
  682. $fn($ref, $key);
  683. }
  684. $rs->Close();
  685. }
  686. $sql = "DELETE FROM $table WHERE $binary sesskey = $qkey";
  687. $rs = $conn->Execute($sql);
  688. if ($rs) {
  689. $rs->Close();
  690. }
  691. return $rs ? true : false;
  692. }
  693. /*!
  694. */
  695. static function gc($maxlifetime)
  696. {
  697. $conn = ADODB_Session::_conn();
  698. $debug = ADODB_Session::debug();
  699. $expire_notify = ADODB_Session::expireNotify();
  700. $optimize = ADODB_Session::optimize();
  701. $table = ADODB_Session::table();
  702. if (!$conn) {
  703. return false;
  704. }
  705. $debug = ADODB_Session::debug();
  706. if ($debug) {
  707. $conn->debug = 1;
  708. $COMMITNUM = 2;
  709. } else {
  710. $COMMITNUM = 20;
  711. }
  712. //assert('$table');
  713. $time = $conn->OffsetDate(-$maxlifetime/24/3600,$conn->sysTimeStamp);
  714. $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
  715. if ($expire_notify) {
  716. reset($expire_notify);
  717. $fn = next($expire_notify);
  718. } else {
  719. $fn = false;
  720. }
  721. $savem = $conn->SetFetchMode(ADODB_FETCH_NUM);
  722. $sql = "SELECT expireref, sesskey FROM $table WHERE expiry < $time ORDER BY 2"; # add order by to prevent deadlock
  723. $rs = $conn->SelectLimit($sql,1000);
  724. ADODB_Session::_dumprs($rs);
  725. if ($debug) $conn->SetFetchMode($savem);
  726. if ($rs) {
  727. $tr = $conn->hasTransactions;
  728. if ($tr) $conn->BeginTrans();
  729. $keys = array();
  730. $ccnt = 0;
  731. while (!$rs->EOF) {
  732. $ref = $rs->fields[0];
  733. $key = $rs->fields[1];
  734. if ($fn) $fn($ref, $key);
  735. $del = $conn->Execute("DELETE FROM $table WHERE sesskey=".$conn->Param('0'),array($key));
  736. $rs->MoveNext();
  737. $ccnt += 1;
  738. if ($tr && $ccnt % $COMMITNUM == 0) {
  739. if ($debug) echo "Commit<br>\n";
  740. $conn->CommitTrans();
  741. $conn->BeginTrans();
  742. }
  743. }
  744. $rs->Close();
  745. if ($tr) $conn->CommitTrans();
  746. }
  747. // suggested by Cameron, "GaM3R" <gamr@outworld.cx>
  748. if ($optimize) {
  749. $driver = ADODB_Session::driver();
  750. if (preg_match('/mysql/i', $driver)) {
  751. $sql = "OPTIMIZE TABLE $table";
  752. }
  753. if (preg_match('/postgres/i', $driver)) {
  754. $sql = "VACUUM $table";
  755. }
  756. if (!empty($sql)) {
  757. $conn->Execute($sql);
  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. ?>