'moderate' ) ); global $bbmodsuite_cache; $bbmodsuite_cache['banplus'] = array( 'bans' => array(), 'options' => array( 'min_level' => 'moderate' ) ); } function bbmodsuite_banplus_uninstall() { global $bbmodsuite_cache; foreach ( $bbmodsuite_cache['banplus']['bans'] as $user_id => $ban ) { do_action( 'bbmodsuite_banplus_unban', $user_id, $ban ); } bb_delete_option( 'bbmodsuite_banplus_current_bans' ); bb_delete_option( 'bbmodsuite_banplus_options' ); } /** * bbmodsuite_banplus_set_ban() - Set a ban on a user * * @param int|string $user_id The ID of the user to ban or an IP address or CIDR range with the prefix ip_. * @param string $type 'temp' for temporary. Other plugins can add more. * @param int $length How long the ban should be in seconds. Defaults to 1 day. * @param string $notes The reason for the ban. * @param bool $override Set this to true if you want to ban a user without checking for permissions. * @return bool true on success, false on error * @global $bbmodsuite_cache Cache of options and bans */ function bbmodsuite_banplus_set_ban( $user_id, $type = 'temp', $length = 86400, $notes = '', $override = false ) { global $bbmodsuite_cache; $the_options = $bbmodsuite_cache['banplus']['options']; if ( !$override && !bb_current_user_can( $the_options['min_level'] ) ) return false; if ( !$override && strpos( $user_id, 'ip_' ) === false ) { $user_id = bb_get_user_id( $user_id ); if ( !$user_id ) return false; if ( $user_id === bb_get_current_user_info( 'ID' ) ) return false; if ( class_exists( 'BP_User' ) ) $user = new BP_User( $user_id ); else $user = new WP_User( $user_id ); if ( ( $user->has_cap( 'moderate' ) && !bb_current_user_can( 'administrate' ) ) || ( $user->has_cap( 'administrate' ) && !bb_current_user_can( 'use_keys' ) ) ) return false; } $current_bans = $bbmodsuite_cache['banplus']['bans']; if ( $type == 'unban' ) { if ( !isset( $current_bans[$user_id] ) ) return true; do_action( 'bbmodsuite_banplus_unban', $user_id, $current_bans[$user_id] ); unset( $current_bans[$user_id] ); $bbmodsuite_cache['bans'] = $current_bans; bb_update_option( 'bbmodsuite_banplus_current_bans', $current_bans ); return true; } $length = (int)$length; if ($length <= 0) return false; $until = time() + $length; if ( !in_array( $type, bbmodsuite_banplus_get_ban_types() ) ) return false; if ( $current_bans[$user_id]['type'] === $type && $current_bans[$user_id]['length'] === $length ) return true; if ( isset( $current_bans[$user_id] ) ) return false; $notes = bb_autop( htmlspecialchars( trim( $notes ) ) ); $current_bans[$user_id] = array( 'type' => $type, 'length' => $length, 'on' => time(), 'until' => $until, 'banned_by' => bb_get_current_user_info( 'ID' ), 'notes' => $notes, ); do_action( 'bbmodsuite_banplus_ban', $user_id, $current_bans[$user_id] ); $bbmodsuite_cache['bans'] = $current_bans; bb_update_option( 'bbmodsuite_banplus_current_bans', $current_bans ); return true; } function bbmodsuite_banplus_init() { $current_bans = (array)bb_get_option( 'bbmodsuite_banplus_current_bans' ); $changed = false; foreach ( $current_bans as $user_id => $ban ) { if ( $ban['until'] < time() ) { do_action( 'bbmodsuite_banplus_automated_unban', $user_id, $current_bans[$user_id] ); unset( $current_bans[$user_id] ); $changed = true; } } if ( $changed ) bb_update_option( 'bbmodsuite_banplus_current_bans', $current_bans ); global $bbmodsuite_cache; if ( empty( $bbmodsuite_cache['banplus'] ) ) $bbmodsuite_cache['banplus'] = array( 'bans' => $current_bans, 'options' => bb_get_option( 'bbmodsuite_banplus_options' ) ); } bbmodsuite_banplus_init(); function bbmodsuite_banplus_convert_ip( $ip ) { if ( strpos( $ip, ':' ) !== false ) { // IPv6 if ( strpos( $ip, '::' ) !== false ) { $ip = str_replace( '::', str_repeat( ':0000', 8 - substr_count( $ip, ':' ) ) . ':', $ip ); } $ip = explode( ':', $ip ); $cur_ip = '' ; foreach ( $ip as $v ) { $cur_ip .= str_pad( base_convert( $v, 16, 2 ), 16, 0, STR_PAD_LEFT ); } } else { // IPv4 $cur_ip = vsprintf( '%08b%08b%08b%08b', explode( '.', $ip ) ); } return $cur_ip; } function bbmodsuite_banplus_maybe_block_ip() { $cur_ip = bbmodsuite_banplus_convert_ip( $_SERVER['REMOTE_ADDR'] ); global $bbmodsuite_cache; $current_bans = $bbmodsuite_cache['banplus']['bans']; foreach ( $current_bans as $id => $ban ) { if ( strpos( $id, 'ip_' ) !== false ) { list( $ip, $cidr ) = explode( '/', substr( $id, 3 ) ); if ( !$cidr ) $cidr = strlen( $cur_ip ); $ip = bbmodsuite_banplus_convert_ip( $ip ); if ( strlen( $ip ) != strlen( $cur_ip ) ) continue; // IPv4 can't test an IPv6 address, etc. if ( substr( $ip, 0, $cidr ) == substr( $cur_ip, 0, $cidr ) ) { if ( bb_get_template( 'ban-plus-ip.php', false ) ) { bb_load_template( 'ban-plus-ip.php', array( 'ban' => $ban, 'ban_ip' => substr( $id, 3 ) ), $ban ); exit; } bb_die( sprintf( __( 'Your IP address (%s) is banned from this forum until %s from now. The person who banned %s said the reason was:
%sIf you are a moderator or administrator, you can still log in.', 'bbpress-moderation-suite' ), $_SERVER['REMOTE_ADDR'], bb_since( time() * 2 - $ban['until'], array( 'levels' => 2 ) ), substr( $id, 3 ), $ban['notes'], bb_get_uri( 'bb-login.php', null, BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_USER_FORMS ) ) ); } } } } function bbmodsuite_banplus_maybe_block_user() { global $bbmodsuite_cache; $current_bans = $bbmodsuite_cache['banplus']['bans']; if ( !empty( $current_bans[bb_get_current_user_info( 'ID' )] ) ) { switch ( $current_bans[bb_get_current_user_info( 'ID' )]['type'] ) { case 'temp': if ( bb_get_template( 'ban-plus.php', false ) ) { bb_load_template( 'ban-plus.php', array( 'ban' => $current_bans[bb_get_current_user_info( 'ID' )] ), $current_bans[bb_get_current_user_info( 'ID' )] ); exit; } bb_die( sprintf( __( 'You are banned from this forum until %s from now. The person who banned you said the reason was: %s', 'bbpress-moderation-suite' ), bb_since( time() * 2 - $current_bans[bb_get_current_user_info( 'ID' )]['until'], true ), $current_bans[bb_get_current_user_info( 'ID' )]['notes'] ) ); } } if ( !bb_current_user_can( 'moderate' ) ) // Moderators and up are excempt from IP bans. bbmodsuite_banplus_maybe_block_ip(); } if ( bb_get_location() != 'login-page' ) // Let them log in and out bbmodsuite_banplus_maybe_block_user(); function bbmodsuite_banplus_get_ban_types() { global $bbmodsuite_active_plugins; $types = array( 'temp' ); return apply_filters( 'bbmodsuite_banplus_ban_types', $types ); } function bbmodsuite_banplus_admin_add_jquery() { wp_enqueue_script( 'jquery' ); } if ( isset( $_GET['page'] ) && $_GET['page'] == 'new_ban' ) add_action( 'bbpress_moderation_suite_ban_plus_pre_head', 'bbmodsuite_banplus_admin_add_jquery' ); function bbmodsuite_banplus_admin_newbanajax() { header( 'Content-Type: application/json' ); if ( !bb_verify_nonce( $_POST['_wpnonce'], 'bbmodsuite-banplus-new-ajax' ) ) exit; $name = stripslashes( $_POST['text'] ); $name = str_replace( array( '%', '?' ), array( '\\%', '\\?' ), substr( $_POST['text'], 0, $_POST['pos'] ) ) . '%' . str_replace( array( '%', '?' ), array( '\\%', '\\?' ), substr( $_POST['text'], $_POST['pos'] ) ); global $bbdb; $results = $bbdb->get_col( $bbdb->prepare( 'SELECT `user_nicename` FROM `' . $bbdb->users . '` WHERE ( `user_nicename` LIKE %s OR `user_login` LIKE %s OR `ID` = %d ) AND `ID` != %d ORDER BY LENGTH(`user_nicename`) ASC LIMIT 15', $name, $name, $_POST['text'], bb_get_current_user_info( 'ID' ) ) ); exit( '["' . implode( '","', array_map( 'addslashes', $results ) ) . '"]' ); } if ( isset( $_GET['page'] ) && $_GET['page'] == 'new_ban_ajax' ) add_action( 'bbpress_moderation_suite_ban_plus_pre_head', 'bbmodsuite_banplus_admin_newbanajax' ); function bbpress_moderation_suite_ban_plus() { ?>