Service.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace addons\blog\library;
  3. use addons\blog\library\aip\AipContentCensor;
  4. use addons\blog\library\SensitiveHelper;
  5. class Service
  6. {
  7. /**
  8. * 检测内容是否合法
  9. * @param $content
  10. * @return bool
  11. */
  12. public static function isContentLegal($content)
  13. {
  14. $config = get_addon_config('blog');
  15. if ($config['audittype'] == 'local') {
  16. // 敏感词过滤
  17. $handle = SensitiveHelper::init()->setTreeByFile(ADDON_PATH . 'blog/data/words.dic');
  18. //首先检测是否合法
  19. $isLegal = $handle->islegal($content);
  20. return $isLegal ? true : false;
  21. } else {
  22. $client = new AipContentCensor($config['aip_appid'], $config['aip_apikey'], $config['aip_secretkey']);
  23. $result = $client->antiSpam($content);
  24. if (isset($result['result']) && $result['result']['spam'] > 0) {
  25. return false;
  26. }
  27. }
  28. return true;
  29. }
  30. /**
  31. * 内容关键字自动加链接
  32. */
  33. public static function autolinks($value)
  34. {
  35. $links = [];
  36. $value = preg_replace_callback('~(<a .*?>.*?</a>|<.*?>)~i', function ($match) use (&$links) {
  37. return '<' . array_push($links, $match[1]) . '>';
  38. }, $value);
  39. $config = get_addon_config('blog');
  40. $autolinks = $config['autolinks'];
  41. $value = preg_replace_callback('/(' . implode('|', array_keys($autolinks)) . ')/i', function ($match) use ($autolinks) {
  42. if (!isset($autolinks[$match[1]])) {
  43. return $match[0];
  44. } else {
  45. return '<a href="' . $autolinks[$match[1]] . '" target="_blank">' . $match[0] . '</a>';
  46. }
  47. }, $value);
  48. return preg_replace_callback('/<(\d+)>/', function ($match) use (&$links) {
  49. return $links[$match[1] - 1];
  50. }, $value);
  51. }
  52. }