SensitivewordsController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/4/19
  6. * Time: 上午10:52
  7. */
  8. namespace backend\controllers;
  9. use backend\base\CommonController;
  10. use common\models\Sensitivewords;
  11. use backend\server\WordMatcher;
  12. use backend\base\Help;
  13. use Yii;
  14. class SensitivewordsController extends CommonController
  15. {
  16. /*
  17. * 敏感词显示
  18. * */
  19. public function actionIndex()
  20. {
  21. $model = new Sensitivewords();
  22. $sensitivewords = $model->getList();
  23. return $this->render('sensitivewords',['words'=>$sensitivewords]);
  24. }
  25. /*
  26. * 敏感词数据修改
  27. * */
  28. public function actionSensitivewordseditform()
  29. {
  30. $model = new Sensitivewords();
  31. $input = Yii::$app->request->post();
  32. $input['content'] = strip_tags($input['content']);
  33. $input['info'] = strip_tags($input['info']);
  34. $rows = $model->Authenticator($input);
  35. if(is_object($rows)){
  36. if(isset($input['id']) && !empty($input['id']))
  37. {
  38. $row = $model->FindById($input['id']);
  39. $authMoel = Help::SetAttr($input,$rows,$row);
  40. if($authMoel->update() == true) { return Help::JsonCode(Help::SUCCESS,'修改成功'); }
  41. }
  42. else
  43. {
  44. if($rows->save() == true) return Help::JsonCode(Help::SUCCESS,'添加成功');
  45. }
  46. }
  47. }
  48. /**检测关键词*/
  49. public function actionCheckword()
  50. {
  51. $matcher = new WordMatcher;
  52. $sensitivewords=$this->getSensitivewords();
  53. $newswords=$this->getNewsContent(Yii::$app->request->post('content'));
  54. foreach($sensitivewords as $k=>$v){
  55. $matcher->addWord($v);
  56. }
  57. $matcher->match($newswords,$matched);
  58. if(!empty($matched)){
  59. return Help::JsonCode(Help::ERROR,'存在违禁词',$matched);
  60. }
  61. return Help::JsonCode(Help::SUCCESS,'通过检测');
  62. }
  63. private function getSensitivewords(){
  64. $model = new Sensitivewords();
  65. $sensitivewords = $model->getList();
  66. $content=urlencode($sensitivewords['content']);//将关键字编码
  67. $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);
  68. $content=urldecode($content);//将过滤后的关键字解码
  69. $sensitivewords=explode('、',$content);
  70. return $sensitivewords;
  71. }
  72. /*处理资讯内容*/
  73. private function getNewsContent($content)
  74. {
  75. //$content=$this->participle($content);
  76. //$content=array_column($content['words'], 'word');
  77. $content=urlencode($content);//将关键字编码
  78. $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);
  79. $content=urldecode($content);//将过滤后的关键字解码
  80. return $content;
  81. }
  82. }