You are here

function cleantalk_webform_submission_presave in Anti Spam by CleanTalk 7.2

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

Implements hook_webform_submission_presave()

File

./cleantalk.module, line 2388
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) {

    # Don't check admin activity
    if (isset($user->roles) && is_array($user->roles)) {
      if (in_array('administrator', $user->roles)) {
        return;
      }
    }
    $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;
    }
  }
  $sender_email = null;
  $message = '';
  $messagify = function ($array) use (&$messagify, &$sender_email, &$message) {
    foreach ($array as $key => $value) {
      if (is_array($value)) {
        $messagify($value);
      }
      else {
        if ($sender_email === null && preg_match("/^\\S+@\\S+\\.\\S+\$/", $value)) {
          $sender_email = $value;
        }
        else {
          $message .= "{$value}\n";
        }
      }
    }
  };
  $messagify($_POST['submitted']);
  $spam_check = array();
  $spam_check['type'] = 'webform';
  $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) {
    $ct_automod = variable_get('cleantalk_automod', 1);
    if ($ct_automod == 1) {
      $submission->is_draft = 1;
      if (variable_get('cleantalk_wf_nomail', 1) == 1) {
        $node->webform['emails'] = array();
      }
      if (variable_get('cleantalk_wf_silent', 1) != 1) {
        drupal_set_message($spam_result['ct_result_comment'], 'error');
      }
    }
  }
}