Security.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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: Security.php 33923 2013-09-03 02:59:43Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. Cloud::loadFile('Service_Server_Restful');
  12. class Cloud_Service_Server_Security extends Cloud_Service_Server_Restful {
  13. protected static $_instance;
  14. public static function getInstance() {
  15. if (!(self::$_instance instanceof self)) {
  16. self::$_instance = new self();
  17. }
  18. return self::$_instance;
  19. }
  20. public function onSecuritySetEvilPost($data) {
  21. $results = array();
  22. foreach ($data as $evilPost) {
  23. $results[] = $this->_handleEvilPost($evilPost['tid'], $evilPost['pid'], $evilPost['evilType'], $evilPost['evilLevel']);
  24. }
  25. return $results;
  26. }
  27. public function onSecuritySetEvilUser($data, $days = 1) {
  28. $results = array();
  29. foreach ($data as $evilUser) {
  30. $results[] = $this->_handleEvilUser($evilUser['uid'], $evilUser['evilType'], $evilUser['evilLevel'], $days);
  31. }
  32. return $results;
  33. }
  34. protected function _handleEvilPost($tid, $pid, $evilType, $evilLevel = 1) {
  35. include_once DISCUZ_ROOT.'./source/language/lang_admincp_cloud.php';
  36. $securityService = Cloud::loadClass('Service_Security');
  37. $securityService->writeLog($pid, 'pid');
  38. $evilPost = C::t('#security#security_evilpost')->fetch($pid);
  39. if (count($evilPost)) {
  40. return true;
  41. } else {
  42. require_once libfile('function/delete');
  43. require_once libfile('function/forum');
  44. require_once libfile('function/post');
  45. $data = array('pid' => $pid, 'tid' => $tid, 'evilcount' => 1, 'eviltype' => $evilType, 'createtime' => TIMESTAMP);
  46. $post = get_post_by_pid($pid);
  47. if (is_array($post) && count($post) > 0) {
  48. if ($tid != $post['tid']) {
  49. return false;
  50. }
  51. $thread = get_thread_by_tid($tid);
  52. if ($post['first']) {
  53. $data['type'] = 1;
  54. if ($this->_checkThreadIgnore($tid)) {
  55. return false;
  56. }
  57. C::t('#security#security_evilpost')->insert($data, false, true);
  58. $this->_updateEvilCount('thread');
  59. deletethread(array($tid), true, true, true);
  60. updatemodlog($tid, 'DEL', 0, 1, $extend_lang['security_modreason']);
  61. } else {
  62. $data['type'] = 0;
  63. if ($this->_checkPostIgnore($pid, $post)) {
  64. return false;
  65. }
  66. C::t('#security#security_evilpost')->insert($data, false, true);
  67. $this->_updateEvilCount('post');
  68. deletepost(array($pid), 'pid', true, false, true);
  69. }
  70. if(!empty($post['authorid'])) {
  71. $data = array('uid' => $post['authorid'], 'createtime' => TIMESTAMP);
  72. C::t('#security#security_eviluser')->insert($data, false, true);
  73. }
  74. } else {
  75. $data['operateresult'] = 2;
  76. C::t('#security#security_evilpost')->insert($data, false, true);
  77. }
  78. if($evilLevel >= 5) {
  79. $user = C::t('common_member')->fetch($post['authorid'], 0, 1);
  80. $this->_handleBandUser($user, 1);
  81. }
  82. }
  83. return true;
  84. }
  85. protected function _handleBandUser($user, $days = 1) {
  86. $uid = $user['uid'];
  87. if($this->_checkUserIgnore($uid)) {
  88. return false;
  89. }
  90. require_once libfile('function/forum');
  91. $setarr = array('groupid' => 4);
  92. if($days) {
  93. $days = !empty($days) ? TIMESTAMP + $days * 86400 : 0;
  94. $days = $days > TIMESTAMP ? $days : 0;
  95. if($days) {
  96. $user['groupterms']['main'] = array('time' => $days, 'adminid' => $user['adminid'], 'groupid' => $user['groupid']);
  97. $user['groupterms']['ext'][4] = $days;
  98. C::t('common_member_field_forum')->update($uid, array('groupterms' => serialize($user['groupterms'])));
  99. $setarr['groupexpiry'] = groupexpiry($user['groupterms']);
  100. } else {
  101. $setarr['groupexpiry'] = 0;
  102. }
  103. }
  104. require_once libfile('function/misc');
  105. return C::t('common_member')->update($uid, $setarr);
  106. }
  107. protected function _handleEvilUser($uid, $evilType, $evilLevel = 1, $days = 1) {
  108. global $_G;
  109. include_once DISCUZ_ROOT.'./source/language/lang_admincp_cloud.php';
  110. $securityService = Cloud::loadClass('Service_Security');
  111. $securityService->writeLog($uid, 'uid');
  112. if($this->_checkUserIgnore($uid)) {
  113. return false;
  114. }
  115. $user = C::t('common_member')->fetch($uid, 0, 1);
  116. if(is_array($user)) {
  117. $update = $this->_handleBandUser($user, $days);
  118. if ($update) {
  119. $_G['member']['username'] = 'SYSTEM';
  120. savebanlog($user['username'], $user['groupid'], 4, 0, $extend_lang['security_modreason']);
  121. }
  122. }
  123. $evilUser = C::t('#security#security_eviluser')->fetch($uid);
  124. if (count($evilUser)) {
  125. return true;
  126. } else {
  127. $data = array('uid' => $uid, 'evilcount' => 1, 'eviltype' => $evilType, 'createtime' => TIMESTAMP);
  128. C::t('#security#security_eviluser')->insert($data, false, true);
  129. $this->_updateEvilCount('member');
  130. }
  131. return true;
  132. }
  133. protected function _checkThreadIgnore($tid) {
  134. if (!intval($tid)) {
  135. return true;
  136. }
  137. require_once libfile('function/forum');
  138. $checkFiled = array('highlight', 'displayorder', 'digest');
  139. $thread = get_thread_by_tid($tid);
  140. $checkResult = false;
  141. $checkResult = $this->_checkBoardIgnore($thread['fid']);
  142. $checkResult = $checkResult ? true : $this->_checkUserIgnore($thread['authorid']);
  143. foreach ($checkFiled as $field) {
  144. if ($thread[$field] > 0) {
  145. $checkResult = true;
  146. };
  147. }
  148. return $checkResult;
  149. }
  150. protected function _updateEvilCount($type) {
  151. if (empty($type)) {
  152. return false;
  153. }
  154. $settingKey = 'cloud_security_stats_' . $type;
  155. $count = intval(C::t('common_setting')->fetch($settingKey));
  156. C::t('common_setting')->update($settingKey, $count + 1);
  157. }
  158. protected function _checkPostIgnore($pid, $post) {
  159. if (!intval($pid)) {
  160. return true;
  161. }
  162. $checkResult = false;
  163. $checkResult = $this->_checkBoardIgnore($post['fid']);
  164. $checkResult = $checkResult ? true : $this->_checkUserIgnore($post['authorid']);
  165. $postStick = C::t('forum_poststick')->count_by_pid($pid);
  166. if ($checkResult || $postStick) {
  167. $checkResult = true;
  168. }
  169. return $checkResult;
  170. }
  171. protected function _checkBoardIgnore($fid) {
  172. global $_G;
  173. $checkResult = false;
  174. $whiteList = $_G['setting']['security_forums_white_list'];
  175. $whiteList = is_array($whiteList) ? $whiteList : array();
  176. if (in_array($fid, $whiteList)) {
  177. $checkResult = true;
  178. }
  179. return $checkResult;
  180. }
  181. protected function _checkUserIgnore($uid) {
  182. global $_G;
  183. if (!intval($uid)) {
  184. return true;
  185. }
  186. $whiteList = $_G['setting']['security_usergroups_white_list'];
  187. $whiteList = is_array($whiteList) ? $whiteList : array();
  188. $memberInfo = C::t('common_member')->fetch($uid, 0, 1);
  189. $checkResult = false;
  190. if (in_array($memberInfo['groupid'], $whiteList)) {
  191. $checkResult = true;
  192. }
  193. return $checkResult;
  194. }
  195. }