Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- !defined('IN_DISCUZ') && exit('ACCESS DENIED');
- //引入阿里云SDK
- include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
- use Green\Request\V20170112 as AliGreen;
- class plugin_acsecurity
- {
- /**
- * 反垃圾请求接口简单封装
- *
- * $thread_id 主贴id
- * $post_id 内容id
- * $ip 请求ip
- * $account 用户账号id
- * $title 标题
- * $content 当前发表的内容
- */
- public function check($thread_id, $post_id, $ip, $account, $title, $content)
- {
- global $_G;
- $api_region = $_G['cache']['plugin']['acsecurity']['api_region'];
- $accesskey_id = $_G['cache']['plugin']['acsecurity']['accesskey_id'];
- $accesskey_secret = $_G['cache']['plugin']['acsecurity']['accesskey_secret'];
- $iClientProfile = DefaultProfile::getProfile($api_region, $accesskey_id, $accesskey_secret);
- DefaultProfile::addEndpoint($api_region, $api_region, "Green", "green." . $api_region . ".aliyuncs.com");
- $client = new DefaultAcsClient($iClientProfile);
- $request = new AliGreen\TextScanRequest();
- $request->setMethod("POST");
- $request->setAcceptFormat("JSON");
- $request->setContent(json_encode([
- "tasks" => [
- 'dataId' => uniqid(),
- 'content' => "《" . $title . "》\n\n\n" . $content,
- ],
- "scenes" => ["antispam"],
- ]));
- $response = $client->getAcsResponse($request);
- if (200 == $response->code) {
- list($taskResult) = $response->data;
- if (200 == $taskResult->code) {
- list($sceneResult) = $taskResult->results;
- $suggestion = $sceneResult->suggestion;
- if ('review' === $suggestion || 'block' === $suggestion) {
- $labels = $this->getDetailsLabels($sceneResult->details);
- $labelTexts = "(" . $this->getLabelTexts($labels, true) . ")";
- } else {
- $labels = [];
- $labelTexts = "";
- }
- switch ($suggestion) {
- case 'pass':
- return ['pass', ""];
- break;
- case 'review':
- return ['review', "您发布的信息可能包含敏感内容" . $labelTexts . ",已提交人工审核,请您谅解。"];
- break;
- case 'block':
- return ['block', "您发布的信息可能包含敏感内容" . $labelTexts . ",请重新编辑后再发布!"];
- break;
- default:
- return ['review', '由于安全系统故障,您发布的内容无法自动审核,待人工审核后才能正常访问,请您谅解。'];
- break;
- }
- }
- }
- return ['review', '由于安全系统故障,您发布的内容无法自动审核,待人工审核后才能正常访问,请您谅解。'];
- }
- public function getDetailsLabels($details)
- {
- return array_map(function ($detail) {
- return $detail->label;
- }, $details);
- }
- public function getLabelTexts($labels, $arr2text = false, $glue = "、")
- {
- $labels = array_map(function ($label) {
- switch ($label) {
- case "normal":return "正常文本";
- break;
- case "spam":return "含垃圾信息";
- break;
- case "ad":return "广告";
- break;
- case "politics":return "政治";
- break;
- case "terrorism":return "暴恐";
- break;
- case "abuse":return "辱骂";
- break;
- case "porn":return "色情";
- break;
- case "flood":return "灌水";
- break;
- case "contraband":return "违禁";
- break;
- case "meaningless":return "无意义";
- break;
- //case "customized":;break;
- default:return "其他不适合展示的内容";
- break;
- }
- }, $labels);
- if ($arr2text) {
- return implode($glue, $labels);
- } else {
- return $labels;
- }
- }
- function handler($params){
- global $_G;
- if (!$_G['cache']['plugin']['acsecurity']['enable_flag']) {
- return;
- }
- list($message, $returnurl, $post) = $params['param'];
- $thread_id = $post['tid'];
- $post_id = $post['pid'];
- $ip = $_G['clientip'];
- $account = $_G['member']['uid'];
- $title = diconv($_GET['subject'], CHARSET, 'UTF-8');
- $content = diconv($_GET['message'], CHARSET, 'UTF-8');
- list($action, $alert) = $this->check($thread_id, $post_id, $ip, $account, $title, $content);
- if ("block" == $action) {
- $this->delete($message, $thread_id, $post_id, $alert);
- } else if ("review" == $action) {
- $this->review($message, $thread_id, $post_id, $alert);
- }
- }
- /**
- * 删除主题/回复
- * $message 发表成功后返回的字符串
- * $thread_id 主题id
- * $post_id 回帖id
- */
- public function delete($message, $thread_id, $post_id, $alert)
- {
- global $_G;
- require_once libfile('function/delete');
- if ($this->isreply($message)) {
- deletepost(array($post_id), 'pid', true, false, true);
- // function deletepost($ids, $idtype = 'pid', $credit = false, $posttableid = false, $recycle = false)
- manage_addnotify('verifyrecyclepost', $modpostsnum);
- updatethreadcount($_G['tid'], 1);
- updateforumcount($_G['fid']);
- $_G['forum']['threadcaches'] && deletethreadcaches($_G['tid']);
- showmessage($alert, null, null, array('alert' => 'error'));
- } else if ($this->isthread($message)) {
- deletethread(array($thread_id), true, true, true);
- // function deletethread($tids, $membercount = false, $credit = false, $ponly = false)
- updateforumcount($_G['fid']);
- showmessage($alert, null, null, array('alert' => 'error'));
- }
- }
- public function review($message, $thread_id, $post_id, $alert)
- {
- global $_G;
- if ($this->isreply($message)) {
- \helper_form::updatemoderate('pid', [$post_id], 0);
- manage_addnotify('verifypost');
- updatethreadcount($_G['tid'], 1);
- updateforumcount($_G['fid']);
- $_G['forum']['threadcaches'] && deletethreadcaches($_G['tid']);
- showmessage($alert, null, null, array('alert' => 'info'));
- } else if ($this->isthread($message)) {
- \helper_form::updatemoderate('tid', [$thread_id], 0);
- manage_addnotify('verifythread');
- showmessage($alert, null, null, array('alert' => 'info'));
- }
- }
- /**
- * 从返回的结果字符串判定当前发表/编辑的是否是主题贴
- * $message 发表之后返回的字符串
- */
- public function isreply($message)
- {
- $reply_msgs = array('post_edit_succeed', 'post_reply_succeed', 'post_reply_mod_succeed', 'edit_reply_mod_succeed');
- return in_array($message, $reply_msgs, true);
- }
- public function isthread($message)
- {
- $thread_msgs = array('post_newthread_succeed', 'post_newthread_mod_succeed', 'edit_newthread_mod_succeed');
- return in_array($message, $thread_msgs, true);
- }
- }
- class plugin_acsecurity_forum extends plugin_acsecurity
- {
- public function post_dz()
- {}
- public function post_dz_message($params)
- {
- return $this->handler($params);
- }
- }
- class plugin_acsecurity_group extends plugin_acsecurity
- {
- public function post()
- {
- }
- public function post_message($params)
- {
- return $this->handler($params);
- }
- }
Add Comment
Please, Sign In to add comment