You are here

class BotchaRecipeUsingJsAbstract in BOTCHA Spam Prevention 6.3

Same name and namespace in other branches
  1. 6.2 controller/botcha_recipe.controller.inc \BotchaRecipeUsingJsAbstract
  2. 7.2 controller/botcha_recipe.controller.inc \BotchaRecipeUsingJsAbstract
  3. 7.3 controller/recipe/botcha.recipe.controller.inc \BotchaRecipeUsingJsAbstract

Hierarchy

Expanded class hierarchy of BotchaRecipeUsingJsAbstract

File

controller/recipe/botcha.recipe.controller.inc, line 469
Controller layer of the BotchaRecipe objects.

View source
class BotchaRecipeUsingJsAbstract extends BotchaRecipe {
  public function getInfo() {
    parent::getInfo();
    $this->error_text .= '<br />' . t('Please enable Javascript to use this form.');
  }
  protected function getFieldCount() {
    return 1;
  }
  public function generateFormElements() {
    $fields = $this
      ->getProperty($this->settings['fields'], 'getFields');
    $js = $this
      ->getProperty($this->settings['js'], 'getJs');
    $form_elements = array(
      $fields[0]['name'] => array(
        '#type' => 'textfield',
        '#title' => t('Enter your name'),
        // Leave the bot enough hints for it to guess it is a good name field
        '#default_value' => $fields[0]['default_value'],
        // Store part of secure_token
        '#description' => t('Your first name.'),
        // This is for human users without CSS.
        '#prefix' => '<div class="' . $fields[0]['class'] . '">' . '<span class="description"> (' . t('If you\'re a human, don\'t change the following field') . ')</span>',
        // @todo Move it to constant since it is also used in error_text.
        // @see?
        '#suffix' => '</div>' . '<noscript>' . t('Please enable Javascript to use this form.') . '</noscript>',
        // @todo Abstract it.
        '#attributes' => array(
          'class' => $fields[0]['class'],
          'autocomplete' => 'off',
        ),
        //'#attributes' => array('class' => array($fields[0]['class']), 'autocomplete' => 'off'),
        '#weight' => -20,
        '!valid_token' => $js['secure_token'],
      ),
    );
    $js_value = $this
      ->getProperty($this->settings['js']['value'], 'getJsValue');
    if (!empty($js_value)) {

      // @todo Abstract it.
      drupal_add_js($js_value, 'inline');

      //drupal_add_js($js_value, array('type' => 'inline', 'preprocess' => FALSE));
    }
    return array_merge(parent::generateFormElements(), $form_elements);
  }
  protected function getField($delta) {
    return array_merge(parent::getField($delta), array(
      'default_value' => $this
        ->getProperty($this->settings['fields'][$delta]['default_value'], 'getFieldDefault', $delta),
    ));
  }

  /**
   * What server sends to JS in the field.
   *
   * @return string
   */
  protected function getFieldDefault($delta) {
    $fields = $this
      ->getProperty($this->settings['fields'], 'getFields');
    $js = $this
      ->getProperty($this->settings['js'], 'getJs');
    $field_prefix = $fields[$delta]['prefix'];
    $chops_positions = array_keys($js['chops']);
    $secure_token = $js['secure_token'];
    return $field_prefix . substr($secure_token, $chops_positions[0], $chops_positions[1]);
  }
  public function getCss() {
    $fields = $this
      ->getProperty($this->settings['fields'], 'getFields');
    return 'div.' . $fields[0]['class'] . ' { display: none; visibility: hidden; }';
  }
  public function getJs() {

    // JS has to reconstruct the token from tok1, part of field_dflt, tok2.
    return array(
      'name' => $this
        ->getProperty($this->settings['js']['name'], 'getJsName'),
      'pos' => $this
        ->getProperty($this->settings['js']['pos'], 'getJsPos'),
      'match' => $this
        ->getProperty($this->settings['js']['match'], 'getJsMatch'),
      'secure_token' => $this
        ->getProperty($this->settings['js']['secure_token'], 'getJsSecureToken'),
      'chops' => $this
        ->getProperty($this->settings['js']['chops'], 'getJsChops'),
    );
  }

  /**
   * Get name of JS-script, attached to field.
   */
  protected function getJsName() {

    // Must start with a literal.
    return 'a' . substr($this->secret, 0, 10) . substr($this
      ->getProperty($this->seed, 'getSeed'), 6, 8);
  }

