PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/adodb5/session/old/adodb-session.php

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