123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace app\admin\controller\blog;
- use addons\blog\library\aip\AipContentCensor;
- use addons\blog\library\SensitiveHelper;
- use addons\blog\library\Service;
- use app\common\controller\Backend;
- /**
- * Ajax
- *
- * @icon fa fa-circle-o
- * @internal
- */
- class Ajax extends Backend
- {
- /**
- * 模型对象
- */
- protected $model = null;
- protected $noNeedRight = ['*'];
- public function _initialize()
- {
- parent::_initialize();
- }
- /**
- * 获取模板列表
- * @internal
- */
- public function get_template_list()
- {
- $files = [];
- $keyValue = $this->request->request("keyValue");
- if (!$keyValue) {
- $type = $this->request->request("type");
- $name = $this->request->request("name");
- if ($name) {
- //$files[] = ['name' => $name . '.html'];
- }
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- $config = get_addon_config('blog');
- $themeDir = ADDON_PATH . 'blog' . DS . 'view' . DS . $config['theme'] . DS;
- $dh = opendir($themeDir);
- while (false !== ($filename = readdir($dh))) {
- if ($filename == '.' || $filename == '..') {
- continue;
- }
- if ($type) {
- $rule = $type == 'channel' ? '(channel|list)' : $type;
- if (!preg_match("/^{$rule}(.*)/i", $filename)) {
- continue;
- }
- }
- $files[] = ['name' => $filename];
- }
- } else {
- $files[] = ['name' => $keyValue];
- }
- return $result = ['total' => count($files), 'list' => $files];
- }
- /**
- * 检查内容是否包含违禁词
- * @throws \Exception
- */
- public function check_content_islegal()
- {
- $config = get_addon_config('blog');
- $content = $this->request->post('content');
- if (!$content) {
- $this->error(__('Please input your content'));
- }
- if ($config['audittype'] == 'local') {
- // 敏感词过滤
- $handle = SensitiveHelper::init()->setTreeByFile(ADDON_PATH . 'blog/data/words.dic');
- //首先检测是否合法
- $arr = $handle->getBadWord($content);
- if ($arr) {
- $this->error(__('The content is not legal'), null, $arr);
- } else {
- $this->success(__('The content is legal'));
- }
- } else {
- $client = new AipContentCensor($config['aip_appid'], $config['aip_apikey'], $config['aip_secretkey']);
- $result = $client->antiSpam($content);
- if (isset($result['result']) && $result['result']['spam'] > 0) {
- $arr = [];
- foreach (array_merge($result['result']['review'], $result['result']['reject']) as $index => $item) {
- $arr[] = $item['hit'];
- }
- $this->error(__('The content is not legal'), null, $arr);
- } else {
- $this->success(__('The content is legal'));
- }
- }
- }
- /**
- * 获取关键字
- * @throws \Exception
- */
- public function get_content_keywords()
- {
- $config = get_addon_config('blog');
- $title = $this->request->post('title');
- $tags = $this->request->post('tags', '');
- $content = $this->request->post('content');
- if (!$content) {
- $this->error(__('Please input your content'));
- }
- $keywords = Service::getContentTags($title);
- $keywords = in_array($title, $keywords) ? [] : $keywords;
- $keywords = array_filter(array_merge([$tags], $keywords));
- $description = mb_substr(strip_tags($content), 0, 200);
- $data = [
- "keywords" => implode(',', $keywords),
- "description" => $description
- ];
- $this->success("提取成功", null, $data);
- }
- /**
- * 获取标题拼音
- */
- public function get_title_pinyin()
- {
- $config = get_addon_config('blog');
- $title = $this->request->post("title");
- //分隔符
- $delimiter = $this->request->post("delimiter", "");
- $pinyin = new \Overtrue\Pinyin\Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
- if ($title) {
- $result = $pinyin->permalink($title, $delimiter);
- $this->success("", null, ['pinyin' => $result]);
- } else {
- $this->error(__('Parameter %s can not be empty', 'name'));
- }
- }
- }
|