PageRenderTime 60ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/cpanel.share.php

https://github.com/gcao/bbs
PHP | 285 lines | 240 code | 38 blank | 7 comment | 52 complexity | d2c9921559ad45be9d64195ae887424a MD5 | raw file
  1. <?php
  2. /*
  3. [Discuz!] (C)2001-2009 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: cpanel.share.php 20964 2009-11-04 03:18:22Z zhaoxiongfei $
  6. */
  7. if(!defined('IN_DISCUZ')) {
  8. exit('Access Denied');
  9. }
  10. class AdminSession {
  11. var $uid = 0;
  12. var $panel = 0;
  13. var $inadmincp = false;
  14. var $isfounder = false;
  15. var $cpaccess = 0;
  16. var $checkip = 1;
  17. var $logfile = 'cplog';
  18. var $timelimit;
  19. var $errorcount = 0;
  20. var $storage = array();
  21. var $db = null;
  22. var $tablepre = '';
  23. function adminsession($uid, $groupid, $adminid, $ip) {
  24. global $adminipaccess, $db, $tablepre;
  25. $this->panel = defined('IN_ADMINCP') ? 1 : (defined('IN_MODCP') ? 2 : -1);
  26. $this->inadmincp = defined('IN_ADMINCP');
  27. $this->uid = $uid;
  28. $this->timelimit = time() - 1800;
  29. $this->db = &$db;
  30. $this->tablepre = &$tablepre;
  31. if($uid < 1 || $adminid < 1 || ($this->inadmincp && $adminid != 1)) {
  32. $cpaccess = 0;
  33. }elseif($this->inadmincp && $adminipaccess && !ipaccess($ip, $adminipaccess)) {
  34. $cpaccess = 2;
  35. } else {
  36. $session = $this->_loadsession($uid, $ip, $GLOBALS['admincp']['checkip']);
  37. $this->errorcount = $session['errorcount'];
  38. $this->storage = $session['storage'];
  39. if(empty($session)) {
  40. $this->creatsession($uid, $adminid, $ip);
  41. $cpaccess = 1;
  42. } elseif($session['errorcount'] == -1) {
  43. $this->update();
  44. $cpaccess = 3;
  45. } elseif($session['errorcount'] <= 3) {
  46. $cpaccess = 1;
  47. } else {
  48. $cpaccess = -1;
  49. }
  50. }
  51. if($cpaccess == 0) {
  52. //clearcookies();
  53. showmessage('admin_cpanel_noaccess', 'logging.php?action=login', 'HALTED');
  54. } elseif($cpaccess == 2) {
  55. showmessage('admin_cpanel_noaccess_ip', NULL, 'HALTED');
  56. } elseif($cpaccess == -1) {
  57. showmessage('admin_cpanel_locked', NULL, 'HALTED');
  58. }
  59. $this->cpaccess = $cpaccess;
  60. }
  61. function _loadsession($uid, $ip, $checkip = 1) {
  62. $session = array();
  63. $query = $this->db->query("SELECT uid, adminid, panel, ip, dateline, errorcount, storage FROM {$this->tablepre}adminsessions
  64. WHERE uid='$uid' ".($checkip ? "AND ip='$ip'" : '')." AND panel='{$this->panel}' AND dateline>'{$this->timelimit}'", 'SILENT');
  65. if(!$this->db->error()) {
  66. $session = $this->db->fetch_array($query);
  67. if(isset($session['storage'])) {
  68. $session['storage'] = $session['storage'] ? unserialize(base64_decode($session['storage'])) : array();
  69. }
  70. } else {
  71. $this->db->query("DROP TABLE IF EXISTS {$this->tablepre}adminsessions");
  72. $this->db->query("CREATE TABLE {$this->tablepre}adminsessions (
  73. uid mediumint(8) UNSIGNED NOT NULL default '0',
  74. adminid smallint(6) unsigned NOT NULL DEFAULT '0',
  75. panel tinyint(1) NOT NULL DEFAULT '0',
  76. ip varchar(15) NOT NULL default '',
  77. dateline int(10) unsigned NOT NULL default '0',
  78. errorcount tinyint(1) NOT NULL default '0',
  79. `storage` mediumtext NOT NULL,
  80. PRIMARY KEY (`uid`, `panel`))".(mysql_get_server_info() > '4.1' ? " ENGINE=MYISAM DEFAULT CHARSET=$GLOBALS[dbcharset]" : " TYPE=MYISAM")
  81. );
  82. }
  83. return $session;
  84. }
  85. function creatsession($uid, $adminid, $ip) {
  86. $url_forward = !empty($_SERVER['QUERY_STRING']) ? addslashes($_SERVER['QUERY_STRING']) : '';
  87. $this->destroy($uid);
  88. $this->db->query("INSERT INTO {$this->tablepre}adminsessions (uid, adminid, panel, ip, dateline, errorcount)
  89. VALUES ('$uid', '$adminid', '$this->panel', '$ip', '".time()."', '0')");
  90. $this->set('url_forward', $url_forward, true);
  91. }
  92. function destroy($uid = 0) {
  93. empty($uid) && $uid = $this->uid;
  94. $this->db->query("DELETE FROM {$this->tablepre}adminsessions WHERE (uid='$uid' AND panel='$this->panel') OR dateline<'$this->timelimit'");
  95. }
  96. function _loadstorage() {
  97. $storage = $this->db->result_first("SELECT storage FROM {$this->tablepre}adminsessions WHERE uid='{$this->uid}' AND panel='$this->panel'");
  98. if(!empty($storage)) {
  99. $this->storage = unserialize(base64_decode($storage));
  100. } else {
  101. $this->storage = array();
  102. }
  103. }
  104. function isfounder($user = '') {
  105. $user = empty($user) ? array('uid' => $GLOBALS['discuz_uid'], 'adminid' => $GLOBALS['adminid'], 'username' => $GLOBALS['discuz_userss']) : $user;
  106. $founders = str_replace(' ', '', $GLOBALS['forumfounders']);
  107. if($user['adminid'] <> 1) {
  108. return FALSE;
  109. } elseif(empty($founders)) {
  110. return TRUE;
  111. } elseif(strexists(",$founders,", ",$user[uid],")) {
  112. return TRUE;
  113. } elseif(!is_numeric($user['username']) && strexists(",$founders,", ",$user[username],")) {
  114. return TRUE;
  115. } else {
  116. return FALSE;
  117. }
  118. }
  119. function set($varname, $value, $updatedb = false) {
  120. $this->storage[$varname] = $value;
  121. $updatedb && $this->update();
  122. }
  123. function get($varname, $fromdb = false) {
  124. $return = null;
  125. $fromdb && $this->_loadstorage();
  126. if(isset($this->storage[$varname])) {
  127. $return = $this->storage[$varname];
  128. }
  129. return $return;
  130. }
  131. function clear($updatedb = false) {
  132. $this->storage = array();
  133. $updatedb && $this->update();
  134. }
  135. function update() {
  136. if($this->uid) {
  137. $timestamp = time();
  138. $storage = !empty($this->storage) ? base64_encode((serialize($this->storage))) : '';
  139. $this->db->query("UPDATE {$this->tablepre}adminsessions SET dateline='$timestamp', errorcount='{$this->errorcount}', storage='{$storage}'
  140. WHERE uid='{$this->uid}' AND panel='$this->panel'", 'UNBUFFERED');
  141. }
  142. }
  143. }
  144. function acpmsg($message, $url = '', $type = '', $extra = '') {
  145. if(defined('IN_ADMINCP')) {
  146. !defined('CPHEADER_SHOWN') && cpheader();
  147. cpmsg($message, $url, $type, $extra);
  148. } else {
  149. showmessage($message, $url, $extra);
  150. }
  151. }
  152. function savebanlog($username, $origgroupid, $newgroupid, $expiration, $reason) {
  153. global $discuz_userss, $groupid, $onlineip, $timestamp, $forum, $reason;
  154. writelog('banlog', dhtmlspecialchars("$timestamp\t$discuz_userss\t$groupid\t$onlineip\t$username\t$origgroupid\t$newgroupid\t$expiration\t$reason"));
  155. }
  156. function clearlogstring($str) {
  157. if(!empty($str)) {
  158. if(!is_array($str)) {
  159. $str = dhtmlspecialchars(trim($str));
  160. $str = str_replace(array("\t", "\r\n", "\n", " ", " "), ' ', $str);
  161. } else {
  162. foreach ($str as $key => $val) {
  163. $str[$key] = clearlogstring($val);
  164. }
  165. }
  166. }
  167. return $str;
  168. }
  169. function implodearray($array, $skip = array()) {
  170. $return = '';
  171. if(is_array($array) && !empty($array)) {
  172. foreach ($array as $key => $value) {
  173. if(empty($skip) || !in_array($key, $skip)) {
  174. if(is_array($value)) {
  175. $return .= "$key={".implodearray($value, $skip)."}; ";
  176. } else {
  177. $return .= "$key=$value; ";
  178. }
  179. }
  180. }
  181. }
  182. return $return;
  183. }
  184. function deletethreads($tids = array()) {
  185. global $db, $tablepre, $losslessdel, $creditspolicy;
  186. static $cleartable = array(
  187. 'threadsmod', 'relatedthreads', 'posts', 'polls',
  188. 'polloptions', 'trades', 'activities', 'activityapplies', 'debates',
  189. 'debateposts', 'attachments', 'favorites', 'typeoptionvars', 'forumrecommend', 'postposition'
  190. );
  191. $threadsdel = 0;
  192. if($tids = implodeids($tids)) {
  193. $auidarray = array();
  194. $query = $db->query("SELECT uid, attachment, dateline, thumb, remote FROM {$tablepre}attachments WHERE tid IN ($tids)");
  195. while($attach = $db->fetch_array($query)) {
  196. dunlink($attach['attachment'], $attach['thumb'], $attach['remote']);
  197. if($attach['dateline'] > $losslessdel) {
  198. $auidarray[$attach['uid']] = !empty($auidarray[$attach['uid']]) ? $auidarray[$attach['uid']] + 1 : 1;
  199. }
  200. }
  201. if($auidarray) {
  202. updateattachcredits('-', $auidarray, $creditspolicy['postattach']);
  203. }
  204. foreach($cleartable as $tb) {
  205. $db->query("DELETE FROM {$tablepre}$tb WHERE tid IN ($tids)", 'UNBUFFERED');
  206. }
  207. $db->query("DELETE FROM {$tablepre}threads WHERE tid IN ($tids)");
  208. $threadsdel = $db->affected_rows();
  209. }
  210. return $threadsdel;
  211. }
  212. function undeletethreads($tids) {
  213. global $db, $tablepre, $creditspolicy;
  214. $threadsundel = 0;
  215. if($tids && is_array($tids)) {
  216. $tids = '\''.implode('\',\'', $tids).'\'';
  217. $tuidarray = $ruidarray = $fidarray = array();
  218. $query = $db->query("SELECT fid, first, authorid FROM {$tablepre}posts WHERE tid IN ($tids)");
  219. while($post = $db->fetch_array($query)) {
  220. if($post['first']) {
  221. $tuidarray[] = $post['authorid'];
  222. } else {
  223. $ruidarray[] = $post['authorid'];
  224. }
  225. if(!in_array($post['fid'], $fidarray)) {
  226. $fidarray[] = $post['fid'];
  227. }
  228. }
  229. if($tuidarray) {
  230. updatepostcredits('+', $tuidarray, $creditspolicy['post']);
  231. }
  232. if($ruidarray) {
  233. updatepostcredits('+', $ruidarray, $creditspolicy['reply']);
  234. }
  235. $db->query("UPDATE {$tablepre}posts SET invisible='0' WHERE tid IN ($tids)", 'UNBUFFERED');
  236. $db->query("UPDATE {$tablepre}threads SET displayorder='0', moderated='1' WHERE tid IN ($tids)");
  237. $threadsundel = $db->affected_rows();
  238. updatemodlog($tids, 'UDL');
  239. updatemodworks('UDL', $threadsundel);
  240. foreach($fidarray as $fid) {
  241. updateforumcount($fid);
  242. }
  243. }
  244. return $threadsundel;
  245. }
  246. ?>