You are here

function cleantalk_validate_comment in Anti Spam by CleanTalk 8

Same name and namespace in other branches
  1. 8.4 cleantalk.module \cleantalk_validate_comment()
  2. 8.3 cleantalk.module \cleantalk_validate_comment()
  3. 7.5 cleantalk.module \cleantalk_validate_comment()
  4. 7 cleantalk.module \cleantalk_validate_comment()
  5. 7.2 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 348
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 (is_object($user) && isset($user->uid) && $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 = \Drupal::config('cleantalk.settings')->get('cleantalk_comments');
        if ($count >= $ct_comments) {
          return;
        }*/
  }
  $comment_lang = !empty($form_state
    ->getValue('language')) ? $form_state
    ->getValue('language') : Language::LANGCODE_NOT_SPECIFIED;
  $comment_body = $form_state
    ->getValue('comment_body')[0]['value'];
  $spam_check = array();
  $spam_check['type'] = 'comment';
  $spam_check['sender_email'] = !empty($form_state
    ->getValue('mail')) ? $form_state
    ->getValue('mail') : '';
  $spam_check['sender_nickname'] = !empty($form_state
    ->getValue('name')) ? $form_state
    ->getValue('name') : '';
  $spam_check['message_title'] = !empty($form_state
    ->getValue('subject')[0]['value']) ? $form_state
    ->getValue('subject')[0]['value'] : '';
  $spam_check['message_body'] = !empty($comment_body) ? $comment_body : '';
  $spam_result = _cleantalk_check_spam($spam_check);
  if (isset($spam_result) && is_array($spam_result) && $spam_result['errno'] == 0 && $spam_result['allow'] != 1) {

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