Please login or register.

Login with username, password and session length
Advanced search  

News:

You need/want an older version of sNews ? Download an older/unsupported version here.

Author Topic: [MOD] Akismet spam protection for comments [v0.1]  (Read 3437 times)

henno

  • Newbie
  • *
  • Karma: 0
  • Posts: 4
[MOD] Akismet spam protection for comments [v0.1]
« on: January 08, 2007, 01:44:36 PM »

Version: 0.1
Revision Date: 8 Jan 2007
sNews Version: Tested on 1.5.30


If you are unfamiliar with Akismet, please see http://akismet.com/

As a (very) brief summary, this mod will use the Akismet service to scan your comments for spam-status on submission, and post them if they are not identified as spam, or place them in the moderation queue if a positive spam-status is returned. The benefit here is that you can set comments to be posted automatically (i.e. not awaiting moderation in 'Settings') and still filter those nasty spam comment bots. This should work in conjuction with a CAPCHA system, but I personally hate the use of CAPCHA and deplore having to squint at those stupid graphics just to post a comment. (Not to mention the accessability issues).

Preliminary Notes: You will need a wordpress.com API key. Signup at the following address, and select 'Just a username, please.' instead of creating an actual wordpress.com blog.
http://wordpress.com/signup/

At this stage there is no use implementing this mod if you are going to check the 'Approve comments before publishing' box in your Settings. This mod merely sets the approval status to 'False', which will be done to every single comment anyway if that box is checked. It won't hurt your system in anyway having both enabled, but approving every comment obviously negates any need for a spam filter. (This situation may change if the features below are enabled). Also, it should be noted that this mod is very much in its infancy. It is not feature-rich, and is missing some functionality. Some of these missing features are as follows: (Consider this a quazi TODO list)

1. There is no functionality (yet) to return positive spam to the Akismet service (as a way of helping Akismet 'learn').

2. Due to my reluctance to mess with the default database structure at this stage, all this script merely does is set the comment approval status to 'False' (i.e. not published, and awaiting moderation). This is less than ideal, as a heavy spam attack of thousands of comments will be a nightmare to administer (this being said, it is still better than the current situation). Ideally, another database field of 'spam_status' or something should be implemented, with an admin page to mass-moderate spam-flagged comments. (This would be a perfect spot to implement item 1 above)

3. Spam counter. I would love to implement a 'spam comments caught' function to display how many comments have been identified as spam by Akismet. Unfortunately, this would depend on features 1 and 2 above to work properly.

