discuz_session.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: discuz_session.php 36284 2016-12-12 00:47:50Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class discuz_session {
  12. public $sid = null;
  13. public $var;
  14. public $isnew = false;
  15. private $newguest = array('sid' => 0, 'ip1' => 0, 'ip2' => 0, 'ip3' => 0, 'ip4' => 0,
  16. 'uid' => 0, 'username' => '', 'groupid' => 7, 'invisible' => 0, 'action' => 0,
  17. 'lastactivity' => 0, 'fid' => 0, 'tid' => 0, 'lastolupdate' => 0);
  18. private $old = array('sid' => '', 'ip' => '', 'uid' => 0);
  19. private $table;
  20. public function __construct($sid = '', $ip = '', $uid = 0) {
  21. $this->old = array('sid' => $sid, 'ip' => $ip, 'uid' => $uid);
  22. $this->var = $this->newguest;
  23. $this->table = C::t('common_session');
  24. if(!empty($ip)) {
  25. $this->init($sid, $ip, $uid);
  26. }
  27. }
  28. public function set($key, $value) {
  29. if(isset($this->newguest[$key])) {
  30. $this->var[$key] = $value;
  31. } elseif ($key == 'ip') {
  32. $ips = explode('.', $value);
  33. $this->set('ip1', $ips[0]);
  34. $this->set('ip2', $ips[1]);
  35. $this->set('ip3', $ips[2]);
  36. $this->set('ip4', $ips[3]);
  37. }
  38. }
  39. public function get($key) {
  40. if(isset($this->newguest[$key])) {
  41. return $this->var[$key];
  42. } elseif ($key == 'ip') {
  43. return $this->get('ip1').'.'.$this->get('ip2').'.'.$this->get('ip3').'.'.$this->get('ip4');
  44. }
  45. }
  46. public function init($sid, $ip, $uid) {
  47. $this->old = array('sid' => $sid, 'ip' => $ip, 'uid' => $uid);
  48. $session = array();
  49. if($sid) {
  50. $session = $this->table->fetch($sid, $ip, $uid);
  51. }
  52. if(empty($session) || $session['uid'] != $uid) {
  53. $session = $this->create($ip, $uid);
  54. }
  55. $this->var = $session;
  56. $this->sid = $session['sid'];
  57. }
  58. public function create($ip, $uid) {
  59. $this->isnew = true;
  60. $this->var = $this->newguest;
  61. $this->set('sid', random(6));
  62. $this->set('uid', $uid);
  63. $this->set('ip', $ip);
  64. $uid && $this->set('invisible', getuserprofile('invisible'));
  65. $this->set('lastactivity', time());
  66. $this->sid = $this->var['sid'];
  67. return $this->var;
  68. }
  69. public function delete() {
  70. return $this->table->delete_by_session($this->var, getglobal('setting/onlinehold'), 60);
  71. }
  72. public function update() {
  73. if($this->sid !== null) {
  74. if($this->isnew) {
  75. $this->delete();
  76. $this->table->insert($this->var, false, false, true);
  77. } else {
  78. $this->table->update($this->var['sid'], $this->var);
  79. }
  80. setglobal('sessoin', $this->var);
  81. dsetcookie('sid', $this->sid, 86400);
  82. }
  83. }
  84. public function count($type = 0) {
  85. return $this->table->count($type);
  86. }
  87. public function fetch_member($ismember = 0, $invisible = 0, $start = 0, $limit = 0) {
  88. return $this->table->fetch_member($ismember, $invisible, $start, $limit);
  89. }
  90. public function count_invisible($type = 1) {
  91. return $this->table->count_invisible($type);
  92. }
  93. public function update_by_ipban($ip1, $ip2, $ip3, $ip4) {
  94. return $this->table->update_by_ipban($ip1, $ip2, $ip3, $ip4);
  95. }
  96. public function update_max_rows($max_rows) {
  97. return $this->table->update_max_rows($max_rows);
  98. }
  99. public function clear() {
  100. return $this->table->clear();
  101. }
  102. public function count_by_fid($fid) {
  103. return $this->table->count_by_fid($fid);
  104. }
  105. public function fetch_all_by_fid($fid, $limit = 0) {
  106. $data = array();
  107. if(!($fid = dintval($fid))) {
  108. return $data;
  109. }
  110. $onlinelist = getglobal('cache/onlinelist');
  111. foreach($this->table->fetch_all_by_fid($fid, $limit) as $online) {
  112. if($online['uid']) {
  113. $online['icon'] = isset($onlinelist[$online['groupid']]) ? $onlinelist[$online['groupid']] : $onlinelist[0];
  114. } else {
  115. $online['icon'] = $onlinelist[7];
  116. $online['username'] = $onlinelist['guest'];
  117. }
  118. $online['lastactivity'] = dgmdate($online['lastactivity'], 't');
  119. $data[] = $online;
  120. }
  121. return $data;
  122. }
  123. public function fetch_by_uid($uid) {
  124. return $this->table->fetch_by_uid($uid);
  125. }
  126. public function fetch_all_by_uid($uids, $start = 0, $limit = 0) {
  127. return $this->table->fetch_all_by_uid($uids, $start, $limit);
  128. }
  129. public function update_by_uid($uid, $data) {
  130. return $this->table->update_by_uid($uid, $data);
  131. }
  132. public function count_by_ip($ip) {
  133. return $this->table->count_by_ip($ip);
  134. }
  135. public function fetch_all_by_ip($ip, $start = 0, $limit = 0) {
  136. return $this->table->fetch_all_by_ip($ip, $start, $limit);
  137. }
  138. public static function updatesession() {
  139. static $updated = false;
  140. if(!$updated) {
  141. global $_G;
  142. if($_G['uid']) {
  143. if($_G['cookie']['ulastactivity']) {
  144. $ulastactivity = authcode($_G['cookie']['ulastactivity'], 'DECODE');
  145. } else {
  146. $ulastactivity = getuserprofile('lastactivity');
  147. dsetcookie('ulastactivity', authcode($ulastactivity, 'ENCODE'), 31536000);
  148. }
  149. }
  150. $oltimespan = $_G['setting']['oltimespan'];
  151. $lastolupdate = C::app()->session->var['lastolupdate'];
  152. if($_G['uid'] && $oltimespan && TIMESTAMP - ($lastolupdate ? $lastolupdate : $ulastactivity) > $oltimespan * 60) {
  153. $isinsert = false;
  154. if(C::app()->session->isnew) {
  155. $oldata = C::t('common_onlinetime')->fetch($_G['uid']);
  156. if(empty($oldata)) {
  157. $isinsert = true;
  158. } else if(TIMESTAMP - $oldata['lastupdate'] > $oltimespan * 60) {
  159. C::t('common_onlinetime')->update_onlinetime($_G['uid'], $oltimespan, $oltimespan, TIMESTAMP);
  160. }
  161. } else {
  162. $isinsert = !C::t('common_onlinetime')->update_onlinetime($_G['uid'], $oltimespan, $oltimespan, TIMESTAMP);
  163. }
  164. if($isinsert) {
  165. C::t('common_onlinetime')->insert(array(
  166. 'uid' => $_G['uid'],
  167. 'thismonth' => $oltimespan,
  168. 'total' => $oltimespan,
  169. 'lastupdate' => TIMESTAMP,
  170. ));
  171. }
  172. C::app()->session->set('lastolupdate', TIMESTAMP);
  173. }
  174. foreach(C::app()->session->var as $k => $v) {
  175. if(isset($_G['member'][$k]) && $k != 'lastactivity') {
  176. C::app()->session->set($k, $_G['member'][$k]);
  177. }
  178. }
  179. foreach($_G['action'] as $k => $v) {
  180. C::app()->session->set($k, $v);
  181. }
  182. C::app()->session->update();
  183. if($_G['uid'] && TIMESTAMP - $ulastactivity > 21600) {
  184. if($oltimespan && TIMESTAMP - $ulastactivity > 43200) {
  185. $onlinetime = C::t('common_onlinetime')->fetch($_G['uid']);
  186. C::t('common_member_count')->update($_G['uid'], array('oltime' => round(intval($onlinetime['total']) / 60)));
  187. }
  188. dsetcookie('ulastactivity', authcode(TIMESTAMP, 'ENCODE'), 31536000);
  189. C::t('common_member_status')->update($_G['uid'], array('lastip' => $_G['clientip'], 'port' => $_G['remoteport'], 'lastactivity' => TIMESTAMP, 'lastvisit' => TIMESTAMP));
  190. }
  191. $updated = true;
  192. }
  193. return $updated;
  194. }
  195. }
  196. ?>