#!/usr/bin/perl
#
# killspam.pl
# 0. Replace the DB parameters below with the ones for your system
# 1. Put this file in a cgi-bin/killspam/ directory
# 2. Put an appropriate .htaccess and .htpasswd in there
# 3. Go to http://[your site]/cgi-bin/killspam/killspam.pl in your browser
# 4. Be happy

$user = "** YOUR DB USER NAME HERE **";
$passwd = "** YOUR DB PASSWORD HERE **";
$database = "** YOUR DB INSTANCE NAME HERE **";

use CGI;
use DBI;

$driver = "mysql";
$dsn = "DBI:$driver:database=$database;host=localhost"; #;port=$port";
$dbh = DBI->connect($dsn, $user, $passwd);
$drh = DBI->install_driver("mysql");

sub execDB {
  $cmd = shift;
  print "<!-- doing $cmd -->\n";
  my $stmt = $dbh->prepare($cmd);
  $stmt->execute;
}

$q = new CGI;
print $q->header;
print $q->start_html(-title=>'Comment Spam Killer');

if ($q->param("spammer")) {
  print "Deleting Spam comments and banning IPs: ";
  @spamIDs = $q->param("spammer");
  for $spamID (@spamIDs) {
   my ($id, $ip) = split ':', $spamID;
   execDB("delete from mt_comment where comment_id=$id");
   execDB("insert into mt_ipbanlist (ipbanlist_blog_id, ipbanlist_ip) VALUES (1, '$ip')");
  }
}

$sth = $dbh->prepare("select comment_id, comment_author, comment_ip, comment_text, comment_url from mt_comment order by comment_id desc;");           
$sth->execute;

print "<form><input type=submit name=Kill value='Kill Spam!'>";

print "<table border=2>\n";

while (my $ref = $sth->fetchrow_hashref()) {
  print "<tr>";
  $id = $ref->{'comment_id'}.':'.$ref->{'comment_ip'};
  print"   <td><input type=checkbox name=spammer value=$id /></td>\n";
  print  map { "   <td>".$ref->{$_}."</td>\n";} ('comment_id', 'comment_author', 'comment_ip', 'comment_url');
  print "</tr>\n";
}
print "</table></form>";

print $q->end_html;