Ajax.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\admin\controller\blog;
  3. use addons\blog\library\aip\AipContentCensor;
  4. use addons\blog\library\SensitiveHelper;
  5. use addons\blog\library\Service;
  6. use app\common\controller\Backend;
  7. /**
  8. * Ajax
  9. *
  10. * @icon fa fa-circle-o
  11. * @internal
  12. */
  13. class Ajax extends Backend
  14. {
  15. /**
  16. * 模型对象
  17. */
  18. protected $model = null;
  19. protected $noNeedRight = ['*'];
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. }
  24. /**
  25. * 获取模板列表
  26. * @internal
  27. */
  28. public function get_template_list()
  29. {
  30. $files = [];
  31. $keyValue = $this->request->request("keyValue");
  32. if (!$keyValue) {
  33. $type = $this->request->request("type");
  34. $name = $this->request->request("name");
  35. if ($name) {
  36. //$files[] = ['name' => $name . '.html'];
  37. }
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags']);
  40. $config = get_addon_config('blog');
  41. $themeDir = ADDON_PATH . 'blog' . DS . 'view' . DS . $config['theme'] . DS;
  42. $dh = opendir($themeDir);
  43. while (false !== ($filename = readdir($dh))) {
  44. if ($filename == '.' || $filename == '..') {
  45. continue;
  46. }
  47. if ($type) {
  48. $rule = $type == 'channel' ? '(channel|list)' : $type;
  49. if (!preg_match("/^{$rule}(.*)/i", $filename)) {
  50. continue;
  51. }
  52. }
  53. $files[] = ['name' => $filename];
  54. }
  55. } else {
  56. $files[] = ['name' => $keyValue];
  57. }
  58. return $result = ['total' => count($files), 'list' => $files];
  59. }
  60. /**
  61. * 检查内容是否包含违禁词
  62. * @throws \Exception
  63. */
  64. public function check_content_islegal()
  65. {
  66. $config = get_addon_config('blog');
  67. $content = $this->request->post('content');
  68. if (!$content) {
  69. $this->error(__('Please input your content'));
  70. }
  71. if ($config['audittype'] == 'local') {
  72. // 敏感词过滤
  73. $handle = SensitiveHelper::init()->setTreeByFile(ADDON_PATH . 'blog/data/words.dic');
  74. //首先检测是否合法
  75. $arr = $handle->getBadWord($content);
  76. if ($arr) {
  77. $this->error(__('The content is not legal'), null, $arr);
  78. } else {
  79. $this->success(__('The content is legal'));
  80. }
  81. } else {
  82. $client = new AipContentCensor($config['aip_appid'], $config['aip_apikey'], $config['aip_secretkey']);
  83. $result = $client->antiSpam($content);
  84. if (isset($result['result']) && $result['result']['spam'] > 0) {
  85. $arr = [];
  86. foreach (array_merge($result['result']['review'], $result['result']['reject']) as $index => $item) {
  87. $arr[] = $item['hit'];
  88. }
  89. $this->error(__('The content is not legal'), null, $arr);
  90. } else {
  91. $this->success(__('The content is legal'));
  92. }
  93. }
  94. }
  95. /**
  96. * 获取关键字
  97. * @throws \Exception
  98. */
  99. public function get_content_keywords()
  100. {
  101. $config = get_addon_config('blog');
  102. $title = $this->request->post('title');
  103. $tags = $this->request->post('tags', '');
  104. $content = $this->request->post('content');
  105. if (!$content) {
  106. $this->error(__('Please input your content'));
  107. }
  108. $keywords = Service::getContentTags($title);
  109. $keywords = in_array($title, $keywords) ? [] : $keywords;
  110. $keywords = array_filter(array_merge([$tags], $keywords));
  111. $description = mb_substr(strip_tags($content), 0, 200);
  112. $data = [
  113. "keywords" => implode(',', $keywords),
  114. "description" => $description
  115. ];
  116. $this->success("提取成功", null, $data);
  117. }
  118. /**
  119. * 获取标题拼音
  120. */
  121. public function get_title_pinyin()
  122. {
  123. $config = get_addon_config('blog');
  124. $title = $this->request->post("title");
  125. //分隔符
  126. $delimiter = $this->request->post("delimiter", "");
  127. $pinyin = new \Overtrue\Pinyin\Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
  128. if ($title) {
  129. $result = $pinyin->permalink($title, $delimiter);
  130. $this->success("", null, ['pinyin' => $result]);
  131. } else {
  132. $this->error(__('Parameter %s can not be empty', 'name'));
  133. }
  134. }
  135. }