4. When (if?) the Akismet service is down, the php4 class returns an ugly error. I have never seen the service unavailable myself, but this is a limitation of the class itself, of which I am not the author. This is planned to be fixed very soon. (refer: http://miphp.net/blog/view/php4_akismet_class#comment74 )

5. Bugs Bugs Bugs. There are bound to be some bugs, or at least less-than-elegant code. If you think there is a better way to achieve something, let me know. You can get me via email at henno at silenceisdefeat.org or if you are a fan of IRC, I am usually idling as 'henno' on irc.oftc.net (more often than not in #sd).

Onto the code.

First, you will nead the awesome Akismet PHP class by either Bret Kuhns or Alex. There are both php4 and php5 versions respectively, so grab the appropriate version for your server configuration here:

PHP4: http://miphp.net/blog/view/php4_akismet_class
PHP5: http://www.achingbrain.net/stuff/akismet/

Save the Akismet.class.php file to your document root (or wherever your includes are located). Then add the following line somewhere at the top of your snews.php file:

Code: [Select]
include ("Akismet.class.php");Seach for the following block of text in snews.php:

Code: [Select]
if(s('approve_comments') != 'on') {$approved = 'True';}
$query = "INSERT INTO ".db('prefix')."comments(articleid, name, url, comment, time, approved) VALUES('$post_article_id', '$name', '$url', '$comment', '$time', '$approved')";
mysql_query($query);
$commentStatus = s('approve_comments') == 'on' ? l('comment_sent_approve') : l('comment_sent');
And replace it with this (or comment it out in the short term just in case):

Code: [Select]
//Akismet hack by henno
$akismet = new Akismet('http://yourdomain.com', '[insert your wordpress API key here]');
$akismet->setCommentAuthor($name);
// Email not yet implemented in sNews commenting
//$akismet->setCommentAuthorEmail($email);
$akismet->setCommentAuthorURL($url);
$akismet->setCommentContent($comment);
// If the comment is identified as spam, set approval to 'False'
if($akismet->isCommentSpam() == true) {
$approved = 'False';
$commentStatus = l('comment_sent_approve');
// If 'Approve comments before publishing' is _not_ checked in 'Settings', mark the comment approval as 'True'
} elseif (s('approve_comments') != 'on') {
$approved = 'True';
$commentStatus = l('comment_sent');
// Otherwise, set the approval to 'False' anyway (i.e. 'Approve comments before publishing' is checked)
} else {
$approved = 'False';
$commentStatus = l('comment_sent_approve');
}
$query = "INSERT INTO ".db('prefix')."comments(articleid, name, url, comment, time, approved) VALUES('$post_article_id', '$name', '$url', '$comment', '$time', '$approved')";
mysql_query($query);
Obviously you need to insert your wordpress.com API key [without brackets] into the above code in order to make it work. Other than that, it should work.

Happy spam-free commenting!
Henno.
Logged

piXelatedEmpire

  • MIA
  • ULTIMATE member
  • ******
  • Karma: 37
  • Posts: 1401
  • currently MIA
[MOD] Akismet spam protection for comments [v0.1]
« Reply #1 on: January 09, 2007, 02:05:02 AM »

wow this is looking like a VERY useful mod!

I too am not a fan of the CAPTCHA method (but unfortunately some sort of spam protection is required) so I would be very happy to see this in working order.

I will play around with this MOD in the next week and post any issues/comments/suggestion I come across.  Hope to see you continue development of this MOD.

Big thumbs up!  :D
Logged
my apologies to the sNews crew, but I will be MIA for the forseeable future

gunnar_soroos

  • Newbie
  • *
  • Karma: 0
  • Posts: 17
    • http://www.rochesterflyers.com
Re: [MOD] Akismet spam protection for comments [v0.1]
« Reply #2 on: November 04, 2008, 06:25:09 PM »

Not sure what I'm doing wrong.   ???  I downloaded and uploaded the php4 class, got my API key and made the mod. Then when I try to submit a comment, it hangs and eventually dies. no comment, not pending approval, nothing.

At the top of my snews.php file I have
Code: [Select]
<?php

// Askimet Include
include ("Akismet.class.php"); //End


/********************************************************

sNews 1.5
Developers version: 1.5.31 Stable
Copyright (C) Solucija.com
sNews is licensed under a Creative Commons License

*********************************************************/
/********************************************************

Gunnar Change and Mod Log
------------------------------------------------------
11.04.08 [Mod] Added support for Askimet

Then in the comment section I have this
Code: [Select]
//for Aksimet comment from here
/*if(s('approve_comments') != 'on') {$approved = 'True';}
$query = "INSERT INTO ".db('prefix')."comments(articleid, name, url, comment, time, approved) VALUES('$post_article_id', '$name', '$url', '$comment', '$time', '$approved')";
mysql_query($query);
$commentStatus = s('approve_comments') == 'on' ? l('comment_sent_approve') : l('comment_sent'); */ //commented out for Askimet MOD -below-
// [MOD] ASKIMET
//Akismet hack by henno
$akismet = new Akismet('http://www.yourdomain.com/', '40f743af2cc5');
$akismet->setCommentAuthor($name);
// Email not yet implemented in sNews commenting
//$akismet->setCommentAuthorEmail($email);
$akismet->setCommentAuthorURL($url);
$akismet->setCommentContent($comment);
// If the comment is identified as spam, set approval to 'False'
if($akismet->isCommentSpam() == true) {
$approved = 'False';
$commentStatus = l('comment_sent_approve');
// If 'Approve comments before publishing' is _not_ checked in 'Settings', mark the comment approval as 'True'
} elseif (s('approve_comments') != 'on') {
$approved = 'True';
$commentStatus = l('comment_sent');
// Otherwise, set the approval to 'False' anyway (i.e. 'Approve comments before publishing' is checked)
} else {
$approved = 'False';
$commentStatus = l('comment_sent_approve');
}
$query = "INSERT INTO ".db('prefix')."comments(articleid, name, url, comment, time, approved) VALUES('$post_article_id', '$name', '$url', '$comment', '$time', '$approved')";
mysql_query($query);
// End ASKIMET
}
else {$commentStatus = l('comment_error'); $commentReason = l('ce_reasons');}
echo '<h2>'.$commentStatus.'</h2>';

I posted a couple lines above and below, maybe I placed it wrong?
« Last Edit: November 04, 2008, 06:28:57 PM by gunnar_soroos »
Logged