You are here

public function Botcha::form_alter in BOTCHA Spam Prevention 7.3

Same name and namespace in other branches
  1. 6.3 controller/application/botcha.application.controller.inc \Botcha::form_alter()

Implements hook_form_alter().

This function adds BOTCHA protection to forms for untrusted users if needed and adds BOTCHA administration links for site administrators if this option is enabled.

Overrides IBotcha::form_alter

File

controller/application/botcha.application.controller.inc, line 69
Contains Botcha class.

Class

Botcha
Just a middleman for achieving purposes such as:

Code

public function form_alter(&$form, &$form_state, $form_id) {

  // Get an instance of BOTCHA form controller.
  $form_controller = $this
    ->getController(Botcha::CONTROLLER_TYPE_FORM);
  $botcha_form = $form_controller
    ->getForm($form_id, FALSE);

  // Check if it is allowed to protect.
  if ($botcha_form
    ->isEnabled()) {

    // Add admin links functionality.
    $botcha_form
      ->addAdminLinks($form);

    // Get a recipe book and apply all applicable recipes to the form.
    $rbid = $botcha_form
      ->getRecipebook();
    $recipebook = $this
      ->getController(Botcha::CONTROLLER_TYPE_RECIPEBOOK)
      ->getRecipebook($rbid, FALSE);
    if ($recipebook
      ->isApplicable($form, $form_state)) {

      // Apply each of recipebook's recipes ...
      $recipe_controller = $this
        ->getController(Botcha::CONTROLLER_TYPE_RECIPE);
      foreach ($recipebook
        ->getRecipes() as $recipe_id) {
        $recipe = $recipe_controller
          ->getRecipe($recipe_id, FALSE);
        $recipe
          ->apply($form, $form_state);

        // CSS.
        if ($css = $recipe
          ->getCss()) {
          $csss[] = $css;
        }

        // JS.
        if ($recipe instanceof BotchaRecipeUsingJsAbstract) {
          $jss[] = $recipe
            ->getJsValue();
        }
      }

      // ... and the recipe book itself.
      $recipebook
        ->apply($form, $form_state);

      // Logging.
      // @todo Refactor logging.
      if (BOTCHA_LOGLEVEL >= 4) {
        watchdog(BOTCHA_LOG, '%form_id form prepared by BOTCHA: added recipes - !botchas!more', array(
          '%form_id' => $form['form_id']['#value'],
          '!botchas' => implode(', ', $recipebook
            ->getRecipes()),
          '!more' => '' . (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 && count($jss) ? '<br /><br />' . 'JS=<pre>' . join("\n", $jss) . '</pre>' : '') . (BOTCHA_LOGLEVEL >= 5 && count($csss) ? '<br /><br />' . 'CSS=<pre>' . join("\n", $csss) . '</pre>' : ''),
        ), WATCHDOG_NOTICE);
      }
    }
  }
}