<?php
/*
 Plugin Name: SpamForceField
 Plugin URI: http://dougal.gunters.org/blog/2005/03/29/plugin-spamforcefield
 Author: Dougal Campbell
 Author URI: http://dougal.gunters.org/
 Description: Perform early anti-spam checks, and return a "403 Forbidden" status code to possible spammers. If SpamForceField appears first in your Plugins list, then the checks will occur early enough to prevent referer spams from even going into the comment database with 'spam' status. This plugin checks your blacklist keys against the Referer of the incoming connection. It also rejects all requests which arrive via 'pinappleproxy' servers. Comments which are flagged as spam for other reasons will now also receive the "Forbidden" status and message.
 Version: 1.2

 History:
   1.0: Initial release
   1.1: Modified declaration of deny_spammer()
   1.2: Fixed URI listed above to match up with reality
*/

// The comment_post action requires that you set 
// the 'accepted_args' param to 2. This action hook 
// will cause the server to return a Forbidden status
// when comments are posted which we flagged as spam:
add_action('comment_post','deny_spammer',10,2);


// All blacklist keys will be checked against the referer
$blacklist_keys get_settings('blacklist_keys');
$spam_referers explode("\n",$blacklist_keys);

    
// Nuke spam referers. Don't bother doing the other normal spam checks.
if (stristr_a($_SERVER['HTTP_REFERER'],$spam_referers)) {
    
deny_spammer(0,'spam');
}


// All 'pinappleproxy' connections will be nuked.
if (stristr($_SERVER['HTTP_VIA'],'pinappleproxy')) {
    
deny_spammer(0,'spam');
}


// Like stristr(), except $needle is an array.
// Returns true if any matches found, false if no matches
function stristr_a($haystack$needles) {
    
// make sure it's a list
    
if (count($needles)) {
        
// Check each needle against the haystack string
        
foreach ($needles as $needle) {
            
// skip blank lines
            
$needle trim($needle);
            if (empty(
$needle)) continue; 

            if (
stristr($haystack$needle))
                return 
true;
        }
    }

    
// No matches found.
    
return false;

// stristr_a()


// If the comment status was 'spam', return an error to the client.
// Perhaps some spam bots will be smart enough to stop trying after
// they get errors.
function deny_spammer($cid=0$status='') {
    if (
$status == 'spam') {
        
$ip_address $_SERVER['REMOTE_ADDR'];

        
// Obfuscate 'foo@bar.com' as 'foo (at) bar (dot) com'
        
$admin_email get_settings('admin_email');
        
$admin_email str_replace('@'' (at) '$admin_email);
        
$admin_email str_replace('.'' (dot) '$admin_email);

        @
header("HTTP/1.0 403 Forbidden");

        
// Send a polite error message explaining what's going on,
        // JUST IN CASE this is an innocent bystander.
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>403 Forbidden</title>
</head>
<body>
<h1>403 Forbidden</h1>
<p>
This server has detected suspicious activity and is rejecting your request.
Possible reasons for rejecting your connection include:
</p>
<ul>
<li>Connection originates from an Open Proxy</li>
<li>Referer contains a blacklisted phrase or URI</li>
<li>Posted comment contains a blacklisted phrase or URI</li>
</ul>
<p>
Due to the security and spam problems associated with open proxies, this
site does not accept connections from them. If you feel that you have
received this message erroneously, please contact your network
administrators and inform them of this issue. To determine if your
connection is via an open proxy, check the 
<a href="http://opm.blitzed.org/proxy?ip=<?php print $ip_address?>">Blitzed 
Open Proxy Monitor List</a>.
</p>
<p>
If you feel that you have received this message erroneously, you can contact
the administrator of this web server at: <b><?php print $admin_email?></b>.
</p>
</body>
</html>
<?php
        
// You are the weakest link. Goodbye!
        
exit();
    } 
// if(spam)

// deny_spammer()

?>