/demo/scalr_newui/app/src/Lib/Data/DB/adodb_lite/session/adodb-session.php

https://github.com/kennethjiang/Wolke · PHP · 259 lines · 204 code · 40 blank · 15 comment · 29 complexity · d7c0aa358571f656c1568a8ccca00e8a MD5 · raw file

  1. <?php
  2. /*
  3. CREATE TABLE sessions (
  4. SessionID VARCHAR(64),
  5. session_data TEXT DEFAULT '',
  6. expiry INT(11),
  7. expireref VARCHAR(250) DEFAULT '',
  8. PRIMARY KEY (SessionID),
  9. INDEX expiry (expiry)
  10. );
  11. */
  12. if (!defined('_ADODB_LAYER')) {
  13. require_once realpath(dirname(__FILE__) . '/../adodb.inc.php');
  14. }
  15. if (defined('ADODB_SESSION')) return 1;
  16. define('ADODB_SESSION', dirname(__FILE__));
  17. class ADODB_Session {
  18. function sess_open($sess_path, $sess_name, $persist = null) {
  19. $database = $GLOBALS['ADODB_SESSION_DB'];
  20. $driver = $GLOBALS['ADODB_SESSION_DRIVER'];
  21. $host = $GLOBALS['ADODB_SESSION_CONNECT'];
  22. $password = $GLOBALS['ADODB_SESSION_PWD'];
  23. $user = $GLOBALS['ADODB_SESSION_USER'];
  24. $GLOBALS['ADODB_SESSION_TBL'] = (!empty($GLOBALS['ADODB_SESSION_TBL'])) ? $GLOBALS['ADODB_SESSION_TBL'] : 'sessions';
  25. $db_object =& ADONewConnection($driver);
  26. if ($persist) {
  27. switch($persist) {
  28. default:
  29. case 'P': $result = $db_object->PConnect($host, $user, $password, $database); break;
  30. case 'C': $result = $db_object->Connect($host, $user, $password, $database); break;
  31. case 'N': $result = $db_object->NConnect($host, $user, $password, $database); break;
  32. }
  33. } else {
  34. $result = $db_object->Connect($host, $user, $password, $database);
  35. }
  36. if ($result == true)
  37. $GLOBALS['ADODB_SESS_CONN'] =& $db_object;
  38. return $result;
  39. }
  40. function sess_close() {
  41. return true;
  42. }
  43. function sess_read($sess_id) {
  44. $filter = ADODB_Session::filter();
  45. $dataFieldName = ADODB_Session::dataFieldName();
  46. $db_object =& $GLOBALS['ADODB_SESS_CONN'];
  47. $table = $GLOBALS['ADODB_SESSION_TBL'];
  48. $result = $db_object->execute("SELECT $dataFieldName FROM $table WHERE SessionID = '$sess_id'");
  49. $CurrentTime = time();
  50. if (!$result->RecordCount()) {
  51. $expire_notify = $GLOBALS['ADODB_SESSION_EXPIRE_NOTIFY'];
  52. $notify = '';
  53. if (isset($expire_notify)) {
  54. $var = reset($expire_notify);
  55. global $$var;
  56. if (isset($$var)) {
  57. $notify = $$var;
  58. }
  59. }
  60. $db_object->execute("INSERT INTO $table (SessionID, expiry, expireref) VALUES ('$sess_id', '$CurrentTime', '$notify')");
  61. return '';
  62. } else {
  63. $data = $result->fields[$dataFieldName];
  64. $filter = array_reverse($filter);
  65. foreach ($filter as $f) {
  66. if (is_object($f)) {
  67. $data = $f->read($data, ADODB_Session::_sessionKey());
  68. }
  69. }
  70. $data = rawurldecode($data);
  71. $db_object->execute("UPDATE $table SET expiry = '$CurrentTime' WHERE SessionID = '$sess_id'");
  72. return $data;
  73. }
  74. }
  75. function sess_write($sess_id, $data) {
  76. $filter = ADODB_Session::filter();
  77. $dataFieldName = ADODB_Session::dataFieldName();
  78. $db_object =& $GLOBALS['ADODB_SESS_CONN'];
  79. $table = $GLOBALS['ADODB_SESSION_TBL'];
  80. if (isset($GLOBALS['ADODB_SESS_LIFE'])) {
  81. $lifetime = $GLOBALS['ADODB_SESS_LIFE'];
  82. }
  83. else
  84. {
  85. $lifetime = ini_get('session.gc_maxlifetime');
  86. if ($lifetime <= 1) {
  87. $lifetime = 1440;
  88. }
  89. }
  90. $expire_notify = $GLOBALS['ADODB_SESSION_EXPIRE_NOTIFY'];
  91. $notify = '';
  92. if (isset($expire_notify)) {
  93. $var = reset($expire_notify);
  94. global $$var;
  95. if (isset($$var)) {
  96. $notify = $$var;
  97. }
  98. }
  99. $CurrentTime = time() + $lifetime;
  100. $data = rawurlencode($data);
  101. foreach ($filter as $f) {
  102. if (is_object($f)) {
  103. $data = $f->write($data, ADODB_Session::_sessionKey());
  104. }
  105. }
  106. $db_object->execute("UPDATE $table SET $dataFieldName = '$data', expiry = '$CurrentTime', expireref = '$notify' WHERE SessionID = '$sess_id'");
  107. return true;
  108. }
  109. function sess_destroy($sess_id) {
  110. $db_object =& $GLOBALS['ADODB_SESS_CONN'];
  111. $table = $GLOBALS['ADODB_SESSION_TBL'];
  112. $expire_notify = $GLOBALS['ADODB_SESSION_EXPIRE_NOTIFY'];
  113. if (isset($expire_notify)) {
  114. reset($expire_notify);
  115. $fn = next($expire_notify);
  116. $result =& $db_object->Execute("SELECT expireref, SessionID FROM $table WHERE SessionID = '$sess_id'");
  117. if (!$result) {
  118. return false;
  119. }
  120. if (!$result->EOF) {
  121. $ref = $result->fields['expireref'];
  122. $key = $result->fields['SessionID'];
  123. $fn($ref, $key);
  124. }
  125. $result->Close();
  126. }
  127. $result =& $db_object->execute("DELETE FROM $table WHERE SessionID = '$sess_id'");
  128. if ($result) {
  129. $result->Close();
  130. }
  131. return $result ? true : false;
  132. }
  133. function sess_gc($sess_maxlifetime) {
  134. $db_object =& $GLOBALS['ADODB_SESS_CONN'];
  135. $table = $GLOBALS['ADODB_SESSION_TBL'];
  136. $time = (isset($sess_maxlifetime)) ? $sess_maxlifetime : time();
  137. $expire_notify = $GLOBALS['ADODB_SESSION_EXPIRE_NOTIFY'];
  138. if (isset($expire_notify)) {
  139. reset($expire_notify);
  140. $fn = next($expire_notify);
  141. $result =& $db_object->Execute("SELECT expireref, SessionID FROM $table WHERE expiry < $time");
  142. if ($result) {
  143. $keys = array();
  144. while (!$result->EOF) {
  145. $ref = $result->fields['expireref'];
  146. $key = $result->fields['SessionID'];
  147. $fn($ref, $key);
  148. $del = $db_object->Execute("DELETE FROM $table WHERE SessionID='$key'");
  149. $result->MoveNext();
  150. }
  151. $result->Close();
  152. }
  153. } else {
  154. $result =& $db_object->Execute("DELETE FROM $table WHERE expiry < $time");
  155. if ($result) {
  156. $result->Close();
  157. }
  158. }
  159. $CurrentTime = time();
  160. $db_object->execute("DELETE FROM $table WHERE expiry < $CurrentTime");
  161. return true;
  162. }
  163. function gc($sess_maxlifetime) {
  164. ADODB_Session::sess_gc($sess_maxlifetime);
  165. }
  166. /*
  167. * Initialize
  168. */
  169. function _init() {
  170. session_module_name('user');
  171. session_set_save_handler(
  172. array('ADODB_Session', 'sess_open'),
  173. array('ADODB_Session', 'sess_close'),
  174. array('ADODB_Session', 'sess_read'),
  175. array('ADODB_Session', 'sess_write'),
  176. array('ADODB_Session', 'sess_destroy'),
  177. array('ADODB_Session', 'sess_gc')
  178. );
  179. register_shutdown_function('session_write_close');
  180. }
  181. function filter($filter = null) {
  182. static $_filter = array();
  183. if (!is_null($filter)) {
  184. if (!is_array($filter)) {
  185. $filter = array($filter);
  186. }
  187. $_filter = $filter;
  188. }
  189. return $_filter;
  190. }
  191. function dataFieldName($data_field_name = null) {
  192. static $_data_field_name = 'session_data';
  193. if (!is_null($data_field_name)) {
  194. $_data_field_name = trim($data_field_name);
  195. }
  196. return $_data_field_name;
  197. }
  198. function _sessionKey() {
  199. return crypt(ADODB_Session::encryptionKey(), session_id());
  200. }
  201. function encryptionKey($encryption_key = null) {
  202. static $_encryption_key = 'ADODB LITE';
  203. if (!is_null($encryption_key)) {
  204. $_encryption_key = $encryption_key;
  205. }
  206. return $_encryption_key;
  207. }
  208. }
  209. ADODB_Session::_init();
  210. // for backwards compatability only
  211. function adodb_sess_open($save_path, $session_name, $persist = true) {
  212. return ADODB_Session::sess_open($save_path, $session_name, $persist);
  213. }
  214. // for backwards compatability only
  215. function adodb_sess_gc($sess_maxlifetime)
  216. {
  217. return ADODB_Session::gc($sess_maxlifetime);
  218. }
  219. ?>