Skip to:
Content

bbPress.org

Ticket #1477: bbp-akismet.php

File bbp-akismet.php, 9.1 KB (added by Nightgunner5, 14 years ago)
Line 
1<?php
2/*
3Plugin Name: bbPress-Akismet Bridge
4Plugin URI: http://trac.bbpress.org/ticket/1477
5Author: Ben L.
6Author URI: http://nightgunner5.wordpress.com
7Version: 0.1
8*/
9
10function bbp_akismet_check_core( $commentdata ) {
11        global $akismet_api_host, $akismet_api_port, $akismet_last_comment;
12
13        $comment = $commentdata;
14        $comment['user_ip']    = $_SERVER['REMOTE_ADDR'];
15        $comment['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
16        $comment['referrer']   = $_SERVER['HTTP_REFERER'];
17        $comment['blog']       = get_option('home');
18        $comment['blog_lang']  = get_locale();
19        $comment['blog_charset'] = get_option('blog_charset');
20        $comment['permalink']  = get_permalink($comment['comment_post_ID']);
21       
22        $comment['user_role'] = akismet_get_user_roles($comment['user_ID']);
23
24        $comment['akismet_comment_nonce'] = 'inactive';
25
26        if ( akismet_test_mode() )
27                $comment['is_test'] = 'true';
28               
29        foreach ($_POST as $key => $value ) {
30                if ( is_string($value) )
31                        $comment["POST_{$key}"] = $value;
32        }
33
34        $ignore = array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' );
35
36        foreach ( $_SERVER as $key => $value ) {
37                if ( !in_array( $key, $ignore ) && is_string($value) )
38                        $comment["$key"] = $value;
39                else
40                        $comment["$key"] = '';
41        }
42
43        $query_string = '';
44        foreach ( $comment as $key => $data )
45                $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
46               
47        $commentdata['comment_as_submitted'] = $comment;
48
49        $response = akismet_http_post($query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);
50        $commentdata['akismet_result'] = $response[1];
51        if ( 'true' == $response[1] ) {
52                do_action( 'akismet_spam_caught' );
53               
54                $akismet_last_comment = $commentdata;
55
56                akismet_result_spam();
57
58                return true;
59        }
60
61        $akismet_last_comment = $commentdata;
62        return false;
63}
64
65function bbp_akismet_check_topic( $topic_id, $forum_id, $anonymous_data, $topic_author ) {
66        $userdata = get_userdata( $topic_author );
67        $topic = get_post( $topic_id );
68
69        if ( bbp_akismet_check_core( array(
70                        'comment_post_ID' => $forum_id,
71                        'comment_author' => $anonymous_data ? $anonymous_data['bbp_anonymous_name'] : $userdata->display_name,
72                        'comment_author_email' => $anonymous_data ? $anonymous_data['bbp_anonymous_email'] : $userdata->user_email,
73                        'comment_author_url' => $anonymous_data ? $anonymous_data['bbp_anonymous_website'] : $userdata->user_url,
74                        'comment_content' => $topic->post_content,
75                        'comment_type' => 'comment',
76                        'comment_parent' => null,
77                        'user_ID' => $topic_author
78                ) ) ) {
79                remove_action( 'bbp_spammed_topic', 'bbp_akismet_send_spam_topic' );
80                bbp_spam_topic( $topic_id );
81        }
82}
83
84function bbp_akismet_check_reply( $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author ) {
85        $userdata = get_userdata( $reply_author );
86        $reply = get_post( $reply_id );
87
88        if ( bbp_akismet_check_core( array(
89                        'comment_post_ID' => $topic_id,
90                        'comment_author' => $anonymous_data ? $anonymous_data['bbp_anonymous_name'] : $userdata->display_name,
91                        'comment_author_email' => $anonymous_data ? $anonymous_data['bbp_anonymous_email'] : $userdata->user_email,
92                        'comment_author_url' => $anonymous_data ? $anonymous_data['bbp_anonymous_website'] : $userdata->user_url,
93                        'comment_content' => $reply->post_content,
94                        'comment_type' => 'comment',
95                        'comment_parent' => null,
96                        'user_ID' => $reply_author
97                ) ) ) {
98                remove_action( 'bbp_spammed_reply', 'bbp_akismet_send_spam_reply' );
99                bbp_spam_reply( $reply_id );
100        }
101}
102
103function bbp_akismet_send_spam_core( $comment ) {
104        global $wpdb, $akismet_api_host, $akismet_api_port, $current_user, $current_site;
105        $comment_id = (int) $comment_id;
106
107        if ( 'spam' != $comment->comment_approved )
108                return;
109       
110        // use the original version stored in comment_meta if available
111        $as_submitted = get_comment_meta( $comment_id, 'akismet_as_submitted', true);
112        if ( $as_submitted && is_array($as_submitted) && isset($as_submitted['comment_content']) ) {
113                $comment = (object) array_merge( (array)$comment, $as_submitted );
114        }
115       
116        $comment->blog = get_bloginfo('url');
117        $comment->blog_lang = get_locale();
118        $comment->blog_charset = get_option('blog_charset');
119        $comment->permalink = get_permalink($comment->comment_post_ID);
120        $comment->reporter_ip = $_SERVER['REMOTE_ADDR'];
121        if ( is_object($current_user) ) {
122            $comment->reporter = $current_user->user_login;
123        }
124        if ( is_object($current_site) ) {
125                $comment->site_domain = $current_site->domain;
126        }
127
128        $comment->user_role = '';
129        if ( isset( $comment->user_ID ) )
130                $comment->user_role = akismet_get_user_roles($comment->user_ID);
131
132        if ( akismet_test_mode() )
133                $comment->is_test = 'true';
134
135        $query_string = '';
136        foreach ( $comment as $key => $data )
137                $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
138
139        $response = akismet_http_post($query_string, $akismet_api_host, "/1.1/submit-spam", $akismet_api_port);
140        do_action('akismet_submit_spam_comment', $comment_id, $response[1]);
141}
142
143function bbp_akismet_send_spam_topic( $topic_id ) {
144        bbp_akismet_send_spam_core( array(
145                'comment_ID' => $topic_id,
146                'comment_post_ID' => bbp_get_topic_forum_id( $topic_id ),
147                'comment_author' => bbp_get_topic_author( $topic_id ),
148                '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 ),
149                'comment_author_url' => bbp_get_topic_author_url( $topic_id ),
150                'comment_date' => get_post_field( 'post_date', $topic_id ),
151                'comment_content' => get_post_field( 'post_content', $topic_id ),
152                'comment_approved' => get_post_field( 'post_status', $topic_id ),
153                'user_ID' => bbp_get_topic_author_id( $topic_id )
154        ) );
155}
156
157function bbp_akismet_send_spam_reply( $reply_id ) {
158        bbp_akismet_send_spam_core( array(
159                'comment_ID' => $reply_id,
160                'comment_post_ID' => bbp_get_reply_topic_id( $reply_id ),
161                'comment_author' => bbp_get_reply_author( $reply_id ),
162                '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 ),
163                'comment_author_url' => bbp_get_reply_author_url( $reply_id ),
164                'comment_date' => get_post_field( 'post_date', $reply_id ),
165                'comment_content' => get_post_field( 'post_content', $reply_id ),
166                'comment_approved' => get_post_field( 'post_status', $reply_id ),
167                'user_ID' => bbp_get_reply_author_id( $reply_id )
168        ) );
169}
170
171function bbp_akismet_send_ham_core( $comment ) {
172        global $wpdb, $akismet_api_host, $akismet_api_port, $current_user, $current_site;
173       
174        $comment->blog = get_bloginfo('url');
175        $comment->blog_lang = get_locale();
176        $comment->blog_charset = get_option('blog_charset');
177        $comment->permalink = get_permalink($comment->comment_post_ID);
178        $comment->reporter_ip = $_SERVER['REMOTE_ADDR'];
179        if ( is_object($current_user) ) {
180            $comment->reporter = $current_user->user_login;
181        }
182        if ( is_object($current_site) ) {
183                $comment->site_domain = $current_site->domain;
184        }
185
186        $comment->user_role = '';
187        if ( isset( $comment->user_ID ) )
188                $comment->user_role = akismet_get_user_roles($comment->user_ID);
189
190        if ( akismet_test_mode() )
191                $comment->is_test = 'true';
192
193        $query_string = '';
194        foreach ( $comment as $key => $data )
195                $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
196
197        $response = akismet_http_post($query_string, $akismet_api_host, "/1.1/submit-ham", $akismet_api_port);
198        do_action('akismet_submit_nonspam_comment', 0, $response[1]);
199}
200
201function bbp_akismet_send_ham_topic( $topic_id ) {
202        bbp_akismet_send_ham_core( array(
203                'comment_ID' => $topic_id,
204                'comment_post_ID' => bbp_get_topic_forum_id( $topic_id ),
205                'comment_author' => bbp_get_topic_author( $topic_id ),
206                '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 ),
207                'comment_author_url' => bbp_get_topic_author_url( $topic_id ),
208                'comment_date' => get_post_field( 'post_date', $topic_id ),
209                'comment_content' => get_post_field( 'post_content', $topic_id ),
210                'comment_approved' => get_post_field( 'post_status', $topic_id ),
211                'user_ID' => bbp_get_topic_author_id( $topic_id )
212        ) );
213}
214
215function bbp_akismet_send_ham_reply( $reply_id ) {
216        bbp_akismet_send_ham_core( array(
217                'comment_ID' => $reply_id,
218                'comment_post_ID' => bbp_get_reply_topic_id( $reply_id ),
219                'comment_author' => bbp_get_reply_author( $reply_id ),
220                '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 ),
221                'comment_author_url' => bbp_get_reply_author_url( $reply_id ),
222                'comment_date' => get_post_field( 'post_date', $reply_id ),
223                'comment_content' => get_post_field( 'post_content', $reply_id ),
224                'comment_approved' => get_post_field( 'post_status', $reply_id ),
225                'user_ID' => bbp_get_reply_author_id( $reply_id )
226        ) );
227}
228
229add_action( 'bbp_new_topic', 'bbp_akismet_check_topic', 10, 4 );
230add_action( 'bbp_new_reply', 'bbp_akismet_check_reply', 10, 5 );
231add_action( 'bbp_spammed_topic', 'bbp_akismet_send_spam_topic' );
232add_action( 'bbp_unspammed_topic', 'bbp_akismet_send_ham_topic' );
233add_action( 'bbp_spammed_reply', 'bbp_akismet_send_spam_reply' );
234add_action( 'bbp_unspammed_reply', 'bbp_akismet_send_ham_reply' );