PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/source/class/class_censor.php

https://github.com/jinbo51/DiscuzX
PHP | 107 lines | 89 code | 12 blank | 6 comment | 15 complexity | 01468ee6113241a366f161b926f6a919 MD5 | raw file
Possible License(s): BSD-3-Clause
  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: class_censor.php 21358 2011-03-24 01:27:41Z monkey $
  7. */
  8. define('DISCUZ_CENSOR_SUCCEED', 0);
  9. define('DISCUZ_CENSOR_BANNED', 1);
  10. define('DISCUZ_CENSOR_MODERATED', 2);
  11. define('DISCUZ_CENSOR_REPLACED', 3);
  12. class discuz_censor {
  13. var $table = 'common_word';
  14. var $censor_words = array();
  15. var $bbcodes_display;
  16. var $result;
  17. var $words_found = array();
  18. var $highlight;
  19. function discuz_censor() {
  20. global $_G;
  21. loadcache(array('censor', 'bbcodes_display'));
  22. $this->censor_words = !empty($_G['cache']['censor']) ? $_G['cache']['censor'] : array();
  23. $this->bbcodes_display = $_G['cache']['bbcodes_display'][$_G['groupid']];
  24. }
  25. function & instance() {
  26. static $instance;
  27. if(!$instance) {
  28. $instance = new discuz_censor();
  29. }
  30. return $instance;
  31. }
  32. function highlight($message, $badwords_regex) {
  33. $color = $this->highlight;
  34. if(empty($color)) {
  35. return $message;
  36. }
  37. $message = preg_replace($badwords_regex, '<span style="color: '.$color.';">\\1</span>', $message);
  38. return $message;
  39. }
  40. function check(&$message, $modword = NULL) {
  41. $limitnum = 1000;
  42. $this->words_found = array();
  43. $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)) : '');
  44. if(is_array($this->censor_words['banned']) && !empty($this->censor_words['banned'])) {
  45. foreach($this->censor_words['banned'] as $banned_words) {
  46. if(preg_match_all($banned_words, @preg_replace(array("/\[($bbcodes)=?.*\]/iU", "/\[\/($bbcodes)\]/i"), '', $message), $matches)) {
  47. $this->words_found = $matches[0];
  48. $this->result = DISCUZ_CENSOR_BANNED;
  49. $this->words_found = array_unique($this->words_found);
  50. $message = $this->highlight($message, $banned_words);
  51. return DISCUZ_CENSOR_BANNED;
  52. }
  53. }
  54. }
  55. if(is_array($this->censor_words['mod']) && !empty($this->censor_words['mod'])) {
  56. if($modword !== NULL) {
  57. $message = preg_replace($this->censor_words['mod'], $modword, $message);
  58. }
  59. foreach($this->censor_words['mod'] as $mod_words) {
  60. if(preg_match_all($mod_words, @preg_replace(array("/\[($bbcodes)=?.*\]/iU", "/\[\/($bbcodes)\]/i"), '', $message), $matches)) {
  61. $this->words_found = $matches[0];
  62. $this->result = DISCUZ_CENSOR_MODERATED;
  63. $message = $this->highlight($message, $mod_words);
  64. $this->words_found = array_unique($this->words_found);
  65. return DISCUZ_CENSOR_MODERATED;
  66. }
  67. }
  68. }
  69. if(!empty($this->censor_words['filter'])) {
  70. $i = 0;
  71. while($find_words = array_slice($this->censor_words['filter']['find'], $i, $limitnum)) {
  72. if(empty($find_words)) break;
  73. $replace_words = array_slice($this->censor_words['filter']['replace'], $i, $limitnum);
  74. $i += $limitnum;
  75. $message = preg_replace($find_words, $replace_words, $message);
  76. }
  77. $this->result = DISCUZ_CENSOR_REPLACED;
  78. return DISCUZ_CENSOR_REPLACED;
  79. }
  80. $this->result = DISCUZ_CENSOR_SUCCEED;
  81. return DISCUZ_CENSOR_SUCCEED;
  82. }
  83. function modbanned() {
  84. return $this->result == DISCUZ_CENSOR_BANNED;
  85. }
  86. function modmoderated() {
  87. return $this->result == DISCUZ_CENSOR_MODERATED;
  88. }
  89. function modreplaced() {
  90. return $this->result == DISCUZ_CENSOR_REPLACED;
  91. }
  92. function modsucceed() {
  93. return $this->result == DISCUZ_CENSOR_SUCCEED;
  94. }
  95. }