PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

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

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