PageRenderTime 65ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/concreteOLD/libraries/3rdparty/adodb/session/old/adodb-session-clob.php

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