2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-28 16:56:44 -04:00
|
|
|
class WordWatcher
|
2021-06-02 21:39:12 -04:00
|
|
|
REPLACEMENT_LETTER ||= CGI.unescape_html("■")
|
2022-08-02 07:11:08 -04:00
|
|
|
CACHE_VERSION = 2
|
2017-06-28 16:56:44 -04:00
|
|
|
|
|
|
|
def initialize(raw)
|
|
|
|
@raw = raw
|
|
|
|
end
|
|
|
|
|
2022-05-23 22:23:54 -04:00
|
|
|
@cache_enabled = true
|
2022-05-12 02:45:05 -04:00
|
|
|
|
2022-05-23 22:23:54 -04:00
|
|
|
def self.disable_cache
|
|
|
|
@cache_enabled = false
|
2022-05-12 02:45:05 -04:00
|
|
|
end
|
|
|
|
|
2022-05-23 22:23:54 -04:00
|
|
|
def self.cache_enabled?
|
|
|
|
@cache_enabled
|
2022-05-12 02:45:05 -04:00
|
|
|
end
|
|
|
|
|
2017-06-28 16:56:44 -04:00
|
|
|
def self.words_for_action(action)
|
2022-08-02 04:06:03 -04:00
|
|
|
WatchedWord
|
2021-11-17 11:59:44 -05:00
|
|
|
.where(action: WatchedWord.actions[action.to_sym])
|
|
|
|
.limit(WatchedWord::MAX_WORDS_PER_ACTION)
|
2022-06-23 09:38:12 -04:00
|
|
|
.order(:id)
|
2022-08-02 04:06:03 -04:00
|
|
|
.pluck(:word, :replacement, :case_sensitive)
|
|
|
|
.map { |w, r, c| [w, { replacement: r, case_sensitive: c }.compact] }
|
|
|
|
.to_h
|
2017-06-28 16:56:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.words_for_action_exists?(action)
|
|
|
|
WatchedWord.where(action: WatchedWord.actions[action.to_sym]).exists?
|
|
|
|
end
|
|
|
|
|
2019-07-22 07:59:56 -04:00
|
|
|
def self.get_cached_words(action)
|
2022-05-23 22:23:54 -04:00
|
|
|
if cache_enabled?
|
2022-05-12 02:45:05 -04:00
|
|
|
Discourse
|
|
|
|
.cache
|
|
|
|
.fetch(word_matcher_regexp_key(action), expires_in: 1.day) do
|
|
|
|
words_for_action(action).presence
|
|
|
|
end
|
2022-05-23 22:23:54 -04:00
|
|
|
else
|
|
|
|
words_for_action(action).presence
|
2019-07-22 07:59:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-02 04:06:03 -04:00
|
|
|
def self.serializable_word_matcher_regexp(action)
|
|
|
|
word_matcher_regexp_list(action).map { |r| { r.source => { case_sensitive: !r.casefold? } } }
|
|
|
|
end
|
|
|
|
|
2019-07-31 13:33:49 -04:00
|
|
|
# This regexp is run in miniracer, and the client JS app
|
|
|
|
# Make sure it is compatible with major browsers when changing
|
|
|
|
# hint: non-chrome browsers do not support 'lookbehind'
|
2022-08-02 04:06:03 -04:00
|
|
|
def self.word_matcher_regexp_list(action, raise_errors: false)
|
2019-07-22 07:59:56 -04:00
|
|
|
words = get_cached_words(action)
|
2022-08-02 04:06:03 -04:00
|
|
|
return [] if words.blank?
|
|
|
|
|
|
|
|
grouped_words = { case_sensitive: [], case_insensitive: [] }
|
|
|
|
|
|
|
|
words.each do |w, attrs|
|
|
|
|
word = word_to_regexp(w)
|
|
|
|
word = "(#{word})" if SiteSetting.watched_words_regular_expressions?
|
|
|
|
|
|
|
|
group_key = attrs[:case_sensitive] ? :case_sensitive : :case_insensitive
|
|
|
|
grouped_words[group_key] << word
|
|
|
|
end
|
|
|
|
|
|
|
|
regexps = grouped_words.select { |_, w| w.present? }.transform_values { |w| w.join("|") }
|
|
|
|
|
|
|
|
if !SiteSetting.watched_words_regular_expressions?
|
|
|
|
regexps.transform_values! do |regexp|
|
2019-07-22 07:59:56 -04:00
|
|
|
regexp = "(#{regexp})"
|
2022-08-02 04:06:03 -04:00
|
|
|
"(?:\\W|^)#{regexp}(?=\\W|$)"
|
2017-11-17 14:10:38 -05:00
|
|
|
end
|
2017-06-28 16:56:44 -04:00
|
|
|
end
|
2022-08-02 04:06:03 -04:00
|
|
|
|
|
|
|
regexps.map { |c, regexp| Regexp.new(regexp, c == :case_sensitive ? nil : Regexp::IGNORECASE) }
|
2021-04-21 11:16:25 -04:00
|
|
|
rescue RegexpError
|
2019-07-31 13:33:49 -04:00
|
|
|
raise if raise_errors
|
2022-08-02 04:06:03 -04:00
|
|
|
[] # Admin will be alerted via admin_dashboard_data.rb
|
2017-06-28 16:56:44 -04:00
|
|
|
end
|
|
|
|
|
2021-05-18 05:09:47 -04:00
|
|
|
def self.word_matcher_regexps(action)
|
|
|
|
if words = get_cached_words(action)
|
2022-08-02 04:06:03 -04:00
|
|
|
words.map { |w, opts| [word_to_regexp(w, whole: true), opts] }.to_h
|
2021-05-18 05:09:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-18 11:54:06 -04:00
|
|
|
def self.word_to_regexp(word, whole: false)
|
2017-11-17 14:10:38 -05:00
|
|
|
if SiteSetting.watched_words_regular_expressions?
|
2022-08-02 04:06:03 -04:00
|
|
|
# Strip ruby regexp format if present
|
2021-06-18 11:54:06 -04:00
|
|
|
regexp = word.start_with?("(?-mix:") ? word[7..-2] : word
|
|
|
|
regexp = "(#{regexp})" if whole
|
|
|
|
return regexp
|
2017-11-17 14:10:38 -05:00
|
|
|
end
|
2021-06-18 11:54:06 -04:00
|
|
|
|
|
|
|
regexp = Regexp.escape(word).gsub("\\*", '\S*')
|
|
|
|
|
|
|
|
if whole && !SiteSetting.watched_words_regular_expressions?
|
|
|
|
regexp = "(?:\\W|^)(#{regexp})(?=\\W|$)"
|
|
|
|
end
|
|
|
|
|
|
|
|
regexp
|
2017-09-27 15:48:57 -04:00
|
|
|
end
|
|
|
|
|
2017-06-28 16:56:44 -04:00
|
|
|
def self.word_matcher_regexp_key(action)
|
2022-08-02 07:11:08 -04:00
|
|
|
"watched-words-list:v#{CACHE_VERSION}:#{action}"
|
2017-06-28 16:56:44 -04:00
|
|
|
end
|
|
|
|
|
2021-06-02 21:39:12 -04:00
|
|
|
def self.censor(html)
|
2022-08-02 04:06:03 -04:00
|
|
|
regexps = word_matcher_regexp_list(:censor)
|
|
|
|
return html if regexps.blank?
|
2021-06-02 21:39:12 -04:00
|
|
|
|
|
|
|
doc = Nokogiri::HTML5.fragment(html)
|
|
|
|
doc.traverse do |node|
|
2022-08-02 04:06:03 -04:00
|
|
|
regexps.each do |regexp|
|
|
|
|
node.content = censor_text_with_regexp(node.content, regexp) if node.text?
|
|
|
|
end
|
2021-06-02 21:39:12 -04:00
|
|
|
end
|
2022-08-02 04:06:03 -04:00
|
|
|
|
2021-06-02 21:39:12 -04:00
|
|
|
doc.to_s
|
|
|
|
end
|
|
|
|
|
2022-05-25 07:51:47 -04:00
|
|
|
def self.censor_text(text)
|
2022-08-08 15:34:51 -04:00
|
|
|
return text if text.blank?
|
|
|
|
|
2022-08-02 04:06:03 -04:00
|
|
|
regexps = word_matcher_regexp_list(:censor)
|
|
|
|
return text if regexps.blank?
|
2022-05-25 07:51:47 -04:00
|
|
|
|
2022-08-02 04:06:03 -04:00
|
|
|
regexps.inject(text) { |txt, regexp| censor_text_with_regexp(txt, regexp) }
|
2022-05-25 07:51:47 -04:00
|
|
|
end
|
|
|
|
|
2022-08-08 15:34:51 -04:00
|
|
|
def self.replace_text(text)
|
|
|
|
return text if text.blank?
|
2022-07-26 11:15:42 -04:00
|
|
|
|
|
|
|
%i[replace link]
|
|
|
|
.flat_map { |type| word_matcher_regexps(type).to_a }
|
2022-08-02 04:06:03 -04:00
|
|
|
.reduce(text) do |t, (word_regexp, attrs)|
|
|
|
|
case_flag = attrs[:case_sensitive] ? nil : Regexp::IGNORECASE
|
|
|
|
replace_text_with_regexp(t, Regexp.new(word_regexp, case_flag), attrs[:replacement])
|
2022-07-26 11:15:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-08 15:34:51 -04:00
|
|
|
def self.apply_to_text(text)
|
|
|
|
text = censor_text(text)
|
|
|
|
text = replace_text(text)
|
|
|
|
text
|
|
|
|
end
|
|
|
|
|
2017-06-28 16:56:44 -04:00
|
|
|
def self.clear_cache!
|
|
|
|
WatchedWord.actions.each { |a, i| Discourse.cache.delete word_matcher_regexp_key(a) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def requires_approval?
|
|
|
|
word_matches_for_action?(:require_approval)
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_flag?
|
|
|
|
word_matches_for_action?(:flag)
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_block?
|
2019-07-22 07:59:56 -04:00
|
|
|
word_matches_for_action?(:block, all_matches: true)
|
2017-06-28 16:56:44 -04:00
|
|
|
end
|
|
|
|
|
2021-05-27 12:19:58 -04:00
|
|
|
def should_silence?
|
|
|
|
word_matches_for_action?(:silence)
|
|
|
|
end
|
|
|
|
|
2019-07-22 07:59:56 -04:00
|
|
|
def word_matches_for_action?(action, all_matches: false)
|
2022-08-02 04:06:03 -04:00
|
|
|
regexps = self.class.word_matcher_regexp_list(action)
|
|
|
|
return if regexps.blank?
|
|
|
|
|
|
|
|
match_list = []
|
|
|
|
regexps.each do |regexp|
|
2019-07-22 07:59:56 -04:00
|
|
|
match = regexp.match(@raw)
|
2022-08-02 04:06:03 -04:00
|
|
|
|
|
|
|
if !all_matches
|
|
|
|
return match if match
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
next if !match
|
2017-06-28 16:56:44 -04:00
|
|
|
|
2019-07-22 07:59:56 -04:00
|
|
|
if SiteSetting.watched_words_regular_expressions?
|
|
|
|
set = Set.new
|
|
|
|
@raw
|
|
|
|
.scan(regexp)
|
|
|
|
.each do |m|
|
|
|
|
if Array === m
|
|
|
|
set.add(m.find(&:present?))
|
|
|
|
elsif String === m
|
|
|
|
set.add(m)
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2019-07-22 07:59:56 -04:00
|
|
|
end
|
2022-08-02 04:06:03 -04:00
|
|
|
|
2019-07-22 07:59:56 -04:00
|
|
|
matches = set.to_a
|
|
|
|
else
|
|
|
|
matches = @raw.scan(regexp)
|
|
|
|
matches.flatten!
|
|
|
|
end
|
2022-08-02 04:06:03 -04:00
|
|
|
|
|
|
|
match_list.concat(matches)
|
2019-07-22 07:59:56 -04:00
|
|
|
end
|
2022-08-02 04:06:03 -04:00
|
|
|
|
|
|
|
return if match_list.blank?
|
|
|
|
|
|
|
|
match_list.compact!
|
|
|
|
match_list.uniq!
|
|
|
|
match_list.sort!
|
|
|
|
match_list
|
|
|
|
end
|
|
|
|
|
|
|
|
def word_matches?(word, case_sensitive: false)
|
|
|
|
Regexp.new(
|
|
|
|
WordWatcher.word_to_regexp(word, whole: true),
|
|
|
|
case_sensitive ? nil : Regexp::IGNORECASE,
|
|
|
|
).match?(@raw)
|
2019-07-22 07:59:56 -04:00
|
|
|
end
|
2021-03-03 03:53:38 -05:00
|
|
|
|
2022-08-02 04:06:03 -04:00
|
|
|
def self.replace_text_with_regexp(text, regexp, replacement)
|
|
|
|
text.gsub(regexp) do |match|
|
|
|
|
prefix = ""
|
|
|
|
# match may be prefixed with a non-word character from the non-capturing group
|
|
|
|
# Ensure this isn't replaced if watched words regular expression is disabled.
|
|
|
|
if !SiteSetting.watched_words_regular_expressions? && (match[0] =~ /\W/) != nil
|
|
|
|
prefix = "#{match[0]}"
|
|
|
|
end
|
|
|
|
|
|
|
|
"#{prefix}#{replacement}"
|
|
|
|
end
|
2021-03-03 03:53:38 -05:00
|
|
|
end
|
2022-05-25 07:51:47 -04:00
|
|
|
|
2022-08-02 04:06:03 -04:00
|
|
|
private_class_method :replace_text_with_regexp
|
2022-05-25 07:51:47 -04:00
|
|
|
|
|
|
|
def self.censor_text_with_regexp(text, regexp)
|
|
|
|
text.gsub(regexp) do |match|
|
|
|
|
# the regex captures leading whitespaces
|
|
|
|
padding = match.size - match.lstrip.size
|
|
|
|
if padding > 0
|
|
|
|
match[0..padding - 1] + REPLACEMENT_LETTER * (match.size - padding)
|
|
|
|
else
|
|
|
|
REPLACEMENT_LETTER * match.size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-08-02 04:06:03 -04:00
|
|
|
|
|
|
|
private_class_method :censor_text_with_regexp
|
2017-06-28 16:56:44 -04:00
|
|
|
end
|