You are here

function cleantalk_webform_submission_presave in Anti Spam by CleanTalk 8

Same name and namespace in other branches
  1. 7 cleantalk.module \cleantalk_webform_submission_presave()
  2. 7.2 cleantalk.module \cleantalk_webform_submission_presave()

Implements hook_webform_submission_presave()

File

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

Code

function cleantalk_webform_submission_presave($node, &$submission) {
  global $user;

  // Don't check reged user with >= 'cleantalk_comments' approved msgs.
  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 = \Drupal::config('cleantalk.settings')
      ->get('cleantalk_comments');
    if ($count >= $ct_comments) {
      return;
    }
  }
  $sender_email = null;
  $message = '';
  foreach ($_POST['submitted'] as $key => $value) {
    if ($sender_email === null && !is_array($value) && preg_match("/^\\S+@\\S+\\.\\S+\$/", $value)) {
      $sender_email = $value;
    }
    else {
      $message .= "{$value}\n";
    }
  }
  $spam_check = array();
  $spam_check['type'] = 'comment';
  $spam_check['sender_email'] = !empty($sender_email) ? $sender_email : '';
  $spam_check['sender_nickname'] = '';
  $spam_check['message_title'] = '';
  $spam_check['message_body'] = $message;
  $spam_check['example_title'] = '';
  $spam_check['example_body'] = '';
  $spam_check['example_comments'] = '';
  $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) {
    $ct_automod = \Drupal::config('cleantalk.settings')
      ->get('cleantalk_automod');
    if ($ct_automod == 1) {
      $submission->is_draft = empty($spam_result['ct_result_comment']) ? 0 : 1;
    }
    drupal_set_message($spam_result['ct_result_comment'], 'error');
  }
}