Monday, October 20, 2008

Prevent Comment Spam in Gallery 2

Even though I've enabled CAPTCHA I'm still seeing a ton of comment spam inserted by spambots on my Menalto Gallery 2 site. I've looked around and there are SQL queries that will rid your Gallery2 site of spam, but it doesn't prevent new ones from being saved. I've finally got sick of it and decided to stop the problem by adding a snippet of code in AddComment.inc (under the gallery/modules/comment directory). Be sure to backup your existing AddComment.inc file first before applying this mod.

Right below this section:

$comment->setCommenterId($gallery->getActiveUserId());
$comment->setHost(GalleryUtilities::getRemoteHostAddress());
$comment->setSubject($form['subject']);
$comment->setComment($form['comment']);
$comment->setAuthor($form['author']);
$comment->setDate(time());

Add the following code:

##BEGIN Gallery Spam Prevention Mod by TechBlogByDave (http://www.techblogbydave.blogspot.com)
$disallowed = array('http', 'url=');
$checks = array($form['author'], $form['subject'], $form['comment']);
foreach ($checks as $check){
foreach ($disallowed as $dis){
if (stristr($check, $dis)){
die("In order to combat spam, $check has been tagged as a disallowed keyword. Your comment has not been saved.");
}
}
}
##END Gallery Spam Prevention Mod

Save the file and voila- any comment with the text "http" or "url=" will be met with an error message and not even be saved to the datbase. You can even add additional keywords into the "disallowed" array that you don't want in comments such as cuss words. Keep in mind that when you upgrade your Gallery 2 to a newer version you will have to re-apply the mod. Hope this helps!

8 comments:

Anonymous said...

Thank You very much for this.

Zds said...

Thanks a lot :-).

raffjones said...

Hey Dave - thanks a lot for this little mod... its been such a pain for a long time, and this is exactly what I was looking for.

Cheers,

Steve Jones

Anonymous said...

Thank you so much for this!

Peter B said...

Great work there Dave. I've been searching for a solution to this problem for a while now.

I'm going to give your script a whirl!

Anonymous said...

Thank you! Works like a charm! Hard to advertise something with out a link to it so this catches most spam!

MadMeower said...

Same change needed in EditComment.inc as well. Sneaky comment spammers will create a seemingly innocent comment that doesn't have any links in it, then change the comment later to have links and the proper spam content.

Arthur Caranta said...

Thank you VERY much ... this was indeed a nightmare ... I didn't go for refusing hhtp links ... I used the 4000 spam comment I had to find the worst keywords and it seems to be working ...