You are here

function BotchaRecipebookAbstract::handle in BOTCHA Spam Prevention 7.2

Same name and namespace in other branches
  1. 6.2 controller/botcha_recipebook.controller.inc \BotchaRecipebookAbstract::handle()

Handle form depending on the result of spam check.

_state

Parameters

string $result: This parameter is string and not boolean to have a chance to easily implement new results of spam check (such as 'postponed', 'suspected' or other).

array $form:

File

controller/botcha_recipebook.controller.inc, line 243
Controller layer of the BotchaRecipebook objects.

Class

BotchaRecipebookAbstract
@file Controller layer of the BotchaRecipebook objects.

Code

function handle($result, $form, $form_state) {
  $recipes_success = $this
    ->getRecipesByStatus('success');
  $recipes_success_count = count($recipes_success);
  $recipes_spam = $this
    ->getRecipesByStatus('spam');
  $recipes_spam_count = count($recipes_spam);

  // !!~ @todo Recipebook handle Reduce code duplication.
  switch ($result) {
    case 'success':
      variable_set('botcha_form_passed_counter', $recipes_success_count);

      // Show good submissions in log.
      if (BOTCHA_LOGLEVEL >= 3) {
        watchdog(BOTCHA_LOG, '%form_id post approved by BOTCHA.!more', array(
          '%form_id' => $form['form_id']['#value'],
          '!more' => '' . (BOTCHA_LOGLEVEL >= 3 ? ' Checked ' . count($this->recipes) . ' botchas (' . join(', ', array_keys($this->recipes)) . ').' : '') . (BOTCHA_LOGLEVEL >= 5 ? '<br /><br />' . 'POST=<pre>' . print_r(_botcha_filter_value($_POST), 1) . '</pre>' : '') . (BOTCHA_LOGLEVEL >= 5 ? '<br /><br />' . 'GET=<pre>' . print_r(_botcha_filter_value($_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_value($form_state['values']), 1) . '</pre>' : ''),
        ), WATCHDOG_INFO);
      }
      $rules_event_name = 'botcha_form_approved';
      break;
    case 'spam':
    default:
      variable_set('botcha_form_blocked_counter', $recipes_spam_count);

      // Prepare a list of failed recipes.
      foreach ($recipes_spam as $recipe_spam) {
        $recipe_spam_ids[$recipe_spam->id] = $recipe_spam->id;
      }

      // Just using the first failed recipe to reject form submission.
      $recipe_spam = reset($recipes_spam);
      form_set_error($recipe_spam->error_field, $recipe_spam->error_text);

      // Show blocked submissions in log.
      if (BOTCHA_LOGLEVEL >= 1) {
        watchdog(BOTCHA_LOG, '%form_id post blocked by BOTCHA: submission looks like from a spambot.!more', array(
          '%form_id' => $form['form_id']['#value'],
          '!more' => '' . (BOTCHA_LOGLEVEL >= 2 ? '<br /><br />' . ' Failed  ' . $recipes_spam_count . ' of ' . count($this->recipes) . ' recipes [' . implode(', ', $recipe_spam_ids) . '] from "' . $this->id . '" recipe book.' : '') . (BOTCHA_LOGLEVEL >= 5 ? '<br /><br />' . 'POST=<pre>' . print_r(_botcha_filter_value($_POST), 1) . '</pre>' : '') . (BOTCHA_LOGLEVEL >= 5 ? '<br /><br />' . 'GET=<pre>' . print_r(_botcha_filter_value($_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_value($form_state['values']), 1) . '</pre>' : ''),
        ), WATCHDOG_WARNING);
      }
      $rules_event_name = 'botcha_form_rejected';
      break;
  }

  // Invoke rules event.
  if (module_exists('rules')) {
    $arguments = array(
      //      'form' => &$form,
      //      'form_state' => &$form_state,
      'form_id' => $form['form_id']['#value'],
      'total_recipes' => count($this->recipes),
      'passed_recipes' => $recipes_success_count,
      'passed_recipes_names' => join(', ', array_keys($recipes_success)),
      // !!~ @todo handle Add last recipe name.

      //'last_recipe_name' => $recipe->name,

      // !!~ @todo Recipebook handle Add a reason of fail to rules event invokation.

      //'fail' => $fail,
      'fail' => 'FAIL',
      'failed_field' => 'mail',
    );

    // !!? Do we need per recipe rules event invoking?
    rules_invoke_event_by_args($rules_event_name, $arguments);
  }

  // Clean $_SESSION.
  Botcha::clean();
}