You are here

lost_character_captcha.module in CAPTCHA Pack 6

File

text_captcha/lost_character_captcha/lost_character_captcha.module
View source
<?php

require_once drupal_get_path('module', 'lost_character_captcha') . '/../text_captcha.inc';
define('LOST_CHARACTER_CAPTCHA_DEFAULT_WORD_POOL', 'information language interesting vocabulary communication computer security presentation infrastructure videotape yesterday xylophone workforce validation supervisor standalone multimedia grapefruit friendship aboriginal alphabetical agriculture atmosphere candidature catastrophe audiovisual fingerprint keyboard testimonial supervision supermarket temperature terminology telephonist ultraviolet scholarship spaceflight shoplifting punctuation screwdriver quarterback');
define('LOST_CHARACTER_CAPTCHA_HINTER', '_');

/**
 * Implementation of hook_help().
 */
function lost_character_captcha_help($path, $arg) {
  switch ($path) {
    case 'admin/user/captcha/lost_character_captcha':
      return '<p>' . t('The challenge in this CAPTCHA is to determine the lost character(s) of a given word.') . '</p>';
  }
}

/**
 * Implementation of hook_menu().
 */
function lost_character_captcha_menu() {
  $items = array();
  $items['admin/user/captcha/lost_character_captcha'] = array(
    'title' => 'Lost characters',
    'file' => 'lost_character_captcha.admin.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'lost_character_captcha_settings_form',
    ),
    'access arguments' => array(
      'administer CAPTCHA settings',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implementation of hook_captcha().
 */
function lost_character_captcha_captcha($op, $captcha_type = '') {
  switch ($op) {
    case 'list':
      return array(
        "Lost characters",
      );
    case 'generate':
      if ($captcha_type == "Lost characters") {

        // get the word pool
        $words = _text_captcha_word_pool_get_content('lost_character_captcha_word_pool', NULL, LOST_CHARACTER_CAPTCHA_DEFAULT_WORD_POOL, TRUE);

        // pick a random word
        $word = $words[array_rand($words)];

        // split in characters
        $characters = _text_captcha_utf8_split($word);

        // lose characters
        $lost = array();
        $lose_quantity = variable_get('lost_character_captcha_quantity', 1);
        for ($i = 0; $i < $lose_quantity; $i++) {

          // pick a random character
          $n = array_rand($characters);
          while ($characters[$n] == LOST_CHARACTER_CAPTCHA_HINTER) {
            $n = array_rand($characters);
          }

          // save it for building the solution
          $lost[] = $characters[$n];

          // and lose it in the given word
          if (variable_get('lost_character_captcha_enable_hint', TRUE)) {
            $characters[$n] = LOST_CHARACTER_CAPTCHA_HINTER;
          }
          else {
            unset($characters[$n]);
          }
        }

        // build the CAPTCHA
        sort($lost);
        $given_word = implode('', $characters);
        $solution = implode('', $lost);
        if ($lose_quantity == 1) {
          $title = t('Enter the missing character from the following word');
        }
        else {
          $title = t('Enter the @num missing characters from the following word', array(
            '@num' => $lose_quantity,
          ));
        }

        //
        $captcha = array();
        $captcha['solution'] = $solution;
        $captcha['form']['captcha_response'] = array(
          '#type' => 'textfield',
          '#title' => $title,
          '#field_prefix' => "{$given_word}: ",
          '#size' => 3,
          '#maxlength' => 3,
          '#required' => TRUE,
          '#process' => array(
            'lost_character_process',
          ),
        );
        return $captcha;
      }
      break;
  }
}

/**
 * Process the response before validation.
 */
function lost_character_process($element, $edit, &$form_state, $complete_form) {
  $response = $element['#value'];

  // remove white spaces
  $parts = _text_captcha_whitespace_explode($response);
  $response = implode('', $parts);

  // split in utf8 characters, sort and rejoin
  $characters = _text_captcha_utf8_split($response);
  sort($characters);
  $response = implode('', $characters);

  // Put back in element and return.
  $element['#value'] = $response;
  return $element;
}

Functions

Namesort descending Description
lost_character_captcha_captcha Implementation of hook_captcha().
lost_character_captcha_help Implementation of hook_help().
lost_character_captcha_menu Implementation of hook_menu().
lost_character_process Process the response before validation.

Constants