PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/adodb/session/adodb-session.php

https://github.com/reinhardsual/openemr
PHP | 817 lines | 553 code | 132 blank | 132 comment | 102 complexity | da05bc3d5b1aea033d9f54545690bbaa MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0
  1. <?php
  2. // $CVSHeader$
  3. /*
  4. V4.01 23 Oct 2003 (c) 2000-2004 John Lim (jlim@natsoft.com.my). All rights reserved.
  5. Contributed by Ross Smith (adodb@netebb.com).
  6. Released under both BSD license and Lesser GPL library license.
  7. Whenever there is any discrepancy between the two licenses,
  8. the BSD license will take precedence.
  9. Set tabs to 4 for best viewing.
  10. */
  11. /*
  12. You may want to rename the 'data' field to 'session_data' as
  13. 'data' appears to be a reserved word for one or more of the following:
  14. ANSI SQL
  15. IBM DB2
  16. MS SQL Server
  17. Postgres
  18. SAP
  19. If you do, then execute:
  20. ADODB_Session::dataFieldName('session_data');
  21. */
  22. if (!defined('_ADODB_LAYER')) {
  23. require_once realpath(dirname(__FILE__) . '/../adodb.inc.php');
  24. }
  25. if (defined('ADODB_SESSION')) return 1;
  26. define('ADODB_SESSION', dirname(__FILE__));
  27. /*!
  28. \static
  29. */
  30. class ADODB_Session {
  31. /////////////////////
  32. // getter/setter methods
  33. /////////////////////
  34. /*!
  35. */
  36. function driver($driver = null) {
  37. static $_driver = 'mysql';
  38. static $set = false;
  39. if (!is_null($driver)) {
  40. $_driver = trim($driver);
  41. $set = true;
  42. } elseif (!$set) {
  43. // backwards compatibility
  44. if (isset($GLOBALS['ADODB_SESSION_DRIVER'])) {
  45. return $GLOBALS['ADODB_SESSION_DRIVER'];
  46. }
  47. }
  48. return $_driver;
  49. }
  50. /*!
  51. */
  52. function host($host = null) {
  53. static $_host = 'localhost';
  54. static $set = false;
  55. if (!is_null($host)) {
  56. $_host = trim($host);
  57. $set = true;
  58. } elseif (!$set) {
  59. // backwards compatibility
  60. if (isset($GLOBALS['ADODB_SESSION_CONNECT'])) {
  61. return $GLOBALS['ADODB_SESSION_CONNECT'];
  62. }
  63. }
  64. return $_host;
  65. }
  66. /*!
  67. */
  68. function user($user = null) {
  69. static $_user = 'root';
  70. static $set = false;
  71. if (!is_null($user)) {
  72. $_user = trim($user);
  73. $set = true;
  74. } elseif (!$set) {
  75. // backwards compatibility
  76. if (isset($GLOBALS['ADODB_SESSION_USER'])) {
  77. return $GLOBALS['ADODB_SESSION_USER'];
  78. }
  79. }
  80. return $_user;
  81. }
  82. /*!
  83. */
  84. function password($password = null) {
  85. static $_password = '';
  86. static $set = false;
  87. if (!is_null($password)) {
  88. $_password = $password;
  89. $set = true;
  90. } elseif (!$set) {
  91. // backwards compatibility
  92. if (isset($GLOBALS['ADODB_SESSION_PWD'])) {
  93. return $GLOBALS['ADODB_SESSION_PWD'];
  94. }
  95. }
  96. return $_password;
  97. }
  98. /*!
  99. */
  100. function database($database = null) {
  101. static $_database = 'xphplens_2';
  102. static $set = false;
  103. if (!is_null($database)) {
  104. $_database = trim($database);
  105. $set = true;
  106. } elseif (!$set) {
  107. // backwards compatibility
  108. if (isset($GLOBALS['ADODB_SESSION_DB'])) {
  109. return $GLOBALS['ADODB_SESSION_DB'];
  110. }
  111. }
  112. return $_database;
  113. }
  114. /*!
  115. */
  116. function persist($persist = null) {
  117. static $_persist = true;
  118. if (!is_null($persist)) {
  119. $_persist = trim($persist);
  120. }
  121. return $_persist;
  122. }
  123. /*!
  124. */
  125. function lifetime($lifetime = null) {
  126. static $_lifetime;
  127. static $set = false;
  128. if (!is_null($lifetime)) {
  129. $_lifetime = (int) $lifetime;
  130. $set = true;
  131. } elseif (!$set) {
  132. // backwards compatibility
  133. if (isset($GLOBALS['ADODB_SESS_LIFE'])) {
  134. return $GLOBALS['ADODB_SESS_LIFE'];
  135. }
  136. }
  137. if (!$_lifetime) {
  138. $_lifetime = ini_get('session.gc_maxlifetime');
  139. if ($_lifetime <= 1) {
  140. // bug in PHP 4.0.3 pl 1 -- how about other versions?
  141. //print "<h3>Session Error: PHP.INI setting <i>session.gc_maxlifetime</i>not set: $lifetime</h3>";
  142. $_lifetime = 1440;
  143. }
  144. }
  145. return $_lifetime;
  146. }
  147. /*!
  148. */
  149. function debug($debug = null) {
  150. static $_debug = false;
  151. static $set = false;
  152. if (!is_null($debug)) {
  153. $_debug = (bool) $debug;
  154. $conn = ADODB_Session::_conn();
  155. if ($conn) {
  156. $conn->debug = $_debug;
  157. }
  158. $set = true;
  159. } elseif (!$set) {
  160. // backwards compatibility
  161. if (isset($GLOBALS['ADODB_SESS_DEBUG'])) {
  162. return $GLOBALS['ADODB_SESS_DEBUG'];
  163. }
  164. }
  165. return $_debug;
  166. }
  167. /*!
  168. */
  169. function expireNotify($expire_notify = null) {
  170. static $_expire_notify;
  171. static $set = false;
  172. if (!is_null($expire_notify)) {
  173. $_expire_notify = $expire_notify;
  174. $set = true;
  175. } elseif (!$set) {
  176. // backwards compatibility
  177. if (isset($GLOBALS['ADODB_SESSION_EXPIRE_NOTIFY'])) {
  178. return $GLOBALS['ADODB_SESSION_EXPIRE_NOTIFY'];
  179. }
  180. }
  181. return $_expire_notify;
  182. }
  183. /*!
  184. */
  185. function table($table = null) {
  186. static $_table = 'sessions';
  187. static $set = false;
  188. if (!is_null($table)) {
  189. $_table = trim($table);
  190. $set = true;
  191. } elseif (!$set) {
  192. // backwards compatibility
  193. if (isset($GLOBALS['ADODB_SESSION_TBL'])) {
  194. return $GLOBALS['ADODB_SESSION_TBL'];
  195. }
  196. }
  197. return $_table;
  198. }
  199. /*!
  200. */
  201. function optimize($optimize = null) {
  202. static $_optimize = false;
  203. static $set = false;
  204. if (!is_null($optimize)) {
  205. $_optimize = (bool) $optimize;
  206. $set = true;
  207. } elseif (!$set) {
  208. // backwards compatibility
  209. if (defined('ADODB_SESSION_OPTIMIZE')) {
  210. return true;
  211. }
  212. }
  213. return $_optimize;
  214. }
  215. /*!
  216. */
  217. function syncSeconds($sync_seconds = null) {
  218. static $_sync_seconds = 60;
  219. static $set = false;
  220. if (!is_null($sync_seconds)) {
  221. $_sync_seconds = (int) $sync_seconds;
  222. $set = true;
  223. } elseif (!$set) {
  224. // backwards compatibility
  225. if (defined('ADODB_SESSION_SYNCH_SECS')) {
  226. return ADODB_SESSION_SYNCH_SECS;
  227. }
  228. }
  229. return $_sync_seconds;
  230. }
  231. /*!
  232. */
  233. function clob($clob = null) {
  234. static $_clob = false;
  235. static $set = false;
  236. if (!is_null($clob)) {
  237. $_clob = strtolower(trim($clob));
  238. $set = true;
  239. } elseif (!$set) {
  240. // backwards compatibility
  241. if (isset($GLOBALS['ADODB_SESSION_USE_LOBS'])) {
  242. return $GLOBALS['ADODB_SESSION_USE_LOBS'];
  243. }
  244. }
  245. return $_clob;
  246. }
  247. /*!
  248. */
  249. function dataFieldName($data_field_name = null) {
  250. static $_data_field_name = 'data';
  251. if (!is_null($data_field_name)) {
  252. $_data_field_name = trim($data_field_name);
  253. }
  254. return $_data_field_name;
  255. }
  256. /*!
  257. */
  258. function filter($filter = null) {
  259. static $_filter = array();
  260. if (!is_null($filter)) {
  261. if (!is_array($filter)) {
  262. $filter = array($filter);
  263. }
  264. $_filter = $filter;
  265. }
  266. return $_filter;
  267. }
  268. /*!
  269. */
  270. function encryptionKey($encryption_key = null) {
  271. static $_encryption_key = 'CRYPTED ADODB SESSIONS ROCK!';
  272. if (!is_null($encryption_key)) {
  273. $_encryption_key = $encryption_key;
  274. }
  275. return $_encryption_key;
  276. }
  277. /////////////////////
  278. // private methods
  279. /////////////////////
  280. /*!
  281. */
  282. function &_conn($conn=null) {
  283. return $GLOBALS['ADODB_SESS_CONN'];
  284. }
  285. /*!
  286. */
  287. function _crc($crc = null) {
  288. static $_crc = false;
  289. if (!is_null($crc)) {
  290. $_crc = $crc;
  291. }
  292. return $_crc;
  293. }
  294. /*!
  295. */
  296. function _init() {
  297. session_module_name('user');
  298. session_set_save_handler(
  299. array('ADODB_Session', 'open'),
  300. array('ADODB_Session', 'close'),
  301. array('ADODB_Session', 'read'),
  302. array('ADODB_Session', 'write'),
  303. array('ADODB_Session', 'destroy'),
  304. array('ADODB_Session', 'gc')
  305. );
  306. }
  307. /*!
  308. */
  309. function _sessionKey() {
  310. // use this function to create the encryption key for crypted sessions
  311. // crypt the used key, ADODB_Session::encryptionKey() as key and session_id() as salt
  312. return crypt(ADODB_Session::encryptionKey(), session_id());
  313. }
  314. /*!
  315. */
  316. function _dumprs($rs) {
  317. $conn =& ADODB_Session::_conn();
  318. $debug = ADODB_Session::debug();
  319. if (!$conn) {
  320. return;
  321. }
  322. if (!$debug) {
  323. return;
  324. }
  325. if (!$rs) {
  326. echo "<br />\$rs is null or false<br />\n";
  327. return;
  328. }
  329. //echo "<br />\nAffected_Rows=",$conn->Affected_Rows(),"<br />\n";
  330. if (!is_object($rs)) {
  331. return;
  332. }
  333. require_once ADODB_SESSION.'/../tohtml.inc.php';
  334. rs2html($rs);
  335. }
  336. /////////////////////
  337. // public methods
  338. /////////////////////
  339. /*!
  340. Create the connection to the database.
  341. If $conn already exists, reuse that connection
  342. */
  343. function open($save_path, $session_name, $persist = null) {
  344. $conn =& ADODB_Session::_conn();
  345. if ($conn) {
  346. return true;
  347. }
  348. $database = ADODB_Session::database();
  349. $debug = ADODB_Session::debug();
  350. $driver = ADODB_Session::driver();
  351. $host = ADODB_Session::host();
  352. $password = ADODB_Session::password();
  353. $user = ADODB_Session::user();
  354. if (!is_null($persist)) {
  355. $persist = (bool) $persist;
  356. ADODB_Session::persist($persist);
  357. } else {
  358. $persist = ADODB_Session::persist();
  359. }
  360. # these can all be defaulted to in php.ini
  361. # assert('$database');
  362. # assert('$driver');
  363. # assert('$host');
  364. // cannot use =& below - do not know why...
  365. $conn = ADONewConnection($driver);
  366. if ($debug) {
  367. $conn->debug = true;
  368. // ADOConnection::outp( " driver=$driver user=$user pwd=$password db=$database ");
  369. }
  370. if ($persist) {
  371. $ok = $conn->PConnect($host, $user, $password, $database);
  372. } else {
  373. $ok = $conn->Connect($host, $user, $password, $database);
  374. }
  375. if ($ok) $GLOBALS['ADODB_SESS_CONN'] =& $conn;
  376. else
  377. ADOConnection::outp('<p>Session: connection failed</p>', false);
  378. return $ok;
  379. }
  380. /*!
  381. Close the connection
  382. */
  383. function close() {
  384. $conn =& ADODB_Session::_conn();
  385. if ($conn) {
  386. $conn->Close();
  387. }
  388. return true;
  389. }
  390. /*
  391. Slurp in the session variables and return the serialized string
  392. */
  393. function read($key) {
  394. $conn =& ADODB_Session::_conn();
  395. $data = ADODB_Session::dataFieldName();
  396. $filter = ADODB_Session::filter();
  397. $table = ADODB_Session::table();
  398. if (!$conn) {
  399. return '';
  400. }
  401. assert('$table');
  402. $qkey = $conn->quote($key);
  403. $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
  404. $sql = "SELECT $data FROM $table WHERE $binary sesskey = $qkey AND expiry >= " . time();
  405. $rs =& $conn->Execute($sql);
  406. //ADODB_Session::_dumprs($rs);
  407. if ($rs) {
  408. if ($rs->EOF) {
  409. $v = '';
  410. } else {
  411. $v = reset($rs->fields);
  412. $filter = array_reverse($filter);
  413. foreach ($filter as $f) {
  414. if (is_object($f)) {
  415. $v = $f->read($v, ADODB_Session::_sessionKey());
  416. }
  417. }
  418. $v = rawurldecode($v);
  419. }
  420. $rs->Close();
  421. ADODB_Session::_crc(strlen($v) . crc32($v));
  422. return $v;
  423. }
  424. return '';
  425. }
  426. /*!
  427. Write the serialized data to a database.
  428. If the data has not been modified since the last read(), we do not write.
  429. */
  430. function write($key, $val) {
  431. $clob = ADODB_Session::clob();
  432. $conn =& ADODB_Session::_conn();
  433. $crc = ADODB_Session::_crc();
  434. $data = ADODB_Session::dataFieldName();
  435. $debug = ADODB_Session::debug();
  436. $driver = ADODB_Session::driver();
  437. $expire_notify = ADODB_Session::expireNotify();
  438. $filter = ADODB_Session::filter();
  439. $lifetime = ADODB_Session::lifetime();
  440. $table = ADODB_Session::table();
  441. if (!$conn) {
  442. return false;
  443. }
  444. assert('$table');
  445. $expiry = time() + $lifetime;
  446. $qkey = $conn->quote($key);
  447. $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
  448. // crc32 optimization since adodb 2.1
  449. // now we only update expiry date, thx to sebastian thom in adodb 2.32
  450. if ($crc !== false && $crc == (strlen($val) . crc32($val))) {
  451. if ($debug) {
  452. echo '<p>Session: Only updating date - crc32 not changed</p>';
  453. }
  454. $sql = "UPDATE $table SET expiry = $expiry WHERE $binary sesskey = $qkey AND expiry >= " . time();
  455. $rs =& $conn->Execute($sql);
  456. ADODB_Session::_dumprs($rs);
  457. if ($rs) {
  458. $rs->Close();
  459. }
  460. return true;
  461. }
  462. $val = rawurlencode($val);
  463. foreach ($filter as $f) {
  464. if (is_object($f)) {
  465. $val = $f->write($val, ADODB_Session::_sessionKey());
  466. }
  467. }
  468. $arr = array('sesskey' => $key, 'expiry' => $expiry, $data => $val, 'expireref' => '');
  469. if ($expire_notify) {
  470. $var = reset($expire_notify);
  471. global $$var;
  472. if (isset($$var)) {
  473. $arr['expireref'] = $$var;
  474. }
  475. }
  476. if (!$clob) { // no lobs, simply use replace()
  477. $rs = $conn->Replace($table, $arr, 'sesskey', $autoQuote = true);
  478. ADODB_Session::_dumprs($rs);
  479. } else {
  480. // what value shall we insert/update for lob row?
  481. switch ($driver) {
  482. // empty_clob or empty_lob for oracle dbs
  483. case 'oracle':
  484. case 'oci8':
  485. case 'oci8po':
  486. case 'oci805':
  487. $lob_value = sprintf('empty_%s()', strtolower($clob));
  488. break;
  489. // null for all other
  490. default:
  491. $lob_value = 'null';
  492. break;
  493. }
  494. // do we insert or update? => as for sesskey
  495. $rs =& $conn->Execute("SELECT COUNT(*) AS cnt FROM $table WHERE $binary sesskey = $qkey");
  496. ADODB_Session::_dumprs($rs);
  497. if ($rs && reset($rs->fields) > 0) {
  498. $sql = "UPDATE $table SET expiry = $expiry, $data = $lob_value WHERE sesskey = $qkey";
  499. } else {
  500. $sql = "INSERT INTO $table (expiry, $data, sesskey) VALUES ($expiry, $lob_value, $qkey)";
  501. }
  502. if ($rs) {
  503. $rs->Close();
  504. }
  505. $err = '';
  506. $rs1 =& $conn->Execute($sql);
  507. ADODB_Session::_dumprs($rs1);
  508. if (!$rs1) {
  509. $err = $conn->ErrorMsg()."\n";
  510. }
  511. $rs2 =& $conn->UpdateBlob($table, $data, $val, " sesskey=$qkey", strtoupper($clob));
  512. ADODB_Session::_dumprs($rs2);
  513. if (!$rs2) {
  514. $err .= $conn->ErrorMsg()."\n";
  515. }
  516. $rs = ($rs && $rs2) ? true : false;
  517. if ($rs1) {
  518. $rs1->Close();
  519. }
  520. if (is_object($rs2)) {
  521. $rs2->Close();
  522. }
  523. }
  524. if (!$rs) {
  525. ADOConnection::outp('<p>Session Replace: ' . $conn->ErrorMsg() . '</p>', false);
  526. return false;
  527. } else {
  528. // bug in access driver (could be odbc?) means that info is not committed
  529. // properly unless select statement executed in Win2000
  530. if ($conn->databaseType == 'access') {
  531. $sql = "SELECT sesskey FROM $table WHERE $binary sesskey = $qkey";
  532. $rs =& $conn->Execute($sql);
  533. ADODB_Session::_dumprs($rs);
  534. if ($rs) {
  535. $rs->Close();
  536. }
  537. }
  538. }
  539. return $rs ? true : false;
  540. }
  541. /*!
  542. */
  543. function destroy($key) {
  544. $conn =& ADODB_Session::_conn();
  545. $table = ADODB_Session::table();
  546. $expire_notify = ADODB_Session::expireNotify();
  547. if (!$conn) {
  548. return false;
  549. }
  550. assert('$table');
  551. $qkey = $conn->quote($key);
  552. $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
  553. if ($expire_notify) {
  554. reset($expire_notify);
  555. $fn = next($expire_notify);
  556. $savem = $conn->SetFetchMode(ADODB_FETCH_NUM);
  557. $sql = "SELECT expireref, sesskey FROM $table WHERE $binary sesskey = $qkey";
  558. $rs =& $conn->Execute($sql);
  559. ADODB_Session::_dumprs($rs);
  560. $conn->SetFetchMode($savem);
  561. if (!$rs) {
  562. return false;
  563. }
  564. if (!$rs->EOF) {
  565. $ref = $rs->fields[0];
  566. $key = $rs->fields[1];
  567. assert('$ref');
  568. assert('$key');
  569. $fn($ref, $key);
  570. }
  571. $rs->Close();
  572. }
  573. $sql = "DELETE FROM $table WHERE $binary sesskey = $qkey";
  574. $rs =& $conn->Execute($sql);
  575. ADODB_Session::_dumprs($rs);
  576. if ($rs) {
  577. $rs->Close();
  578. }
  579. return $rs ? true : false;
  580. }
  581. /*!
  582. */
  583. function gc($maxlifetime) {
  584. $conn =& ADODB_Session::_conn();
  585. $debug = ADODB_Session::debug();
  586. $expire_notify = ADODB_Session::expireNotify();
  587. $optimize = ADODB_Session::optimize();
  588. $sync_seconds = ADODB_Session::syncSeconds();
  589. $table = ADODB_Session::table();
  590. if (!$conn) {
  591. return false;
  592. }
  593. assert('$table');
  594. $time = time();
  595. $binary = $conn->dataProvider === 'mysql' ? '/*! BINARY */' : '';
  596. if ($expire_notify) {
  597. reset($expire_notify);
  598. $fn = next($expire_notify);
  599. $savem = $conn->SetFetchMode(ADODB_FETCH_NUM);
  600. $sql = "SELECT expireref, sesskey FROM $table WHERE expiry < $time";
  601. $rs =& $conn->Execute($sql);
  602. ADODB_Session::_dumprs($rs);
  603. $conn->SetFetchMode($savem);
  604. if ($rs) {
  605. $conn->BeginTrans();
  606. $keys = array();
  607. while (!$rs->EOF) {
  608. $ref = $rs->fields[0];
  609. $key = $rs->fields[1];
  610. $fn($ref, $key);
  611. $del = $conn->Execute("DELETE FROM $table WHERE sesskey='$key'");
  612. $rs->MoveNext();
  613. }
  614. $rs->Close();
  615. $conn->CommitTrans();
  616. }
  617. } else {
  618. $sql = "DELETE FROM $table WHERE expiry < $time";
  619. $rs =& $conn->Execute($sql);
  620. ADODB_Session::_dumprs($rs);
  621. if ($rs) {
  622. $rs->Close();
  623. }
  624. if ($debug) {
  625. ADOConnection::outp("<p><b>Garbage Collection</b>: $sql</p>");
  626. }
  627. }
  628. // suggested by Cameron, "GaM3R" <gamr@outworld.cx>
  629. if ($optimize) {
  630. $driver = ADODB_Session::driver();
  631. if (preg_match('/mysql/i', $driver)) {
  632. $sql = "OPTIMIZE TABLE $table";
  633. }
  634. if (preg_match('/postgres/i', $driver)) {
  635. $sql = "VACUUM $table";
  636. }
  637. if (!empty($sql)) {
  638. $conn->Execute($sql);
  639. }
  640. }
  641. if ($sync_seconds) {
  642. $sql = 'SELECT ';
  643. if ($conn->dataProvider === 'oci8') {
  644. $sql .= "TO_CHAR({$conn->sysTimeStamp}, 'RRRR-MM-DD HH24:MI:SS')";
  645. } else {
  646. $sql .= $conn->sysTimeStamp;
  647. }
  648. $sql .= " FROM $table";
  649. $rs =& $conn->SelectLimit($sql, 1);
  650. if ($rs && !$rs->EOF) {
  651. $dbts = reset($rs->fields);
  652. $rs->Close();
  653. $dbt = $conn->UnixTimeStamp($dbts);
  654. $t = time();
  655. if (abs($dbt - $t) >= $sync_seconds) {
  656. global $HTTP_SERVER_VARS;
  657. $msg = __FILE__ .
  658. ": Server time for webserver {$HTTP_SERVER_VARS['HTTP_HOST']} not in synch with database: " .
  659. " database=$dbt ($dbts), webserver=$t (diff=". (abs($dbt - $t) / 3600) . ' hours)';
  660. error_log($msg);
  661. if ($debug) {
  662. ADOConnection::outp("<p>$msg</p>");
  663. }
  664. }
  665. }
  666. }
  667. return true;
  668. }
  669. }
  670. ADODB_Session::_init();
  671. // for backwards compatability only
  672. function adodb_sess_open($save_path, $session_name, $persist = true) {
  673. return ADODB_Session::open($save_path, $session_name, $persist);
  674. }
  675. // for backwards compatability only
  676. function adodb_sess_gc($t)
  677. {
  678. return ADODB_Session::gc($t);
  679. }
  680. ?>