PageRenderTime 44ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/source/class/discuz/discuz_censor.php

https://github.com/kuaileshike/upload
PHP | 111 lines | 92 code | 13 blank | 6 comment | 16 complexity | 2c503b32827ff0d905816d4ddf43cb75 MD5 | raw file
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: discuz_censor.php 31080 2012-07-13 07:03:32Z liulanbo $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. define('DISCUZ_CENSOR_SUCCEED', 0);
  12. define('DISCUZ_CENSOR_BANNED', 1);
  13. define('DISCUZ_CENSOR_MODERATED', 2);
  14. define('DISCUZ_CENSOR_REPLACED', 3);
  15. class discuz_censor {
  16. var $table = 'common_word';
  17. var $censor_words = array();
  18. var $bbcodes_display;
  19. var $result;
  20. var $words_found = array();
  21. var $highlight;
  22. public function __construct() {
  23. global $_G;
  24. loadcache(array('censor', 'bbcodes_display'));
  25. $this->censor_words = !empty($_G['cache']['censor']) ? $_G['cache']['censor'] : array();
  26. $this->bbcodes_display = $_G['cache']['bbcodes_display'][$_G['groupid']];
  27. }
  28. public static function & instance() {
  29. static $instance;
  30. if(!$instance) {
  31. $instance = new self();
  32. }
  33. return $instance;
  34. }
  35. function highlight($message, $badwords_regex) {
  36. $color = $this->highlight;
  37. if(empty($color)) {
  38. return $message;
  39. }
  40. $message = preg_replace($badwords_regex, '<span style="color: '.$color.';">\\1</span>', $message);
  41. return $message;
  42. }
  43. function check(&$message, $modword = NULL) {
  44. $limitnum = 500;
  45. $this->words_found = array();
  46. $bbcodes = 'b|i|color|size|font|align|list|indent|email|hide|quote|code|free|table|tr|td|img|swf|attach|payto|float'.($this->bbcodes_display ? '|'.implode('|', array_keys($this->bbcodes_display)) : '');
  47. if(is_array($this->censor_words['banned']) && !empty($this->censor_words['banned'])) {
  48. foreach($this->censor_words['banned'] as $banned_words) {
  49. if(preg_match_all($banned_words, @preg_replace(array("/\[($bbcodes)=?.*\]/iU", "/\[\/($bbcodes)\]/i"), '', $message), $matches)) {
  50. $this->words_found = $matches[0];
  51. $this->result = DISCUZ_CENSOR_BANNED;
  52. $this->words_found = array_unique($this->words_found);
  53. $message = $this->highlight($message, $banned_words);
  54. return DISCUZ_CENSOR_BANNED;
  55. }
  56. }
  57. }
  58. if(is_array($this->censor_words['mod']) && !empty($this->censor_words['mod'])) {
  59. if($modword !== NULL) {
  60. $message = preg_replace($this->censor_words['mod'], $modword, $message);
  61. }
  62. foreach($this->censor_words['mod'] as $mod_words) {
  63. if(preg_match_all($mod_words, @preg_replace(array("/\[($bbcodes)=?.*\]/iU", "/\[\/($bbcodes)\]/i"), '', $message), $matches)) {
  64. $this->words_found = $matches[0];
  65. $this->result = DISCUZ_CENSOR_MODERATED;
  66. $message = $this->highlight($message, $mod_words);
  67. $this->words_found = array_unique($this->words_found);
  68. return DISCUZ_CENSOR_MODERATED;
  69. }
  70. }
  71. }
  72. if(!empty($this->censor_words['filter'])) {
  73. $i = 0;
  74. while($find_words = array_slice($this->censor_words['filter']['find'], $i, $limitnum)) {
  75. if(empty($find_words)) break;
  76. $replace_words = array_slice($this->censor_words['filter']['replace'], $i, $limitnum);
  77. $i += $limitnum;
  78. $message = preg_replace($find_words, $replace_words, $message);
  79. }
  80. $this->result = DISCUZ_CENSOR_REPLACED;
  81. return DISCUZ_CENSOR_REPLACED;
  82. }
  83. $this->result = DISCUZ_CENSOR_SUCCEED;
  84. return DISCUZ_CENSOR_SUCCEED;
  85. }
  86. function modbanned() {
  87. return $this->result == DISCUZ_CENSOR_BANNED;
  88. }
  89. function modmoderated() {
  90. return $this->result == DISCUZ_CENSOR_MODERATED;
  91. }
  92. function modreplaced() {
  93. return $this->result == DISCUZ_CENSOR_REPLACED;
  94. }
  95. function modsucceed() {
  96. return $this->result == DISCUZ_CENSOR_SUCCEED;
  97. }
  98. }