<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/4/19
 * Time: 上午10:52
 */

namespace backend\controllers;

use backend\base\CommonController;
use common\models\Sensitivewords;
use backend\server\WordMatcher;
use backend\base\Help;
use Yii;
class SensitivewordsController  extends CommonController
{

        /*
         * 敏感词显示
         * */
        public function actionIndex()
        {
            $model = new Sensitivewords();
            $sensitivewords = $model->getList();
            return $this->render('sensitivewords',['words'=>$sensitivewords]);
        }

        /*
         *  敏感词数据修改
         * */
        public function actionSensitivewordseditform()
        {
            $model = new Sensitivewords();
            $input = Yii::$app->request->post();
            $input['content'] = strip_tags($input['content']);
            $input['info'] = strip_tags($input['info']);
            $rows = $model->Authenticator($input);
            if(is_object($rows)){
                if(isset($input['id']) && !empty($input['id']))
                {
                   $row = $model->FindById($input['id']);
                   $authMoel = Help::SetAttr($input,$rows,$row);
                   if($authMoel->update() == true) { return Help::JsonCode(Help::SUCCESS,'修改成功'); }
                }
                else
                {
                    if($rows->save() == true) return Help::JsonCode(Help::SUCCESS,'添加成功');
                }
            }
        }

     /**检测关键词*/
        public function actionCheckword()
        {
            $matcher = new WordMatcher;
            $sensitivewords=$this->getSensitivewords();
            $newswords=$this->getNewsContent(Yii::$app->request->post('content'));
            foreach($sensitivewords as $k=>$v){
                $matcher->addWord($v); 
            }
            $matcher->match($newswords,$matched);
            if(!empty($matched)){
                return Help::JsonCode(Help::ERROR,'存在违禁词',$matched);
            }
            return Help::JsonCode(Help::SUCCESS,'通过检测');
        }

        private function  getSensitivewords(){
            $model = new Sensitivewords();
            $sensitivewords = $model->getList();
            $content=urlencode($sensitivewords['content']);//将关键字编码
            $content=preg_replace("/(%7E|%60|%21|%40|%23|%24|%25|%5E|%26|%27|%2A|%28|%29|%2B|%7C|%5C|%3D|\-|_|%5B|%5D|%7D|%7B|%3B|%22|%3A|%3F|%3E|%3C|%2C|%2F|%7D|%E3%80%82|%EF%BC%81|%EF%BC%8C|%EF%BC%9B|%EF%BC%9F|%EF%BC%9A|%E3%80%81|%E2%80%A6%E2%80%A6|%E2%80%9D|%E2%80%9C|%E2%80%98|%E2%80%99)+/",'、',$content);
            $content=urldecode($content);//将过滤后的关键字解码
            $sensitivewords=explode('、',$content);
            return $sensitivewords;
        }

        /*处理资讯内容*/
        private function getNewsContent($content)
        {
            //$content=$this->participle($content);
            //$content=array_column($content['words'], 'word');
            $content=urlencode($content);//将关键字编码
            $content=preg_replace("/(%7E|%60|%21|%40|%23|%24|%25|%5E|%26|%27|%2A|%28|%29|%2B|%7C|%5C|%3D|\-|_|%5B|%5D|%7D|%7B|%3B|%22|%3A|%3F|%3E|%3C|%2C|%2F|%7D|%E3%80%82|%EF%BC%81|%EF%BC%8C|%EF%BC%9B|%EF%BC%9F|%EF%BC%9A|%E3%80%81|%E2%80%A6%E2%80%A6|%E2%80%9D|%E2%80%9C|%E2%80%98|%E2%80%99)+/",'',$content);
            $content=urldecode($content);//将过滤后的关键字解码
            return $content;
        }

}