  /**
   * What position is the part of the token.
   */
  protected function getJsPos() {
    return strlen($this
      ->getProperty($this->settings['fields'][0]['prefix'], 'getFieldPrefix'));
  }

  /**
   *  What JS matches in the field.
   */
  protected function getJsMatch() {
    $chop_positions = array_keys($this
      ->getProperty($this->settings['js']['chops'], 'getJsChops'));
    return substr($this
      ->getFieldDefault(0), 0, $this
      ->getJsPos() + mt_rand(2, $chop_positions[1]));
  }
  protected function getJsChops() {
    $secure_token = $this
      ->getProperty($this->settings['js']['secure_token'], 'getJsSecureToken');

    // Chop the token in 3 parts.
    $js_chops = array();
    $chop1 = 2;
    $js_chops[$chop1] = substr($secure_token, 0, $chop1);
    $chop2 = mt_rand(5, 8);
    $js_chops[$chop2] = substr($secure_token, $chop1 + $chop2);
    return $js_chops;
  }
  protected function getJsSecureToken() {
    return substr($this
      ->getProperty($this->seed, 'getSeed'), 4, -2) . '_form';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BotchaRecipe::$css protected property CSS to add to the page.
BotchaRecipe::$description protected property Brief description of the recipe. It should contain explanation of how bots would fail with it and what the recipe exactly does.
BotchaRecipe::$error_field public property Name of the field in the form to use in error messages (to mask botcha fields).
BotchaRecipe::$error_text public property Text to give users if botcha recipe blocks submission. It should give some help to real human users in cases of disabled Javascript or CSS.
BotchaRecipe::$id public property Identifier of the recipe.
BotchaRecipe::$js protected property Javascript to add to the page.
BotchaRecipe::$method protected property Method of recipe genration.
BotchaRecipe::$recipebooks protected property
BotchaRecipe::$secret protected property Secret.
BotchaRecipe::$settings protected property Options that received as parameters turned into settings by merging with default values.
BotchaRecipe::apply public function 1
BotchaRecipe::getDefaultSettings public function Used to get default recipe data structure. @todo ?Do we need it?
BotchaRecipe::getDescription public function
BotchaRecipe::getFieldClass protected function
BotchaRecipe::getFieldName protected function 3
BotchaRecipe::getFieldPrefix protected function
BotchaRecipe::getFields protected function
BotchaRecipe::getMethod public function
BotchaRecipe::getProperty protected function Universal getter. Wrapper getProperty is used to let class methods be used not only in getting default settings. It gives flexibility to make calls to the class methods in any order: the first of them will always calculate the property value and set…
BotchaRecipe::getSecret public function
BotchaRecipe::getSeed protected function
BotchaRecipe::getSetting public function
BotchaRecipe::getTitle public function
BotchaRecipe::handle public function Handle form depending on the result of spam check. 1
BotchaRecipe::isSpam public function Spam check. 4
BotchaRecipe::prepare protected function 1
BotchaRecipe::setDescription public function
BotchaRecipe::setMethod public function
BotchaRecipe::setRecipebook public function
BotchaRecipe::setSecret public function
BotchaRecipe::setSetting public function
BotchaRecipe::setTitle public function
BotchaRecipe::__construct public function Magic method __construct.
BotchaRecipeUsingJsAbstract::generateFormElements public function Used to get information about the recipe. Must be overridden with calling to parent::generateFormElements. @todo Switch from indexed array to associative. @see? Overrides BotchaRecipe::generateFormElements 2
BotchaRecipeUsingJsAbstract::getCss public function Should be overridden. Overrides BotchaRecipe::getCss
BotchaRecipeUsingJsAbstract::getField protected function Overrides BotchaRecipe::getField
BotchaRecipeUsingJsAbstract::getFieldCount protected function 1
BotchaRecipeUsingJsAbstract::getFieldDefault protected function What server sends to JS in the field.
BotchaRecipeUsingJsAbstract::getInfo public function Used to get information about the recipe. Must be overridden. Overrides BotchaRecipe::getInfo 2
BotchaRecipeUsingJsAbstract::getJs public function Should be overridden. Overrides BotchaRecipe::getJs
BotchaRecipeUsingJsAbstract::getJsChops protected function
BotchaRecipeUsingJsAbstract::getJsMatch protected function What JS matches in the field.
BotchaRecipeUsingJsAbstract::getJsName protected function Get name of JS-script, attached to field.
BotchaRecipeUsingJsAbstract::getJsPos protected function What position is the part of the token.
BotchaRecipeUsingJsAbstract::getJsSecureToken protected function