I hate comment spam. I love movable type. What’s a boy to do when he receives more than 20 comment spams a day?
This boy edited the Movable Type source code to filter the comments.
Because I hated having to go through ever day and filter the valid comments from the spam, I wondered if there was a way to filter out the spam before it got posted. I noticed that most of my spam messages contained the <H1> tag and some sort of message about Cheap Viagra, Texas Hold’em or Online Poker. I then realized I might be able to flag comments containing words such as “poker” or “Viagra”, and certain tags, like the <h1> tag, as spam – thus giving the spammer an error. This solution works much the same way that your email spam filtering system does.
After a bit of searching I found the “Comments.pm” file in the “lib/apps/” folder. This file contains the code that preps and posts the comments to the database.
Do not confuse this file with the “comment.pm” file in the “lib/” folder and please, before you begin, make a backup of the “Comments.pm” file.
If you open the “Comments.pm” file, you’ll see a lot of code – somewhere around a thousand lines to be exact. We’re going to need to find a few lines of code around the 230-250 line count (the exact line depends on the release of movable type you’re using. I’m using a version of release 3). The code should look something like:
if (!$q->param(‘text’)) {
return $app->handle_error($app->translate(“Comment text is required.”));
}
Directly *after* those lines is where we want to put our code. Our code will look something like this:
my @postFilter = (“h1″);
my $indexCompare = -1;
my $postFilterInst = “”;
foreach $postFilterInst (@postFilter)
{
if (rindex(lc($q->param(‘text’)),$postFilterInst) != $indexCompare) {
return $app->handle_error($app->translate(“Your comment has been flagged as potential spam. Please email me if you are getting this error in error.”));
}
}
The first line of our code contains an array with the words we want to look for. Since all of the spam we get contains the “<h1>” tag, we set “h1″ as a flag by including it in this array. We have to be sure to type in all lower case because the rindex function we’ll be using on line 6 is case sensitive.
The second line is a variable containing the value of the rindex function if it doesn’t find a match.
The third line initiates the variable “$postFilterInst” – post filter instance. This will contain each individual instance of the postFilter array as we loop through to check the comment post against our flag phrases.
The fourth line contains the loop that will pull each value in our @postFilter array.
The sixth line is the part of the code that really does the work. This is where we check to see if the comment posting contains any of our flag phrases. If the comment does, the eighth line will pop out an error that tells the comment poster (aka spammer) that their post wasn’t valid and, in turn, not allow the program to insert the comment into the database. This is also where we convert the comment to all lower case so H1 or h1 will both create a match.
That’s it! So if we’d like to filter out poker, the code would look like this (all you have to do is replace the “h1″ in double quotations in the first line of our script with “poker”):
my @postFilter = (“poker”);
my $indexCompare = -1;
my $postFilterInst = “”;
foreach $postFilterInst (@postFilter)
{
if (rindex(lc($q->param(‘text’)),$postFilterInst) != $indexCompare) {
return $app->handle_error($app->translate(“Your comment has been flagged as potential spam. Please email me if you are getting this error in error.”));
}
}
Now lets say we want to get really fancy and filter out multiple words. It’s pretty simple, all you need to do is add more words to the @postFilter array by adding ,”whatever”. So if we wanted to filter our comments for “h1″, “viagra” and “poker” the code would look like this:
my @postFilter = (“h1″,”viagra”,”poker”);
my $indexCompare = -1;
my $postFilterInst = “”;
foreach $postFilterInst (@postFilter)
{
if (rindex(lc($q->param(‘text’)),$postFilterInst) != $indexCompare) {
return $app->handle_error($app->translate(“Your comment has been flagged as potential spam. Please email me if you are getting this error in error.”));
}
}
And that’s it. You can add as many filters as you’d like by adding more words or phrases to the @postFilter array.
Don’t forget, if you’ve edited the “Comments.pm” file locally on your computer, you’ll need to be sure to upload it in ASCII otherwise Movable Type will not work.