This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/ban_words/main.php

126 lines
2.8 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
class BanWords extends Extension
{
public function onInitExt(InitExtEvent $event): void
{
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
viagra
2010-03-24 02:27:54 +00:00
xanax
");
}
public function onCommentPosting(CommentPostingEvent $event): void
{
global $user;
2019-07-09 14:10:21 +00:00
if (!$user->can(Permissions::BYPASS_COMMENT_CHECKS)) {
$this->test_text($event->comment, new CommentPostingException("Comment contains banned terms"));
}
}
public function onSourceSet(SourceSetEvent $event): void
{
2024-02-11 15:47:40 +00:00
$this->test_text($event->source, new UserError("Source contains banned terms"));
}
public function onTagSet(TagSetEvent $event): void
{
2024-02-11 15:47:40 +00:00
$this->test_text(Tag::implode($event->new_tags), new UserError("Tags contain banned terms"));
}
public function onSetupBuilding(SetupBuildingEvent $event): void
{
$sb = $event->panel->create_new_block("Banned Phrases");
$sb->add_label("One per line, lines that start with slashes are treated as regex<br/>");
$sb->add_longtext_option("banned_words");
$failed = [];
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));
}
}
/**
* Throws if the comment contains banned words.
*/
2020-01-26 13:19:35 +00:00
private function test_text(string $comment, SCoreException $ex): void
{
$comment = strtolower($comment);
2009-01-04 19:18:37 +00:00
foreach ($this->get_words() as $word) {
if ($word[0] == '/') {
// lines that start with slash are regex
if (preg_match($word, $comment) === 1) {
throw $ex;
}
} else {
// other words are literal
if (str_contains($comment, $word)) {
throw $ex;
}
}
}
}
/**
* @return string[]
*/
private function get_words(): array
{
global $config;
$words = [];
2014-10-15 17:31:06 +00:00
$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;
}
2014-10-15 17:31:06 +00:00
return $words;
}
2014-10-15 17:31:06 +00:00
public function get_priority(): int
{
return 30;
}
}