discuz_panel.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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_panel.php 26205 2011-12-05 10:09:32Z zhangguosheng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. define('ADMINCP_PANEL', 1);
  12. define('MODCP_PANEL', 2);
  13. define('PORTALCP_PANEL', 3);
  14. class discuz_panel {
  15. private $table;
  16. var $ttl = 3600;
  17. var $lockttl = 900;
  18. var $uid;
  19. var $adminid;
  20. var $groupid;
  21. var $panel;
  22. var $ip;
  23. var $storage = array();
  24. var $session = array();
  25. var $islogin = false;
  26. public function __construct($panel) {
  27. global $_G;
  28. $this->uid = (int)$_G['uid'];
  29. $this->adminid = (int)$_G['adminid'];
  30. $this->groupid = (int)$_G['groupid'];
  31. $this->panel = (int)$panel;
  32. $this->ip = $_G['clientip'];
  33. $this->table = C::t('common_admincp_session');
  34. $this->_cpaccess();
  35. }
  36. function _session_load() {
  37. $this->session = $this->table->fetch($this->uid, $this->panel);
  38. if(empty($this->session) || (time() - $this->session['dateline'] > $this->ttl)) {
  39. $this->session = array();
  40. } elseif($this->session['errorcount'] >=5 && (time() - $this->session['dateline'] > $this->lockttl)) {
  41. $this->session = array();
  42. } elseif(!empty($this->session['storage'])) {
  43. $this->storage = dunserialize(base64_decode($this->session['storage']));
  44. $this->session['storage'] = '';
  45. }
  46. return $this->session;
  47. }
  48. function _session_destroy($uid = 0) {
  49. $uid = empty($uid) ? $this->uid : $uid;
  50. $this->table->delete($uid, $this->panel, $this->ttl);
  51. }
  52. function _loadstorage() {
  53. $ret = $this->table->fetch($this->uid, $this->panel);
  54. $storage = $ret['storage'];
  55. if(!empty($storage)) {
  56. $this->storage = dunserialize(base64_decode($storage));
  57. } else {
  58. $this->storage = array();
  59. }
  60. }
  61. function geturl() {
  62. $url = getglobal('basefilename').'?';
  63. if(!empty($_GET)) {
  64. foreach ($_GET as $key => $value) {
  65. $url .= urlencode($key).'='.urlencode($value).'&';
  66. }
  67. }
  68. return $url;
  69. }
  70. function isfounder($user = '') {
  71. global $_G;
  72. $user = empty($user) ? array('uid' => $_G['uid'], 'adminid' => $_G['adminid'], 'username' => $_G['member']['username']) : $user;
  73. $founders = str_replace(' ', '', $GLOBALS['forumfounders']);
  74. if($user['adminid'] <> 1) {
  75. return FALSE;
  76. } elseif(empty($founders)) {
  77. return TRUE;
  78. } elseif(strexists(",$founders,", ",$user[uid],")) {
  79. return TRUE;
  80. } elseif(!is_numeric($user['username']) && strexists(",$founders,", ",$user[username],")) {
  81. return TRUE;
  82. } else {
  83. return FALSE;
  84. }
  85. }
  86. function set($varname, $value, $updatedb = false) {
  87. $this->storage[$varname] = $value;
  88. $updatedb && $this->update();
  89. }
  90. function get($varname, $fromdb = false) {
  91. $return = null;
  92. $fromdb && $this->_loadstorage();
  93. if(isset($this->storage[$varname])) {
  94. $return = $this->storage[$varname];
  95. }
  96. return $return;
  97. }
  98. function clear($updatedb = false) {
  99. $this->storage = array();
  100. $updatedb && $this->update();
  101. }
  102. function _sesssion_creat() {
  103. $this->_session_destroy();
  104. $this->set('url_forward', $this->geturl());
  105. $this->session = array(
  106. 'uid' => $this->uid,
  107. 'adminid' => $this->adminid,
  108. 'panel' => $this->panel,
  109. 'ip' => $this->ip,
  110. 'errorcount' => 0,
  111. );
  112. $this->update(true);
  113. }
  114. function update($isnew = false) {
  115. $data = array();
  116. $this->session['dateline'] = time();
  117. $this->session['storage'] = !empty($this->storage) ? base64_encode((serialize($this->storage))) : '';
  118. if($isnew) {
  119. $this->table->insert($this->session, false, true);
  120. } else {
  121. $this->table->update($this->uid, $this->panel, $this->session);
  122. }
  123. }
  124. function _cpaccess() {
  125. if(empty($this->uid)) {
  126. $this->_user_login();
  127. } elseif($this->panel == MODCP_PANEL && $this->adminid <= 0) {
  128. $this->showmessage('admin_cpanel_noaccess');
  129. }
  130. $this->_session_load();
  131. if(empty($this->session)) {
  132. $this->_sesssion_creat();
  133. } elseif($this->session['errorcount'] > 5) {
  134. $this->_panel_locked();
  135. } elseif($this->session['errorcount'] == -1) {
  136. $this->islogin = true;
  137. $this->update();
  138. } else {
  139. $this->islogin = false;
  140. }
  141. }
  142. function dologin($username, $password, $isuid = false) {
  143. loaducenter();
  144. if(!$isuid) {
  145. $username = addslashes($username);
  146. }
  147. $ucresult = uc_user_login($username, $password, $isuid ? 1 : 0);
  148. if($ucresult[0] > 0) {
  149. $this->loginsucced();
  150. } else {
  151. $this->session['errorcount'] ++;
  152. }
  153. $this->update();
  154. return $this->islogin;
  155. }
  156. function dologout() {
  157. $this->_session_destroy();
  158. }
  159. function loginsucced() {
  160. $this->session['errorcount'] = '-1';
  161. $this->islogin = true;
  162. $this->update();
  163. dheader('Location: '.$this->get('url_forward'));
  164. }
  165. function showmessage($message, $url_forward = '', $values = array(), $ext = array()) {
  166. showmessage($message, $url_forward, $values, $ext);
  167. dexit();
  168. }
  169. function _panel_locked() {
  170. $unlocktime = dgmdate($this->session['dateline'] + $this->lockttl + 30);
  171. $this->showmessage('admin_cpanel_locked', '', array('unlocktime' => $unlocktime));
  172. }
  173. function _user_login() {
  174. $this->showmessage('to_login', 'member.php?mod=logging&action=login', array(), array('showmsg' => true, 'login' => 1));
  175. }
  176. }
  177. ?>