You are here

function cleantalk_validate_comment in Anti Spam by CleanTalk 7.2

Same name and namespace in other branches
  1. 8.4 cleantalk.module \cleantalk_validate_comment()
  2. 8 cleantalk.module \cleantalk_validate_comment()
  3. 8.3 cleantalk.module \cleantalk_validate_comment()
  4. 7.5 cleantalk.module \cleantalk_validate_comment()
  5. 7 cleantalk.module \cleantalk_validate_comment()
  6. 7.4 cleantalk.module \cleantalk_validate_comment()
  7. 9.1.x cleantalk.module \cleantalk_validate_comment()

Cleantalk comment validation function.

1 string reference to 'cleantalk_validate_comment'
_cleantalk_form_alter in ./cleantalk.module
Cleantalk inner function - alters needed form.

File

./cleantalk.module, line 1467
Main CleanTalk integration module functions.

Code

function cleantalk_validate_comment(&$form, &$form_state) {
  global $user;

  // Don't check reged user with >= 'cleantalk_comments' approved msgs.
  if ($user->uid > 0) {

    # Don't check admin activity
    if (isset($user->roles) && is_array($user->roles)) {
      if (in_array('administrator', $user->roles)) {
        return;
      }
    }
  }
  $spam_check = array();
  if (isset($form_state['values']) && is_array($form_state['values'])) {
    $base_form_id = $form_state['build_info']['base_form_id'];
    if ($base_form_id == 'comment_form') {

      // Fill parameters array for usual comment
      $spam_check['type'] = 'comment';
      $comment_lang = array_key_exists('language', $form_state['values']) ? $form_state['values']['language'] : LANGUAGE_NONE;
      if (isset($form_state['values']['comment_body']) && is_array($form_state['values']['comment_body']) && isset($form_state['values']['comment_body'][$comment_lang]) && is_array($form_state['values']['comment_body'][$comment_lang]) && isset($form_state['values']['comment_body'][$comment_lang][0])) {
        $comment_body = $form_state['values']['comment_body'][$comment_lang][0];
      }

      // Values below can be empty, we'll check as comment because of $base_form_id
      $spam_check['sender_email'] = !empty($form_state['values']['mail']) ? $form_state['values']['mail'] : '';
      $spam_check['sender_nickname'] = !empty($form_state['values']['name']) ? $form_state['values']['name'] : '';
      $spam_check['message_title'] = !empty($form_state['values']['subject']) ? $form_state['values']['subject'] : '';
      $spam_check['message_body'] = is_array($comment_body) && isset($comment_body['value']) ? $comment_body['value'] : '';
    }
    else {
      if ($base_form_id == 'node_form') {

        // Fill paramaters array for something looking like a comment
        $comment_lang = array_key_exists('language', $form_state['values']) ? $form_state['values']['language'] : LANGUAGE_NONE;
        if (isset($form_state['values']['body']) && is_array($form_state['values']['body']) && isset($form_state['values']['body'][$comment_lang]) && is_array($form_state['values']['body'][$comment_lang]) && isset($form_state['values']['body'][$comment_lang][0])) {
          $comment_body = $form_state['values']['body'][$comment_lang][0];

          // Values below cannot be empty, we won't check as comment because of $base_form_id
          if (!empty($form_state['values']['mail'])) {
            $spam_check['sender_email'] = $form_state['values']['mail'];
          }
          if (!empty($form_state['values']['name'])) {
            $spam_check['sender_nickname'] = $form_state['values']['name'];
          }
          if (!empty($form_state['values']['title'])) {
            $spam_check['message_title'] = $form_state['values']['title'];
          }
          if (is_array($comment_body) && isset($comment_body['value'])) {
            $spam_check['message_body'] = $comment_body['value'];
          }
          if (!empty($spam_check)) {
            $spam_check['type'] = 'node';
          }
        }
      }
    }
  }
  if (empty($spam_check)) {
    return;
  }
  else {
    if ($user->uid > 0) {
      $result = db_query('SELECT count(*) AS count FROM {comment} WHERE uid=:uid AND status=1', array(
        ':uid' => $user->uid,
      ));
      $count = intval($result
        ->fetchObject()->count);
      $ct_comments = variable_get('cleantalk_comments', 3);
      if ($count >= $ct_comments) {
        return;
      }
    }
  }
  $spam_result = _cleantalk_check_spam($spam_check);
  if (isset($spam_result) && is_array($spam_result) && $spam_result['errno'] == 0 && $spam_result['allow'] != 1 && $spam_result['stop_queue'] == 1) {

    // Value of ct_result_comment is sanitized already (before storing).
    form_set_error('comment_body', $spam_result['ct_result_comment']);
  }
}