Wiki source for SpamBlacklist


Show raw source

=====SpamBlacklist Plugin=====

This is a spam blacklisting plugin, I've written. The blacklist is stored on a wiki page. You may optionally enable a log file to log successfully blocked spam.

The plugin is using [[http://en.wikipedia.org/wiki/teergrubing teergrubing]] to keep the connection of the spammer open for at least 20 seconds!

At first, place the following code as a new file, with the filename **spamblacklist.php**, under **3rdparty/plugins**, into your wikka installation:

%%(php;1)
<?php
// Spam Blacklisting Plugin for Wikka Wiki
// Copyright (C) Manuel Reimer (Manuel _dot_ Reimer _at_ gmx _dot_ de)
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 2 as published by the Free Software Foundation

// More information about SpamBlacklist here: http://wikkawiki.org/SpamBlacklist

// Main spam detection routine. If the message has been spam, then this
// one will call "sb_do_output_magic" and will *exit* the script immediately!
function sb_checkit($wikkaref, $body) {
if (!$wikkaref->config["sbl_page"])
die("SpamBlacklist: Please configure the plugin first!");
$body = sb_unhtmlentities(trim($body));
$sb_blacklist = $wikkaref->LoadPage($wikkaref->config["sbl_page"]);
if ((!$wikkaref->GetUser() || !$wikkaref->config["sbl_only_anon"]) && $wikkaref->tag != $wikkaref->config["sbl_page"]) {
if ($sb_blacklist && isset($sb_blacklist["body"])) {
$sb_blacklist = $sb_blacklist["body"];
$sb_blacklist = explode("\n", $sb_blacklist);
foreach ($sb_blacklist as $sb_expression) {
if (preg_match('/(^\s*$|^\s*#)/', $sb_expression))
continue;
if (preg_match($sb_expression, $body)) {
if ($wikkaref->config["sbl_logfile"]) {
$sb_fp = fopen($wikkaref->config["sbl_logfile"], "a");
if ($sb_fp && flock($sb_fp, LOCK_EX)) {
$sb_logline = date("M d Y H:i:s") . "\t";
$sb_logline .= $sb_expression . "\t";
$sb_logline .= $wikkaref->GetUserName() . "\n";
fwrite($sb_fp, $sb_logline);
fclose($sb_fp);
}
}
sb_do_output_magic($wikkaref);
exit();
}
}
}
}
}

// Function for decoding all html entities
// http://www.php.net/manual/en/function.html-entity-decode.php
function sb_unhtmlentities($string) {
$string = html_entity_decode($string);
$string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
$string = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $string);
return $string;
}

// Function for doing the output magic
// Will send the user a message first
// Then a short definition of "spam" is sent *really* slow, to slow down
// the spammer (teergrubing). The whole process takes about 20 seconds.
// This should be within the "max_execution_time" of most providers.
function sb_do_output_magic($wikkaref) {
$slow_message = array("Spamming", "is", "the", "abuse", "of", "electronic", "messaging", "systems", "to", "send", "unsolicited", "bulk", "messages,", "which", "are", "almost", "universally", "undesired.");

while(@ob_end_clean());

$headercode = file_get_contents("actions/header.php");
$headercode = str_replace('$this->', '$wikkaref->', $headercode);
eval("?>" . $headercode);

print("<div class=\"page\">");
print $wikkaref->config["sbl_message"] . "<br/>\n<br/>\n";
flush();
sleep(1);
foreach ($slow_message as $word) {
print $word . " ";
flush();
sleep(1);
}
print "</div>";

$footercode = file_get_contents("actions/footer.php");
$footercode = str_replace('$this->', '$wikkaref->', $footercode);
eval("?>" . $footercode);

flush();
sleep(1);
print "<div class=\"smallprint\">Spam notice was generated in > 20 seconds. ";
flush();
sleep(1);
print "Spam filtering powered by <a href=\"http://www.wikkawiki.org/SpamBlacklist\">SpamBlacklist<a>. <a href=\"http://en.wikipedia.org/wiki/teergrubing\">Teergrubing</a> ends here ;-)</div>\n</body>\n</html>";
flush();
sleep(1);
}
?>%%

Now add the following entries to your wikka.config.php and edit them for your needs:

%%(php;1)
"sbl_page" => "SpamBlacklist", // Name of Wiki-Page with blacklist on it
"sbl_only_anon" => true, // Only append blacklist to anonymous users?
"sbl_logfile" => "spam.log", // Optional logfile (relative to wikka.php)
"sbl_message" => "No SPAM here!!!", // A short excuse message to your users.%%

Here are the two messages, used by me to inform the user about what happened:

English:
%%(php;1)
"sbl_message" => "We are sorry, but our spam filter detected your text as spam. Please use the \"back\" button and re-edit your text. Please don't use spam-like words (meds, ...) and don't send links without giving a short comment about it (explain the link. Where does it point to?)."
%%
German:
%%(php;1)
"sbl_message" => "Es tut uns leid, aber leider hat unser Spam-Filter Ihren Text als Spam erkannt. Bitte klicken Sie auf \"Zurück\" und bearbeiten Sie ihren Text. Bitte verwenden Sie keine spamtypischen Worte (Medikamente, Potenzmittel) und senden Sie Links nicht als unkommentierte Linkliste (Links kurz erklären. Wohin führt der Link?)."
%%

Now open the file **handlers/page/addcomment.php** and add the following lines on top of this file:

%%(php;1)
<?php
include("3rdparty/plugins/spamblacklist.php");
sb_checkit($this, $_POST["body"]);

if ($this->HasAccess ...... And so on. Now the code, alredy in the file, follows
%%

If you like, you may also do the same for **handlers/page/edit.php**

The next step is to create a new page called "SpamBlacklist" and maybe set the ACLs to block users from reading, or even writing, this page. On this page you may now add several lines of regular expressions. As soon as one of your expressions match against the body of the comment/page, someone tries to publish, the user will get a message, your logfile gets updated and the comment/page **does not** get published.

Comments on your blacklist are possible if you prefix them with "#".

An example for an expression could be:

%%
/viagra/i
%%


----
CategoryUserContributions
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki
Page was generated in 0.0133 seconds