[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 Plugin Name: Akismet 4 Plugin URI: http://akismet.com/ 5 Description: Akismet checks posts against the Akismet web service to see if they look like spam or not. You need a <a href="http://wordpress.com/api-keys/">WordPress.com API key</a> to use this service. 6 Author: Michael Adams 7 Version: 1.1 8 Author URI: http://blogwaffe.com/ 9 */ 10 11 12 13 $bb_ksd_api_host = bb_get_option( 'akismet_key' ) . '.rest.akismet.com'; 14 $bb_ksd_api_port = 80; 15 $bb_ksd_user_agent = 'bbPress/' . bb_get_option( 'version' ) . ' | bbAkismet/'. bb_get_option( 'version' ); 16 17 function bb_akismet_verify_key( $key ) 18 { 19 global $bb_ksd_api_port; 20 $blog = urlencode( bb_get_uri( null, null, BB_URI_CONTEXT_TEXT + BB_URI_CONTEXT_AKISMET ) ); 21 $response = bb_ksd_http_post( 'key=' . $key . '&blog=' . $blog, 'rest.akismet.com', '/1.1/verify-key', $bb_ksd_api_port ); 22 if ( 'valid' == $response[1] ) { 23 return true; 24 } else { 25 return false; 26 } 27 } 28 29 // Returns array with headers in $response[0] and entity in $response[1] 30 function bb_ksd_http_post( $request, $host, $path, $port = 80 ) 31 { 32 global $bb_ksd_user_agent; 33 34 $http_request = 'POST ' . $path . ' HTTP/1.0' . "\r\n"; 35 $http_request .= 'Host: ' . $host . "\r\n"; 36 $http_request .= 'Content-Type: application/x-www-form-urlencoded; charset=utf-8' . "\r\n"; // for now 37 $http_request .= 'Content-Length: ' . strlen($request) . "\r\n"; 38 $http_request .= 'User-Agent: ' . $bb_ksd_user_agent . "\r\n"; 39 $http_request .= "\r\n"; 40 $http_request .= $request; 41 $response = ''; 42 if ( false != ( $fs = @fsockopen( $host, $port, $errno, $errstr, 10 ) ) ) { 43 fwrite( $fs, $http_request ); 44 45 while ( !feof( $fs ) ) { 46 $response .= fgets( $fs, 1160 ); // One TCP-IP packet 47 } 48 fclose( $fs ); 49 $response = explode( "\r\n\r\n", $response, 2 ); 50 } 51 return $response; 52 } 53 54 function bb_ksd_configuration_page() 55 { 56 ?> 57 <h2><?php _e( 'Akismet Settings' ); ?></h2> 58 <?php do_action( 'bb_admin_notices' ); ?> 59 60 <form class="settings" method="post" action="<?php bb_uri( 'bb-admin/admin-base.php', array( 'plugin' => 'bb_ksd_configuration_page'), BB_URI_CONTEXT_FORM_ACTION + BB_URI_CONTEXT_BB_ADMIN ); ?>"> 61 <fieldset> 62 <p><?php printf( __( 'For many people, <a href="%s">Akismet</a> will greatly reduce or even completely eliminate the spam you get on your site. If one does happen to get through, simply mark it as "spam" and Akismet will learn from the mistakes.' ), 'http://akismet.com/' ); ?></p> 63 64 <?php 65 $after = ''; 66 if ( false !== $key = bb_get_option( 'akismet_key' ) ) { 67 if ( bb_akismet_verify_key( $key ) ) { 68 $after = __( 'This key is valid' ); 69 } else { 70 bb_delete_option( 'akismet_key' ); 71 } 72 } 73 74 bb_option_form_element( 'akismet_key', array( 75 'title' => __( 'WordPress.com API Key' ), 76 'attributes' => array( 'maxlength' => 12 ), 77 'after' => $after, 78 'note' => sprintf( __( 'If you don\'t have a WordPress.com API Key, you can get one at <a href="%s">WordPress.com</a>' ), 'http://wordpress.com/api-keys/' ) 79 ) ); 80 81 bb_option_form_element( 'akismet_stats', array( 82 'title' => __( 'Enable stats page' ), 83 'type' => 'checkbox', 84 'options' => array( 85 1 => __( 'Create a page that shows spam statistics.' ) 86 ), 87 'note' => __( 'This page will be viewable by moderators or higher.' ) 88 ) ); 89 ?> 90 91 </fieldset> 92 <fieldset class="submit"> 93 <?php bb_nonce_field( 'options-akismet-update' ); ?> 94 <input type="hidden" name="action" value="update-akismet-settings" /> 95 <input class="submit" type="submit" name="submit" value="<?php _e('Save Changes') ?>" /> 96 </fieldset> 97 </form> 98 <?php 99 } 100 101 function bb_ksd_configuration_page_add() 102 { 103 bb_admin_add_submenu( __( 'Akismet' ), 'use_keys', 'bb_ksd_configuration_page', 'options-general.php' ); 104 } 105 add_action( 'bb_admin_menu_generator', 'bb_ksd_configuration_page_add' ); 106 107 function bb_ksd_configuration_page_process() 108 { 109 if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) && $_POST['action'] == 'update-akismet-settings') { 110 bb_check_admin_referer( 'options-akismet-update' ); 111 112 $goback = remove_query_arg( array( 'invalid-akismet', 'updated-akismet' ), wp_get_referer() ); 113 114 if ( !isset( $_POST['akismet_stats'] ) ) { 115 $_POST['akismet_stats'] = false; 116 } 117 118 if ( true === (bool) $_POST['akismet_stats'] ) { 119 bb_update_option( 'akismet_stats', 1 ); 120 } else { 121 bb_delete_option( 'akismet_stats' ); 122 } 123 124 if ( $_POST['akismet_key'] ) { 125 $value = stripslashes_deep( trim( $_POST['akismet_key'] ) ); 126 if ( $value ) { 127 if ( bb_akismet_verify_key( $value ) ) { 128 bb_update_option( 'akismet_key', $value ); 129 } else { 130 $goback = add_query_arg( 'invalid-akismet', 'true', $goback ); 131 bb_safe_redirect( $goback ); 132 exit; 133 } 134 } else { 135 bb_delete_option( 'akismet_key' ); 136 } 137 } else { 138 bb_delete_option( 'akismet_key' ); 139 } 140 141 $goback = add_query_arg( 'updated-akismet', 'true', $goback ); 142 bb_safe_redirect( $goback ); 143 exit; 144 } 145 146 if ( !empty( $_GET['updated-akismet'] ) ) { 147 bb_admin_notice( __( '<strong>Settings saved.</strong>' ) ); 148 } 149 150 if ( !empty( $_GET['invalid-akismet'] ) ) { 151 bb_admin_notice( __( '<strong>The key you attempted to enter is invalid. Reverting to previous setting.</strong>' ), 'error' ); 152 } 153 154 global $bb_admin_body_class; 155 $bb_admin_body_class = ' bb-admin-settings'; 156 } 157 add_action( 'bb_ksd_configuration_page_pre_head', 'bb_ksd_configuration_page_process' ); 158 159 // Bail here if no key is set 160 if ( !bb_get_option( 'akismet_key' ) ) { 161 return; 162 } 163 164 function bb_ksd_stats_script() 165 { 166 ?> 167 <style> 168 #bb-ksd-stats-frame { 169 -moz-box-shadow: 0 0 15px rgb(255, 255, 255); 170 -webkit-box-shadow: 0 0 15px rgb(255, 255, 255); 171 box-shadow: 0 0 15px rgb(255, 255, 255); 172 margin-top: 16px; 173 width: 100%; 174 height: 700px; 175 border-width: 0; 176 } 177 </style> 178 <script type="text/javascript"> 179 function resizeIframe() { 180 var height = document.documentElement.clientHeight; 181 height -= document.getElementById('bb-ksd-stats-frame').offsetTop; 182 height -= 60; 183 document.getElementById('bb-ksd-stats-frame').style.height = height +"px"; 184 }; 185 function resizeIframeInit() { 186 document.getElementById('bb-ksd-stats-frame').onload = resizeIframe; 187 window.onresize = resizeIframe; 188 } 189 addLoadEvent(resizeIframeInit); 190 </script> 191 <?php 192 } 193 194 function bb_ksd_stats_display_pre_head() 195 { 196 if ( !bb_get_option( 'akismet_stats' ) ) { 197 return; 198 } 199 add_action( 'bb_admin_head', 'bb_ksd_stats_script' ); 200 } 201 add_action( 'bb_ksd_stats_display_pre_head', 'bb_ksd_stats_display_pre_head' ); 202 203 function bb_ksd_stats_display() 204 { 205 $site = urlencode( bb_get_uri( null, null, BB_URI_CONTEXT_TEXT + BB_URI_CONTEXT_AKISMET ) ); 206 $url = "http://".bb_get_option( 'akismet_key' ).".web.akismet.com/1.0/user-stats.php?blog={$site}&type=forum"; 207 ?> 208 <iframe src="<?php echo $url; ?>" id="bb-ksd-stats-frame"></iframe> 209 <?php 210 } 211 212 function bb_ksd_stats_page() 213 { 214 if ( !bb_get_option( 'akismet_stats' ) ) { 215 return; 216 } 217 if ( function_exists( 'bb_admin_add_submenu' ) ) { 218 bb_admin_add_submenu( __( 'Akismet Stats' ), 'moderate', 'bb_ksd_stats_display', 'index.php' ); 219 } 220 } 221 add_action( 'bb_admin_menu_generator', 'bb_ksd_stats_page' ); 222 223 function bb_ksd_submit( $submit, $type = false ) 224 { 225 global $bb_ksd_api_host; 226 global $bb_ksd_api_port; 227 228 switch ( $type ) { 229 case 'ham': 230 case 'spam': 231 $path = '/1.1/submit-' . $type; 232 233 $bb_post = bb_get_post( $submit ); 234 if ( !$bb_post ) { 235 return; 236 } 237 $user = bb_get_user( $bb_post->poster_id ); 238 if ( bb_is_trusted_user( $user->ID ) ) { 239 return; 240 } 241 242 $_submit = array( 243 'blog' => bb_get_uri( null, null, BB_URI_CONTEXT_TEXT + BB_URI_CONTEXT_AKISMET ), 244 'user_ip' => $bb_post->poster_ip, 245 'permalink' => get_topic_link( $bb_post->topic_id ), // First page 246 'comment_type' => 'forum', 247 'comment_author' => get_user_name( $user->ID ), 248 'comment_author_email' => bb_get_user_email( $user->ID ), 249 'comment_author_url' => get_user_link( $user->ID ), 250 'comment_content' => $bb_post->post_text, 251 'comment_date_gmt' => $bb_post->post_time 252 ); 253 break; 254 255 case 'hammer': 256 case 'spammer': 257 $path = '/1.1/submit-' . substr( $type, 0, -3 ); 258 259 $user = bb_get_user( $submit ); 260 if ( !$user ) { 261 return; 262 } 263 if ( bb_is_trusted_user( $user->ID ) ) { 264 return; 265 } 266 267 $_submit = array( 268 'blog' => bb_get_uri( null, null, BB_URI_CONTEXT_TEXT + BB_URI_CONTEXT_AKISMET ), 269 'permalink' => get_user_profile_link( $user->ID ), 270 'comment_type' => 'profile', 271 'comment_author' => get_user_name( $user->ID ), 272 'comment_author_email' => bb_get_user_email( $user->ID ), 273 'comment_author_url' => get_user_link( $user->ID ), 274 'comment_content' => $user->occ . ' ' . $user->interests, 275 'comment_date_gmt' => $user->user_registered 276 ); 277 break; 278 279 default: 280 if ( bb_is_trusted_user( bb_get_current_user() ) ) { 281 return; 282 } 283 284 $path = '/1.1/comment-check'; 285 286 $_submit = array( 287 'blog' => bb_get_uri( null, null, BB_URI_CONTEXT_TEXT + BB_URI_CONTEXT_AKISMET ), 288 'user_ip' => preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] ), 289 'user_agent' => $_SERVER['HTTP_USER_AGENT'], 290 'referrer' => $_SERVER['HTTP_REFERER'], 291 'comment_type' => isset($_POST['topic_id']) ? 'forum' : 'profile', 292 'comment_author' => bb_get_current_user_info( 'name' ), 293 'comment_author_email' => bb_get_current_user_info( 'email' ), 294 'comment_author_url' => bb_get_current_user_info( 'url' ), 295 'comment_content' => $submit 296 ); 297 if ( isset( $_POST['topic_id'] ) ) { 298 $_submit['permalink'] = get_topic_link( $_POST['topic_id'] ); // First page 299 } 300 break; 301 } 302 303 $query_string = ''; 304 foreach ( $_submit as $key => $data ) { 305 $query_string .= $key . '=' . urlencode( stripslashes( $data ) ) . '&'; 306 } 307 return bb_ksd_http_post( $query_string, $bb_ksd_api_host, $path, $bb_ksd_api_port ); 308 } 309 310 function bb_ksd_submit_ham( $post_id ) 311 { 312 bb_ksd_submit( $post_id, 'ham' ); 313 } 314 315 function bb_ksd_submit_spam( $post_id ) 316 { 317 bb_ksd_submit( $post_id, 'spam' ); 318 } 319 320 function bb_ksd_check_post( $post_text ) 321 { 322 global $bb_ksd_pre_post_status, $bb_ksd_pre_post; 323 324 $bb_ksd_pre_post = $post_text; 325 326 return $post_text; 327 } 328 add_action( 'pre_post', 'bb_ksd_check_post', 1 ); 329 330 function bb_ksd_check_profile( $user_id ) 331 { 332 global $bb_current_user, $user_obj; 333 $bb_current_id = bb_get_current_user_info( 'id' ); 334 bb_set_current_user( $user_id ); 335 if ( $bb_current_id && $bb_current_id != $user_id ) { 336 if ( $user_obj->data->is_bozo && !$bb_current_user->data->is_bozo ) { 337 bb_ksd_submit( $user_id, 'hammer' ); 338 } 339 if ( !$user_obj->data->is_bozo && $bb_current_user->data->is_bozo ) { 340 bb_ksd_submit( $user_id, 'spammer' ); 341 } 342 } else { 343 $response = bb_ksd_submit( $bb_current_user->data->occ . ' ' . $bb_current_user->data->interests ); 344 if ( 'true' == $response[1] && function_exists( 'bb_bozon' ) ) { 345 bb_bozon( bb_get_current_user_info( 'id' ) ); 346 } 347 } 348 bb_set_current_user( (int) $bb_current_id ); 349 } 350 add_action( 'register_user', 'bb_ksd_check_profile', 1); 351 add_action( 'profile_edited', 'bb_ksd_check_profile', 1); 352 353 function bb_ksd_new_post( $post_id ) 354 { 355 global $bb_ksd_pre_post_status; 356 if ( '2' != $bb_ksd_pre_post_status ) { 357 return; 358 } 359 $bb_post = bb_get_post( $post_id ); 360 $topic = get_topic( $bb_post->topic_id ); 361 if ( 0 == $topic->topic_posts ) { 362 bb_delete_topic( $topic->topic_id, 2 ); 363 } 364 } 365 add_filter( 'bb_new_post', 'bb_ksd_new_post' ); 366 367 function bb_akismet_delete_old() 368 { 369 // Delete old every 20 370 $n = mt_rand( 1, 20 ); 371 if ( $n % 20 ) { 372 return; 373 } 374 global $bbdb; 375 $now = bb_current_time( 'mysql' ); 376 $posts = (array) $bbdb->get_col( $bbdb->prepare( 377 "SELECT post_id FROM $bbdb->posts WHERE DATE_SUB(%s, INTERVAL 15 DAY) > post_time AND post_status = '2'", 378 $now 379 ) ); 380 foreach ( $posts as $post ) { 381 bb_delete_post( $post, 1 ); 382 } 383 } 384 385 function bb_ksd_pre_post_status( $post_status, $post_ID ) 386 { 387 global $bb_current_user, $bb_ksd_pre_post_status, $bb_ksd_pre_post; 388 389 // Don't filter content from users with a trusted role 390 if ( in_array( $bb_current_user->roles[0], bb_trusted_roles() ) ) { 391 return $post_status; 392 } 393 394 $response = bb_ksd_submit( $bb_ksd_pre_post ); 395 396 if ( isset( $response[1] ) ) { 397 bb_update_postmeta( $post_ID, 'akismet_response', $response[1] ); 398 } 399 400 if ( 'true' == $response[1] ) { 401 $bb_ksd_pre_post_status = '2'; 402 return $bb_ksd_pre_post_status; 403 } 404 return $post_status; 405 } 406 add_filter( 'pre_post_status', 'bb_ksd_pre_post_status', 10, 2 ); 407 408 function bb_ksd_delete_post( $post_id, $new_status, $old_status ) 409 { 410 // Don't report post deletion 411 if ( 1 == $new_status ) { 412 return; 413 } 414 // Don't report no change in post status 415 if ( $new_status == $old_status ) { 416 return; 417 } 418 // It's being marked as spam, so report it 419 if ( 2 == $new_status ) { 420 bb_ksd_submit_spam( $post_id ); 421 return; 422 } 423 // It's not spam (and not being deleted), so it's ham now 424 if ( 2 == $old_status ) { 425 bb_ksd_submit_ham( $post_id ); 426 return; 427 } 428 } 429 add_action( 'bb_delete_post', 'bb_ksd_delete_post', 10, 3); 430 431 function bb_ksd_post_delete_link( $parts, $args ) 432 { 433 if ( !bb_current_user_can( 'moderate' ) ) { 434 return $parts; 435 } 436 $bb_post = bb_get_post( get_post_id( $args['post_id'] ) ); 437 438 if ( 2 == $bb_post->post_status ) { 439 $query = array( 440 'id' => $bb_post->post_id, 441 'status' => 0, 442 'view' => 'all' 443 ); 444 $display = __('Not Spam'); 445 } else { 446 $query = array( 447 'id' => $bb_post->post_id, 448 'status' => 2 449 ); 450 $display = __('Spam'); 451 } 452 $uri = bb_get_uri( 'bb-admin/delete-post.php', $query, BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_ADMIN ); 453 $uri = esc_url( bb_nonce_url( $uri, 'delete-post_' . $bb_post->post_id ) ); 454 if ( !is_array( $parts ) ) { 455 $parts = array(); 456 $before = ''; 457 $after = ''; 458 } else { 459 $before = isset( $args['last_each']['before'] ) ? $args['last_each']['before'] : ''; 460 $after = isset( $args['last_each']['after'] ) ? $args['last_each']['after'] : ''; 461 } 462 463 // Make sure that the last tag in $before gets a class (if it's there) 464 if ( preg_match( '/.*(<[^>]+>)[^<]*/', $before, $_node ) ) { 465 if ( preg_match( '/class=(\'|")(.*)\1/U', $_node[1], $_class ) ) { 466 $before = str_replace( $_class[0], 'class=' . $_class[1] . 'before-post-spam-link ' . $_class[2] . $_class[1], $before ); 467 } else { 468 $before = preg_replace( '/(.*)<([a-z0-9_-]+)(\s?)([^>]*)>([^<]*)/i', '$1<$2 class="before-post-spam-link"$3$4>$5', $before, 1 ); 469 } 470 } 471 472 $parts[] = $before . '<a class="post-spam-link" href="' . $uri . '" >' . $display . '</a>' . $after; 473 return $parts; 474 } 475 add_filter( 'bb_post_admin', 'bb_ksd_post_delete_link', 10, 2 ); 476 477 function bb_ksd_bulk_post_actions( &$bulk_actions, &$post_query ) { 478 $status = $post_query->get( 'post_status' ); 479 480 $bulk_actions['unspam'] = __( 'Not Spam' ); 481 $bulk_actions['spam'] = __( 'Mark as Spam' ); 482 483 if ( 2 == $status ) 484 unset( $bulk_actions['undelete'], $bulk_actions['spam'] ); 485 elseif ( is_numeric( $status ) ) 486 unset( $bulk_actions['unspam'] ); 487 } 488 489 add_action( 'bulk_post_actions', 'bb_ksd_bulk_post_actions', 10, 2 ); 490 491 function bb_ksd_bulk_post__action( $query_vars, $post_ids, $action ) { 492 $count = 0; 493 494 switch ( $action ) { 495 case 'spam' : 496 foreach ( $post_ids as $post_id ) { 497 $count += (int) (bool) bb_delete_post( $post_id, 2 ); 498 } 499 return array( 'message' => 'spammed', 'count' => $count ); 500 case 'unspam' : 501 foreach ( $post_ids as $post_id ) { 502 $count += (int) (bool) bb_delete_post( $post_id, 0 ); 503 } 504 return array( 'message' => 'unspammed-normal', 'count' => $count ); 505 } 506 } 507 508 add_action( 'bulk_post__spam', 'bb_ksd_bulk_post__action', 10, 3 ); 509 add_action( 'bulk_post__unspam', 'bb_ksd_bulk_post__action', 10, 3 ); 510 511 function bb_ksd_add_post_status_to_forms( $stati, $type ) 512 { 513 if ( 'post' === $type ) { 514 $stati['2'] = __( 'Spam' ); 515 } 516 return $stati; 517 } 518 add_filter( 'bb_query_form_post_status', 'bb_ksd_add_post_status_to_forms', 10, 2 ); 519 520 function bb_ksd_post_del_class( $classes, $post_id, $post ) 521 { 522 if ( '2' === (string) $post->post_status ) { 523 if ( $classes ) { 524 return $classes . ' spam'; 525 } 526 return 'spam'; 527 } 528 return $classes; 529 } 530 add_filter( 'post_del_class', 'bb_ksd_post_del_class', 10, 3 );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Dec 7 01:01:35 2017 | Cross-referenced by PHPXref 0.7.1 |