PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/php/extlib/adodb5/session/old/adodb-session-clob.php

https://github.com/usualoma/movabletype
PHP | 447 lines | 255 code | 50 blank | 142 comment | 55 complexity | ba1ffd8fd16f6df65eb8efc40d93ebc1 MD5 | raw file
  1. <?php
  2. /*
  3. @version v5.20.20 01-Feb-2021
  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. 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. Latest version is available at http://adodb.org/
  11. ======================================================================
  12. This file provides PHP4 session management using the ADODB database
  13. wrapper library, using Oracle CLOB's to store data. Contributed by achim.gosse@ddd.de.
  14. Example
  15. =======
  16. include('adodb.inc.php');
  17. include('adodb-session.php');
  18. session_start();
  19. session_register('AVAR');
  20. $_SESSION['AVAR'] += 1;
  21. print "
  22. -- \$_SESSION['AVAR']={$_SESSION['AVAR']}</p>";
  23. To force non-persistent connections, call adodb_session_open first before session_start():
  24. include('adodb.inc.php');
  25. include('adodb-session.php');
  26. adodb_session_open(false,false,false);
  27. session_start();
  28. session_register('AVAR');
  29. $_SESSION['AVAR'] += 1;
  30. print "
  31. -- \$_SESSION['AVAR']={$_SESSION['AVAR']}</p>";
  32. Installation
  33. ============
  34. 1. Create this table in your database (syntax might vary depending on your db):
  35. create table sessions (
  36. SESSKEY char(32) not null,
  37. EXPIRY int(11) unsigned not null,
  38. EXPIREREF varchar(64),
  39. DATA CLOB,
  40. primary key (sesskey)
  41. );
  42. 2. Then define the following parameters in this file:
  43. $ADODB_SESSION_DRIVER='database driver, eg. mysql or ibase';
  44. $ADODB_SESSION_CONNECT='server to connect to';
  45. $ADODB_SESSION_USER ='user';
  46. $ADODB_SESSION_PWD ='password';
  47. $ADODB_SESSION_DB ='database';
  48. $ADODB_SESSION_TBL = 'sessions'
  49. $ADODB_SESSION_USE_LOBS = false; (or, if you wanna use CLOBS (= 'CLOB') or ( = 'BLOB')
  50. 3. Recommended is PHP 4.1.0 or later. There are documented
  51. session bugs in earlier versions of PHP.
  52. 4. If you want to receive notifications when a session expires, then
  53. you can tag a session with an EXPIREREF, and before the session
  54. record is deleted, we can call a function that will pass the EXPIREREF
  55. as the first parameter, and the session key as the second parameter.
  56. To do this, define a notification function, say NotifyFn:
  57. function NotifyFn($expireref, $sesskey)
  58. {
  59. }
  60. Then you need to define a global variable $ADODB_SESSION_EXPIRE_NOTIFY.
  61. This is an array with 2 elements, the first being the name of the variable
  62. you would like to store in the EXPIREREF field, and the 2nd is the
  63. notification function's name.
  64. In this example, we want to be notified when a user's session
  65. has expired, so we store the user id in the global variable $USERID,
  66. store this value in the EXPIREREF field:
  67. $ADODB_SESSION_EXPIRE_NOTIFY = array('USERID','NotifyFn');
  68. Then when the NotifyFn is called, we are passed the $USERID as the first
  69. parameter, eg. NotifyFn($userid, $sesskey).
  70. */
  71. if (!defined('_ADODB_LAYER')) {
  72. include (dirname(__FILE__).'/adodb.inc.php');
  73. }
  74. if (!defined('ADODB_SESSION')) {
  75. define('ADODB_SESSION',1);
  76. /* if database time and system time is difference is greater than this, then give warning */
  77. define('ADODB_SESSION_SYNCH_SECS',60);
  78. /****************************************************************************************\
  79. Global definitions
  80. \****************************************************************************************/
  81. GLOBAL $ADODB_SESSION_CONNECT,
  82. $ADODB_SESSION_DRIVER,
  83. $ADODB_SESSION_USER,
  84. $ADODB_SESSION_PWD,
  85. $ADODB_SESSION_DB,
  86. $ADODB_SESS_CONN,
  87. $ADODB_SESS_LIFE,
  88. $ADODB_SESS_DEBUG,
  89. $ADODB_SESSION_EXPIRE_NOTIFY,
  90. $ADODB_SESSION_CRC,
  91. $ADODB_SESSION_USE_LOBS,
  92. $ADODB_SESSION_TBL;
  93. if (!isset($ADODB_SESSION_USE_LOBS)) $ADODB_SESSION_USE_LOBS = 'CLOB';
  94. $ADODB_SESS_LIFE = ini_get('session.gc_maxlifetime');
  95. if ($ADODB_SESS_LIFE <= 1) {
  96. // bug in PHP 4.0.3 pl 1 -- how about other versions?
  97. //print "<h3>Session Error: PHP.INI setting <i>session.gc_maxlifetime</i>not set: $ADODB_SESS_LIFE</h3>";
  98. $ADODB_SESS_LIFE=1440;
  99. }
  100. $ADODB_SESSION_CRC = false;
  101. //$ADODB_SESS_DEBUG = true;
  102. //////////////////////////////////
  103. /* SET THE FOLLOWING PARAMETERS */
  104. //////////////////////////////////
  105. if (empty($ADODB_SESSION_DRIVER)) {
  106. $ADODB_SESSION_DRIVER='mysql';
  107. $ADODB_SESSION_CONNECT='localhost';
  108. $ADODB_SESSION_USER ='root';
  109. $ADODB_SESSION_PWD ='';
  110. $ADODB_SESSION_DB ='xphplens_2';
  111. }
  112. if (empty($ADODB_SESSION_EXPIRE_NOTIFY)) {
  113. $ADODB_SESSION_EXPIRE_NOTIFY = false;
  114. }
  115. // Made table name configurable - by David Johnson djohnson@inpro.net
  116. if (empty($ADODB_SESSION_TBL)){
  117. $ADODB_SESSION_TBL = 'sessions';
  118. }
  119. // defaulting $ADODB_SESSION_USE_LOBS
  120. if (!isset($ADODB_SESSION_USE_LOBS) || empty($ADODB_SESSION_USE_LOBS)) {
  121. $ADODB_SESSION_USE_LOBS = false;
  122. }
  123. /*
  124. $ADODB_SESS['driver'] = $ADODB_SESSION_DRIVER;
  125. $ADODB_SESS['connect'] = $ADODB_SESSION_CONNECT;
  126. $ADODB_SESS['user'] = $ADODB_SESSION_USER;
  127. $ADODB_SESS['pwd'] = $ADODB_SESSION_PWD;
  128. $ADODB_SESS['db'] = $ADODB_SESSION_DB;
  129. $ADODB_SESS['life'] = $ADODB_SESS_LIFE;
  130. $ADODB_SESS['debug'] = $ADODB_SESS_DEBUG;
  131. $ADODB_SESS['debug'] = $ADODB_SESS_DEBUG;
  132. $ADODB_SESS['table'] = $ADODB_SESS_TBL;
  133. */
  134. /****************************************************************************************\
  135. Create the connection to the database.
  136. If $ADODB_SESS_CONN already exists, reuse that connection
  137. \****************************************************************************************/
  138. function adodb_sess_open($save_path, $session_name,$persist=true)
  139. {
  140. GLOBAL $ADODB_SESS_CONN;
  141. if (isset($ADODB_SESS_CONN)) return true;
  142. GLOBAL $ADODB_SESSION_CONNECT,
  143. $ADODB_SESSION_DRIVER,
  144. $ADODB_SESSION_USER,
  145. $ADODB_SESSION_PWD,
  146. $ADODB_SESSION_DB,
  147. $ADODB_SESS_DEBUG;
  148. // cannot use & below - do not know why...
  149. $ADODB_SESS_CONN = ADONewConnection($ADODB_SESSION_DRIVER);
  150. if (!empty($ADODB_SESS_DEBUG)) {
  151. $ADODB_SESS_CONN->debug = true;
  152. ADOConnection::outp( " conn=$ADODB_SESSION_CONNECT user=$ADODB_SESSION_USER pwd=$ADODB_SESSION_PWD db=$ADODB_SESSION_DB ");
  153. }
  154. if ($persist) $ok = $ADODB_SESS_CONN->PConnect($ADODB_SESSION_CONNECT,
  155. $ADODB_SESSION_USER,$ADODB_SESSION_PWD,$ADODB_SESSION_DB);
  156. else $ok = $ADODB_SESS_CONN->Connect($ADODB_SESSION_CONNECT,
  157. $ADODB_SESSION_USER,$ADODB_SESSION_PWD,$ADODB_SESSION_DB);
  158. if (!$ok) ADOConnection::outp( "
  159. -- Session: connection failed</p>",false);
  160. }
  161. /****************************************************************************************\
  162. Close the connection
  163. \****************************************************************************************/
  164. function adodb_sess_close()
  165. {
  166. global $ADODB_SESS_CONN;
  167. if ($ADODB_SESS_CONN) $ADODB_SESS_CONN->Close();
  168. return true;
  169. }
  170. /****************************************************************************************\
  171. Slurp in the session variables and return the serialized string
  172. \****************************************************************************************/
  173. function adodb_sess_read($key)
  174. {
  175. global $ADODB_SESS_CONN,$ADODB_SESSION_TBL,$ADODB_SESSION_CRC;
  176. $rs = $ADODB_SESS_CONN->Execute("SELECT data FROM $ADODB_SESSION_TBL WHERE sesskey = '$key' AND expiry >= " . time());
  177. if ($rs) {
  178. if ($rs->EOF) {
  179. $v = '';
  180. } else
  181. $v = rawurldecode(reset($rs->fields));
  182. $rs->Close();
  183. // new optimization adodb 2.1
  184. $ADODB_SESSION_CRC = strlen($v).crc32($v);
  185. return $v;
  186. }
  187. return ''; // thx to Jorma Tuomainen, webmaster#wizactive.com
  188. }
  189. /****************************************************************************************\
  190. Write the serialized data to a database.
  191. If the data has not been modified since adodb_sess_read(), we do not write.
  192. \****************************************************************************************/
  193. function adodb_sess_write($key, $val)
  194. {
  195. global
  196. $ADODB_SESS_CONN,
  197. $ADODB_SESS_LIFE,
  198. $ADODB_SESSION_TBL,
  199. $ADODB_SESS_DEBUG,
  200. $ADODB_SESSION_CRC,
  201. $ADODB_SESSION_EXPIRE_NOTIFY,
  202. $ADODB_SESSION_DRIVER, // added
  203. $ADODB_SESSION_USE_LOBS; // added
  204. $expiry = time() + $ADODB_SESS_LIFE;
  205. // crc32 optimization since adodb 2.1
  206. // now we only update expiry date, thx to sebastian thom in adodb 2.32
  207. if ($ADODB_SESSION_CRC !== false && $ADODB_SESSION_CRC == strlen($val).crc32($val)) {
  208. if ($ADODB_SESS_DEBUG) echo "
  209. -- Session: Only updating date - crc32 not changed</p>";
  210. $qry = "UPDATE $ADODB_SESSION_TBL SET expiry=$expiry WHERE sesskey='$key' AND expiry >= " . time();
  211. $rs = $ADODB_SESS_CONN->Execute($qry);
  212. return true;
  213. }
  214. $val = rawurlencode($val);
  215. $arr = array('sesskey' => $key, 'expiry' => $expiry, 'data' => $val);
  216. if ($ADODB_SESSION_EXPIRE_NOTIFY) {
  217. $var = reset($ADODB_SESSION_EXPIRE_NOTIFY);
  218. global $$var;
  219. $arr['expireref'] = $$var;
  220. }
  221. if ($ADODB_SESSION_USE_LOBS === false) { // no lobs, simply use replace()
  222. $rs = $ADODB_SESS_CONN->Replace($ADODB_SESSION_TBL,$arr, 'sesskey',$autoQuote = true);
  223. if (!$rs) {
  224. $err = $ADODB_SESS_CONN->ErrorMsg();
  225. }
  226. } else {
  227. // what value shall we insert/update for lob row?
  228. switch ($ADODB_SESSION_DRIVER) {
  229. // empty_clob or empty_lob for oracle dbs
  230. case "oracle":
  231. case "oci8":
  232. case "oci8po":
  233. case "oci805":
  234. $lob_value = sprintf("empty_%s()", strtolower($ADODB_SESSION_USE_LOBS));
  235. break;
  236. // null for all other
  237. default:
  238. $lob_value = "null";
  239. break;
  240. }
  241. // do we insert or update? => as for sesskey
  242. $res = $ADODB_SESS_CONN->Execute("select count(*) as cnt from $ADODB_SESSION_TBL where sesskey = '$key'");
  243. if ($res && reset($res->fields) > 0) {
  244. $qry = sprintf("update %s set expiry = %d, data = %s where sesskey = '%s'", $ADODB_SESSION_TBL, $expiry, $lob_value, $key);
  245. } else {
  246. // insert
  247. $qry = sprintf("insert into %s (sesskey, expiry, data) values ('%s', %d, %s)", $ADODB_SESSION_TBL, $key, $expiry, $lob_value);
  248. }
  249. $err = "";
  250. $rs1 = $ADODB_SESS_CONN->Execute($qry);
  251. if (!$rs1) {
  252. $err .= $ADODB_SESS_CONN->ErrorMsg()."\n";
  253. }
  254. $rs2 = $ADODB_SESS_CONN->UpdateBlob($ADODB_SESSION_TBL, 'data', $val, "sesskey='$key'", strtoupper($ADODB_SESSION_USE_LOBS));
  255. if (!$rs2) {
  256. $err .= $ADODB_SESS_CONN->ErrorMsg()."\n";
  257. }
  258. $rs = ($rs1 && $rs2) ? true : false;
  259. }
  260. if (!$rs) {
  261. ADOConnection::outp( '
  262. -- Session Replace: '.nl2br($err).'</p>',false);
  263. } else {
  264. // bug in access driver (could be odbc?) means that info is not commited
  265. // properly unless select statement executed in Win2000
  266. if ($ADODB_SESS_CONN->databaseType == 'access')
  267. $rs = $ADODB_SESS_CONN->Execute("select sesskey from $ADODB_SESSION_TBL WHERE sesskey='$key'");
  268. }
  269. return !empty($rs);
  270. }
  271. function adodb_sess_destroy($key)
  272. {
  273. global $ADODB_SESS_CONN, $ADODB_SESSION_TBL,$ADODB_SESSION_EXPIRE_NOTIFY;
  274. if ($ADODB_SESSION_EXPIRE_NOTIFY) {
  275. reset($ADODB_SESSION_EXPIRE_NOTIFY);
  276. $fn = next($ADODB_SESSION_EXPIRE_NOTIFY);
  277. $savem = $ADODB_SESS_CONN->SetFetchMode(ADODB_FETCH_NUM);
  278. $rs = $ADODB_SESS_CONN->Execute("SELECT expireref,sesskey FROM $ADODB_SESSION_TBL WHERE sesskey='$key'");
  279. $ADODB_SESS_CONN->SetFetchMode($savem);
  280. if ($rs) {
  281. $ADODB_SESS_CONN->BeginTrans();
  282. while (!$rs->EOF) {
  283. $ref = $rs->fields[0];
  284. $key = $rs->fields[1];
  285. $fn($ref,$key);
  286. $del = $ADODB_SESS_CONN->Execute("DELETE FROM $ADODB_SESSION_TBL WHERE sesskey='$key'");
  287. $rs->MoveNext();
  288. }
  289. $ADODB_SESS_CONN->CommitTrans();
  290. }
  291. } else {
  292. $qry = "DELETE FROM $ADODB_SESSION_TBL WHERE sesskey = '$key'";
  293. $rs = $ADODB_SESS_CONN->Execute($qry);
  294. }
  295. return $rs ? true : false;
  296. }
  297. function adodb_sess_gc($maxlifetime)
  298. {
  299. global $ADODB_SESS_DEBUG, $ADODB_SESS_CONN, $ADODB_SESSION_TBL,$ADODB_SESSION_EXPIRE_NOTIFY;
  300. if ($ADODB_SESSION_EXPIRE_NOTIFY) {
  301. reset($ADODB_SESSION_EXPIRE_NOTIFY);
  302. $fn = next($ADODB_SESSION_EXPIRE_NOTIFY);
  303. $savem = $ADODB_SESS_CONN->SetFetchMode(ADODB_FETCH_NUM);
  304. $t = time();
  305. $rs = $ADODB_SESS_CONN->Execute("SELECT expireref,sesskey FROM $ADODB_SESSION_TBL WHERE expiry < $t");
  306. $ADODB_SESS_CONN->SetFetchMode($savem);
  307. if ($rs) {
  308. $ADODB_SESS_CONN->BeginTrans();
  309. while (!$rs->EOF) {
  310. $ref = $rs->fields[0];
  311. $key = $rs->fields[1];
  312. $fn($ref,$key);
  313. $del = $ADODB_SESS_CONN->Execute("DELETE FROM $ADODB_SESSION_TBL WHERE sesskey='$key'");
  314. $rs->MoveNext();
  315. }
  316. $rs->Close();
  317. //$ADODB_SESS_CONN->Execute("DELETE FROM $ADODB_SESSION_TBL WHERE expiry < $t");
  318. $ADODB_SESS_CONN->CommitTrans();
  319. }
  320. } else {
  321. $ADODB_SESS_CONN->Execute("DELETE FROM $ADODB_SESSION_TBL WHERE expiry < " . time());
  322. if ($ADODB_SESS_DEBUG) ADOConnection::outp("
  323. -- <b>Garbage Collection</b>: $qry</p>");
  324. }
  325. // suggested by Cameron, "GaM3R" <gamr@outworld.cx>
  326. if (defined('ADODB_SESSION_OPTIMIZE')) {
  327. global $ADODB_SESSION_DRIVER;
  328. switch( $ADODB_SESSION_DRIVER ) {
  329. case 'mysql':
  330. case 'mysqlt':
  331. $opt_qry = 'OPTIMIZE TABLE '.$ADODB_SESSION_TBL;
  332. break;
  333. case 'postgresql':
  334. case 'postgresql7':
  335. $opt_qry = 'VACUUM '.$ADODB_SESSION_TBL;
  336. break;
  337. }
  338. if (!empty($opt_qry)) {
  339. $ADODB_SESS_CONN->Execute($opt_qry);
  340. }
  341. }
  342. if ($ADODB_SESS_CONN->dataProvider === 'oci8') $sql = 'select TO_CHAR('.($ADODB_SESS_CONN->sysTimeStamp).', \'RRRR-MM-DD HH24:MI:SS\') from '. $ADODB_SESSION_TBL;
  343. else $sql = 'select '.$ADODB_SESS_CONN->sysTimeStamp.' from '. $ADODB_SESSION_TBL;
  344. $rs = $ADODB_SESS_CONN->SelectLimit($sql,1);
  345. if ($rs && !$rs->EOF) {
  346. $dbts = reset($rs->fields);
  347. $rs->Close();
  348. $dbt = $ADODB_SESS_CONN->UnixTimeStamp($dbts);
  349. $t = time();
  350. if (abs($dbt - $t) >= ADODB_SESSION_SYNCH_SECS) {
  351. $msg =
  352. __FILE__.": Server time for webserver {$_SERVER['HTTP_HOST']} not in synch with database: database=$dbt ($dbts), webserver=$t (diff=".(abs($dbt-$t)/3600)." hrs)";
  353. error_log($msg);
  354. if ($ADODB_SESS_DEBUG) ADOConnection::outp("
  355. -- $msg</p>");
  356. }
  357. }
  358. return true;
  359. }
  360. session_set_save_handler(
  361. "adodb_sess_open",
  362. "adodb_sess_close",
  363. "adodb_sess_read",
  364. "adodb_sess_write",
  365. "adodb_sess_destroy",
  366. "adodb_sess_gc");
  367. }
  368. /* TEST SCRIPT -- UNCOMMENT */
  369. if (0) {
  370. session_start();
  371. session_register('AVAR');
  372. $_SESSION['AVAR'] += 1;
  373. ADOConnection::outp( "
  374. -- \$_SESSION['AVAR']={$_SESSION['AVAR']}</p>",false);
  375. }