2007-12-09 05:18:13 +00:00
|
|
|
<?php
|
2009-08-20 22:37:17 +00:00
|
|
|
/*
|
2007-12-09 05:18:13 +00:00
|
|
|
* Name: Comment Word Ban
|
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
2012-02-08 02:52:11 +00:00
|
|
|
* Link: http://code.shishnet.org/shimmie2/
|
2007-12-09 05:18:13 +00:00
|
|
|
* License: GPLv2
|
|
|
|
* Description: For stopping spam and other comment abuse
|
2009-01-16 08:18:41 +00:00
|
|
|
* Documentation:
|
|
|
|
* Allows an administrator to ban certain words
|
|
|
|
* from comments. This can be a very simple but effective way
|
|
|
|
* of stopping spam; just add "viagra", "porn", etc to the
|
|
|
|
* banned words list.
|
|
|
|
* <p>Regex bans are also supported, allowing more complicated
|
|
|
|
* bans like <code>/http:.*\.cn\//</code> to block links to
|
|
|
|
* chinese websites, or <code>/.*?http.*?http.*?http.*?http.*?/</code>
|
|
|
|
* to block comments with four (or more) links in.
|
|
|
|
* <p>Note that for non-regex matches, only whole words are
|
|
|
|
* matched, eg banning "sex" would block the comment "get free
|
|
|
|
* sex call this number", but allow "This is a photo of Bob
|
|
|
|
* from Essex"
|
2007-12-09 05:18:13 +00:00
|
|
|
*/
|
|
|
|
|
2012-02-08 12:07:01 +00:00
|
|
|
class BanWords extends Extension {
|
2010-05-28 01:07:33 +00:00
|
|
|
public function onInitExt(InitExtEvent $event) {
|
|
|
|
global $config;
|
|
|
|
$config->set_default_string('banned_words', "
|
2010-03-24 02:27:54 +00:00
|
|
|
a href=
|
|
|
|
anal
|
|
|
|
blowjob
|
|
|
|
/buy-.*-online/
|
|
|
|
casino
|
|
|
|
cialis
|
|
|
|
doors.txt
|
|
|
|
fuck
|
|
|
|
hot video
|
|
|
|
kaboodle.com
|
|
|
|
lesbian
|
|
|
|
nexium
|
|
|
|
penis
|
|
|
|
/pokerst.*/
|
|
|
|
pornhub
|
|
|
|
porno
|
|
|
|
purchase
|
|
|
|
sex
|
|
|
|
sex tape
|
|
|
|
spinnenwerk.de
|
|
|
|
thx for all
|
|
|
|
TRAMADOL
|
|
|
|
ultram
|
|
|
|
very nice site
|
2007-12-09 05:18:13 +00:00
|
|
|
viagra
|
2010-03-24 02:27:54 +00:00
|
|
|
xanax
|
2010-05-28 01:07:33 +00:00
|
|
|
");
|
|
|
|
}
|
2007-12-09 05:18:13 +00:00
|
|
|
|
2010-05-28 01:07:33 +00:00
|
|
|
public function onCommentPosting(CommentPostingEvent $event) {
|
2014-12-18 14:38:22 +00:00
|
|
|
global $user;
|
2014-11-30 13:11:01 +00:00
|
|
|
if(!$user->can("bypass_comment_checks")) {
|
|
|
|
$this->test_text($event->comment, new CommentPostingException("Comment contains banned terms"));
|
|
|
|
}
|
2012-04-05 16:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onSourceSet(SourceSetEvent $event) {
|
|
|
|
$this->test_text($event->source, new SCoreException("Source contains banned terms"));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onTagSet(TagSetEvent $event) {
|
|
|
|
$this->test_text(Tag::implode($event->tags), new SCoreException("Tags contain banned terms"));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event) {
|
|
|
|
$sb = new SetupBlock("Banned Phrases");
|
|
|
|
$sb->add_label("One per line, lines that start with slashes are treated as regex<br/>");
|
|
|
|
$sb->add_longtext_option("banned_words");
|
2014-10-15 17:31:06 +00:00
|
|
|
$failed = array();
|
|
|
|
foreach($this->get_words() as $word) {
|
|
|
|
if($word[0] == '/') {
|
|
|
|
if(preg_match($word, "") === false) {
|
|
|
|
$failed[] = $word;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($failed) {
|
|
|
|
$sb->add_label("Failed regexes: ".join(", ", $failed));
|
|
|
|
}
|
2012-04-05 16:22:06 +00:00
|
|
|
$event->panel->add_block($sb);
|
|
|
|
}
|
|
|
|
|
2017-03-09 07:02:26 +00:00
|
|
|
/**
|
|
|
|
* Throws if the comment contains banned words.
|
|
|
|
*/
|
2019-05-28 16:31:20 +00:00
|
|
|
private function test_text(string $comment, Exception $ex): void {
|
2012-04-05 16:22:06 +00:00
|
|
|
$comment = strtolower($comment);
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2014-10-15 17:31:06 +00:00
|
|
|
foreach($this->get_words() as $word) {
|
|
|
|
if($word[0] == '/') {
|
2010-05-28 01:07:33 +00:00
|
|
|
// lines that start with slash are regex
|
2014-10-15 17:31:06 +00:00
|
|
|
if(preg_match($word, $comment) === 1) {
|
2012-04-05 16:22:06 +00:00
|
|
|
throw $ex;
|
2007-12-09 05:18:13 +00:00
|
|
|
}
|
2010-05-28 01:07:33 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// other words are literal
|
|
|
|
if(strpos($comment, $word) !== false) {
|
2012-04-05 16:22:06 +00:00
|
|
|
throw $ex;
|
2007-12-09 05:18:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-28 01:07:33 +00:00
|
|
|
}
|
2007-12-09 05:18:13 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
private function get_words(): array {
|
2014-10-15 17:31:06 +00:00
|
|
|
global $config;
|
|
|
|
$words = array();
|
|
|
|
|
|
|
|
$banned = $config->get_string("banned_words");
|
|
|
|
foreach(explode("\n", $banned) as $word) {
|
|
|
|
$word = trim(strtolower($word));
|
|
|
|
if(strlen($word) == 0) {
|
|
|
|
// line is blank
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$words[] = $word;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $words;
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get_priority(): int {return 30;}
|
2007-12-09 05:18:13 +00:00
|
|
|
}
|
2014-04-25 02:34:45 +00:00
|
|
|
|