='));
Class NoSpamNX
{
var $nospamnx_names;
var $nospamnx_checkip;
var $nospamnx_count;
var $nospamnx_operate;
var $nospamnx_blocktime;
var $nospamnx_checkuser;
var $nospamnx_blockips;
function nospamnx()
{
//load language strings
if (function_exists('load_plugin_textdomain'))
load_plugin_textdomain('nospamnx', PLUGINDIR.'/nospamnx');
//check if wordpress is at least 2.6
if (NOSPAMNXREQWP26 != true){
add_action('admin_notices', array(&$this, 'wpVersionNotice'));
return;
}
//add nospamnx wordpress actions
add_action('init', array(&$this, 'checkCommentForm'));
add_action('template_redirect', array(&$this, 'modifyTemplate'));
add_action('wp_head', array(&$this, 'nospamnxStyle'));
add_action('admin_menu', array(&$this, 'nospamnxAdminMenu'));
add_action('rightnow_end', array(&$this, 'nospamnxStats'));
//tell wp what to do when activated and deactivated
register_activation_hook(__FILE__, array(&$this, 'activate'));
register_deactivation_hook(__FILE__, array(&$this, 'deactivate'));
//load nospamnx options
$this->loadOptions();
}
function wpVersionNotice()
{
echo "
".__('Your WordPress is to old. NoSpamNX requires at least WordPress 2.6!','nospamnx')."
";
}
function modifyTemplate()
{
//check if we only display the page/post
if (is_singular())
{
//start output buffer and add callback function
ob_start(array(&$this, 'addHiddenFields'));
}
}
function addHiddenFields($template)
{
//get the formfields names and value from wp options
$nospamnx = $this->nospamnx_names;
//replace the textfields within the ouput buffer
if (rand(1,2) == 1)
return str_replace ('', '', $template);
else
return str_replace ('', '', $template);
}
function checkCommentForm()
{
//check if logged in user does not require check
if ($this->nospamnx_checkuser == 0 && is_user_logged_in())
return true;
else
{
//check if we are in wp-comments-post.php
if (basename($_SERVER['PHP_SELF']) == 'wp-comments-post.php')
{
//if ip lock is enabled, check if we have the spambot already catched
if ($this->nospamnx_checkip == 1 && $this->checkIp() === true)
$this->birdbrained(true);
//get current formfield names from wp options
$nospamnx = $this->nospamnx_names;
//check if first hidden field is in $_POST data
if (!array_key_exists($nospamnx['nospamnx-1'],$_POST))
$this->birdbrained(false);
//check if first hidden field is empty
else if ($_POST[$nospamnx['nospamnx-1']] != "")
$this->birdbrained(false);
//check if second hidden field is in $_POST data
else if (!array_key_exists($nospamnx['nospamnx-2'],$_POST))
$this->birdbrained(false);
//check if the value of the second formfield matches stored value
else if ($_POST[$nospamnx['nospamnx-2']] != $nospamnx['nospamnx-2-value'])
$this->birdbrained(false);
}
}
}
function birdbrained($catched)
{
//lets cleanup some old blocked ips, the spambot has enoguh time
($this->nospamnx_checkip == 1) ? $this->cleanup() : false;
//check if we already catched the spambot (based on ip adress) or not
if ($catched == false)
{
//save the spambots ip if option is enabled
if ($this->nospamnx_checkip == 1 && $this->checkIp() === false)
$this->saveIp();
//count spambot
$this->nospamnx_count++;
$this->updateOptions();
}
else
wp_die(__('Sorry, but it seems you are a Spambot.','nospamnx'));
//check in which mode we are and block, mark as spam or put in moderation queue
if ($this->nospamnx_operate == 'mark')
add_filter('pre_comment_approved', create_function('$a', 'return \'spam\';'));
else if ($this->nospamnx_operate == 'moderate')
add_filter('pre_comment_approved', create_function('$a', 'return \'0\';'));
else
wp_die(__('Sorry, but it seems you are a Spambot.','nospamnx'));
}
function generateNames()
{
//generate random names and value for the hidden formfields
$nospamnx = array(
'nospamnx-1' => md5(uniqid(rand(), true)),
'nospamnx-2' => md5(uniqid(rand(), true)),
'nospamnx-2-value' => md5(uniqid(rand(), true))
);
return $nospamnx;
}
function nospamnxAdminMenu()
{
add_options_page('NoSpamNX', 'NoSpamNX', 8, 'nospamnx', array(&$this, 'nospamnxOptionPage'));
}
function saveIp()
{
//get the curren time
$currentime = time();
//get current list of blocked ips
$blockips = $this->nospamnx_blockips;
//do we have more than 100 entries in our databse?
if (count($blockips) >= 100)
return;
//set the time the ip will be blocked
switch ($this->nospamnx_blocktime)
{
case 0:
$until = 2147483647;
break;
case 1:
$until = $currentime + 3600;
break;
case 24:
$until = $currentime + 86400;
break;
default:
$until = $currentime + 86400;
}
//lets generate a new entry
$newentry = array(
'remoteip' => $_SERVER['REMOTE_ADDR'],
'until' => $until
);
//add the new entry to our list
$blockips [] = $newentry;
//now save the new entry
$this->nospamnx_blockips = $blockips;
$this->updateOptions();
}
/*
* Returns true if we already catched the spambot
* Returns false if we didnt catched the spambot yet
*/
function checkIp()
{
//get current list of blocked ips
$blockips = $this->nospamnx_blockips;
//get the current time
$currenttime = time();
//loop through all entries and check if the entry is already in our list
for ($i = 0; $i <= count($blockips); $i++)
{
//check ip against entries
if ($_SERVER['REMOTE_ADDR'] == $blockips [$i]['remoteip'])
{
//found the entry, but do we still block it?
if ($blockips [$i]['until'] > $currenttime)
return true;
}
}
return false;
}
function cleanup()
{
//get current list of blocked ips
$blockips = $this->nospamnx_blockips;
//get the current time
$currenttime = time();
//loop through all entries and check if the time is up
for ($i = 0; $i <= count($blockips); $i++)
{
//do we still have to block the ip?
if ($blockips [$i]['until'] < $currenttime)
//delete ip adress from entries
unset($blockips [$i]);
}
//store the entries back to wp options
$this->nospamnx_blockips = $blockips;
$this->updateOptions();
}
function nospamnxOptionPage()
{
if (!current_user_can('manage_options'))
wp_die(__('Sorry, but you have no permissions to change settings.','nospamnx'));
//set some variables for default form values
$ipyes = '';
$ipno = '';
$blocktime0 = '';
$blocktime1 = '';
$blocktime24 = '';
$block = '';
$mark = '';
$moderate = '';
//do we have to update any settings?
if ($_POST['save_settings'])
{
//which operation mode do we have to save
switch($_POST['nospamnx_operate'])
{
case 'block':
$this->nospamnx_operate = 'block';
break;
case 'mark':
$this->nospamnx_operate = 'mark';
break;
case 'moderate':
$this->nospamnx_operate = 'moderate';
break;
default:
$this->nospamnx_operate = 'block';
}
if (!empty($_POST['nospamnx_blocktime']))
{
//how long will the ips be blocked?
switch($_POST['nospamnx_blocktime'])
{
case 0:
$this->nospamnx_blocktime = 0;
break;
case 1:
$this->nospamnx_blocktime = 1;
break;
case 24;
$this->nospamnx_blocktime = 24;
break;
default:
$this->nospamnx_blocktime = 1;
}
}
//do we have to check logged in users?
($_POST['nospamnx_checkuser'] == 1) ? $this->nospamnx_checkuser = 1 : $this->nospamnx_checkuser = 0;
//do we have to save options for checking ips?
($_POST['nospamnx_ip'] == 1) ? $this->nospamnx_checkip = 1 : $this->nospamnx_checkip = 0;
//save options and display message
$this->updateOptions();
echo "
".__('NoSpamNX settings were saved successfully.','nospamnx')."
";
}
else if ($_POST['reset_counter'])
{
//reset counter
$this->nospamnx_count = 0;
//save options and display message
$this->updateOptions();
echo "
".__('NoSpamNX Counter was reseted successfully.','nospamnx')."
";
}
//set checked values for radio buttons
($this->nospamnx_checkip == 1) ? $ipyes = 'checked' : $ipno = 'checked';
($this->nospamnx_checkuser == 1) ? $useryes = 'checked' : $userno = 'checked';
//set checked values for block time
switch ($this->nospamnx_blocktime)
{
case 0:
$blocktime0 = 'checked';
break;
case 1:
$blocktime1 = 'checked';
break;
case 24:
$blocktime24 = 'checked';
break;
}
//set checked values for operating mode
switch ($this->nospamnx_operate)
{
case 'block':
$block = 'checked';
break;
case 'mark':
$mark = 'checked';
break;
case 'moderate':
$moderate = 'checked';
break;
}
//get the entries from stored ips
$entries = count($this->nospamnx_blockips);
//confirmation text for reseting the counter
$confirm = __('Are you sure you want to reset the counter?','nospamnx');
?>