is_valid ) {
setcookie( 'nospamuser-override', bb_create_nonce( 'nospamuser-override-' . $_SERVER['REMOTE_ADDR'] ), bb_nonce_tick() * apply_filters( 'bb_nonce_life', 86400 ) / 2 );
}
bb_safe_redirect( bb_get_uri( 'register.php', null, BB_URI_CONTEXT_BB_USER_FORMS + BB_URI_CONTEXT_HEADER ) );
}
exit;
}
function nospamuser_install() {
bb_update_option( 'nospamuser-settings', wp_parse_args( bb_get_option( 'nospamuser-settings' ), array(
'days' => 30,
'min_occur' => 5,
'max_occur' => 10,
'api_key' => '',
'recaptcha_mode' => 'aggressive',
'recapthca_pub' => '',
'recaptcha_priv' => '',
'stats_public' => 0
) ) );
}
bb_register_plugin_activation_hook( __FILE__, 'nospamuser_install' );
function nospamuser_admin_parse() {
bb_check_admin_referer( 'nospamuser-admin' );
$settings = bb_get_option( 'nospamuser-settings' );
$success = array();
$error = array();
if ( $_POST['days'] != $settings['days'] ) {
if ( (int)$_POST['days'] > 0 ) {
$settings['days'] = $_POST['days'];
$success[] = __( 'Maximum days', 'nospamuser' );
} else {
$error[] = __( 'Maximum days', 'nospamuser' );
}
}
if ( $_POST['min_occur'] != $settings['min_occur'] ) {
if ( (int)$_POST['min_occur'] > 0 ) {
$settings['min_occur'] = $_POST['min_occur'];
$success[] = __( 'Minimum frequency', 'nospamuser' );
} else {
$error[] = __( 'Minimum frequency', 'nospamuser' );
}
}
if ( $_POST['max_occur'] != $settings['max_occur'] ) {
if ( (int)$_POST['max_occur'] > 0 ) {
$settings['max_occur'] = $_POST['max_occur'];
$success[] = __( 'Maximum frequency', 'nospamuser' );
} else {
$error[] = __( 'Maximum frequency', 'nospamuser' );
}
}
if ( $_POST['api_key'] != $settings['api_key'] ) { // There's not a way I know of to check this.
$settings['api_key'] = $_POST['api_key'];
$success[] = __( 'Stop Forum Spam API key', 'nospamuser' );
}
if ( $_POST['recaptcha_mode'] != $settings['recaptcha_mode'] ) {
if ( in_array( $_POST['recaptcha_mode'], array( 'aggressive', 'adaptive', 'friendly' ) ) ) {
$settings['recaptcha_mode'] = $_POST['recaptcha_mode'];
$success[] = __( 'reCAPTCHA mode', 'nospamuser' );
} else {
$error[] = __( 'reCAPTCHA mode', 'nospamuser' );
}
}
if ( $_POST['recaptcha_pub'] != $settings['recaptcha_pub'] ) { // There's not a way I know of to check this.
$settings['recaptcha_pub'] = $_POST['recaptcha_pub'];
$success[] = __( 'reCAPTCHA public key', 'nospamuser' );
}
if ( $_POST['recaptcha_priv'] != $settings['recaptcha_priv'] ) { // There's not a way I know of to check this.
$settings['recaptcha_priv'] = $_POST['recaptcha_priv'];
$success[] = __( 'reCAPTCHA private key', 'nospamuser' );
}
if ( (int)$_POST['stats-public'] != $settings['stats-public'] ) {
$settings['stats-public'] = (int)$_POST['stats-public'];
$success[] = __( 'Public statistics', 'nospamuser' );
}
if ( $success ) {
bb_update_option( 'nospamuser-settings', $settings );
bb_admin_notice( __( 'The following settings were updated successfully:', 'nospamuser' ) . '
- ' . implode( '
- ', $success ) . '
', 'updated' );
}
if ( $error ) {
bb_admin_notice( __( 'The following settings had errors and were not updated:', 'nospamuser' ) . '- ' . implode( '
- ', $error ) . '
', 'error' );
}
}
if ( strtoupper( $_SERVER['REQUEST_METHOD'] ) == 'POST' )
add_action( 'nospamuser_admin_pre_head', 'nospamuser_admin_parse' );
function nospamuser_admin() {
$settings = bb_get_option( 'nospamuser-settings' );
$options = array(
'days' => array(
'title' => __( 'Maximum days', 'nospamuser' ),
'note' => __( 'Any possible spammer that was last active over this many days ago will be allowed through.', 'nospamuser' ),
'class' => 'short',
'value' => $settings['days']
),
'min_occur' => array(
'title' => __( 'Minimum frequency', 'nospamuser' ),
'note' => __( 'Any possible spammer that does not have at least this many reports will be allowed through.', 'nospamuser' ),
'class' => 'short',
'value' => $settings['min_occur']
),
'max_occur' => array(
'title' => __( 'Maximum frequency', 'nospamuser' ),
'note' => __( 'Possible spammers that have at least this many reports will be disallowed in adaptive mode. This also affects agressive mode, where spammers with at least this many reports will be blocked even if the maximum days prerequisite is not met.', 'nospamuser' ),
'class' => 'short',
'value' => $settings['max_occur']
),
'api_key' => array(
'title' => __( 'Stop Forum Spam API key', 'nospamuser' ),
'note' => __( 'Required to submit spammers to Stop Forum Spam. Get a Stop Forum Spam API key here.', 'nospamuser' ),
'class' => array( 'code', 'long' ),
'value' => $settings['api_key']
),
'recaptcha_mode' => array(
'title' => __( 'reCAPTCHA mode', 'nospamuser' ),
'note' => __( 'All modes except aggressive require reCAPTCHA public and private keys.', 'nospamuser' ),
'type' => 'radio',
'options' => array(
'aggressive' => __( 'Aggressive: Never allow possible spammers to override blocks.', 'nospamuser' ),
'adaptive' => __( 'Adaptive: Allow possible spammers between the minimum and maximum frequency to override blocks.', 'nospamuser' ),
'friendly' => __( 'Friendly: Allow all possible spammers to override blocks.', 'nospamuser' )
),
'value' => $settings['recaptcha_mode']
),
'recaptcha_pub' => array(
'title' => __( 'reCAPTCHA public key', 'nospamuser' ),
'note' => sprintf( __( 'Get it here.', 'nospamuser' ), 'http://recaptcha.net/api/getkey?domain=' . urlencode( $_SERVER['SERVER_NAME'] ) . '&app=bb-NoSpamUser' ),
'class' => array( 'code', 'long' ),
'value' => $settings['recaptcha_pub']
),
'recaptcha_priv' => array(
'title' => __( 'reCAPTCHA private key', 'nospamuser' ),
'note' => sprintf( __( 'Get it here.', 'nospamuser' ), 'http://recaptcha.net/api/getkey?domain=' . urlencode( $_SERVER['SERVER_NAME'] ) . '&app=bb-NoSpamUser' ),
'class' => array( 'code', 'long' ),
'value' => $settings['recaptcha_priv']
),
'stats-public' => array(
'title' => __( 'Public statistics', 'nospamuser' ),
'type' => 'radio',
'options' => array(
0 => __( 'Keep all statistics private', 'nospamuser' ),
1 => sprintf( __( 'Display the number of caught spammers on the statistics page.', 'nospamuser' ), bb_get_uri( 'statistics.php' ) )
),
'value' => $settings['stats-public'] ? $settings['stats-public'] : 0
)
);
?>
30,
'min_occur' => 5,
'max_occur' => 10,
'api_key' => '',
'recaptcha_mode' => 'aggressive',
'recapthca_pub' => '',
'recaptcha_priv' => '',
'stats_public' => 0
) );
if ( !is_array( $result = bb_get_transient( 'nospamuser-' . $type . '-' . md5( $data ) ) ) ) {
$wp_http = new WP_Http;
$response = $wp_http->get( 'http://www.stopforumspam.com/api?' . urlencode( $type ) . '=' . urlencode( $data ), array(
'user-agent' => apply_filters( 'http_headers_useragent', backpress_get_option( 'wp_http_version' ) ) . NOSPAMUSER_AGENT
) );
$response = $response['body'];
if ( strpos( $response, '' ) === false )
return;
if ( strpos( $response, 'no' ) !== false )
$result = array( 0, 0 );
else {
preg_match( '/([^<>]+)<\/lastseen>/', $response, $matches );
$result = array( (int)substr( $response, strpos( $response, '' ) + 11 ), strtotime( $matches[1] ) );
}
bb_set_transient( 'nospamuser-' . $type . '-' . md5( $data ), $result, 604800 );
}
if ( $result == array( 0, 0 ) ) // Even if the settings are set incorrectly, non-spammers shouldn't be blocked.
return;
if ( ( $result[0] >= $settings['min_occur'] && $result[1] >= time() - $settings['days'] * 86400 ) ||
( $result[0] >= $settings['max_occur'] && $settings['recaptcha_mode'] == 'aggressive' ) ) {
if ( $result[0] >= $settings['max_occur'] && $settings['recaptcha_mode'] == 'adaptive' )
nospamuser_block( $type, $data, true );
elseif ( $settings['recaptcha_mode'] == 'aggressive' || !$settings['recaptcha_pub'] || !$settings['recaptcha_priv'] )
nospamuser_block( $type, $data, true );
else
nospamuser_block( $type, $data, false );
}
}
function nospamuser_block( $type, $data, $noway ) {
$settings = bb_get_option( 'nospamuser-settings' );
bb_update_option( 'nospamuser-blocks', bb_get_option( 'nospamuser-blocks' ) + 1 );
$types = array(
'email' => __( 'email address', 'nospamuser' ),
'ip' => __( 'IP address', 'nospamuser' ),
'username' => __( 'username', 'nospamuser' )
);
if ( $noway )
bb_die( sprintf( __( 'Your %1$s (%2$s) is listed in Stop Forum Spam\'s database. You have been automatically blocked. If you are not a spammer, you may appeal this listing.', 'nospamuser' ), $types[$type], $data, 'http://www.stopforumspam.com/' . ( $type == 'ip' ? 'ipcheck/' : 'search?q=' ) . $data ), 'Registration forbidden', 403 );
if ( !isset( $_COOKIE['nospamuser_override'] ) || !bb_verify_nonce( $_COOKIE['nospamuser_override'], 'nospamuser-override-' . $_SERVER['REMOTE_ADDR'] ) ) {
if ( !function_exists( 'recaptcha_check_answer' ) ) // Compatibility with anything else that uses reCAPTCHA
require_once dirname( __FILE__ ) . '/recaptchalib.php';
bb_die( sprintf( __( 'Your %1$s (%2$s) is listed in Stop Forum Spam\'s database. You have been automatically blocked. If you are not a spammer, you may appeal this listing or solve the CAPTCHA below.', 'nospamuser' ), $types[$type], $data, 'http://www.stopforumspam.com/' . ( $type == 'ip' ? 'ipcheck/' : 'search?q=' ) . $data ) . '', 'Registration forbidden', 401 );
}
}
function nospamuser_check_email( $r, $email, $type ) {
if ( $type ) // Only check the last time.
return $r;
if ( $r )
nospamuser_check( 'email', $email );
return $r;
}
function nospamuser_check_username( $username ) {
if ( $username )
nospamuser_check( 'username', $username );
return $username;
}
function nospamuser_check_ip() {
nospamuser_check( 'ip', $_SERVER['REMOTE_ADDR'] );
}
if ( bb_get_location() == 'register-page' ) {
nospamuser_check_ip();
add_filter( 'sanitize_user', 'nospamuser_check_username' );
add_filter( 'is_email', 'nospamuser_check_email', 10, 3 );
}
function nospamuser_check_bozo( $user_id ) { // Most of this function is taken from Akismet
$settings = bb_get_option( 'nospamuser-settings' );
if ( empty( $settings['api_key'] ) )
return;
global $bb_current_user, $user_obj;
$bb_current_id = bb_get_current_user_info( 'id' );
bb_set_current_user( $user_id );
if ( $bb_current_id && $bb_current_id != $user_id )
if ( $user_obj->data->is_bozo || !$bb_current_user->data->is_bozo )
return;
bb_set_current_user( (int) $bb_current_id );
$wp_http = new WP_Http;
$wp_http->post( 'http://www.stopforumspam.com/post.php', array(
'body' => array(
'username' => $user_obj->user_login,
'ip_addr' => $user_obj->data->nospamuser_ip,
'email' => $user_obj->user_email,
'api_key' => $settings['api_key']
),
'user-agent' => apply_filters( 'http_headers_useragent', backpress_get_option( 'wp_http_version' ) ) . NOSPAMUSER_AGENT
) );
}
add_action( 'profile_edited', 'nospamuser_check_bozo' );
function nospamuser_set_user_ip_field( $user_id ) {
bb_update_usermeta( $user_id, 'nospamuser_ip', $_SERVER['REMOTE_ADDR'] );
}
add_action( 'register_user', 'nospamuser_set_user_ip_field' );
function nospamuser_maybe_set_user_ip_field() {
if ( bb_is_user_logged_in() && !bb_get_usermeta( bb_get_current_user_info( 'ID' ), 'nospamuser_ip' ) )
nospamuser_set_user_ip_field( bb_get_current_user_info( 'ID' ) );
}
add_action( 'bb_init', 'nospamuser_maybe_set_user_ip_field' );
function nospamuser_stats_display() {
if ( bb_is_statistics() && ( $settings = bb_get_option( 'nospamuser-settings' ) ) &&
( $settings['stats-public'] & 1 ) && ( $blocks = (int)bb_get_option( 'nospamuser-blocks' ) ) )
echo '' . sprintf( __( 'Spammers blocked by bb-NoSpamUser', 'nospamuser' ),
'http://bbpress.org/plugins/topic/nospamuser/' ) . '' . bb_number_format_i18n( $blocks ) .
'';
}
add_action( 'bb_stats_left', 'nospamuser_stats_display' );
load_plugin_textdomain( 'nospamuser', dirname( __FILE__ ) . '/translations' );