Guest User

Untitled

a guest
Jun 23rd, 2018
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.95 KB | None | 0 0
  1. <?php
  2.  
  3. !defined('IN_DISCUZ') && exit('ACCESS DENIED');
  4.  
  5. //引入阿里云SDK
  6. include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
  7. use Green\Request\V20170112 as AliGreen;
  8.  
  9. class plugin_acsecurity
  10. {
  11.  
  12. /**
  13. * 反垃圾请求接口简单封装
  14. *
  15. * $thread_id 主贴id
  16. * $post_id 内容id
  17. * $ip 请求ip
  18. * $account 用户账号id
  19. * $title 标题
  20. * $content 当前发表的内容
  21. */
  22. public function check($thread_id, $post_id, $ip, $account, $title, $content)
  23. {
  24. global $_G;
  25. $api_region = $_G['cache']['plugin']['acsecurity']['api_region'];
  26. $accesskey_id = $_G['cache']['plugin']['acsecurity']['accesskey_id'];
  27. $accesskey_secret = $_G['cache']['plugin']['acsecurity']['accesskey_secret'];
  28.  
  29. $iClientProfile = DefaultProfile::getProfile($api_region, $accesskey_id, $accesskey_secret);
  30. DefaultProfile::addEndpoint($api_region, $api_region, "Green", "green." . $api_region . ".aliyuncs.com");
  31.  
  32. $client = new DefaultAcsClient($iClientProfile);
  33.  
  34. $request = new AliGreen\TextScanRequest();
  35. $request->setMethod("POST");
  36. $request->setAcceptFormat("JSON");
  37. $request->setContent(json_encode([
  38. "tasks" => [
  39. 'dataId' => uniqid(),
  40. 'content' => "《" . $title . "》\n\n\n" . $content,
  41. ],
  42. "scenes" => ["antispam"],
  43. ]));
  44.  
  45. $response = $client->getAcsResponse($request);
  46. if (200 == $response->code) {
  47. list($taskResult) = $response->data;
  48. if (200 == $taskResult->code) {
  49. list($sceneResult) = $taskResult->results;
  50. $suggestion = $sceneResult->suggestion;
  51. if ('review' === $suggestion || 'block' === $suggestion) {
  52. $labels = $this->getDetailsLabels($sceneResult->details);
  53. $labelTexts = "(" . $this->getLabelTexts($labels, true) . ")";
  54. } else {
  55. $labels = [];
  56. $labelTexts = "";
  57. }
  58. switch ($suggestion) {
  59. case 'pass':
  60. return ['pass', ""];
  61. break;
  62. case 'review':
  63. return ['review', "您发布的信息可能包含敏感内容" . $labelTexts . ",已提交人工审核,请您谅解。"];
  64. break;
  65. case 'block':
  66. return ['block', "您发布的信息可能包含敏感内容" . $labelTexts . ",请重新编辑后再发布!"];
  67. break;
  68. default:
  69. return ['review', '由于安全系统故障,您发布的内容无法自动审核,待人工审核后才能正常访问,请您谅解。'];
  70. break;
  71. }
  72. }
  73. }
  74. return ['review', '由于安全系统故障,您发布的内容无法自动审核,待人工审核后才能正常访问,请您谅解。'];
  75. }
  76.  
  77. public function getDetailsLabels($details)
  78. {
  79. return array_map(function ($detail) {
  80. return $detail->label;
  81. }, $details);
  82. }
  83.  
  84. public function getLabelTexts($labels, $arr2text = false, $glue = "、")
  85. {
  86. $labels = array_map(function ($label) {
  87. switch ($label) {
  88. case "normal":return "正常文本";
  89. break;
  90. case "spam":return "含垃圾信息";
  91. break;
  92. case "ad":return "广告";
  93. break;
  94. case "politics":return "政治";
  95. break;
  96. case "terrorism":return "暴恐";
  97. break;
  98. case "abuse":return "辱骂";
  99. break;
  100. case "porn":return "色情";
  101. break;
  102. case "flood":return "灌水";
  103. break;
  104. case "contraband":return "违禁";
  105. break;
  106. case "meaningless":return "无意义";
  107. break;
  108. //case "customized":;break;
  109. default:return "其他不适合展示的内容";
  110. break;
  111. }
  112. }, $labels);
  113. if ($arr2text) {
  114. return implode($glue, $labels);
  115. } else {
  116. return $labels;
  117. }
  118. }
  119.  
  120. function handler($params){
  121. global $_G;
  122. if (!$_G['cache']['plugin']['acsecurity']['enable_flag']) {
  123. return;
  124. }
  125. list($message, $returnurl, $post) = $params['param'];
  126.  
  127. $thread_id = $post['tid'];
  128. $post_id = $post['pid'];
  129. $ip = $_G['clientip'];
  130. $account = $_G['member']['uid'];
  131. $title = diconv($_GET['subject'], CHARSET, 'UTF-8');
  132. $content = diconv($_GET['message'], CHARSET, 'UTF-8');
  133.  
  134. list($action, $alert) = $this->check($thread_id, $post_id, $ip, $account, $title, $content);
  135. if ("block" == $action) {
  136. $this->delete($message, $thread_id, $post_id, $alert);
  137. } else if ("review" == $action) {
  138. $this->review($message, $thread_id, $post_id, $alert);
  139. }
  140. }
  141.  
  142. /**
  143. * 删除主题/回复
  144. * $message 发表成功后返回的字符串
  145. * $thread_id 主题id
  146. * $post_id 回帖id
  147. */
  148. public function delete($message, $thread_id, $post_id, $alert)
  149. {
  150. global $_G;
  151. require_once libfile('function/delete');
  152. if ($this->isreply($message)) {
  153. deletepost(array($post_id), 'pid', true, false, true);
  154. // function deletepost($ids, $idtype = 'pid', $credit = false, $posttableid = false, $recycle = false)
  155. manage_addnotify('verifyrecyclepost', $modpostsnum);
  156. updatethreadcount($_G['tid'], 1);
  157. updateforumcount($_G['fid']);
  158. $_G['forum']['threadcaches'] && deletethreadcaches($_G['tid']);
  159.  
  160. showmessage($alert, null, null, array('alert' => 'error'));
  161. } else if ($this->isthread($message)) {
  162. deletethread(array($thread_id), true, true, true);
  163. // function deletethread($tids, $membercount = false, $credit = false, $ponly = false)
  164. updateforumcount($_G['fid']);
  165.  
  166. showmessage($alert, null, null, array('alert' => 'error'));
  167. }
  168. }
  169.  
  170. public function review($message, $thread_id, $post_id, $alert)
  171. {
  172. global $_G;
  173. if ($this->isreply($message)) {
  174. \helper_form::updatemoderate('pid', [$post_id], 0);
  175. manage_addnotify('verifypost');
  176.  
  177. updatethreadcount($_G['tid'], 1);
  178. updateforumcount($_G['fid']);
  179. $_G['forum']['threadcaches'] && deletethreadcaches($_G['tid']);
  180.  
  181. showmessage($alert, null, null, array('alert' => 'info'));
  182. } else if ($this->isthread($message)) {
  183. \helper_form::updatemoderate('tid', [$thread_id], 0);
  184. manage_addnotify('verifythread');
  185. showmessage($alert, null, null, array('alert' => 'info'));
  186. }
  187. }
  188.  
  189. /**
  190. * 从返回的结果字符串判定当前发表/编辑的是否是主题贴
  191. * $message 发表之后返回的字符串
  192. */
  193. public function isreply($message)
  194. {
  195. $reply_msgs = array('post_edit_succeed', 'post_reply_succeed', 'post_reply_mod_succeed', 'edit_reply_mod_succeed');
  196. return in_array($message, $reply_msgs, true);
  197. }
  198.  
  199. public function isthread($message)
  200. {
  201. $thread_msgs = array('post_newthread_succeed', 'post_newthread_mod_succeed', 'edit_newthread_mod_succeed');
  202. return in_array($message, $thread_msgs, true);
  203. }
  204. }
  205.  
  206. class plugin_acsecurity_forum extends plugin_acsecurity
  207. {
  208. public function post_dz()
  209. {}
  210. public function post_dz_message($params)
  211. {
  212. return $this->handler($params);
  213. }
  214. }
  215.  
  216. class plugin_acsecurity_group extends plugin_acsecurity
  217. {
  218. public function post()
  219. {
  220. }
  221. public function post_message($params)
  222. {
  223. return $this->handler($params);
  224. }
  225. }
Add Comment
Please, Sign In to add comment