PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/cubi/modules/system/lib/SessionDBHandler.php

http://openbiz-cubi.googlecode.com/
PHP | 187 lines | 123 code | 23 blank | 41 comment | 11 complexity | 6fd136e06dab1bbb44f6cf97efed6205 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * Openbiz Cubi Application Platform
  4. *
  5. * LICENSE http://code.google.com/p/openbiz-cubi/wiki/CubiLicense
  6. *
  7. * @package cubi.system.lib
  8. * @copyright Copyright (c) 2005-2011, Openbiz Technology LLC
  9. * @license http://code.google.com/p/openbiz-cubi/wiki/CubiLicense
  10. * @link http://code.google.com/p/openbiz-cubi/
  11. * @version $Id: SessionDBHandler.php 3372 2012-05-31 06:19:06Z rockyswen@gmail.com $
  12. */
  13. define("SESSION_DBNAME","Default");
  14. define("SESSION_TABLE","session");
  15. class SessionDBHandler {
  16. protected $lifeTime;
  17. protected $initSessionData;
  18. protected $sessionDb;
  19. function __construct()
  20. {
  21. $this->lifeTime = TIMEOUT;
  22. $this->sessionDO = SESSION_DATAOBJ;
  23. }
  24. function open($savePath,$sessionName) {
  25. // echo "session open".nl;
  26. // connect to session db
  27. $this->sessionDb = BizSystem::dbConnection(SESSION_DBNAME);
  28. $sessionID = session_id();
  29. if ($sessionID !== "") {
  30. $this->initSessionData = $this->read($sessionID);
  31. }
  32. return true;
  33. }
  34. function close() {
  35. //echo "session close".nl;
  36. $this->lifeTime = null;
  37. $this->initSessionData = null;
  38. return true;
  39. }
  40. function read($sessionID) {
  41. //echo "session read".nl;
  42. //debug_print_backtrace();
  43. $sql = "SELECT `data` FROM `".SESSION_TABLE."` WHERE `id`=?";
  44. $data = $this->sessionDb->fetchOne($sql, $sessionID);
  45. $this->initSessionData = $data;
  46. return $data;
  47. }
  48. function write($sessionID,$data) {
  49. //echo "session write".nl;
  50. // This is called upon script termination or when session_write_close() is called, which ever is first.
  51. $expiration = ($this->lifeTime + time());
  52. $dataArr = self::unserializesession($data);
  53. $user_id = (int)$dataArr['_USER_PROFILE']["Id"];
  54. $ip_addr = $_SERVER["REMOTE_ADDR"];
  55. $last_url = $_SERVER["REQUEST_URI"];
  56. $update_time = date("Y-m-d H:i:s");
  57. try {
  58. if(SESSION_STRICT==1){
  59. //limited to single session delete prev sessions
  60. $sql = "DELETE FROM `session` WHERE `id`!='$sessionID' AND `user_id`='$user_id' ;";
  61. $this->sessionDb->query($sql);
  62. }
  63. if ($this->initSessionData == null) {
  64. //echo "insert session data";
  65. $create_time = date("Y-m-d H:i:s");
  66. $this->sessionDb->insert('session', array('id'=>$sessionID,
  67. 'data'=>$data,
  68. 'expiration'=>$expiration,
  69. 'user_id'=>$user_id,
  70. 'ipaddr'=>$ip_addr,
  71. 'last_url'=>$last_url,
  72. 'create_time'=>$create_time,
  73. 'update_time'=>$update_time));
  74. }
  75. else {
  76. if ($this->initSessionData == $data) {
  77. //echo "update session w/o data change";
  78. $this->sessionDb->update('session', array('expiration'=>$expiration,
  79. 'user_id'=>$user_id,
  80. 'ipaddr'=>$ip_addr,
  81. 'last_url'=>$last_url,
  82. 'update_time'=>$update_time
  83. ), "id = '$sessionID'");
  84. }
  85. else {
  86. //echo "update session w/ data change";
  87. $this->sessionDb->update('session', array('data'=>$data,
  88. 'expiration'=>$expiration,
  89. 'user_id'=>$user_id,
  90. 'ipaddr'=>$ip_addr,
  91. 'last_url'=>$last_url,
  92. 'update_time'=>$update_time
  93. ), "id = '$sessionID'");
  94. }
  95. }
  96. }
  97. catch (Exception $e) {
  98. echo "SQL error: ".$e->getMessage();
  99. }
  100. return true;
  101. }
  102. function destroy($sessionID) {
  103. //echo "session destroy".nl;
  104. // Called when a user logs out...
  105. $this->sessionDb->delete('session', "id='$sessionID'");
  106. return true;
  107. }
  108. function gc($maxlifetime) {
  109. //echo "session gc";
  110. // garbage collection to delete expired session entried
  111. $expireTime = time(); // time() - $this->lifeTime;
  112. $this->sessionDb->delete('session', "expiration < $expireTime");
  113. return true;
  114. }
  115. public static function unserializesession( $data )
  116. {
  117. if( strlen( $data) == 0)
  118. {
  119. return array();
  120. }
  121. // match all the session keys and offsets
  122. preg_match_all('/(^|;|\})([a-zA-Z0-9_]+)\|/i', $data, $matchesarray, PREG_OFFSET_CAPTURE);
  123. $returnArray = array();
  124. $lastOffset = null;
  125. $currentKey = '';
  126. foreach ( $matchesarray[2] as $value )
  127. {
  128. $offset = $value[1];
  129. if(!is_null( $lastOffset))
  130. {
  131. $valueText = substr($data, $lastOffset, $offset - $lastOffset );
  132. $returnArray[$currentKey] = unserialize($valueText);
  133. }
  134. $currentKey = $value[0];
  135. $lastOffset = $offset + strlen( $currentKey )+1;
  136. }
  137. $valueText = substr($data, $lastOffset );
  138. $returnArray[$currentKey] = unserialize($valueText);
  139. return $returnArray;
  140. }
  141. }
  142. $sessionHandler = new SessionDBHandler();
  143. session_set_save_handler(
  144. array (&$sessionHandler,"open"),
  145. array (&$sessionHandler,"close"),
  146. array (&$sessionHandler,"read"),
  147. array (&$sessionHandler,"write"),
  148. array (&$sessionHandler,"destroy"),
  149. array (&$sessionHandler,"gc"));
  150. /*
  151. CREATE TABLE IF NOT EXISTS `session` (
  152. `id` varchar(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
  153. `user_id` int(11) NOT NULL,
  154. `expiration` int(10) unsigned NOT NULL,
  155. `data` text COLLATE utf8_unicode_ci NOT NULL,
  156. `ipaddr` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  157. `create_time` datetime NOT NULL,
  158. `update_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
  159. PRIMARY KEY (`id`),
  160. KEY `expiration` (`expiration`)
  161. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  162. */
  163. ?>