Thursday, November 11, 2004

Blocking some BLOG SPAM

href="http://philringnalda.com/blog/2003/07/urls_including_zipcode_are_prohibited.php">This
handy URL gave me the initial code to start with. The code samples
started off with and older version of Movable Type, but I've updated it
a bit and come up with something that will block the BLOG SPAM I've
been getting.


So, borrowing some text from the parent post, here are the changes
necessary for Movable Type Version 3.01D


You will be adding some code to /{your MT
directory}/lib/MT/App/Comments.pm


Starting around line 229 is this block of code:


    if (!$q->param('text')) {
return $app->handle_error($app->translate("Comment text is required."));
}

Add some code to get this:



    if (!$q->param('text')) {
return $app->handle_error($app->translate("Comment text is required."));
}
#Begin inserted anti SPAM-BLOG Code
else {
if (($q->param('text') =~ m/poker online/)
or ($q->param('text') =~ m/chat\.ru/)
){
return $app->handle_error($app->translate(
"Prohibited data"));
return $app->handle_error($app->errstr());
}
}#End outer ELSE
#END inserted anti SPAM-BLOG Code


The two regular expressions will give an error message on any comments that contain poker online or chat.ru and yes, the matches are case sensitive, but that can be changed by a few quick tweaks to the Regular Expressions.



Next, you'll want to filter the URLS people enter. Around line 273, you'll see some code starting with:



    if ($comment->url) {
require MT::Util;
if (my $fixed = MT::Util::is_valid_url($comment->url)) {
$comment->url($fixed);


You want this block of code to be:




if ($comment->url) {
require MT::Util;
if (my $fixed = MT::Util::is_valid_url($comment->url)) {
$comment->url($fixed);
}else {
return $app->handle_error($app->translate(
"Invalid URL '[_1]'", $comment->url));
}
#Begin inserted anti SPAM-BLOG Code
if ((($comment->url) =~ m/poker/)
or (($comment->url) =~ m/chat\.ru/)
){
return $app->handle_error($app->translate(
"Prohibited data"));
return $app->handle_error($app->errstr());
}
#END inserted anti SPAM-BLOG Code
}
return $app->handle_error($app->errstr()) unless $comment;

No comments: