akismet.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. ** Akismet Filter
  4. ** Akismet API: http://akismet.com/development/api/
  5. **/
  6. add_filter( 'wpcf7_spam', 'wpcf7_akismet', 10, 1 );
  7. function wpcf7_akismet( $spam ) {
  8. if ( $spam ) {
  9. return $spam;
  10. }
  11. if ( ! wpcf7_akismet_is_available() ) {
  12. return false;
  13. }
  14. if ( ! $params = wpcf7_akismet_submitted_params() ) {
  15. return false;
  16. }
  17. $c = array();
  18. $c['comment_author'] = $params['author'];
  19. $c['comment_author_email'] = $params['author_email'];
  20. $c['comment_author_url'] = $params['author_url'];
  21. $c['comment_content'] = $params['content'];
  22. $c['blog'] = get_option( 'home' );
  23. $c['blog_lang'] = get_locale();
  24. $c['blog_charset'] = get_option( 'blog_charset' );
  25. $c['user_ip'] = $_SERVER['REMOTE_ADDR'];
  26. $c['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
  27. $c['referrer'] = $_SERVER['HTTP_REFERER'];
  28. // http://blog.akismet.com/2012/06/19/pro-tip-tell-us-your-comment_type/
  29. $c['comment_type'] = 'contact-form';
  30. if ( $permalink = get_permalink() ) {
  31. $c['permalink'] = $permalink;
  32. }
  33. $ignore = array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' );
  34. foreach ( $_SERVER as $key => $value ) {
  35. if ( ! in_array( $key, (array) $ignore ) ) {
  36. $c["$key"] = $value;
  37. }
  38. }
  39. if ( wpcf7_akismet_comment_check( $c ) ) {
  40. $spam = true;
  41. $submission = WPCF7_Submission::get_instance();
  42. $submission->add_spam_log( array(
  43. 'agent' => 'akismet',
  44. 'reason' => __( "Akismet returns a spam response.", 'contact-form-7' ),
  45. ) );
  46. } else {
  47. $spam = false;
  48. }
  49. return $spam;
  50. }
  51. function wpcf7_akismet_is_available() {
  52. if ( is_callable( array( 'Akismet', 'get_api_key' ) ) ) {
  53. return (bool) Akismet::get_api_key();
  54. }
  55. return false;
  56. }
  57. function wpcf7_akismet_submitted_params() {
  58. $params = array(
  59. 'author' => '',
  60. 'author_email' => '',
  61. 'author_url' => '',
  62. 'content' => '',
  63. );
  64. $has_akismet_option = false;
  65. foreach ( (array) $_POST as $key => $val ) {
  66. if ( '_wpcf7' == substr( $key, 0, 6 )
  67. or '_wpnonce' == $key ) {
  68. continue;
  69. }
  70. if ( is_array( $val ) ) {
  71. $val = implode( ', ', wpcf7_array_flatten( $val ) );
  72. }
  73. $val = trim( $val );
  74. if ( 0 == strlen( $val ) ) {
  75. continue;
  76. }
  77. if ( $tags = wpcf7_scan_form_tags( array( 'name' => $key ) ) ) {
  78. $tag = $tags[0];
  79. $akismet = $tag->get_option( 'akismet',
  80. '(author|author_email|author_url)', true );
  81. if ( $akismet ) {
  82. $has_akismet_option = true;
  83. if ( 'author' == $akismet ) {
  84. $params[$akismet] = trim( $params[$akismet] . ' ' . $val );
  85. continue;
  86. } elseif ( '' == $params[$akismet] ) {
  87. $params[$akismet] = $val;
  88. continue;
  89. }
  90. }
  91. }
  92. $params['content'] .= "\n\n" . $val;
  93. }
  94. if ( ! $has_akismet_option ) {
  95. return false;
  96. }
  97. $params['content'] = trim( $params['content'] );
  98. return $params;
  99. }
  100. function wpcf7_akismet_comment_check( $comment ) {
  101. $spam = false;
  102. $query_string = wpcf7_build_query( $comment );
  103. if ( is_callable( array( 'Akismet', 'http_post' ) ) ) {
  104. $response = Akismet::http_post( $query_string, 'comment-check' );
  105. } else {
  106. return $spam;
  107. }
  108. if ( 'true' == $response[1] ) {
  109. $spam = true;
  110. }
  111. if ( $submission = WPCF7_Submission::get_instance() ) {
  112. $submission->akismet = array( 'comment' => $comment, 'spam' => $spam );
  113. }
  114. return apply_filters( 'wpcf7_akismet_comment_check', $spam, $comment );
  115. }