You are here

class BotchaRecipeUsingJsAbstract in BOTCHA Spam Prevention 7.2

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

Hierarchy

Expanded class hierarchy of BotchaRecipeUsingJsAbstract

File

controller/botcha_recipe.controller.inc, line 414
Controller layer of the BotchaRecipe objects.

View source
class BotchaRecipeUsingJsAbstract extends BotchaRecipe {
  function getInfo() {
    parent::getInfo();
    $this->error_text .= '<br />' . t('Please enable Javascript to use this form.');
  }
  function getFieldCount() {
    return 1;
  }
  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.
        '#suffix' => '</div>' . '<noscript>' . t('Please enable Javascript to use this form.') . '</noscript>',
        '#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)) {
      drupal_add_js($js_value, array(
        'type' => 'inline',
        'preprocess' => FALSE,
      ));
    }
    return array_merge(parent::generateFormElements(), $form_elements);
  }
  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
   */
  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.
   */
  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.
   */
  function getJsPos() {
    return strlen($this
      ->getProperty($this->settings['fields'][0]['prefix'], 'getFieldPrefix'));
  }

  /**
   *  What JS matches in the field.
   */
  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]));
  }
  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;
  }
  function getJsSecureToken() {
    return substr($this
      ->getProperty($this->seed, 'getSeed'), 4, -2) . '_form';
  }
  function getJsValue() {
  }

}

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 @todo Do we really need it? Probably the best way is to provide mail field always - it hides our fields. 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::$status public property Status of the spam check.
BotchaRecipe::applyRecipe public function 2
BotchaRecipe::getDefaultSettings function Used to get default recipe data structure.
BotchaRecipe::getDescription function
BotchaRecipe::getFieldClass function
BotchaRecipe::getFieldName function 3
BotchaRecipe::getFieldPrefix function
BotchaRecipe::getFields function
BotchaRecipe::getMethod function
BotchaRecipe::getProperty 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::getRecipe public static function
BotchaRecipe::getSecret function
BotchaRecipe::getSeed function
BotchaRecipe::getSetting function
BotchaRecipe::getStatus function
BotchaRecipe::getTitle function
BotchaRecipe::handle function Handle form depending on the result of spam check. 1
BotchaRecipe::isSpam function Spam check. 4
BotchaRecipe::save public function
BotchaRecipe::setDescription function
BotchaRecipe::setMethod function
BotchaRecipe::setRecipebook public function
BotchaRecipe::setSecret function
BotchaRecipe::setSetting function
BotchaRecipe::setStatus function
BotchaRecipe::setTitle function
BotchaRecipe::__construct function Magic method __construct.
BotchaRecipeUsingJsAbstract::generateFormElements function Used to get information about the recipe. Must be overridden with calling to parent::generateFormElements. @todo BotchaRecipe generateFormElements Switch from indexed array to associative. Overrides BotchaRecipe::generateFormElements 1
BotchaRecipeUsingJsAbstract::getCss public function Should be overridden. Overrides BotchaRecipe::getCss 1
BotchaRecipeUsingJsAbstract::getField function Overrides BotchaRecipe::getField
BotchaRecipeUsingJsAbstract::getFieldCount function 1
BotchaRecipeUsingJsAbstract::getFieldDefault function What server sends to JS in the field.
BotchaRecipeUsingJsAbstract::getInfo 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 function
BotchaRecipeUsingJsAbstract::getJsMatch function What JS matches in the field.
BotchaRecipeUsingJsAbstract::getJsName function Get name of JS-script, attached to field.
BotchaRecipeUsingJsAbstract::getJsPos function What position is the part of the token.
BotchaRecipeUsingJsAbstract::getJsSecureToken function
BotchaRecipeUsingJsAbstract::getJsValue function 2