array( __( 'cabbage', 'profanity-filter' ) ), 'secondary_words' => array( __( 'soup', 'profanity-filter' ) ), 'whitelist' => array( __( 'soap', 'profanity-filter' ) ), 'type' => 'cartoon', // replace, char, cartoon 'replacement' => __( '!@#$%^&*', 'profanity-filter' ), 'last_update' => 0, 'allow_subscriptions' => false, 'send_data_words' => false, 'send_data_secondary' => false, 'send_data_whitelist' => false, 'subscriptions' => array() // url => array( subscribed, secret, last_seen, data => array( words, secondary_words, whitelist ) ) ) ) ); static $profanity_filter_lastmod; if ( !isset( $profanity_filter_lastmod ) ) $profanity_filter_lastmod = filemtime( __FILE__ ); $args['last_update'] = max( $profanity_filter_lastmod, $args['last_update'] ); $profanity_filter_cache_args[$_args] = $args; return $args; } function profanity_filter_prepare( $string, $wordOnly = false ) { $ret = '/'; if ( $wordOnly ) $ret .= '(?]+>)?'; foreach ( str_split( $string ) as $notfirst => $char ) { if ( $notfirst ) $ret .= '(<[^>]+>)?'; $ret .= preg_quote( $char, '/' ); } $ret .= '(<[^>]+>)?)'; if ( $wordOnly ) $ret .= '(?!\p{L})'; else $ret .= '\p{L}*'; return $ret . '/i'; } function profanity_filter_prepare_secondary( $string ) { return profanity_filter_prepare( $string, true ); } function profanity_filter_replace( $found ) { global $profanity_filter_settings; $fullword = strip_tags( $fullhtmlword = array_shift( $found ) ); if ( profanity_filter_on_whitelist( $profanity_filter_settings['whitelist'], $fullword ) ) return $fullhtmlword; $len = strlen( $word = strip_tags( array_shift( $found ) ) ); switch ( $profanity_filter_settings['type'] ) { case 'cartoon': $string = substr( str_shuffle( str_repeat( $profanity_filter_settings['replacement'], $len ) ), 0, $len ); break; case 'char': $string = substr( str_repeat( $profanity_filter_settings['replacement'], ceil( $len / strlen( $profanity_filter_settings['replacement'] ) + 1 ) ), mt_rand( 0, strlen( $profanity_filter_settings['replacement'] ) - 1 ), $len ); break; case 'replace': default: if ( is_array( $profanity_filter_settings['replacement'] ) ) { global $profanity_filter_words; $string = $profanity_filter_settings['replacement'][$profanity_filter_words[$word]]; } else { $string = $profanity_filter_settings['replacement']; } } $ret = array_shift( $found ); $ret .= array_shift( $found ); $ret .= esc_html( $string ); $ret .= implode( '', $found ); return $ret; } function profanity_filter_on_whitelist( $whitelist, $word ) { $word = preg_replace( '/[^\p{L}]+/S', '', strtolower( $word ) ); foreach ( $whitelist as $entry ) { $entry = preg_replace( '/[^\p{L}]+/S', '', strtolower( $entry ) ); if ( $word == $entry ) // DEMOMAN'S SAPPIN MAH SENTRY return true; } return false; } function profanity_filter_censor( $text, $args = '' ) { if ( !function_exists( 'double_metaphone_2' ) ) require_once dirname( __FILE__ ) . '/doublemetaphone.php'; global $profanity_filter_words; $options = profanity_filter_parse_args( $args ); $words = array_unique( $options['words'] ); $secondary_words = array_unique( $options['secondary_words'] ); $whitelist = array_unique( $options['whitelist'] ); foreach ( $options['subscriptions'] as $data ) { if ( $data['subscribed'] ) { foreach ( array( 'words', 'secondary_words', 'whitelist' ) as $key ) { if ( !empty( $data['data'][$key] ) && is_array( $data['data'][$key] ) ) $$key = array_merge( $$key, $data['data'][$key] ); } } } $_sentence = strip_tags( $text ); $sentence = double_metaphone_2( $_sentence ); $sentence['positions'][] = strlen( $_sentence ); $cabbageFound = array(); foreach ( array_map( 'double_metaphone_2', $words ) as $pos => $word ) { if ( !$word['primary'] ) continue; while ( true ) { $_cabbageStart = strpos( $sentence['primary'], $word['primary'] ); if ( $_cabbageStart === false ) break; $sentence['primary'] = substr( $sentence['primary'], 0, $_cabbageStart ) . str_repeat( ' ', strlen( $word['primary'] ) ) . substr( $sentence['primary'], $_cabbageStart + strlen( $word['primary'] ) ); $cabbageStart = $sentence['positions'][$_cabbageStart]; $cabbageMinStart = $sentence['positions'][$_cabbageStart - 1]; $cabbageLen = $sentence['positions'][$_cabbageStart + strlen( $word['primary'] )] - $cabbageStart; $cabbageMinLen = $sentence['positions'][$_cabbageStart + strlen( $word['primary'] ) - 1] - $cabbageStart; if ( false !== $cabbageLenAdjust = strpos( substr( $_sentence, $cabbageStart + $cabbageMinLen, $cabbageLen - $cabbageMinLen ), ' ' ) ) $cabbageLen = $cabbageMinLen + $cabbageLenAdjust; if ( false !== $cabbageStartAdjust = strpos( substr( $_sentence, 0, $cabbageStart ), ' ', $cabbageMinStart ) ) $cabbageStart = $cabbageStartAdjust + 1; $cabbage = substr( $_sentence, $cabbageStart, $cabbageLen ); $cabbageFound[] = $cabbage; $profanity_filter_words[$cabbage] = $pos; } } $cabbageFound = array_unique( $cabbageFound ); $soupFound = array(); foreach ( array_map( 'double_metaphone_2', $secondary_words ) as $pos => $word ) { if ( !$word['primary'] ) continue; while ( true ) { $_soupStart = strpos( $sentence['primary'], $word['primary'] ); if ( $_soupStart === false ) break; $sentence['primary'] = substr( $sentence['primary'], 0, $_soupStart ) . str_repeat( ' ', strlen( $word['primary'] ) ) . substr( $sentence['primary'], $_soupStart + strlen( $word['primary'] ) ); $soupStart = $sentence['positions'][$_soupStart]; $soupMinStart = $sentence['positions'][$_soupStart - 1]; $soupLen = $sentence['positions'][$_soupStart + strlen( $word['primary'] )] - $soupStart; $soupMinLen = $sentence['positions'][$_soupStart + strlen( $word['primary'] ) - 1] - $soupStart; if ( false !== $soupLenAdjust = strpos( substr( $_sentence, $soupStart + $soupMinLen, $soupLen - $soupMinLen ), ' ' ) ) $soupLen = $soupMinLen + $soupLenAdjust; if ( false !== $soupStartAdjust = strpos( substr( $_sentence, 0, $soupStart ), ' ', $soupMinStart ) ) $soupStart = $soupStartAdjust + 1; $soup = substr( $_sentence, $soupStart, $soupLen ); $soupFound[] = $soup; $profanity_filter_words[$soup] = $pos; } } $soupFound = array_unique( $soupFound ); global $profanity_filter_settings; $profanity_filter_settings = $options + compact( 'words', 'secondary_words', 'whitelist' ); $censored = preg_replace_callback( array_map( 'profanity_filter_prepare', $cabbageFound ), 'profanity_filter_replace', $text ); $censored = preg_replace_callback( array_map( 'profanity_filter_prepare_secondary', $soupFound ), 'profanity_filter_replace', $censored ); return $censored; } function profanity_filter_filter( $text, $id = 0, $args = '' ) { $profanity_filter_options = profanity_filter_parse_args( $args ); switch ( current_filter() ) { case 'bb_xmlrpc_prepare_topic': case 'get_topic_title': if ( md5( $text ) == bb_get_topicmeta( get_topic_id( $id ), 'profanity_filter_unfiltered' ) ) if ( bb_get_topicmeta( get_topic_id( $id ), 'profanity_filter_date' ) > $profanity_filter_options['last_update'] ) if ( $clean_text = bb_get_topicmeta( get_topic_id( $id ), 'profanity_filter_filtered' ) ) return $clean_text; break; case 'bb_xmlrpc_prepare_post': case 'get_post_text': if ( md5( $text ) == bb_get_postmeta( get_post_id( $id ), 'profanity_filter_unfiltered' ) ) if ( bb_get_postmeta( get_post_id( $id ), 'profanity_filter_date' ) > $profanity_filter_options['last_update'] ) if ( $clean_text = bb_get_postmeta( get_post_id( $id ), 'profanity_filter_filtered' ) ) return $clean_text; break; case 'bb_xmlrpc_prepare_topic_tag': case 'wp_get_object_terms': } $clean_text = profanity_filter_censor( $text, $args ); switch ( current_filter() ) { case 'bb_xmlrpc_prepare_topic': case 'get_topic_title': bb_update_topicmeta( get_topic_id( $id ), 'profanity_filter_date', time() ); bb_update_topicmeta( get_topic_id( $id ), 'profanity_filter_unfiltered', md5( $text ) ); bb_update_topicmeta( get_topic_id( $id ), 'profanity_filter_filtered', $clean_text ); break; case 'bb_xmlrpc_prepare_post': case 'get_post_text': bb_update_postmeta( get_post_id( $id ), 'profanity_filter_date', time() ); bb_update_postmeta( get_post_id( $id ), 'profanity_filter_unfiltered', md5( $text ) ); bb_update_postmeta( get_post_id( $id ), 'profanity_filter_filtered', $clean_text ); break; case 'bb_xmlrpc_prepare_topic_tag': case 'wp_get_object_terms': } return $clean_text; } function profanity_filter_user( $name, $id, $args = '' ) { $profanity_filter_options = profanity_filter_parse_args( $args ); $keyprefix = 'profanity_filter_' . current_filter() . '_'; if ( bb_get_usermeta( $id, $keyprefix . 'unfiltered' ) == md5( $name ) && bb_get_usermeta( $id, $keyprefix . 'date' ) > $profanity_filter_options['last_update'] && ( $clean_text = bb_get_usermeta( $id, $keyprefix . 'filtered' ) ) ) return $clean_text; $clean_text = profanity_filter_censor( $name, $args ); bb_update_usermeta( $id, $keyprefix . 'unfiltered', md5( $name ) ); bb_update_usermeta( $id, $keyprefix . 'date', time() ); bb_update_usermeta( $id, $keyprefix . 'filtered', $clean_text ); return $clean_text; } function profanity_filter_xmlrpc( $data, $_data ) { switch ( current_filter() ) { case 'bb_xmlrpc_prepare_post': $data['post_text'] = profanity_filter_filter( $data['post_text'], $_data['post_id'] ); break; case 'bb_xmlrpc_prepare_topic': $data['topic_title'] = profanity_filter_filter( $data['topic_title'], $_data['topic_id'] ); break; case 'bb_xmlrpc_prepare_topic_tag': $data['topic_tag_name'] = profanity_filter_filter( $data['topic_tag_name'], $_data['tag_id'] ); break; } unset( $data['profanity_filter_date'], $data['profanity_filter_unfiltered'], $data['profanity_filter_filtered'] ); return $data; } function profanity_filter_tags( $terms ) { global $bb_log; foreach ( $terms as &$term ) { if ( $term->taxonomy != 'bb_topic_tag' ) continue; $term->name = profanity_filter_filter( $term->name, $term->term_id ); } return $terms; } function profanity_filter_activate() { add_filter( 'get_topic_title', 'profanity_filter_filter', 9, 2 ); add_filter( 'get_post_text', 'profanity_filter_filter', 9, 2 ); add_filter( 'get_user_name', 'profanity_filter_user', 9, 2 ); add_filter( 'get_user_display_name', 'profanity_filter_user', 9, 2 ); add_filter( 'wp_get_object_terms', 'profanity_filter_tags', 9 ); add_filter( 'bb_xmlrpc_prepare_post', 'profanity_filter_xmlrpc', 9, 2 ); add_filter( 'bb_xmlrpc_prepare_topic', 'profanity_filter_xmlrpc', 9, 2 ); add_filter( 'bb_xmlrpc_prepare_topic_tag', 'profanity_filter_xmlrpc', 9, 2 ); add_filter( 'tag_heat_map', 'profanity_filter_censor', 9 ); } function profanity_filter_deactivate() { remove_filter( 'get_topic_title', 'profanity_filter_filter', 9, 2 ); remove_filter( 'get_post_text', 'profanity_filter_filter', 9, 2 ); remove_filter( 'get_user_name', 'profanity_filter_filter', 9, 2 ); remove_filter( 'get_user_display_name', 'profanity_filter_filter', 9, 2 ); remove_filter( 'wp_get_object_terms', 'profanity_filter_tags', 9 ); remove_filter( 'bb_xmlrpc_prepare_post', 'profanity_filter_xmlrpc', 9, 2 ); remove_filter( 'bb_xmlrpc_prepare_topic', 'profanity_filter_xmlrpc', 9, 2 ); remove_filter( 'bb_xmlrpc_prepare_topic_tag', 'profanity_filter_xmlrpc', 9, 2 ); remove_filter( 'tag_heat_map', 'profanity_filter_censor', 9 ); } if ( !defined( 'DOING_CRON' ) ) profanity_filter_activate(); add_action( 'pre_edit_form', 'profanity_filter_deactivate' ); add_action( 'post_edit_form', 'profanity_filter_activate' ); function profanity_filter_filter_slug( $slug ) { if ( false !== $filtered = wp_cache_get( $slug, 'profanity_filter_slug' ) ) return $filtered; $filtered = profanity_filter_censor( $slug, array( 'type' => 'replace', 'replacement' => '' ) ); $filtered = preg_replace( '/--+/', '-', trim( $filtered, '-' ) ); wp_cache_add( $slug, $filtered, 'profanity_filter_slug' ); return $filtered; } function profanity_filter_slug_activate() { add_filter( 'bb_slug_sanitize', 'profanity_filter_filter_slug', 9 ); add_filter( 'bb_user_nicename_sanitize', 'profanity_filter_filter_slug', 9 ); add_filter( 'pre_bb_topic_tag_slug', 'profanity_filter_filter_slug', 9 ); } function profanity_filter_slug_deactivate() { remove_filter( 'bb_slug_sanitize', 'profanity_filter_filter_slug', 9 ); remove_filter( 'bb_user_nicename_sanitize', 'profanity_filter_filter_slug', 9 ); remove_filter( 'pre_bb_topic_tag_slug', 'profanity_filter_filter_slug', 9 ); } profanity_filter_slug_activate(); add_action( 'pre_permalink', 'profanity_filter_slug_deactivate' ); add_action( 'post_permalink', 'profanity_filter_slug_activate' ); function profanity_filter_filter_tags() { if ( bb_is_tag() ) { global $tag, $profanity_filter_tag_name; $profanity_filter_tag_name = $tag->name; $tag->name = profanity_filter_filter( $tag->name, $tag->term_id ); add_action( 'pre_post_form', 'profanity_filter_uncensor_tags' ); add_action( 'post_post_form', 'profanity_filter_recensor_tags' ); } } function profanity_filter_uncensor_tags() { global $tag, $profanity_filter_tag_name; $tag->name = $profanity_filter_tag_name; } function profanity_filter_recensor_tags() { global $tag; $tag->name = profanity_filter_filter( $tag->name, $tag->term_id ); } add_action( 'post_permalink', 'profanity_filter_filter_tags' ); function profanity_filter_admin() { if ( !empty( $_GET['updated'] ) ) { bb_admin_notice( __( 'Settings saved.' ) ); } $options = profanity_filter_parse_args(); ?>
' . esc_html( $response->get_error_code() ) . ' - ' . esc_html( $response->get_error_message( $response->get_error_code() ) ) );
bb_die( __( 'There was an error parsing the endpoint\'s response. Make sure you entered the URL correctly.', 'profanity-filter' ) );
}
} else {
if ( $_GET['action'] == 'unsubscribe' ) {
$url = $_GET['unsubscribe'];
bb_check_admin_referer( 'profanity-filter-unsubscribe-' . $url );
$options = profanity_filter_parse_args();
if ( !empty( $options['subscriptions'][$url]['subscriber'] ) )
$options['subscriptions'][$url]['subscribed'] = false;
else
unset( $options['subscriptions'][$url] );
$options['last_update'] = time();
bb_update_option( 'profanity_filter', $options );
profanity_filter_request( $url, 'unsubscribe' );
$goback = bb_get_uri( 'bb-admin/admin-base.php', array( 'plugin' => 'profanity_filter_admin', 'updated' => 'true' ), BB_URI_CONTEXT_HEADER + BB_URI_CONTEXT_BB_ADMIN );
bb_safe_redirect( $goback );
exit;
}
}
}
add_action( 'profanity_filter_admin_pre_head', 'profanity_filter_admin_parse' );
function profanity_filter_subscription_info() {
$options = profanity_filter_parse_args(); ?>