From 77a3936ac04dd98611792b47ef81a9cbf1bb3289 Mon Sep 17 00:00:00 2001
Message-Id: <77a3936ac04dd98611792b47ef81a9cbf1bb3289.1332947571.git.dmitriy.trt@gmail.com>
From: "Dmitriy.trt" <dmitriy.trt@gmail.com>
Date: Wed, 28 Mar 2012 22:12:45 +0700
Subject: [PATCH] Issue #1483796: fix fatal errors when posting comments

---
 antispam.module |   21 ++++++++++++++-------
 1 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/antispam.module b/antispam.module
index 3bcffc0..56fe3b2 100755
--- a/antispam.module
+++ b/antispam.module
@@ -992,29 +992,36 @@ function antispam_comment_presave($comment) {
 
   $query = db_select('antispam_spam_marks', 's');
   $query->fields('s', array('content_id'));
-  if ($antispambot_rules['body']) {
-    $query->join('comment', 'c', 's.content_id = c.cid');
-  }
   $query->condition('s.content_type', 'comment');
   $query->range(0, 1);
 
+  $rules_conditions = db_or();
+
   if ($antispambot_rules['ip']) {
-    $query->condition('s.hostname', ip_address());
+    $rules_conditions->condition('s.hostname', ip_address());
     $debug_info['IP-address'] = ip_address();
     $num_condition++;
   }
   if ($antispambot_rules['mail'] && !empty($comment->mail)) {
-    $query->condition('s.mail', $comment->mail);
+    $rules_conditions->condition('s.mail', $comment->mail);
     $debug_info['E-mail'] = $comment->mail;
     $num_condition++;
   }
   if ($antispambot_rules['body'] && !empty($comment->comment_body)) {
-    $query->condition('c.comment', render($comment->comment_body[$comment->language][0]['value']));
-    $debug_info['Content'] = $comment->mail[$comment->language][0]['value'];
+    if ($num_condition) {
+      $join_type = 'LEFT OUTER';
+    }
+    else {
+      $join_type = 'INNER';
+    }
+    $query->addJoin($join_type, 'field_data_comment_body', 'c', 's.content_type = c.entity_type AND s.content_id = c.entity_id');
+    $rules_conditions->condition('c.comment_body_value', $comment->comment_body[$comment->language][0]['value']);
+    $debug_info['Content'] = $comment->comment_body[$comment->language][0]['value'];
     $num_condition++;
   }
 
   if ($num_condition) {
+    $query->condition($rules_conditions);
     $has_rows = $query->execute()->fetchField();
     if ($has_rows) {
       antispam_anti_spambot_action($debug_info);
-- 
1.7.8.3

