adodb-session2.php 21 KB

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