You are here

function _botcha_form_validate in BOTCHA Spam Prevention 6

Same name and namespace in other branches
  1. 6.2 botcha.module \_botcha_form_validate()
  2. 7 botcha.module \_botcha_form_validate()
  3. 7.2 botcha.module \_botcha_form_validate()

Custom form validation.

1 string reference to '_botcha_form_validate'
botcha_form_alter_botcha in ./botcha.botcha.inc
Main BOTCHA worker - process the form and apply BOTCHA protection

File

./botcha.botcha.inc, line 700
Implementation of botcha form logic.

Code

function _botcha_form_validate($form, &$form_state) {

  // FIXME: where does this empty value come from ? happens with comments
  unset($form_state['values']['']);
  $build_id = isset($_POST['form_build_id']) ? $_POST['form_build_id'] : $form['#build_id'];
  $recipes = isset($form_state['#botcha_recipes_subm']) ? $form_state['#botcha_recipes_subm'] : $form_state['#botcha_recipes'];
  $botcha_names = array();
  $i = 0;
  $fail = '';
  foreach ($recipes as $recipe) {
    if (isset($recipe->form_elements)) {
      foreach ($recipe->form_elements as $field => $value) {
        if (isset($value['!valid_token']) && $form_state['botcha_submit_values'][$field] !== $value['!valid_token']) {
          $fail = $form_state['botcha_submit_values'][$field] . '!=' . $value['!valid_token'];

          //          unset($form_state['post'][$field], $form_state['values'][$field], $_POST[$field]);
          break 2;

          // No need to finish other botchas, we got a bot
        }

        //        unset($form_state['post'][$field], $form_state['values'][$field], $_POST[$field]);
      }
    }
    if (isset($recipe->url_elements)) {
      foreach ($recipe->url_elements as $field => $value) {
        if (isset($value['!valid_token']) && $_GET[$field] !== $value['!valid_token']) {
          $fail = $_GET[$field] . '!=' . $value['!valid_token'];
          unset($_GET[$field]);
          break 2;
        }
        unset($_GET[$field]);
      }
    }
    if (!empty($recipe->proc)) {
      switch ($recipe->proc) {
        case 'check_cache':
          if (!_botcha_get_form_cache($build_id)) {
            $fail = 'botcha_resubmit';
            break 2;

            // No need to finish other botchas, we got a bot
          }
          break;
      }
    }
    $botcha_names[] = $recipe->name;
    $i++;
  }
  _botcha_clear_form_cache($build_id);

  // Invalidate cache so resubmit will not work
  if ($i < count($recipes)) {
    variable_set('botcha_form_blocked_counter', variable_get('botcha_form_blocked_counter', 0) + 1);
    form_set_error($recipe->error_field, $recipe->error_text);

    // show blocked submissions in log
    // FIXME: 1) more generic statement:
    if (BOTCHA_LOGLEVEL >= 1) {
      watchdog(BOTCHA_LOG, '%form_id post blocked by BOTCHA: submission looks like from a spambot.!more', array(
        '%form_id' => $form['#id'],
        '!more' => '' . (BOTCHA_LOGLEVEL >= 2 ? '<br /><br />' . ' Failed [' . $fail . '] botcha \'' . $recipe->name . '\' #' . ($i + 1) . ' of ' . count($recipes) . ' recipes from "' . $form_state['#botcha'] . '" book.' : '') . (BOTCHA_LOGLEVEL >= 5 ? '<br /><br />' . 'POST=<pre>' . print_r(_botcha_filter_form_values_log($_POST), 1) . '</pre>' : '') . (BOTCHA_LOGLEVEL >= 5 ? '<br /><br />' . 'GET=<pre>' . print_r(_botcha_filter_form_values_log($_GET), 1) . '</pre>' : '') . (BOTCHA_LOGLEVEL >= 5 ? '<br /><br />' . 'SERVER=<pre>' . print_r($_SERVER, 1) . '</pre>' : '') . (BOTCHA_LOGLEVEL >= 5 ? '<br /><br />' . ' values=<pre>' . print_r(_botcha_filter_form_values_log($form_state['values']), 1) . '</pre>' : ''),
      ), WATCHDOG_WARNING);
    }
    $rules_event_name = 'botcha_form_rejected';
  }
  else {
    variable_set('botcha_form_passed_counter', variable_get('botcha_form_passed_counter', 0) + 1);

    // show good submissions in log
    if (BOTCHA_LOGLEVEL >= 3) {
      watchdog(BOTCHA_LOG, '%form_id post approved by BOTCHA.!more', array(
        '%form_id' => $form['#id'],
        '!more' => '' . (BOTCHA_LOGLEVEL >= 3 ? ' Checked ' . count($recipes) . ' botchas (' . join(', ', $botcha_names) . ').' : '') . (BOTCHA_LOGLEVEL >= 5 ? '<br /><br />' . 'POST=<pre>' . print_r(_botcha_filter_form_values_log($_POST), 1) . '</pre>' : '') . (BOTCHA_LOGLEVEL >= 5 ? '<br /><br />' . 'GET=<pre>' . print_r(_botcha_filter_form_values_log($_GET), 1) . '</pre>' : '') . (BOTCHA_LOGLEVEL >= 5 ? '<br /><br />' . 'SERVER=<pre>' . print_r($_SERVER, 1) . '</pre>' : '') . (BOTCHA_LOGLEVEL >= 5 ? '<br /><br />' . ' form=<pre>' . print_r(_botcha_filter_form_log($form), 1) . '</pre>' : '') . (BOTCHA_LOGLEVEL >= 5 ? '<br /><br />' . ' values=<pre>' . print_r(_botcha_filter_form_values_log($form_state['values']), 1) . '</pre>' : ''),
      ), WATCHDOG_INFO);
    }
    $rules_event_name = 'botcha_form_approved';
  }
  if (module_exists('rules')) {
    $arguments = array(
      //      'form' => &$form,
      //      'form_state' => &$form_state,
      'form_id' => $form['#id'],
      'total_recipes' => count($recipes),
      'passed_recipes' => $i,
      'passed_recipes_names' => join(', ', $botcha_names),
      'last_recipe_name' => $recipe->name,
      'fail' => $fail,
      'failed_field' => $recipe->error_field,
      'failed_error' => $recipe->error_text,
    );
    rules_invoke_event($rules_event_name, $arguments);
  }
}