You are here

function cleantalk_validate_webform in Anti Spam by CleanTalk 8.3

Same name and namespace in other branches
  1. 8.4 cleantalk.module \cleantalk_validate_webform()
  2. 7.5 cleantalk.module \cleantalk_validate_webform()
  3. 7.4 cleantalk.module \cleantalk_validate_webform()
  4. 9.1.x cleantalk.module \cleantalk_validate_webform()

Cleantalk inner function - webform validation function.

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

File

./cleantalk.module, line 254

Code

function cleantalk_validate_webform(&$form, &$form_state) {
  if (\Drupal::config('cleantalk.settings')
    ->get('cleantalk_check_webforms')) {
    if (in_array('::submit', $form_state
      ->getSubmitHandlers()) || CleantalkFuncs::_cleantalk_check_form_submit_handlers($form_state
      ->getSubmitHandlers())) {
      $current_user = \Drupal::currentUser();
      $spam_check = array();
      $spam_check['type'] = 'webform';
      if (!empty($form_state
        ->get('multistep'))) {
        $ct_temp_msg_data = CleantalkHelper::get_fields_any($form_state
          ->get('multistep'), \Drupal::config('cleantalk.settings')
          ->get('cleantalk_fields_exclusions'));
        $spam_check['multistep_submit_time'] = $form_state
          ->get('multistep_submit_time');
      }
      else {
        $ct_temp_msg_data = CleantalkHelper::get_fields_any(\Drupal::request()->request
          ->all(), \Drupal::config('cleantalk.settings')
          ->get('cleantalk_fields_exclusions'));
      }
      if ($current_user
        ->id()) {
        $user = \Drupal\user\Entity\User::load($current_user
          ->id());
        $spam_check['sender_nickname'] = !empty($user
          ->get('name')) ? $user
          ->get('name')->value : '';
      }
      else {
        $spam_check['sender_nickname'] = $ct_temp_msg_data['nickname'] ? $ct_temp_msg_data['nickname'] : '';
      }
      $spam_check['sender_email'] = $ct_temp_msg_data['email'] ? $ct_temp_msg_data['email'] : '';
      $spam_check['message_title'] = $ct_temp_msg_data['subject'] ? $ct_temp_msg_data['subject'] : '';
      $spam_check['message_body'] = $ct_temp_msg_data['message'] ? $ct_temp_msg_data['message'] : '';
      $spam_result = CleantalkFuncs::_cleantalk_check_spam($spam_check, $form_state
        ->getErrors());
      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('message', strip_tags($spam_result['ct_result_comment'], '<p><a>'));
      }
    }
    else {
      if (empty($form_state
        ->get('multistep'))) {
        $form_state
          ->set('multistep', \Drupal::request()->request
          ->all());
        $form_state
          ->set('multistep_submit_time', CleantalkFuncs::apbct_getcookie('apbct_timestamp'));
      }
      else {
        $previous_storage = $form_state
          ->get('multistep');
        $form_state
          ->set('multistep', array_merge($previous_storage, \Drupal::request()->request
          ->all()));
      }
    }
  }
}