You are here

function antispam_comment_presave in AntiSpam 7

Implements hook_comment_presave()

File

./antispam.module, line 943
Primary hook implementations for the Antispam module.

Code

function antispam_comment_presave($comment) {

  // NOTE: Called from comment_save().
  //
  // $comment->cid is not yet determined at this moment for a new comment.
  // If this is updating existing comment, then cid is already assigned.
  $num_condition = 0;
  $debug_info = array();

  // Ok, let's build a quick query to see if we can catch a spambot.
  $antispambot_rules = antispam_get_anti_spambot_rules();
  $query = db_select('antispam_spam_marks', 's');
  $query
    ->fields('s', array(
    'content_id',
  ));
  $query
    ->condition('s.content_type', 'comment');
  $query
    ->range(0, 1);
  $rules_conditions = db_or();
  if (!empty($antispambot_rules['ip'])) {
    $rules_conditions
      ->condition('s.hostname', ip_address());
    $debug_info['IP-address'] = ip_address();
    $num_condition++;
  }
  if (!empty($antispambot_rules['mail']) && !empty($comment->mail)) {
    $rules_conditions
      ->condition('s.mail', $comment->mail);
    $debug_info['E-mail'] = $comment->mail;
    $num_condition++;
  }
  if (!empty($antispambot_rules['body']) && !empty($comment->comment_body)) {
    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);
    }
  }
}