<?php
/*
Plugin Name: bbPress-Akismet Bridge
Plugin URI: http://trac.bbpress.org/ticket/1477
Author: Ben L.
Author URI: http://nightgunner5.wordpress.com
Version: 0.1
*/

function bbp_akismet_check_core( $commentdata ) {
	global $akismet_api_host, $akismet_api_port, $akismet_last_comment;

	$comment = $commentdata;
	$comment['user_ip']    = $_SERVER['REMOTE_ADDR'];
	$comment['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
	$comment['referrer']   = $_SERVER['HTTP_REFERER'];
	$comment['blog']       = get_option('home');
	$comment['blog_lang']  = get_locale();
	$comment['blog_charset'] = get_option('blog_charset');
	$comment['permalink']  = get_permalink($comment['comment_post_ID']);
	
	$comment['user_role'] = akismet_get_user_roles($comment['user_ID']);

	$comment['akismet_comment_nonce'] = 'inactive';

	if ( akismet_test_mode() )
		$comment['is_test'] = 'true';
		
	foreach ($_POST as $key => $value ) {
		if ( is_string($value) )
			$comment["POST_{$key}"] = $value;
	}

	$ignore = array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' );

	foreach ( $_SERVER as $key => $value ) {
		if ( !in_array( $key, $ignore ) && is_string($value) )
			$comment["$key"] = $value;
		else
			$comment["$key"] = '';
	}

	$query_string = '';
	foreach ( $comment as $key => $data )
		$query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
		
	$commentdata['comment_as_submitted'] = $comment;

	$response = akismet_http_post($query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);
	$commentdata['akismet_result'] = $response[1];
	if ( 'true' == $response[1] ) {
		do_action( 'akismet_spam_caught' );
		
		$akismet_last_comment = $commentdata;

		akismet_result_spam();

		return true;
	}

	$akismet_last_comment = $commentdata;
	return false;
}

function bbp_akismet_check_topic( $topic_id, $forum_id, $anonymous_data, $topic_author ) {
	$userdata = get_userdata( $topic_author );
	$topic = get_post( $topic_id );

	if ( bbp_akismet_check_core( array(
			'comment_post_ID' => $forum_id,
			'comment_author' => $anonymous_data ? $anonymous_data['bbp_anonymous_name'] : $userdata->display_name,
			'comment_author_email' => $anonymous_data ? $anonymous_data['bbp_anonymous_email'] : $userdata->user_email,
			'comment_author_url' => $anonymous_data ? $anonymous_data['bbp_anonymous_website'] : $userdata->user_url,
			'comment_content' => $topic->post_content,
			'comment_type' => 'comment',
			'comment_parent' => null,
			'user_ID' => $topic_author
		) ) ) {
		remove_action( 'bbp_spammed_topic', 'bbp_akismet_send_spam_topic' );
		bbp_spam_topic( $topic_id );
	}
}

function bbp_akismet_check_reply( $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author ) {
	$userdata = get_userdata( $reply_author );
	$reply = get_post( $reply_id );

	if ( bbp_akismet_check_core( array(
			'comment_post_ID' => $topic_id,
			'comment_author' => $anonymous_data ? $anonymous_data['bbp_anonymous_name'] : $userdata->display_name,
			'comment_author_email' => $anonymous_data ? $anonymous_data['bbp_anonymous_email'] : $userdata->user_email,
			'comment_author_url' => $anonymous_data ? $anonymous_data['bbp_anonymous_website'] : $userdata->user_url,
			'comment_content' => $reply->post_content,
			'comment_type' => 'comment',
			'comment_parent' => null,
			'user_ID' => $reply_author
		) ) ) {
		remove_action( 'bbp_spammed_reply', 'bbp_akismet_send_spam_reply' );
		bbp_spam_reply( $reply_id );
	}
}

function bbp_akismet_send_spam_core( $comment ) {
	global $wpdb, $akismet_api_host, $akismet_api_port, $current_user, $current_site;
	$comment_id = (int) $comment_id;

	if ( 'spam' != $comment->comment_approved )
		return;
	
	// use the original version stored in comment_meta if available	
	$as_submitted = get_comment_meta( $comment_id, 'akismet_as_submitted', true);
	if ( $as_submitted && is_array($as_submitted) && isset($as_submitted['comment_content']) ) {
		$comment = (object) array_merge( (array)$comment, $as_submitted );
	}
	
	$comment->blog = get_bloginfo('url');
	$comment->blog_lang = get_locale();
	$comment->blog_charset = get_option('blog_charset');
	$comment->permalink = get_permalink($comment->comment_post_ID);
	$comment->reporter_ip = $_SERVER['REMOTE_ADDR'];
	if ( is_object($current_user) ) {
	    $comment->reporter = $current_user->user_login;
	}
	if ( is_object($current_site) ) {
		$comment->site_domain = $current_site->domain;
	}

	$comment->user_role = '';
	if ( isset( $comment->user_ID ) )
		$comment->user_role = akismet_get_user_roles($comment->user_ID);

	if ( akismet_test_mode() )
		$comment->is_test = 'true';

	$query_string = '';
	foreach ( $comment as $key => $data )
		$query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';

	$response = akismet_http_post($query_string, $akismet_api_host, "/1.1/submit-spam", $akismet_api_port);
	do_action('akismet_submit_spam_comment', $comment_id, $response[1]);
}

function bbp_akismet_send_spam_topic( $topic_id ) {
	bbp_akismet_send_spam_core( array(
		'comment_ID' => $topic_id,
		'comment_post_ID' => bbp_get_topic_forum_id( $topic_id ),
		'comment_author' => bbp_get_topic_author( $topic_id ),
		'comment_author_email' => bbp_get_topic_author_id( $topic_id ) ? get_the_author_meta( 'email', bbp_get_topic_author_id( $topic_id ) ) : get_post_meta( $topic_id, '_bbp_anonymous_email', true ),
		'comment_author_url' => bbp_get_topic_author_url( $topic_id ),
		'comment_date' => get_post_field( 'post_date', $topic_id ),
		'comment_content' => get_post_field( 'post_content', $topic_id ),
		'comment_approved' => get_post_field( 'post_status', $topic_id ),
		'user_ID' => bbp_get_topic_author_id( $topic_id )
	) );
}

function bbp_akismet_send_spam_reply( $reply_id ) {
	bbp_akismet_send_spam_core( array(
		'comment_ID' => $reply_id,
		'comment_post_ID' => bbp_get_reply_topic_id( $reply_id ),
		'comment_author' => bbp_get_reply_author( $reply_id ),
		'comment_author_email' => bbp_get_reply_author_id( $reply_id ) ? get_the_author_meta( 'email', bbp_get_reply_author_id( $reply_id ) ) : get_post_meta( $reply_id, '_bbp_anonymous_email', true ),
		'comment_author_url' => bbp_get_reply_author_url( $reply_id ),
		'comment_date' => get_post_field( 'post_date', $reply_id ),
		'comment_content' => get_post_field( 'post_content', $reply_id ),
		'comment_approved' => get_post_field( 'post_status', $reply_id ),
		'user_ID' => bbp_get_reply_author_id( $reply_id )
	) );
}

function bbp_akismet_send_ham_core( $comment ) {
	global $wpdb, $akismet_api_host, $akismet_api_port, $current_user, $current_site;
	
	$comment->blog = get_bloginfo('url');
	$comment->blog_lang = get_locale();
	$comment->blog_charset = get_option('blog_charset');
	$comment->permalink = get_permalink($comment->comment_post_ID);
	$comment->reporter_ip = $_SERVER['REMOTE_ADDR'];
	if ( is_object($current_user) ) {
	    $comment->reporter = $current_user->user_login;
	}
	if ( is_object($current_site) ) {
		$comment->site_domain = $current_site->domain;
	}

	$comment->user_role = '';
	if ( isset( $comment->user_ID ) )
		$comment->user_role = akismet_get_user_roles($comment->user_ID);

	if ( akismet_test_mode() )
		$comment->is_test = 'true';

	$query_string = '';
	foreach ( $comment as $key => $data )
		$query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';

	$response = akismet_http_post($query_string, $akismet_api_host, "/1.1/submit-ham", $akismet_api_port);
	do_action('akismet_submit_nonspam_comment', 0, $response[1]);
}

function bbp_akismet_send_ham_topic( $topic_id ) {
	bbp_akismet_send_ham_core( array(
		'comment_ID' => $topic_id,
		'comment_post_ID' => bbp_get_topic_forum_id( $topic_id ),
		'comment_author' => bbp_get_topic_author( $topic_id ),
		'comment_author_email' => bbp_get_topic_author_id( $topic_id ) ? get_the_author_meta( 'email', bbp_get_topic_author_id( $topic_id ) ) : get_post_meta( $topic_id, '_bbp_anonymous_email', true ),
		'comment_author_url' => bbp_get_topic_author_url( $topic_id ),
		'comment_date' => get_post_field( 'post_date', $topic_id ),
		'comment_content' => get_post_field( 'post_content', $topic_id ),
		'comment_approved' => get_post_field( 'post_status', $topic_id ),
		'user_ID' => bbp_get_topic_author_id( $topic_id )
	) );
}

function bbp_akismet_send_ham_reply( $reply_id ) {
	bbp_akismet_send_ham_core( array(
		'comment_ID' => $reply_id,
		'comment_post_ID' => bbp_get_reply_topic_id( $reply_id ),
		'comment_author' => bbp_get_reply_author( $reply_id ),
		'comment_author_email' => bbp_get_reply_author_id( $reply_id ) ? get_the_author_meta( 'email', bbp_get_reply_author_id( $reply_id ) ) : get_post_meta( $reply_id, '_bbp_anonymous_email', true ),
		'comment_author_url' => bbp_get_reply_author_url( $reply_id ),
		'comment_date' => get_post_field( 'post_date', $reply_id ),
		'comment_content' => get_post_field( 'post_content', $reply_id ),
		'comment_approved' => get_post_field( 'post_status', $reply_id ),
		'user_ID' => bbp_get_reply_author_id( $reply_id )
	) );
}

add_action( 'bbp_new_topic', 'bbp_akismet_check_topic', 10, 4 );
add_action( 'bbp_new_reply', 'bbp_akismet_check_reply', 10, 5 );
add_action( 'bbp_spammed_topic', 'bbp_akismet_send_spam_topic' );
add_action( 'bbp_unspammed_topic', 'bbp_akismet_send_ham_topic' );
add_action( 'bbp_spammed_reply', 'bbp_akismet_send_spam_reply' );
add_action( 'bbp_unspammed_reply', 'bbp_akismet_send_ham_reply' );